10000 Add precision_hint to feature (#871) · python-kasa/python-kasa@3b041b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b041b5

Browse files
rytilahtisdb9696
authored andcommitted
Add precision_hint to feature (#871)
This can be used to hint how the sensor value should be rounded when displaying it to users. The values are adapted from the values used by homeassistant.
1 parent fb16366 commit 3b041b5

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

kasa/feature.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class Category(Enum):
7979
#: Type of the feature
8080
type: Feature.Type = Type.Sensor
8181

82+
# Display hints offer a way suggest how the value should be shown to users
83+
#: Hint to help rounding the sensor values to given after-comma digits
84+
precision_hint: int | None = None
85+
8286
# Number-specific attributes
8387
#: Minimum value
8488
minimum_value: int = 0
@@ -151,7 +155,10 @@ async def set_value(self, value):
151155
return await getattr(container, self.attribute_setter)(value)
152156

153157
def __repr__(self):
154-
s = f"{self.name} ({self.id}): {self.value}"
158+
value = self.value
159+
if self.precision_hint is not None and value is not None:
160+
value = round(self.value, self.precision_hint)
161+
s = f"{self.name} ({self.id}): {value}"
155162
if self.unit is not None:
156163
s += f" {self.unit}"
157164

kasa/iot/modules/emeter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, device: Device, module: str):
2323
container=self,
2424
unit="W",
2525
id="current_power_w", # for homeassistant backwards compat
26+
precision_hint=1,
2627
)
2728
)
2829
self._add_feature(
@@ -33,6 +34,7 @@ def __init__(self, device: Device, module: str):
3334
container=self,
3435
unit="kWh",
3536
id="today_energy_kwh", # for homeassistant backwards compat
37+
precision_hint=3,
3638
)
3739
)
3840
self._add_feature(
@@ -42,6 +44,7 @@ def __init__(self, device: Device, module: str):
4244
attribute_getter="emeter_this_month",
4345
container=self,
4446
unit="kWh",
47+
precision_hint=3,
4548
)
4649
)
4750
self._add_feature(
@@ -52,6 +55,7 @@ def __init__(self, device: Device, module: str):
5255
container=self,
5356
unit="kWh",
5457
id="total_energy_kwh", # for homeassistant backwards compat
58+
precision_hint=3,
5559
)
5660
)
5761
self._add_feature(
@@ -62,6 +66,7 @@ def __init__(self, device: Device, module: str):
6266
container=self,
6367
unit="V",
6468
id="voltage", # for homeassistant backwards compat
69+
precision_hint=1,
6570
)
6671
)
6772
self._add_feature(
@@ -72,6 +77,7 @@ def __init__(self, device: Device, module: str):
7277
container=self,
7378
unit="A",
7479
id="current_a", # for homeassistant backwards compat
80+
precision_hint=2,
7581
)
7682
)
7783

kasa/smart/modules/energymodule.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self, device: SmartDevice, module: str):
2626
attribute_getter="current_power",
2727
container=self,
2828
unit="W",
29+
precision_hint=1,
2930
)
3031
)
3132
self._add_feature(
@@ -35,6 +36,7 @@ def __init__(self, device: SmartDevice, module: str):
3536
attribute_getter="emeter_today",
3637
container=self,
3738
unit="Wh",
39+
precision_hint=2,
3840
)
3941
)
4042
self._add_feature(
@@ -44,6 +46,7 @@ def __init__(self, device: SmartDevice, module: str):
4446
attribute_getter="emeter_this_month",
4547
container=self,
4648
unit="Wh",
49+
precision_hint=2,
4750
)
4851
)
4952

kasa/tests/test_feature.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,15 @@ async def test_feature_action(mocker):
108108
assert feat.value == "<Action>"
109109
await feat.set_value(1234)
110110
mock_call_action.assert_called()
111+
112+
113+
@pytest.mark.parametrize("precision_hint", [1, 2, 3])
114+
async def test_precision_hint(dummy_feature, precision_hint):
115+
"""Test that precision hint works as expected."""
116+
dummy_value = 3.141593
117+
dummy_feature.type = Feature.Type.Sensor
118+
dummy_feature.precision_hint = precision_hint
119+
120+
dummy_feature.attribute_getter = lambda x: dummy_value
121+
assert dummy_feature.value == dummy_value
122+
assert f"{round(dummy_value, precision_hint)} dummyunit" in repr(dummy_feature)

0 commit comments

Comments
 (0)
0