Skip to content
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

Merged
merged 4 commits into from
Sep 19, 2024
Merged

Conversation

iskrakranj
Copy link
Contributor

@iskrakranj iskrakranj commented Sep 16, 2024

Proposed change

This is a follow up PR to #121488
It adds Energy counter sensor entities for displaying consumption

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @Iskramis, mind taking a look at this pull request as it has been labeled with an integration (iskra) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of iskra can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign iskra Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@@ -7,5 +7,5 @@
"integration_type": "hub",
"iot_class": "local_polling",
"loggers": ["pyiskra"],
"requirements": ["pyiskra==0.1.8"]
"requirements": ["pyiskra==0.1.11"]
Copy link
Member

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

@joostlek
Copy link
Member

That PR can be a PR that we merge before this PR. Separating dependency bumps has a lot of benefits. :)

@iskrakranj
Copy link
Contributor Author

will do as suggested :P

@iskrakranj iskrakranj mentioned this pull request Sep 16, 2024
19 tasks
@iskrakranj iskrakranj marked this pull request as ready for review September 16, 2024 12:28
@@ -163,6 +167,46 @@ class IskraSensorEntityDescription(SensorEntityDescription):
)


def get_counter_sensor(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor this to return a EntityDescription?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure, will do

Comment on lines 179 to 193
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}
)
Copy link
Member

@joostlek joostlek Sep 16, 2024

Choose a reason for hiding this comment

The 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 .replace to add the UoM and device class

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with .replace you meant like that?
entity_description.native_unit_of_measurement = UnitOfEnergy.WATT_HOUR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataclasses.replace

@home-assistant home-assistant bot marked this pull request as draft September 16, 2024 12:42
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@MartinHjelmare MartinHjelmare changed the title Added counters to iskra integration Add counters to iskra integration Sep 16, 2024
@iskrakranj iskrakranj marked this pull request as ready for review September 16, 2024 13:32
@home-assistant home-assistant bot requested a review from joostlek September 16, 2024 13:32
key=key,
translation_key=key,
state_class=SensorStateClass.TOTAL_INCREASING,
value_func=(lambda device: device.counters.non_resettable[index].value),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you only have to parenthesize lambdas if they span more than one line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remove

@@ -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]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will the unique_id be?

Copy link
Contributor Author

@iskrakranj iskrakranj Sep 17, 2024

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i hope this is OK.

Copy link
Member

Choose a reason for hiding this comment

The 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 1 to be counter 2 and vice versa

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

@joostlek joostlek merged commit 3c99fad into home-assistant:dev Sep 19, 2024
29 of 30 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants