From ade0d6fcae1f78c67d5af856744a25eda5e1876c Mon Sep 17 00:00:00 2001 From: ollo69 <60491700+ollo69@users.noreply.github.com> Date: Mon, 6 Feb 2023 11:05:10 +0100 Subject: [PATCH] Use suggested_display_precision for AsusWrt sensors (#87396) * Use native_precision for AsusWrt sensors * Use suggested_display_precision --- homeassistant/components/asuswrt/sensor.py | 25 ++++++++++------------ tests/components/asuswrt/test_sensor.py | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/asuswrt/sensor.py b/homeassistant/components/asuswrt/sensor.py index a959b174c0d..0af9ace8883 100644 --- a/homeassistant/components/asuswrt/sensor.py +++ b/homeassistant/components/asuswrt/sensor.py @@ -36,7 +36,6 @@ class AsusWrtSensorEntityDescription(SensorEntityDescription): """A class that describes AsusWrt sensor entities.""" factor: int | None = None - precision: int = 2 UNIT_DEVICES = "Devices" @@ -57,6 +56,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND, entity_registry_enabled_default=False, + suggested_display_precision=2, factor=125000, ), AsusWrtSensorEntityDescription( @@ -67,6 +67,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND, entity_registry_enabled_default=False, + suggested_display_precision=2, factor=125000, ), AsusWrtSensorEntityDescription( @@ -77,6 +78,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( native_unit_of_measurement=UnitOfInformation.GIGABYTES, device_class=SensorDeviceClass.DATA_SIZE, entity_registry_enabled_default=False, + suggested_display_precision=2, factor=1000000000, ), AsusWrtSensorEntityDescription( @@ -87,6 +89,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( native_unit_of_measurement=UnitOfInformation.GIGABYTES, device_class=SensorDeviceClass.DATA_SIZE, entity_registry_enabled_default=False, + suggested_display_precision=2, factor=1000000000, ), AsusWrtSensorEntityDescription( @@ -96,8 +99,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, - factor=1, - precision=1, + suggested_display_precision=1, ), AsusWrtSensorEntityDescription( key=SENSORS_LOAD_AVG[1], @@ -106,8 +108,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, - factor=1, - precision=1, + suggested_display_precision=1, ), AsusWrtSensorEntityDescription( key=SENSORS_LOAD_AVG[2], @@ -116,8 +117,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, - factor=1, - precision=1, + suggested_display_precision=1, ), AsusWrtSensorEntityDescription( key=SENSORS_TEMPERATURES[0], @@ -127,8 +127,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( native_unit_of_measurement=UnitOfTemperature.CELSIUS, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, - factor=1, - precision=1, + suggested_display_precision=1, ), AsusWrtSensorEntityDescription( key=SENSORS_TEMPERATURES[1], @@ -138,8 +137,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( native_unit_of_measurement=UnitOfTemperature.CELSIUS, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, - factor=1, - precision=1, + suggested_display_precision=1, ), AsusWrtSensorEntityDescription( key=SENSORS_TEMPERATURES[2], @@ -149,8 +147,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( native_unit_of_measurement=UnitOfTemperature.CELSIUS, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, - factor=1, - precision=1, + suggested_display_precision=1, ), ) @@ -203,5 +200,5 @@ class AsusWrtSensor(CoordinatorEntity, SensorEntity): descr = self.entity_description state: float | int | str | None = self.coordinator.data.get(descr.key) if state is not None and descr.factor and isinstance(state, (float, int)): - return round(state / descr.factor, descr.precision) + return state / descr.factor return state diff --git a/tests/components/asuswrt/test_sensor.py b/tests/components/asuswrt/test_sensor.py index 1854a32fd2b..cf704acc3a0 100644 --- a/tests/components/asuswrt/test_sensor.py +++ b/tests/components/asuswrt/test_sensor.py @@ -50,7 +50,7 @@ MAC_ADDR = "a1:b2:c3:d4:e5:f6" MOCK_BYTES_TOTAL = [60000000000, 50000000000] MOCK_CURRENT_TRANSFER_RATES = [20000000, 10000000] MOCK_LOAD_AVG = [1.1, 1.2, 1.3] -MOCK_TEMPERATURES = {"2.4GHz": 40, "5.0GHz": 0, "CPU": 71.2} +MOCK_TEMPERATURES = {"2.4GHz": 40.0, "5.0GHz": 0.0, "CPU": 71.2} MOCK_MAC_1 = "A1:B1:C1:D1:E1:F1" MOCK_MAC_2 = "A2:B2:C2:D2:E2:F2" MOCK_MAC_3 = "A3:B3:C3:D3:E3:F3"