From df5915e1539dff05d02bc4d40ee60c28302d5d33 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Tue, 7 May 2024 16:52:43 +0200 Subject: [PATCH 1/2] Add 'battery_percentage' only when it's available At least some firmware versions of T110 are known not to report this. --- kasa/smart/modules/battery.py | 37 ++++++++++++++++------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/kasa/smart/modules/battery.py b/kasa/smart/modules/battery.py index 6f914bdf2..10157d585 100644 --- a/kasa/smart/modules/battery.py +++ b/kasa/smart/modules/battery.py @@ -2,14 +2,9 @@ from __future__ import annotations -from typing import TYPE_CHECKING - from ...feature import Feature from ..smartmodule import SmartModule -if TYPE_CHECKING: - from ..smartdevice import SmartDevice - class BatterySensor(SmartModule): """Implementation of battery module.""" @@ -17,23 +12,11 @@ class BatterySensor(SmartModule): REQUIRED_COMPONENT = "battery_detect" QUERY_GETTER_NAME = "get_battery_detect_info" - def __init__(self, device: SmartDevice, module: str): - super().__init__(device, module) + def _initialize_features(self): + """Initialize features.""" self._add_feature( Feature( - device, - "battery_level", - "Battery level", - container=self, - attribute_getter="battery", - icon="mdi:battery", - unit="%", - category=Feature.Category.Info, - ) - ) - self._add_feature( - Feature( - device, + self._device, "battery_low", "Battery low", container=self, @@ -44,6 +27,20 @@ def __init__(self, device: SmartDevice, module: str): ) ) + # 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", + category=Feature.Category.Info, + ) + ) + @property def battery(self): """Return battery level.""" From 3b812a0177ed138a06f1be0439fb671bcc91fe9f Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Tue, 7 May 2024 16:57:37 +0200 Subject: [PATCH 2/2] Improve feature metadata --- kasa/smart/modules/battery.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kasa/smart/modules/battery.py b/kasa/smart/modules/battery.py index 10157d585..415e47d1e 100644 --- a/kasa/smart/modules/battery.py +++ b/kasa/smart/modules/battery.py @@ -37,7 +37,9 @@ def _initialize_features(self): container=self, attribute_getter="battery", icon="mdi:battery", + unit="%", category=Feature.Category.Info, + type=Feature.Type.Sensor, ) )