From 7f98acd477fdfa68ce26eacbc733926797689bc0 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Tue, 7 May 2024 20:56:24 +0200 Subject: [PATCH] Add 'battery_percentage' only when it's available (#906) At least some firmware versions of T110 are known not to report this. --- kasa/smart/modules/battery.py | 39 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/kasa/smart/modules/battery.py b/kasa/smart/modules/battery.py index 6f914bdf..415e47d1 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,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 def battery(self): """Return battery level."""