2024-02-22 22:09:38 +00:00
|
|
|
"""Implementation of battery module."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
from ...feature import Feature
|
2024-02-22 22:09:38 +00:00
|
|
|
from ..smartmodule import SmartModule
|
|
|
|
|
|
|
|
|
|
|
|
class BatterySensor(SmartModule):
|
|
|
|
"""Implementation of battery module."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "battery_detect"
|
|
|
|
QUERY_GETTER_NAME = "get_battery_detect_info"
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def _initialize_features(self) -> None:
|
2024-05-07 18:56:24 +00:00
|
|
|
"""Initialize features."""
|
2024-02-22 22:09:38 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
2024-05-07 18:56:24 +00:00
|
|
|
self._device,
|
2024-05-07 09:13:35 +00:00
|
|
|
"battery_low",
|
2024-02-22 22:09:38 +00:00
|
|
|
"Battery low",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="battery_low",
|
|
|
|
icon="mdi:alert",
|
2024-04-24 16:38:52 +00:00
|
|
|
type=Feature.Type.BinarySensor,
|
2024-05-07 09:13:35 +00:00
|
|
|
category=Feature.Category.Debug,
|
2024-02-22 22:09:38 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-05-07 18:56:24 +00:00
|
|
|
# Some devices, like T110 contact sensor do not report the battery percentage
|
|
|
|
if "battery_percentage" in self._device.sys_info:
|
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
self._device,
|
|
|
|
"battery_level",
|
|
|
|
"Battery level",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="battery",
|
|
|
|
icon="mdi:battery",
|
2024-07-30 18:23:07 +00:00
|
|
|
unit_getter=lambda: "%",
|
2024-05-07 18:56:24 +00:00
|
|
|
category=Feature.Category.Info,
|
|
|
|
type=Feature.Type.Sensor,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-07-04 07:02:50 +00:00
|
|
|
def query(self) -> dict:
|
|
|
|
"""Query to execute during the update cycle."""
|
|
|
|
return {}
|
|
|
|
|
2024-02-22 22:09:38 +00:00
|
|
|
@property
|
2024-11-10 18:55:13 +00:00
|
|
|
def battery(self) -> int:
|
2024-02-22 22:09:38 +00:00
|
|
|
"""Return battery level."""
|
|
|
|
return self._device.sys_info["battery_percentage"]
|
|
|
|
|
|
|
|
@property
|
2024-11-10 18:55:13 +00:00
|
|
|
def battery_low(self) -> bool:
|
2024-02-22 22:09:38 +00:00
|
|
|
"""Return True if battery is low."""
|
|
|
|
return self._device.sys_info["at_low_battery"]
|