Add missing device class to sensor.DEVICE_CLASS_UNITS (#101256)

This commit is contained in:
Erik Montnemery 2023-10-02 13:01:26 +02:00 committed by GitHub
parent 41cb8526d1
commit d0dc4d0963
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View file

@ -542,6 +542,7 @@ DEVICE_CLASS_UNITS: dict[SensorDeviceClass, set[type[StrEnum] | str | None]] = {
},
SensorDeviceClass.VOLTAGE: set(UnitOfElectricPotential),
SensorDeviceClass.VOLUME: set(UnitOfVolume),
SensorDeviceClass.VOLUME_STORAGE: set(UnitOfVolume),
SensorDeviceClass.WATER: {
UnitOfVolume.CENTUM_CUBIC_FEET,
UnitOfVolume.CUBIC_FEET,

View file

@ -901,3 +901,13 @@ async def test_name(hass: HomeAssistant) -> None:
"mode": NumberMode.AUTO,
"step": 1.0,
}
def test_device_class_units(hass: HomeAssistant) -> None:
"""Test all numeric device classes have unit."""
# DEVICE_CLASS_UNITS should include all device classes except:
# - NumberDeviceClass.MONETARY
# - Device classes enumerated in NON_NUMERIC_DEVICE_CLASSES
assert set(NUMBER_DEVICE_CLASS_UNITS) == set(
NumberDeviceClass
) - NON_NUMERIC_DEVICE_CLASSES - {NumberDeviceClass.MONETARY}

View file

@ -13,6 +13,7 @@ from homeassistant.components.sensor import (
DEVICE_CLASS_STATE_CLASSES,
DEVICE_CLASS_UNITS,
DOMAIN as SENSOR_DOMAIN,
NON_NUMERIC_DEVICE_CLASSES,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
@ -2483,3 +2484,15 @@ def test_async_rounded_state_registered_entity_with_display_precision(
hass.states.async_set(entity_id, "-0.0")
state = hass.states.get(entity_id)
assert async_rounded_state(hass, entity_id, state) == "0.0000"
def test_device_class_units_state_classes(hass: HomeAssistant) -> None:
"""Test all numeric device classes have unit and state class."""
# DEVICE_CLASS_UNITS should include all device classes except:
# - SensorDeviceClass.MONETARY
# - Device classes enumerated in NON_NUMERIC_DEVICE_CLASSES
assert set(DEVICE_CLASS_UNITS) == set(
SensorDeviceClass
) - NON_NUMERIC_DEVICE_CLASSES - {SensorDeviceClass.MONETARY}
# DEVICE_CLASS_STATE_CLASSES should include all device classes
assert set(DEVICE_CLASS_STATE_CLASSES) == set(SensorDeviceClass)