Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a battery sensor to battery powered devices #234

Merged
merged 48 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
024f68d
Added battery & deepsleep
stefanbode Jul 25, 2023
e7df50b
Update const.py
stefanbode Jul 25, 2023
fbc932c
Update device_status.py
stefanbode Jul 25, 2023
4084386
Update status_sensor.py
stefanbode Jul 25, 2023
00fa1eb
Update device_status.py
stefanbode Jul 25, 2023
8e64232
Update utils.py
stefanbode Jul 25, 2023
c9b69c5
Update discovery.py
stefanbode Jul 25, 2023
ca6a994
Create deepsleep.py
stefanbode Jul 25, 2023
5c572a2
Update const.py
stefanbode Jul 25, 2023
c210564
New class for deepsleep
stefanbode Jul 25, 2023
6e2e884
Update device_status.py
stefanbode Jul 25, 2023
27eda72
Update discovery.py
stefanbode Aug 16, 2023
eeb22b9
Update discovery.py
stefanbode Aug 16, 2023
63bcf7f
Update discovery.py
stefanbode Aug 16, 2023
f7d8506
Update status_sensor.py
stefanbode Aug 16, 2023
a9a18fd
Update const.py
stefanbode Aug 16, 2023
f85fd67
Update status_sensor.py
stefanbode Aug 16, 2023
63597b4
Update status_sensor.py
stefanbode Aug 16, 2023
a07d628
Update status_sensor.py
stefanbode Aug 16, 2023
792dbcf
Update status_sensor.py
stefanbode Aug 16, 2023
03caf89
Update status_sensor.py
stefanbode Aug 17, 2023
0c796db
Update status_sensor.py
stefanbode Aug 17, 2023
a752d77
Update status_sensor.py
stefanbode Aug 17, 2023
572c6ed
Update status_sensor.py
stefanbode Aug 17, 2023
e0d6d56
Update sensor.py
stefanbode Aug 17, 2023
e093a68
Update sensor.py
stefanbode Aug 17, 2023
dbd5757
Update const.py
stefanbode Aug 17, 2023
acb302e
Update status_sensor.py
stefanbode Aug 17, 2023
eae0286
Update utils.py
stefanbode Aug 17, 2023
5eb9541
Delete deepsleep.py
stefanbode Aug 17, 2023
734080a
Update device_status.py
stefanbode Aug 17, 2023
31aca04
Update status_sensor.py
stefanbode Aug 17, 2023
52a342a
Update hatasmota/discovery.py
stefanbode Aug 18, 2023
8114bea
Update hatasmota/status_sensor.py
stefanbode Aug 18, 2023
cb53db8
Update hatasmota/status_sensor.py
stefanbode Aug 18, 2023
9ed26a0
Update hatasmota/const.py
stefanbode Aug 18, 2023
a207503
Update hatasmota/const.py
stefanbode Aug 18, 2023
591d39f
Update hatasmota/device_status.py
stefanbode Aug 18, 2023
f251421
Update hatasmota/const.py
stefanbode Aug 18, 2023
93fb0a6
Update hatasmota/status_sensor.py
stefanbode Aug 18, 2023
3f41d3f
Update hatasmota/status_sensor.py
stefanbode Aug 18, 2023
1beb93c
Update hatasmota/discovery.py
stefanbode Aug 18, 2023
2d6db30
Update hatasmota/discovery.py
stefanbode Aug 18, 2023
2243978
Update hatasmota/status_sensor.py
stefanbode Aug 18, 2023
1b18e8d
Update discovery.py
stefanbode Aug 18, 2023
9ee1eae
Update hatasmota/discovery.py
emontnemery Aug 18, 2023
2806145
Sort imports
emontnemery Aug 18, 2023
be1ff8f
Sort imports
emontnemery Aug 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hatasmota/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
CONF_OPTIONS: Final = "so"
CONF_PREFIX: Final = "tp"
CONF_SENSOR: Final = "sn"
CONF_BATTERY: Final = "bat"
CONF_SHUTTER_OPTIONS: Final = "sho"
CONF_SHUTTER_TILT: Final = "sht"
CONF_STATE: Final = "state"
Expand Down Expand Up @@ -161,6 +162,8 @@
SENSOR_STATUS_SSID: Final = "status_ssid"
SENSOR_STATUS_VERSION: Final = "status_version"

SENSOR_STATUS_BATTERY_PERCENTAGE: Final = "status_battery_percentage"

