diff --git a/homeassistant/components/iskra/const.py b/homeassistant/components/iskra/const.py index 5fc3b501962ebb..a4ed36b50b28be 100644 --- a/homeassistant/components/iskra/const.py +++ b/homeassistant/components/iskra/const.py @@ -21,5 +21,9 @@ ATTR_PHASE2_CURRENT = "phase2_current" ATTR_PHASE3_CURRENT = "phase3_current" +# Counters +ATTR_NON_RESETTABLE_COUNTER = "non_resettable_counter_{}" +ATTR_RESETTABLE_COUNTER = "resettable_counter_{}" + # Frequency ATTR_FREQUENCY = "frequency" diff --git a/homeassistant/components/iskra/sensor.py b/homeassistant/components/iskra/sensor.py index 9e9976749a15be..df9e3ec53f9e5f 100644 --- a/homeassistant/components/iskra/sensor.py +++ b/homeassistant/components/iskra/sensor.py @@ -3,9 +3,10 @@ from __future__ import annotations from collections.abc import Callable -from dataclasses import dataclass +from dataclasses import dataclass, replace from pyiskra.devices import Device +from pyiskra.helper import Counter, CounterType from homeassistant.components.sensor import ( SensorDeviceClass, @@ -17,6 +18,7 @@ UnitOfApparentPower, UnitOfElectricCurrent, UnitOfElectricPotential, + UnitOfEnergy, UnitOfFrequency, UnitOfPower, UnitOfReactivePower, @@ -27,6 +29,7 @@ from . import IskraConfigEntry from .const import ( ATTR_FREQUENCY, + ATTR_NON_RESETTABLE_COUNTER, ATTR_PHASE1_CURRENT, ATTR_PHASE1_POWER, ATTR_PHASE1_VOLTAGE, @@ -36,6 +39,7 @@ ATTR_PHASE3_CURRENT, ATTR_PHASE3_POWER, ATTR_PHASE3_VOLTAGE, + ATTR_RESETTABLE_COUNTER, ATTR_TOTAL_ACTIVE_POWER, ATTR_TOTAL_APPARENT_POWER, ATTR_TOTAL_REACTIVE_POWER, @@ -163,6 +167,44 @@ class IskraSensorEntityDescription(SensorEntityDescription): ) +def get_counter_entity_description( + counter: Counter, + index: int, + entity_name: str, +) -> IskraSensorEntityDescription: + """Dynamically create IskraSensor object as energy meter's counters are customizable.""" + + key = entity_name.format(index + 1) + + if entity_name == ATTR_NON_RESETTABLE_COUNTER: + entity_description = IskraSensorEntityDescription( + key=key, + translation_key=key, + state_class=SensorStateClass.TOTAL_INCREASING, + value_func=lambda device: device.counters.non_resettable[index].value, + native_unit_of_measurement=counter.units, + ) + else: + entity_description = IskraSensorEntityDescription( + key=key, + translation_key=key, + state_class=SensorStateClass.TOTAL_INCREASING, + value_func=lambda device: device.counters.resettable[index].value, + native_unit_of_measurement=counter.units, + ) + + # Set unit of measurement and device class based on counter type + # HA's Energy device class supports only active energy + if counter.counter_type in [CounterType.ACTIVE_IMPORT, CounterType.ACTIVE_EXPORT]: + entity_description = replace( + entity_description, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ) + + return entity_description + + async def async_setup_entry( hass: HomeAssistant, entry: IskraConfigEntry, @@ -205,6 +247,19 @@ async def async_setup_entry( if description.key in sensors ) + if device.supports_counters: + for index, counter in enumerate(device.counters.non_resettable[:4]): + description = get_counter_entity_description( + counter, index, ATTR_NON_RESETTABLE_COUNTER + ) + entities.append(IskraSensor(coordinator, description)) + + for index, counter in enumerate(device.counters.resettable[:8]): + description = get_counter_entity_description( + counter, index, ATTR_RESETTABLE_COUNTER + ) + entities.append(IskraSensor(coordinator, description)) + async_add_entities(entities) diff --git a/homeassistant/components/iskra/strings.json b/homeassistant/components/iskra/strings.json index bd70336f637a12..5818cdfa1db1af 100644 --- a/homeassistant/components/iskra/strings.json +++ b/homeassistant/components/iskra/strings.json @@ -86,6 +86,42 @@ }, "phase3_current": { "name": "Phase 3 current" + }, + "non_resettable_counter_1": { + "name": "Non Resettable counter 1" + }, + "non_resettable_counter_2": { + "name": "Non Resettable counter 2" + }, + "non_resettable_counter_3": { + "name": "Non Resettable counter 3" + }, + "non_resettable_counter_4": { + "name": "Non Resettable counter 4" + }, + "resettable_counter_1": { + "name": "Resettable counter 1" + }, + "resettable_counter_2": { + "name": "Resettable counter 2" + }, + "resettable_counter_3": { + "name": "Resettable counter 3" + }, + "resettable_counter_4": { + "name": "Resettable counter 4" + }, + "resettable_counter_5": { + "name": "Resettable counter 5" + }, + "resettable_counter_6": { + "name": "Resettable counter 6" + }, + "resettable_counter_7": { + "name": "Resettable counter 7" + }, + "resettable_counter_8": { + "name": "Resettable counter 8" } } }