Add number + sensor device class energy storage (#88310)

* Add number + sensor device class energy storage

* Format code

* Update device automations
This commit is contained in:
Erik Montnemery 2023-02-28 19:35:43 +01:00 committed by GitHub
parent c444e1c860
commit 69ce6980d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 0 deletions

View file

@ -127,6 +127,15 @@ class NumberDeviceClass(StrEnum):
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`
"""
ENERGY_STORAGE = "energy_storage"
"""Stored energy.
Use this device class for sensors measuring stored energy, for example the amount
of electric energy currently stored in a battery or the capacity of a battery.
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`
"""
FREQUENCY = "frequency"
"""Frequency.
@ -365,6 +374,7 @@ DEVICE_CLASS_UNITS: dict[NumberDeviceClass, set[type[StrEnum] | str | None]] = {
NumberDeviceClass.DATA_SIZE: set(UnitOfInformation),
NumberDeviceClass.DISTANCE: set(UnitOfLength),
NumberDeviceClass.ENERGY: set(UnitOfEnergy),
NumberDeviceClass.ENERGY_STORAGE: set(UnitOfEnergy),
NumberDeviceClass.FREQUENCY: set(UnitOfFrequency),
NumberDeviceClass.GAS: {
UnitOfVolume.CENTUM_CUBIC_FEET,

View file

@ -160,6 +160,17 @@ class SensorDeviceClass(StrEnum):
ENERGY = "energy"
"""Energy.
Use this device class for sensors measuring energy consumption, for example
electric energy consumption.
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`
"""
ENERGY_STORAGE = "energy_storage"
"""Stored energy.
Use this device class for sensors measuring stored energy, for example the amount
of electric energy currently stored in a battery or the capacity of a battery.
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`
"""
@ -429,6 +440,7 @@ UNIT_CONVERTERS: dict[SensorDeviceClass | str | None, type[BaseUnitConverter]] =
SensorDeviceClass.DATA_SIZE: InformationConverter,
SensorDeviceClass.DISTANCE: DistanceConverter,
SensorDeviceClass.ENERGY: EnergyConverter,
SensorDeviceClass.ENERGY_STORAGE: EnergyConverter,
SensorDeviceClass.GAS: VolumeConverter,
SensorDeviceClass.POWER: PowerConverter,
SensorDeviceClass.POWER_FACTOR: UnitlessRatioConverter,
@ -462,6 +474,7 @@ DEVICE_CLASS_UNITS: dict[SensorDeviceClass, set[type[StrEnum] | str | None]] = {
UnitOfTime.SECONDS,
},
SensorDeviceClass.ENERGY: set(UnitOfEnergy),
SensorDeviceClass.ENERGY_STORAGE: set(UnitOfEnergy),
SensorDeviceClass.FREQUENCY: set(UnitOfFrequency),
SensorDeviceClass.GAS: {
UnitOfVolume.CENTUM_CUBIC_FEET,
@ -526,6 +539,7 @@ DEVICE_CLASS_STATE_CLASSES: dict[SensorDeviceClass, set[SensorStateClass]] = {
SensorStateClass.TOTAL,
SensorStateClass.TOTAL_INCREASING,
},
SensorDeviceClass.ENERGY_STORAGE: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.ENUM: set(),
SensorDeviceClass.FREQUENCY: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.GAS: {SensorStateClass.TOTAL, SensorStateClass.TOTAL_INCREASING},

View file

@ -89,6 +89,7 @@ ENTITY_CONDITIONS = {
SensorDeviceClass.DISTANCE: [{CONF_TYPE: CONF_IS_DISTANCE}],
SensorDeviceClass.DURATION: [{CONF_TYPE: CONF_IS_DURATION}],
SensorDeviceClass.ENERGY: [{CONF_TYPE: CONF_IS_ENERGY}],
SensorDeviceClass.ENERGY_STORAGE: [{CONF_TYPE: CONF_IS_ENERGY}],
SensorDeviceClass.FREQUENCY: [{CONF_TYPE: CONF_IS_FREQUENCY}],
SensorDeviceClass.GAS: [{CONF_TYPE: CONF_IS_GAS}],
SensorDeviceClass.HUMIDITY: [{CONF_TYPE: CONF_IS_HUMIDITY}],

View file

@ -88,6 +88,7 @@ ENTITY_TRIGGERS = {
SensorDeviceClass.DISTANCE: [{CONF_TYPE: CONF_DISTANCE}],
SensorDeviceClass.DURATION: [{CONF_TYPE: CONF_DURATION}],
SensorDeviceClass.ENERGY: [{CONF_TYPE: CONF_ENERGY}],
SensorDeviceClass.ENERGY_STORAGE: [{CONF_TYPE: CONF_ENERGY}],
SensorDeviceClass.FREQUENCY: [{CONF_TYPE: CONF_FREQUENCY}],
SensorDeviceClass.GAS: [{CONF_TYPE: CONF_GAS}],
SensorDeviceClass.HUMIDITY: [{CONF_TYPE: CONF_HUMIDITY}],

View file

@ -51,12 +51,14 @@ def test_matches_device_classes(device_class: SensorDeviceClass) -> None:
SensorDeviceClass.BATTERY: "CONF_IS_BATTERY_LEVEL",
SensorDeviceClass.CO: "CONF_IS_CO",
SensorDeviceClass.CO2: "CONF_IS_CO2",
SensorDeviceClass.ENERGY_STORAGE: "CONF_IS_ENERGY",
}.get(device_class, f"CONF_IS_{device_class.value.upper()}")
assert hasattr(device_condition, constant_name), f"Missing constant {constant_name}"
# Ensure it has correct value
constant_value = {
SensorDeviceClass.BATTERY: "is_battery_level",
SensorDeviceClass.ENERGY_STORAGE: "is_energy",
}.get(device_class, f"is_{device_class.value}")
assert getattr(device_condition, constant_name) == constant_value

View file

@ -55,12 +55,14 @@ def test_matches_device_classes(device_class: SensorDeviceClass) -> None:
SensorDeviceClass.BATTERY: "CONF_BATTERY_LEVEL",
SensorDeviceClass.CO: "CONF_CO",
SensorDeviceClass.CO2: "CONF_CO2",
SensorDeviceClass.ENERGY_STORAGE: "CONF_ENERGY",
}.get(device_class, f"CONF_{device_class.value.upper()}")
assert hasattr(device_trigger, constant_name), f"Missing constant {constant_name}"
# Ensure it has correct value
constant_value = {
SensorDeviceClass.BATTERY: "battery_level",
SensorDeviceClass.ENERGY_STORAGE: "energy",
}.get(device_class, device_class.value)
assert getattr(device_trigger, constant_name) == constant_value