From fa0d68b1d73211f52c33462add67558bcb4135bd Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 20 Jul 2023 11:10:03 +0200 Subject: [PATCH] Add NumberDeviceClass.DURATION (#96932) --- homeassistant/components/number/const.py | 14 ++++++++++++++ homeassistant/components/sensor/const.py | 12 ++++++------ tests/components/number/test_init.py | 12 +++--------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index 1a2580cfc61..849581b6f9f 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -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), diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index fe01058fda7..2c6883e4a71 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -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. diff --git a/tests/components/number/test_init.py b/tests/components/number/test_init.py index d9cf27c12aa..37c0b175faa 100644 --- a/tests/components/number/test_init.py +++ b/tests/components/number/test_init.py @@ -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]