From 962f1bad32ea47ba9454aebd37eb7c4e4f307900 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Thu, 19 Dec 2024 12:40:05 +0100 Subject: [PATCH] Add mW as unit of measurement for Matter electrical power sensors (#133504) --- homeassistant/components/matter/sensor.py | 4 ++-- homeassistant/components/number/const.py | 2 +- homeassistant/components/sensor/const.py | 2 +- homeassistant/const.py | 1 + homeassistant/util/unit_conversion.py | 2 ++ .../matter/snapshots/test_sensor.ambr | 6 ++++++ tests/components/sensor/test_recorder.py | 20 +++++++++++++++---- tests/util/test_unit_conversion.py | 1 + 8 files changed, 30 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/matter/sensor.py b/homeassistant/components/matter/sensor.py index de4fdfe2685907..847c9439b81c73 100644 --- a/homeassistant/components/matter/sensor.py +++ b/homeassistant/components/matter/sensor.py @@ -580,10 +580,10 @@ def _update_from_device(self) -> None: key="ElectricalPowerMeasurementWatt", device_class=SensorDeviceClass.POWER, entity_category=EntityCategory.DIAGNOSTIC, - native_unit_of_measurement=UnitOfPower.WATT, + native_unit_of_measurement=UnitOfPower.MILLIWATT, + suggested_unit_of_measurement=UnitOfPower.WATT, suggested_display_precision=2, state_class=SensorStateClass.MEASUREMENT, - measurement_to_ha=lambda x: x / 1000, ), entity_class=MatterSensor, required_attributes=( diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index 56466934e5f351..91a9d6adfe4ce4 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -280,7 +280,7 @@ class NumberDeviceClass(StrEnum): POWER = "power" """Power. - Unit of measurement: `W`, `kW`, `MW`, `GW`, `TW` + Unit of measurement: `mW`, `W`, `kW`, `MW`, `GW`, `TW`, `BTU/h` """ PRECIPITATION = "precipitation" diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index 2fb563051a970f..8c3c39255135c0 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -308,7 +308,7 @@ class SensorDeviceClass(StrEnum): POWER = "power" """Power. - Unit of measurement: `W`, `kW`, `MW`, `GW`, `TW` + Unit of measurement: `mW`, `W`, `kW`, `MW`, `GW`, `TW`, `BTU/h` """ PRECIPITATION = "precipitation" diff --git a/homeassistant/const.py b/homeassistant/const.py index c026a8e5427134..eed8d73a4ee017 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -589,6 +589,7 @@ class UnitOfApparentPower(StrEnum): class UnitOfPower(StrEnum): """Power units.""" + MILLIWATT = "mW" WATT = "W" KILO_WATT = "kW" MEGA_WATT = "MW" diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 8bf6d4b9fc9b89..8ea290f01d12d8 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -340,6 +340,7 @@ class PowerConverter(BaseUnitConverter): UNIT_CLASS = "power" _UNIT_CONVERSION: dict[str | None, float] = { + UnitOfPower.MILLIWATT: 1 * 1000, UnitOfPower.WATT: 1, UnitOfPower.KILO_WATT: 1 / 1000, UnitOfPower.MEGA_WATT: 1 / 1e6, @@ -347,6 +348,7 @@ class PowerConverter(BaseUnitConverter): UnitOfPower.TERA_WATT: 1 / 1e12, } VALID_UNITS = { + UnitOfPower.MILLIWATT, UnitOfPower.WATT, UnitOfPower.KILO_WATT, UnitOfPower.MEGA_WATT, diff --git a/tests/components/matter/snapshots/test_sensor.ambr b/tests/components/matter/snapshots/test_sensor.ambr index e452ce45f1dfa9..f88604e7d46b1b 100644 --- a/tests/components/matter/snapshots/test_sensor.ambr +++ b/tests/components/matter/snapshots/test_sensor.ambr @@ -1744,6 +1744,9 @@ 'sensor': dict({ 'suggested_display_precision': 2, }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -2850,6 +2853,9 @@ 'sensor': dict({ 'suggested_display_precision': 2, }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index 44eaa9fde0d218..636fb9871c9759 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -4247,8 +4247,14 @@ def set_state(entity_id, state, **kwargs): @pytest.mark.parametrize( ("units", "attributes", "unit", "unit2", "supported_unit"), [ - (US_CUSTOMARY_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"), - (METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"), + ( + US_CUSTOMARY_SYSTEM, + POWER_SENSOR_ATTRIBUTES, + "W", + "kW", + "GW, MW, TW, W, kW, mW", + ), + (METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW, mW"), ( US_CUSTOMARY_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, @@ -4459,8 +4465,14 @@ async def test_validate_statistics_unit_ignore_device_class( @pytest.mark.parametrize( ("units", "attributes", "unit", "unit2", "supported_unit"), [ - (US_CUSTOMARY_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"), - (METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"), + ( + US_CUSTOMARY_SYSTEM, + POWER_SENSOR_ATTRIBUTES, + "W", + "kW", + "GW, MW, TW, W, kW, mW", + ), + (METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW, mW"), ( US_CUSTOMARY_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index 4be32b2851e9e1..9c123d93f62340 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -537,6 +537,7 @@ (10, UnitOfPower.GIGA_WATT, 10e9, UnitOfPower.WATT), (10, UnitOfPower.TERA_WATT, 10e12, UnitOfPower.WATT), (10, UnitOfPower.WATT, 0.01, UnitOfPower.KILO_WATT), + (10, UnitOfPower.MILLIWATT, 0.01, UnitOfPower.WATT), ], PressureConverter: [ (1000, UnitOfPressure.HPA, 14.5037743897, UnitOfPressure.PSI),