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.
This commit is contained in:
Teemu R
2024-04-29 13:31:42 +02:00
committed by GitHub
parent fe6b1892cc
commit d7a36fe071
4 changed files with 29 additions and 1 deletions

View File

@@ -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)