-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add counters to iskra integration #126046
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from dataclasses import dataclass | ||
|
||
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,46 @@ class IskraSensorEntityDescription(SensorEntityDescription): | |
) | ||
|
||
|
||
def get_counter_sensor( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we refactor this to return a EntityDescription? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for sure, will do |
||
coordinator: IskraDataUpdateCoordinator, | ||
counter: Counter, | ||
index: int, | ||
entity_name: str, | ||
) -> IskraSensor: | ||
"""Dynamically create IskraSensor object as energy meter's counters are customizable.""" | ||
|
||
key = entity_name.format(index + 1) | ||
sensor_description = { | ||
"key": key, | ||
"translation_key": key, | ||
"state_class": SensorStateClass.TOTAL_INCREASING, | ||
"native_unit_of_measurement": counter.units, | ||
} | ||
|
||
if entity_name == ATTR_NON_RESETTABLE_COUNTER: | ||
sensor_description.update( | ||
{"value_func": lambda device: device.counters.non_resettable[index].value} | ||
) | ||
else: | ||
sensor_description.update( | ||
{"value_func": lambda device: device.counters.resettable[index].value} | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this part should be like if ...:
entity_description = IskraSensorEntityDescription()
else:
entity_description = IskraSensorEntityDescription() and the thing down below can use WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# 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]: | ||
sensor_description.update( | ||
{ | ||
"native_unit_of_measurement": UnitOfEnergy.WATT_HOUR, | ||
"device_class": SensorDeviceClass.ENERGY, | ||
} | ||
) | ||
|
||
description = IskraSensorEntityDescription(**sensor_description) | ||
|
||
return IskraSensor(coordinator, description) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: IskraConfigEntry, | ||
|
@@ -205,6 +249,21 @@ async def async_setup_entry( | |
if description.key in sensors | ||
) | ||
|
||
if device.supports_counters: | ||
for index, counter in enumerate(device.counters.non_resettable[:4]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will the unique_id be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unique_id is assembled in entity.py using device serial and entity description key. entity description key is ATTR_RESETTABLE_COUNTER or ATTR_NON_RESETTABLE_COUNTER with added counter index In line 177 in sensor.py (get_counter_entity_description) i use .format to add index There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i hope this is OK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the counter change? We want to avoid setting up and having the data point that previously represented counter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they cannot those counters are directly from the energy meter's registers, always in same order |
||
entities.append( | ||
get_counter_sensor( | ||
coordinator, counter, index, ATTR_NON_RESETTABLE_COUNTER | ||
) | ||
) | ||
|
||
for index, counter in enumerate(device.counters.resettable[:8]): | ||
entities.append( | ||
get_counter_sensor( | ||
coordinator, counter, index, ATTR_RESETTABLE_COUNTER | ||
) | ||
) | ||
|
||
async_add_entities(entities) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please bump in a separate PR