Add mW as unit of measurement for Matter electrical power sensors (#133504)

This commit is contained in:
Stefan Agner 2024-12-19 12:40:05 +01:00 committed by GitHub
parent dd215b3d5d
commit 962f1bad32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 30 additions and 8 deletions

View File

@ -580,10 +580,10 @@ DISCOVERY_SCHEMAS = [
key="ElectricalPowerMeasurementWatt", key="ElectricalPowerMeasurementWatt",
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
entity_category=EntityCategory.DIAGNOSTIC, 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, suggested_display_precision=2,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
measurement_to_ha=lambda x: x / 1000,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=( required_attributes=(

View File

@ -280,7 +280,7 @@ class NumberDeviceClass(StrEnum):
POWER = "power" POWER = "power"
"""Power. """Power.
Unit of measurement: `W`, `kW`, `MW`, `GW`, `TW` Unit of measurement: `mW`, `W`, `kW`, `MW`, `GW`, `TW`, `BTU/h`
""" """
PRECIPITATION = "precipitation" PRECIPITATION = "precipitation"

View File

@ -308,7 +308,7 @@ class SensorDeviceClass(StrEnum):
POWER = "power" POWER = "power"
"""Power. """Power.
Unit of measurement: `W`, `kW`, `MW`, `GW`, `TW` Unit of measurement: `mW`, `W`, `kW`, `MW`, `GW`, `TW`, `BTU/h`
""" """
PRECIPITATION = "precipitation" PRECIPITATION = "precipitation"

View File

@ -589,6 +589,7 @@ class UnitOfApparentPower(StrEnum):
class UnitOfPower(StrEnum): class UnitOfPower(StrEnum):
"""Power units.""" """Power units."""
MILLIWATT = "mW"
WATT = "W" WATT = "W"
KILO_WATT = "kW" KILO_WATT = "kW"
MEGA_WATT = "MW" MEGA_WATT = "MW"

View File

@ -340,6 +340,7 @@ class PowerConverter(BaseUnitConverter):
UNIT_CLASS = "power" UNIT_CLASS = "power"
_UNIT_CONVERSION: dict[str | None, float] = { _UNIT_CONVERSION: dict[str | None, float] = {
UnitOfPower.MILLIWATT: 1 * 1000,
UnitOfPower.WATT: 1, UnitOfPower.WATT: 1,
UnitOfPower.KILO_WATT: 1 / 1000, UnitOfPower.KILO_WATT: 1 / 1000,
UnitOfPower.MEGA_WATT: 1 / 1e6, UnitOfPower.MEGA_WATT: 1 / 1e6,
@ -347,6 +348,7 @@ class PowerConverter(BaseUnitConverter):
UnitOfPower.TERA_WATT: 1 / 1e12, UnitOfPower.TERA_WATT: 1 / 1e12,
} }
VALID_UNITS = { VALID_UNITS = {
UnitOfPower.MILLIWATT,
UnitOfPower.WATT, UnitOfPower.WATT,
UnitOfPower.KILO_WATT, UnitOfPower.KILO_WATT,
UnitOfPower.MEGA_WATT, UnitOfPower.MEGA_WATT,

View File

@ -1744,6 +1744,9 @@
'sensor': dict({ 'sensor': dict({
'suggested_display_precision': 2, 'suggested_display_precision': 2,
}), }),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfPower.WATT: 'W'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.POWER: 'power'>, 'original_device_class': <SensorDeviceClass.POWER: 'power'>,
'original_icon': None, 'original_icon': None,
@ -2850,6 +2853,9 @@
'sensor': dict({ 'sensor': dict({
'suggested_display_precision': 2, 'suggested_display_precision': 2,
}), }),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfPower.WATT: 'W'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.POWER: 'power'>, 'original_device_class': <SensorDeviceClass.POWER: 'power'>,
'original_icon': None, 'original_icon': None,

View File

@ -4247,8 +4247,14 @@ async def async_record_states(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("units", "attributes", "unit", "unit2", "supported_unit"), ("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, US_CUSTOMARY_SYSTEM,
TEMPERATURE_SENSOR_ATTRIBUTES, TEMPERATURE_SENSOR_ATTRIBUTES,
@ -4459,8 +4465,14 @@ async def test_validate_statistics_unit_ignore_device_class(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("units", "attributes", "unit", "unit2", "supported_unit"), ("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, US_CUSTOMARY_SYSTEM,
TEMPERATURE_SENSOR_ATTRIBUTES, TEMPERATURE_SENSOR_ATTRIBUTES,

View File

@ -537,6 +537,7 @@ _CONVERTED_VALUE: dict[
(10, UnitOfPower.GIGA_WATT, 10e9, UnitOfPower.WATT), (10, UnitOfPower.GIGA_WATT, 10e9, UnitOfPower.WATT),
(10, UnitOfPower.TERA_WATT, 10e12, UnitOfPower.WATT), (10, UnitOfPower.TERA_WATT, 10e12, UnitOfPower.WATT),
(10, UnitOfPower.WATT, 0.01, UnitOfPower.KILO_WATT), (10, UnitOfPower.WATT, 0.01, UnitOfPower.KILO_WATT),
(10, UnitOfPower.MILLIWATT, 0.01, UnitOfPower.WATT),
], ],
PressureConverter: [ PressureConverter: [
(1000, UnitOfPressure.HPA, 14.5037743897, UnitOfPressure.PSI), (1000, UnitOfPressure.HPA, 14.5037743897, UnitOfPressure.PSI),