8000 Fallback to is_low for batterysensor's battery_low (#1420) · python-kasa/python-kasa@2ab42f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ab42f5

Browse files
authored
Fallback to is_low for batterysensor's battery_low (#1420)
Fallback to `is_low` if `at_low_battery` is not available.
1 parent 1355e85 commit 2ab42f5

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

kasa/smart/modules/batterysensor.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
from __future__ import annotations
44

5+
from typing import Annotated
6+
7+
from ...exceptions import KasaException
58
from ...feature import Feature
9+
from ...module import FeatureAttribute
610
from ..smartmodule import SmartModule
711

812

@@ -14,18 +18,22 @@ class BatterySensor(SmartModule):
1418

1519
def _initialize_features(self) -> None:
1620
"""Initialize features."""
17-
self._add_feature(
18-
Feature(
19-
self._device,
20-
"battery_low",
21-
"Battery low",
22-
container=self,
23-
attribute_getter="battery_low",
24-
icon="mdi:alert",
25-
type=Feature.Type.BinarySensor,
26-
category=Feature.Category.Debug,
21+
if (
22+
"at_low_battery" in self._device.sys_info
23+
or "is_low" in self._device.sys_info
24+
):
25+
self._add_feature(
26+
Feature(
27+
self._device,
28+
"battery_low",
29+
"Battery low",
30+
container=self,
31+
attribute_getter="battery_low",
32+
icon="mdi:alert",
33+
type=Feature.Type.BinarySensor,
34+
category=Feature.Category.Debug,
35+
)
2736
)
28-
)
2937

3038
# Some devices, like T110 contact sensor do not report the battery percentage
3139
if "battery_percentage" in self._device.sys_info:
@@ -48,11 +56,17 @@ def query(self) -> dict:
4856
return {}
4957

5058
@property
51-
def battery(self) -> int:
59+
def battery(self) -> Annotated[int, FeatureAttribute()]:
5260
"""Return battery level."""
5361
return self._device.sys_info["battery_percentage"]
5462

5563
@property
56-
def battery_low(self) -> bool:
64+
def battery_low(self) -> Annotated[bool, FeatureAttribute()]:
5765
"""Return True if battery is low."""
58-
return self._device.sys_info["at_low_battery"]
66+
is_low = self._device.sys_info.get(
67+
"at_low_battery", self._device.sys_info.get("is_low")
68+
)
69+
if is_low is None:
70+
raise KasaException("Device does not report battery low status")
71+
72+
return is_low

0 commit comments

Comments
 (0)
0