8000 Add 'battery_percentage' only when it's available (#906) · python-kasa/python-kasa@7f98acd · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f98acd

Browse files
authored
Add 'battery_percentage' only when it's available (#906)
At least some firmware versions of T110 are known not to report this.
1 parent b66a337 commit 7f98acd

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

kasa/smart/modules/battery.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,21 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
6-
75
from ...feature import Feature
86
from ..smartmodule import SmartModule
97

10-
if TYPE_CHECKING:
11-
from ..smartdevice import SmartDevice
12-
138

149
class BatterySensor(SmartModule):
1510
"""Implementation of battery module."""
1611

1712
REQUIRED_COMPONENT = "battery_detect"
1813
QUERY_GETTER_NAME = "get_battery_detect_info"
1914

20-
def __init__(self, device: SmartDevice, module: str):
21-
super().__init__(device, module)
15+
def _initialize_features(self):
16+
"""Initialize features."""
2217
self._add_feature(
2318
Feature(
24-
device,
25-
"battery_level",
26-
"Battery level",
27-
container=self,
28-
attribute_getter="battery",
29-
icon="mdi:battery",
30-
unit="%",
31-
category=Feature.Category.Info,
32-
)
33-
)
34-
self._add_feature(
35-
Feature(
36-
device,
19+
self._device,
3720
"battery_low",
3821
"Battery low",
3922
container=self,
@@ -44,6 +27,22 @@ def __init__(self, device: SmartDevice, module: str):
4427
)
4528
)
4629

30+
# Some devices, like T110 contact sensor do not report the battery percentage
31+
if "battery_percentage" in self._device.sys_info:
32+
self._add_feature(
33+
Feature(
34+
self._device,
35+
"battery_level",
36+
"Battery level",
37+
container=self,
38+
attribute_getter="battery",
39+
icon="mdi:battery",
40+
unit="%",
41+
category=Feature.Category.Info,
42+
type=Feature.Type.Sensor,
43+
)
44+
)
45+
4746
@property
4847
def battery(self):
4948
"""Return battery level."""

0 commit comments

Comments
 (0)
0