SENSOR_UNIT_PRESSURE: Final = "PressureUnit"
SENSOR_UNIT_SPEED: Final = "SpeedUnit"
SENSOR_UNIT_TEMPERATURE: Final = "TempUnit"
Expand Down
1 change: 1 addition & 0 deletions hatasmota/device_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# "Module or Template":"Generic", <NONE>
# "RestartReason":"Software/System restart", stat/STATUS1:"StatusPRM"."RestartReason"
# "Uptime":"1T17:04:28", stat/STATUS11:"StatusSTS"."Uptime"; tele/STATE:"Uptime"
# "BatteryPercentage":60, tele/STATE: "BatteryPercentage"
# "Hostname":"tasmota_B94927", stat/STATUS5:"StatusNET":"Hostname"
# "IPAddress":"192.168.0.114", stat/STATUS5:"StatusNET":"IPAddress"
# "RSSI":"100", stat/STATUS11:"StatusSTS":"RSSI"; tele/STATE:"RSSI"
Expand Down
4 changes: 4 additions & 0 deletions hatasmota/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from . import config_validation as cv
from .button import TasmotaButtonTrigger, TasmotaButtonTriggerConfig
from .const import (
CONF_BATTERY,
CONF_BUTTON,
CONF_DEVICENAME,
CONF_FRIENDLYNAME,
Expand Down Expand Up @@ -122,6 +123,9 @@
CONF_OPTIONS: TASMOTA_OPTIONS_SCHEMA,
CONF_PREFIX: vol.All(cv.ensure_list, [cv.string]),
CONF_STATE: vol.All(cv.ensure_list, [cv.string]),
vol.Optional(
CONF_BATTERY, default=0
): cv.positive_int, # Added in Tasmota 13.0.0.3
vol.Optional(CONF_SHUTTER_OPTIONS, default=[]): vol.All(
cv.ensure_list, [cv.positive_int]
), # Added in Tasmota 9.2
Expand Down
13 changes: 12 additions & 1 deletion hatasmota/status_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import attr

from .const import (
CONF_BATTERY,
CONF_DEVICENAME,
CONF_IP,
CONF_MAC,
PERCENTAGE,
SENSOR_BATTERY,
SENSOR_STATUS_BATTERY_PERCENTAGE,
SENSOR_STATUS_IP,
SENSOR_STATUS_LAST_RESTART_TIME,
SENSOR_STATUS_LINK_COUNT,
Expand Down Expand Up @@ -46,6 +49,7 @@
# "Module or Template":"Generic", <NONE>
# "RestartReason":"Software/System restart", stat/STATUS1:"StatusPRM"."RestartReason"
# "Uptime":"1T17:04:28", stat/STATUS11:"StatusSTS"."Uptime"; tele/STATE:"Uptime"
# "BatteryPercentage":60, tele/STATE: "BatteryPercentage"
# "Hostname":"tasmota_B94927", stat/STATUS5:"StatusNET":"Hostname"
# "IPAddress":"192.168.0.114", stat/STATUS5:"StatusNET":"IPAddress"
# "RSSI":"100", stat/STATUS11:"StatusSTS":"RSSI"; tele/STATE:"RSSI"
Expand Down Expand Up @@ -74,6 +78,7 @@
SENSOR_STATUS_LINK_COUNT: "WiFi Connect Count",
SENSOR_STATUS_MQTT_COUNT: "MQTT Connect Count",
SENSOR_STATUS_RESTART_REASON: "Restart Reason",
SENSOR_BATTERY: "Battery Level",
emontnemery marked this conversation as resolved.
Show resolved Hide resolved
SENSOR_STATUS_RSSI: "RSSI",
SENSOR_STATUS_SIGNAL: "Signal",
SENSOR_STATUS_SSID: "SSID",
Expand All @@ -89,6 +94,7 @@
STATE_PATHS: dict[str, list[str | int]] = {
SENSOR_STATUS_LINK_COUNT: ["Wifi", "LinkCount"],
SENSOR_STATUS_MQTT_COUNT: ["MqttCount"],
SENSOR_STATUS_BATTERY_PERCENTAGE: ["BatteryPercentage"],
SENSOR_STATUS_RSSI: ["Wifi", "RSSI"],
SENSOR_STATUS_SIGNAL: ["Wifi", "Signal"],
}
Expand Down Expand Up @@ -121,6 +127,7 @@
SENSOR_STATUS_LINK_COUNT: SENSOR_STATUS_LINK_COUNT,
SENSOR_STATUS_MQTT_COUNT: SENSOR_STATUS_MQTT_COUNT,
SENSOR_STATUS_RESTART_REASON: SENSOR_STATUS_RESTART_REASON,
SENSOR_STATUS_BATTERY_PERCENTAGE: SENSOR_BATTERY,
SENSOR_STATUS_RSSI: SENSOR_STATUS_RSSI,
SENSOR_STATUS_SIGNAL: SENSOR_STATUS_SIGNAL,
SENSOR_STATUS_SSID: SENSOR_STATUS_SSID,
Expand All @@ -133,6 +140,7 @@
SENSOR_STATUS_LINK_COUNT: None,
SENSOR_STATUS_MQTT_COUNT: None,
SENSOR_STATUS_RESTART_REASON: None,
SENSOR_STATUS_BATTERY_PERCENTAGE: PERCENTAGE,
SENSOR_STATUS_RSSI: PERCENTAGE,
SENSOR_STATUS_SIGNAL: SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
SENSOR_STATUS_SSID: None,
Expand All @@ -155,6 +163,9 @@ def from_discovery_message(
cls, config: dict, platform: str
) -> list[TasmotaStatusSensorConfig]:
"""Instantiate from discovery message."""
sensor_types = list(SENSORS)
if config[CONF_BATTERY] == 1:
sensor_types.append(SENSOR_STATUS_BATTERY_PERCENTAGE)
sensors = [
cls(
endpoint="status_sensor",
Expand All @@ -172,7 +183,7 @@ def from_discovery_message(
state_topic=get_topic_tele_state(config),
status_topic=get_topic_stat_status(config, STATUS_TOPICS.get(sensor)),
)
for sensor in SENSORS
for sensor in sensor_types
]
return sensors

Expand Down
Loading