Skip to content

Commit

Permalink
Add mW as unit of measurement for Matter electrical power sensors (ho…
Browse files Browse the repository at this point in the history
  • Loading branch information
agners authored Dec 19, 2024
1 parent dd215b3 commit 962f1ba
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/matter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/number/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ class UnitOfApparentPower(StrEnum):
class UnitOfPower(StrEnum):
"""Power units."""

MILLIWATT = "mW"
WATT = "W"
KILO_WATT = "kW"
MEGA_WATT = "MW"
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,15 @@ 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,
UnitOfPower.GIGA_WATT: 1 / 1e9,
UnitOfPower.TERA_WATT: 1 / 1e12,
}
VALID_UNITS = {
UnitOfPower.MILLIWATT,
UnitOfPower.WATT,
UnitOfPower.KILO_WATT,
UnitOfPower.MEGA_WATT,
Expand Down
6 changes: 6 additions & 0 deletions tests/components/matter/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,9 @@
'sensor': dict({
'suggested_display_precision': 2,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfPower.WATT: 'W'>,
}),
}),
'original_device_class': <SensorDeviceClass.POWER: 'power'>,
'original_icon': None,
Expand Down Expand Up @@ -2850,6 +2853,9 @@
'sensor': dict({
'suggested_display_precision': 2,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfPower.WATT: 'W'>,
}),
}),
'original_device_class': <SensorDeviceClass.POWER: 'power'>,
'original_icon': None,
Expand Down
20 changes: 16 additions & 4 deletions tests/components/sensor/test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 962f1ba

Please sign in to comment.