Skip to content

Commit

Permalink
Add counters to iskra integration (home-assistant#126046)
Browse files Browse the repository at this point in the history
* Added counters to iskra integration

* reverted pyiskra bump as reviewed

* Fixed iskra integration according to review

* fixed iskra integration according to review
  • Loading branch information
iskrakranj authored Sep 19, 2024
1 parent 3981c87 commit 3c99fad
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
4 changes: 4 additions & 0 deletions homeassistant/components/iskra/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
57 changes: 56 additions & 1 deletion homeassistant/components/iskra/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +18,7 @@
UnitOfApparentPower,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfReactivePower,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)


Expand Down
36 changes: 36 additions & 0 deletions homeassistant/components/iskra/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down

0 comments on commit 3c99fad

Please sign in to comment.