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

Emoncms cleanup #86840

Closed
wants to merge 14 commits into from
Closed

Emoncms cleanup #86840

wants to merge 14 commits into from

Conversation

bwduncan
Copy link
Contributor

Breaking change

Proposed change

Tidy up the code a bit and add some of the newer changes in HA. Specifically, this adds unique_id and sets the state_class for all sensors.

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
  • The code has been formatted using Black (black --fast 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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
Unless I'm missing something obvious, it is never used in the
EmonCmsData class, which is instead decorated by
@Throttle(MIN_TIME_BETWEEN_UPDATES)

If this is supposed to work then it is a bug.

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
@home-assistant
Copy link

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

Code owner commands

Code owners of emoncms can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign emoncms Removes the current integration label and assignees on the issue, add the integration domain after the command.

- apikey is the only parameter, so just create the parameters once at
  init.
- Don't squash the exception when logging it, use logger.exception
- Use req.raise_for_status() instead of checking status code manually
- Put req.json() in the try block

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
emoncms.org allows Keep-Alive so use a requests.Session object which
allows urllib3 connection pooling.

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
Use _attr_name, _attr_native_unit_of_measurement and _attr_native_value.
Convert the _identifier into a _attr_unique_id

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
It always takes the same elements of a dict, so just pass the dict.

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
native_value is set twice, so deduplicate the code

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
Set state_class even if the unit of measurement is not recognised, since
the value will certainly be numeric or null. Emoncms can only return
numeric values or null.

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
@borpin
Copy link

borpin commented Jan 28, 2023

Looks good to me.

@frenck
Copy link
Member

frenck commented Jan 30, 2023

The CI is failing, please make sure the build passes.

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
@bwduncan
Copy link
Contributor Author

Sorry about that, the build is now passing. Although I'm not sure the resulting code is "better" 😆


if value_template is not None:
value_template.hass = hass

data = EmonCmsData(hass, url, apikey, interval)
assert url is not None and apikey is not None # Keep mypy happy
Copy link
Contributor

Choose a reason for hiding this comment

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

If they are compulsory, then it should be using config[key] not config.get(key)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a really good point. I had forgotten I was looking at a dict, rather than some opaque data class 😆 They certainly are compulsory, and marked as Required in the schema.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be also worried about this PR getting bigger and bigger.

  • Adding type hints can usually be a single PR.
  • Adding unique_id probably can be also it's own PR
  • and maybe the state_class in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, if that is the convention. It seems like there will be a lot of admin overheard for adding one line of code at a time but if that's the standard I am happy to comply.

I don't have the time to pull it all apart this week though. Maybe next week.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not compulsory - but it makes reviewing ssssoooo much easier if the PR is smaller.
It also means that if there is a difference of opinion on one point then the other points can be merged anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK yes I see your point. I guess my view was that a lot of this is just tidying things up and doesn't really improve the user experience, so it makes sense to carry it along with features rather than bothering reviewers to look through multiple PRs.

I am certainly expecting to move the integration to use a config flow in a separate PR.

Copy link
Member

@thecode thecode Jan 31, 2023

Choose a reason for hiding this comment

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

Actually if you look at the instructions when making a new PR:

  If your PR requires multiple boxes to be checked, you'll most likely need to
  split it into multiple PRs. This makes things easier and faster to code review.

Which ended up with:
image

Don't get me wrong, I can't say I meet these instructions, many times dependency bump is also a bugfix, etc...

However, code quality are usually easy to split out to a new PR, they are also good candidates for people to review since unlike bug fixes which change the code and understanding for the change, code quality usually try to keep the same functionality and is easier to review.

There is no overhead from creating multiple PRs (@epenet is expert in creating huge number of PRs ❤️), the opposite, it makes easier to review small PRs, although this PR is still in a reasonable size (EDIT: or even small size).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, that makes sense, thanks for explaining 😄 Unfortunately I have lots of work to do this week but I hope I will be able to sort out my mess next week 😄 Thanks again! ❤️

Signed-off-by: Bruce Duncan <bwduncan@gmail.com>
@github-actions
Copy link

github-actions bot commented May 1, 2023

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label May 1, 2023
@borpin
Copy link

borpin commented May 2, 2023

Be good if this could be progressed.

@github-actions github-actions bot removed the stale label May 2, 2023
def native_value(self):
"""Return the state of the device."""
return self._state
def native_value(self) -> str | float | None:
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this property and use attr_native_value directly in the update instead.

"""Initialize the data object."""
self._apikey = apikey
self._sess = requests.Session()
Copy link
Member

Choose a reason for hiding this comment

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

@home-assistant home-assistant bot marked this pull request as draft June 20, 2023 21:25
@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.

@waako waako mentioned this pull request Aug 20, 2023
20 tasks
@github-actions
Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Sep 18, 2023
@borpin
Copy link

borpin commented Sep 19, 2023

@bwduncan - do you think you will get a chance to look at this again?

@bwduncan
Copy link
Contributor Author

I can try. If the requirement to tidy this up a bit is to create an entire package on pypi just to wrap requests.Session then I don't think I have the appetite for that...

@github-actions github-actions bot removed the stale label Sep 19, 2023
@frenck
Copy link
Member

frenck commented Nov 29, 2023

Because there hasn't been any activity on this PR for quite some time now, I've decided to close it for being stale.

Feel free to re-open this PR when you are ready to pick up work on it again 👍

@frenck frenck closed this Nov 29, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 30, 2023
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.

6 participants