Add 'battery_percentage' only when it's available (#906)

At least some firmware versions of T110 are known not to report this.
This commit is contained in:
Teemu R 2024-05-07 20:56:24 +02:00 committed by GitHub
parent b66a337f40
commit 7f98acd477
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,14 +2,9 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING
from ...feature import Feature from ...feature import Feature
from ..smartmodule import SmartModule from ..smartmodule import SmartModule
if TYPE_CHECKING:
from ..smartdevice import SmartDevice
class BatterySensor(SmartModule): class BatterySensor(SmartModule):
"""Implementation of battery module.""" """Implementation of battery module."""
@ -17,23 +12,11 @@ class BatterySensor(SmartModule):
REQUIRED_COMPONENT = "battery_detect" REQUIRED_COMPONENT = "battery_detect"
QUERY_GETTER_NAME = "get_battery_detect_info" QUERY_GETTER_NAME = "get_battery_detect_info"
def __init__(self, device: SmartDevice, module: str): def _initialize_features(self):
super().__init__(device, module) """Initialize features."""
self._add_feature( self._add_feature(
Feature( Feature(
device, self._device,
"battery_level",
"Battery level",
container=self,
attribute_getter="battery",
icon="mdi:battery",
unit="%",
category=Feature.Category.Info,
)
)
self._add_feature(
Feature(
device,
"battery_low", "battery_low",
"Battery low", "Battery low",
container=self, container=self,
@ -44,6 +27,22 @@ class BatterySensor(SmartModule):
) )
) )
# 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,
)
)
@property @property
def battery(self): def battery(self):
"""Return battery level.""" """Return battery level."""