Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion kasa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class Category(Enum):
#: Type of the feature
type: Feature.Type = Type.Sensor

# Display hints offer a way suggest how the value should be shown to users
#: Hint to help rounding the sensor values to given after-comma digits
precision_hint: int | None = None

# Number-specific attributes
#: Minimum value
minimum_value: int = 0
Expand Down Expand Up @@ -151,7 +155,10 @@ async def set_value(self, value):
return await getattr(container, self.attribute_setter)(value)

def __repr__(self):
s = f"{self.name} ({self.id}): {self.value}"
value = self.value
if self.precision_hint is not None and value is not None:
value = round(self.value, self.precision_hint)
s = f"{self.name} ({self.id}): {value}"
if self.unit is not None:
s += f" {self.unit}"

Expand Down
6 changes: 6 additions & 0 deletions kasa/iot/modules/emeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, device: Device, module: str):
container=self,
unit="W",
id="current_power_w", # for homeassistant backwards compat
precision_hint=1,
)
)
self._add_feature(
Expand All @@ -33,6 +34,7 @@ def __init__(self, device: Device, module: str):
container=self,
unit="kWh",
id="today_energy_kwh", # for homeassistant backwards compat
precision_hint=3,
)
)
self._add_feature(
Expand All @@ -42,6 +44,7 @@ def __init__(self, device: Device, module: str):
attribute_getter="emeter_this_month",
container=self,
unit="kWh",
precision_hint=3,
)
)
self._add_feature(
Expand All @@ -52,6 +55,7 @@ def __init__(self, device: Device, module: str):
container=self,
unit="kWh",
id="total_energy_kwh", # for homeassistant backwards compat
precision_hint=3,
)
)
self._add_feature(
Expand All @@ -62,6 +66,7 @@ def __init__(self, device: Device, module: str):
container=self,
unit="V",
id="voltage", # for homeassistant backwards compat
precision_hint=1,
)
)
self._add_feature(
Expand All @@ -72,6 +77,7 @@ def __init__(self, device: Device, module: str):
container=self,
unit="A",
id="current_a", # for homeassistant backwards compat
precision_hint=2,
)
)

Expand Down
3 changes: 3 additions & 0 deletions kasa/smart/modules/energymodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, device: SmartDevice, module: str):
attribute_getter="current_power",
container=self,
unit="W",
precision_hint=1,
)
)
self._add_feature(
Expand All @@ -35,6 +36,7 @@ def __init__(self, device: SmartDevice, module: str):
attribute_getter="emeter_today",
container=self,
unit="Wh",
precision_hint=2,
)
)
self._add_feature(
Expand All @@ -44,6 +46,7 @@ def __init__(self, device: SmartDevice, module: str):
attribute_getter="emeter_this_month",
container=self,
unit="Wh",
precision_hint=2,
)
)

Expand Down
12 changes: 12 additions & 0 deletions kasa/tests/test_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,15 @@ async def test_feature_action(mocker):
assert feat.value == "<Action>"
await feat.set_value(1234)
mock_call_action.assert_called()


@pytest.mark.parametrize("precision_hint", [1, 2, 3])
async def test_precision_hint(dummy_feature, precision_hint):
"""Test that precision hint works as expected."""
dummy_value = 3.141593
dummy_feature.type = Feature.Type.Sensor
dummy_feature.precision_hint = precision_hint

dummy_feature.attribute_getter = lambda x: dummy_value
assert dummy_feature.value == dummy_value
assert f"{round(dummy_value, precision_hint)} dummyunit" in repr(dummy_feature)