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-05-07 18:56:24 +00:00
|
|
|
def _initialize_features(self):
|
|
|
|
"""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",
|
|
|
|
unit="%",
|
|
|
|
category=Feature.Category.Info,
|
|
|
|
type=Feature.Type.Sensor,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-02-22 22:09:38 +00:00
|
|
|
@property
|
|
|
|
def battery(self):
|
|
|
|
"""Return battery level."""
|
|
|
|
return self._device.sys_info["battery_percentage"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def battery_low(self):
|
|
|
|
"""Return True if battery is low."""
|
|
|
|
return self._device.sys_info["at_low_battery"]
|