Add sensor tests for device class enums (#145523)

This commit is contained in:
Abílio Costa 2025-05-26 14:28:30 +01:00 committed by GitHub
parent a3b7cd7b4d
commit 54dce53628
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,7 +24,7 @@ from homeassistant.components.sensor import (
async_rounded_state, async_rounded_state,
async_update_suggested_units, async_update_suggested_units,
) )
from homeassistant.components.sensor.const import STATE_CLASS_UNITS from homeassistant.components.sensor.const import STATE_CLASS_UNITS, UNIT_CONVERTERS
from homeassistant.config_entries import ConfigEntry, ConfigFlow from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.const import ( from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
@ -2812,3 +2812,54 @@ async def test_suggested_unit_guard_valid_unit(
assert entry.options == { assert entry.options == {
"sensor.private": {"suggested_unit_of_measurement": suggested_unit}, "sensor.private": {"suggested_unit_of_measurement": suggested_unit},
} }
def test_device_class_units_are_complete() -> None:
"""Test that the device class units enum is complete."""
no_unit_device_classes = {
SensorDeviceClass.DATE,
SensorDeviceClass.ENUM,
SensorDeviceClass.MONETARY,
SensorDeviceClass.TIMESTAMP,
}
unit_device_classes = {
device_class.value for device_class in SensorDeviceClass
} - no_unit_device_classes
assert set(DEVICE_CLASS_UNITS.keys()) == unit_device_classes
def test_device_class_converters_are_complete() -> None:
"""Test that the device class converters enum is complete."""
no_converter_device_classes = {
SensorDeviceClass.APPARENT_POWER,
SensorDeviceClass.AQI,
SensorDeviceClass.BATTERY,
SensorDeviceClass.CO,
SensorDeviceClass.CO2,
SensorDeviceClass.DATE,
SensorDeviceClass.ENUM,
SensorDeviceClass.FREQUENCY,
SensorDeviceClass.HUMIDITY,
SensorDeviceClass.ILLUMINANCE,
SensorDeviceClass.IRRADIANCE,
SensorDeviceClass.MOISTURE,
SensorDeviceClass.MONETARY,
SensorDeviceClass.NITROGEN_DIOXIDE,
SensorDeviceClass.NITROGEN_MONOXIDE,
SensorDeviceClass.NITROUS_OXIDE,
SensorDeviceClass.OZONE,
SensorDeviceClass.PH,
SensorDeviceClass.PM1,
SensorDeviceClass.PM10,
SensorDeviceClass.PM25,
SensorDeviceClass.REACTIVE_POWER,
SensorDeviceClass.SIGNAL_STRENGTH,
SensorDeviceClass.SOUND_PRESSURE,
SensorDeviceClass.SULPHUR_DIOXIDE,
SensorDeviceClass.TIMESTAMP,
SensorDeviceClass.WIND_DIRECTION,
}
converter_device_classes = {
device_class.value for device_class in SensorDeviceClass
} - no_converter_device_classes
assert set(UNIT_CONVERTERS.keys()) == converter_device_classes