Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion homeassistant/components/overkiz/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyoverkiz.types import StateType as OverkizStateType

from homeassistant.components.sensor import (
DEVICE_CLASS_UNITS,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
Expand Down Expand Up @@ -604,10 +605,25 @@ def native_unit_of_measurement(self) -> str | None:
if (unit := attrs[OverkizAttribute.CORE_MEASURED_VALUE_TYPE]) and (
unit_value := unit.value_as_str
):
return OVERKIZ_UNIT_TO_HA.get(unit_value, default_unit)
ha_unit = OVERKIZ_UNIT_TO_HA.get(unit_value, default_unit)
if self._is_unit_valid_for_device_class(ha_unit):
return ha_unit

return default_unit

def _is_unit_valid_for_device_class(self, unit: str) -> bool:
"""Check if a unit is valid for this sensor's device class.

The device-level core:MeasuredValueType attribute describes the primary
sensor (e.g. luminance/temperature), but must not override the unit of
unrelated sensors on the same device (e.g. RSSI).
"""
if not (device_class := self.entity_description.device_class):
return True
if (valid_units := DEVICE_CLASS_UNITS.get(device_class)) is None:
return True
return unit in valid_units


class OverkizHomeKitSetupCodeSensor(OverkizEntity, SensorEntity):
"""Representation of an Overkiz HomeKit Setup Code."""
Expand Down
12 changes: 6 additions & 6 deletions tests/components/overkiz/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,7 @@
'supported_features': 0,
'translation_key': None,
'unique_id': 'io://1234-5678-1698/15702199#2-core:RSSILevelState',
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
'unit_of_measurement': 'dB',
})
# ---
# name: test_sensor_entities_snapshot[cloud_nexity_rail_din_europe.json][sensor.garden_radiator_bathroom_temperature_sensor_rssi_level-state]
Expand All @@ -1819,7 +1819,7 @@
'device_class': 'signal_strength',
'friendly_name': 'Garden Radiator Bathroom Temperature Sensor RSSI level',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
'unit_of_measurement': 'dB',
}),
'context': <ANY>,
'entity_id': 'sensor.garden_radiator_bathroom_temperature_sensor_rssi_level',
Expand Down Expand Up @@ -2804,7 +2804,7 @@
'supported_features': 0,
'translation_key': None,
'unique_id': 'io://1234-5678-1698/9253412#2-core:RSSILevelState',
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
'unit_of_measurement': 'dB',
})
# ---
# name: test_sensor_entities_snapshot[cloud_nexity_rail_din_europe.json][sensor.living_room_radiator_study_temp_probe_rssi_level-state]
Expand All @@ -2813,7 +2813,7 @@
'device_class': 'signal_strength',
'friendly_name': 'Living Room Radiator Study Temp Probe RSSI level',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
'unit_of_measurement': 'dB',
}),
'context': <ANY>,
'entity_id': 'sensor.living_room_radiator_study_temp_probe_rssi_level',
Expand Down Expand Up @@ -4182,7 +4182,7 @@
'supported_features': 0,
'translation_key': None,
'unique_id': 'io://1234-5678-1698/9187218#2-core:RSSILevelState',
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
'unit_of_measurement': 'dB',
})
# ---
# name: test_sensor_entities_snapshot[cloud_nexity_rail_din_europe.json][sensor.study_radiator_nursery_temp_probe_rssi_level-state]
Expand All @@ -4191,7 +4191,7 @@
'device_class': 'signal_strength',
'friendly_name': 'Study Radiator Nursery Temp Probe RSSI level',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
'unit_of_measurement': 'dB',
}),
'context': <ANY>,
'entity_id': 'sensor.study_radiator_nursery_temp_probe_rssi_level',
Expand Down