Skip to content

Commit 6108569

Browse files
authored
Merge pull request #328 from kratz00/#179
SensorInfo: Add 'suggested_display_precision' (fixes #179)
2 parents 31a1c0e + 135a283 commit 6108569

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

ha_mqtt_discoverable/sensors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import json
2020
import logging
21-
from typing import Any, Optional
21+
from typing import Annotated, Any, Optional
2222

2323
from pydantic import Field
2424

@@ -68,6 +68,10 @@ class SensorInfo(EntityInfo):
6868
When last_reset_value_template is set, the state_class option must be total.
6969
Available variables: entity_id.
7070
The entity_id can be used to reference the entity’s attributes."""
71+
suggested_display_precision: None | Annotated[int, Field(ge=0)] = None
72+
"""
73+
The number of decimals which should be used in the sensor’s state after rounding.
74+
"""
7175

7276

7377
class SwitchInfo(EntityInfo):

tests/test_sensor.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,38 @@
1616
from unittest.mock import patch
1717

1818
import pytest
19+
from pydantic import ValidationError
1920

2021
from ha_mqtt_discoverable import Settings
2122
from ha_mqtt_discoverable.sensors import Sensor, SensorInfo
2223

2324

24-
@pytest.fixture(params=["°C", "kWh"])
25-
def sensor(request) -> Sensor:
26-
mqtt_settings = Settings.MQTT(host="localhost")
27-
sensor_info = SensorInfo(name="test", unit_of_measurement=request.param)
28-
settings = Settings(mqtt=mqtt_settings, entity=sensor_info)
29-
return Sensor(settings)
25+
@pytest.fixture
26+
def make_sensor():
27+
def _make_sensor(suggested_display_precision: None | int = 2):
28+
mqtt_settings = Settings.MQTT(host="localhost")
29+
sensor_info = SensorInfo(name="test", unit_of_measurement="kWh", suggested_display_precision=suggested_display_precision)
30+
settings = Settings(mqtt=mqtt_settings, entity=sensor_info)
31+
return Sensor(settings)
3032

33+
return _make_sensor
3134

32-
def test_required_config():
33-
mqtt_settings = Settings.MQTT(host="localhost")
34-
sensor_info = SensorInfo(name="test")
35-
settings = Settings(mqtt=mqtt_settings, entity=sensor_info)
36-
sensor = Sensor(settings)
35+
36+
@pytest.fixture
37+
def sensor(make_sensor) -> Sensor:
38+
return make_sensor()
39+
40+
41+
def test_required_config(sensor: Sensor):
3742
assert sensor is not None
3843

3944

4045
def test_generate_config(sensor: Sensor):
4146
config = sensor.generate_config()
4247

4348
assert config is not None
44-
# If we have defined a custom unit of measurement, check that is part of the
45-
# output config
46-
if sensor._entity.unit_of_measurement:
47-
assert config["unit_of_measurement"] == sensor._entity.unit_of_measurement
49+
assert config["unit_of_measurement"] == sensor._entity.unit_of_measurement
50+
assert config["suggested_display_precision"] == sensor._entity.suggested_display_precision
4851

4952

5053
def test_update_state(sensor: Sensor):
@@ -66,3 +69,8 @@ def test_update_state_with_last_reset(sensor: Sensor):
6669

6770
parameter_json = json.loads(parameter)
6871
assert parameter_json["last_reset"] == midnight.isoformat()
72+
73+
74+
def test_invalid_suggested_display_precision(make_sensor):
75+
with pytest.raises(ValidationError):
76+
make_sensor(suggested_display_precision=-1)

0 commit comments

Comments
 (0)