Add counters to iskra integration (#126046)

* Added counters to iskra integration

* reverted pyiskra bump as reviewed

* Fixed iskra integration according to review

* fixed iskra integration according to review
This commit is contained in:
Iskra kranj 2024-09-19 10:48:42 +02:00 committed by GitHub
parent 3981c87860
commit 3c99fad6b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 96 additions and 1 deletions

View File

@ -21,5 +21,9 @@ ATTR_PHASE1_CURRENT = "phase1_current"
ATTR_PHASE2_CURRENT = "phase2_current" ATTR_PHASE2_CURRENT = "phase2_current"
ATTR_PHASE3_CURRENT = "phase3_current" ATTR_PHASE3_CURRENT = "phase3_current"
# Counters
ATTR_NON_RESETTABLE_COUNTER = "non_resettable_counter_{}"
ATTR_RESETTABLE_COUNTER = "resettable_counter_{}"
# Frequency # Frequency
ATTR_FREQUENCY = "frequency" ATTR_FREQUENCY = "frequency"

View File

@ -3,9 +3,10 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass, replace
from pyiskra.devices import Device from pyiskra.devices import Device
from pyiskra.helper import Counter, CounterType
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -17,6 +18,7 @@ from homeassistant.const import (
UnitOfApparentPower, UnitOfApparentPower,
UnitOfElectricCurrent, UnitOfElectricCurrent,
UnitOfElectricPotential, UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency, UnitOfFrequency,
UnitOfPower, UnitOfPower,
UnitOfReactivePower, UnitOfReactivePower,
@ -27,6 +29,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import IskraConfigEntry from . import IskraConfigEntry
from .const import ( from .const import (
ATTR_FREQUENCY, ATTR_FREQUENCY,
ATTR_NON_RESETTABLE_COUNTER,
ATTR_PHASE1_CURRENT, ATTR_PHASE1_CURRENT,
ATTR_PHASE1_POWER, ATTR_PHASE1_POWER,
ATTR_PHASE1_VOLTAGE, ATTR_PHASE1_VOLTAGE,
@ -36,6 +39,7 @@ from .const import (
ATTR_PHASE3_CURRENT, ATTR_PHASE3_CURRENT,
ATTR_PHASE3_POWER, ATTR_PHASE3_POWER,
ATTR_PHASE3_VOLTAGE, ATTR_PHASE3_VOLTAGE,
ATTR_RESETTABLE_COUNTER,
ATTR_TOTAL_ACTIVE_POWER, ATTR_TOTAL_ACTIVE_POWER,
ATTR_TOTAL_APPARENT_POWER, ATTR_TOTAL_APPARENT_POWER,
ATTR_TOTAL_REACTIVE_POWER, ATTR_TOTAL_REACTIVE_POWER,
@ -163,6 +167,44 @@ SENSOR_TYPES: tuple[IskraSensorEntityDescription, ...] = (
) )
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( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: IskraConfigEntry, entry: IskraConfigEntry,
@ -205,6 +247,19 @@ async def async_setup_entry(
if description.key in sensors 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) async_add_entities(entities)

View File

@ -86,6 +86,42 @@
}, },
"phase3_current": { "phase3_current": {
"name": "Phase 3 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"
} }
} }
} }