Add NumberDeviceClass.DURATION (#96932)

pull/96937/head
Erik Montnemery 2023-07-20 11:10:03 +02:00 committed by GitHub
parent db76bf3a9f
commit fa0d68b1d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View File

@ -31,6 +31,7 @@ from homeassistant.const import (
UnitOfSoundPressure,
UnitOfSpeed,
UnitOfTemperature,
UnitOfTime,
UnitOfVolume,
UnitOfVolumetricFlux,
)
@ -122,6 +123,12 @@ class NumberDeviceClass(StrEnum):
- USCS / imperial: `in`, `ft`, `yd`, `mi`
"""
DURATION = "duration"
"""Fixed duration.
Unit of measurement: `d`, `h`, `min`, `s`, `ms`
"""
ENERGY = "energy"
"""Energy.
@ -392,6 +399,13 @@ DEVICE_CLASS_UNITS: dict[NumberDeviceClass, set[type[StrEnum] | str | None]] = {
NumberDeviceClass.DATA_RATE: set(UnitOfDataRate),
NumberDeviceClass.DATA_SIZE: set(UnitOfInformation),
NumberDeviceClass.DISTANCE: set(UnitOfLength),
NumberDeviceClass.DURATION: {
UnitOfTime.DAYS,
UnitOfTime.HOURS,
UnitOfTime.MINUTES,
UnitOfTime.SECONDS,
UnitOfTime.MILLISECONDS,
},
NumberDeviceClass.ENERGY: set(UnitOfEnergy),
NumberDeviceClass.ENERGY_STORAGE: set(UnitOfEnergy),
NumberDeviceClass.FREQUENCY: set(UnitOfFrequency),

View File

@ -73,12 +73,6 @@ class SensorDeviceClass(StrEnum):
ISO8601 format: https://en.wikipedia.org/wiki/ISO_8601
"""
DURATION = "duration"
"""Fixed duration.
Unit of measurement: `d`, `h`, `min`, `s`, `ms`
"""
ENUM = "enum"
"""Enumeration.
@ -158,6 +152,12 @@ class SensorDeviceClass(StrEnum):
- USCS / imperial: `in`, `ft`, `yd`, `mi`
"""
DURATION = "duration"
"""Fixed duration.
Unit of measurement: `d`, `h`, `min`, `s`, `ms`
"""
ENERGY = "energy"
"""Energy.

View File

@ -22,6 +22,7 @@ from homeassistant.components.number.const import (
)
from homeassistant.components.sensor import (
DEVICE_CLASS_UNITS as SENSOR_DEVICE_CLASS_UNITS,
NON_NUMERIC_DEVICE_CLASSES,
SensorDeviceClass,
)
from homeassistant.config_entries import ConfigEntry, ConfigFlow
@ -769,22 +770,15 @@ async def test_custom_unit_change(
def test_device_classes_aligned() -> None:
"""Make sure all sensor device classes are also available in NumberDeviceClass."""
non_numeric_device_classes = {
SensorDeviceClass.DATE,
SensorDeviceClass.DURATION,
SensorDeviceClass.ENUM,
SensorDeviceClass.TIMESTAMP,
}
for device_class in SensorDeviceClass:
if device_class in non_numeric_device_classes:
if device_class in NON_NUMERIC_DEVICE_CLASSES:
continue
assert hasattr(NumberDeviceClass, device_class.name)
assert getattr(NumberDeviceClass, device_class.name).value == device_class.value
for device_class in SENSOR_DEVICE_CLASS_UNITS:
if device_class in non_numeric_device_classes:
if device_class in NON_NUMERIC_DEVICE_CLASSES:
continue
assert (
SENSOR_DEVICE_CLASS_UNITS[device_class]