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

Remove duplicate code in emoncms #118610

Merged
merged 7 commits into from
Jun 4, 2024
Merged

Conversation

alexandrecuer
Copy link
Contributor

Proposed change

Remove duplicate & property extra_state_attributes

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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Jun 1, 2024

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 pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign emoncms 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.

@MartinHjelmare MartinHjelmare changed the title Remove duplicate & property extra_state_attributes Remove emoncms duplicate & property extra_state_attributes Jun 1, 2024
Comment on lines 199 to 208
if self._value_template is not None:
self._attr_native_value = (
self._value_template.render_with_possible_json_value(
elem["value"], STATE_UNKNOWN
)
)
elif elem["value"] is not None:
self._attr_native_value = round(float(elem["value"]), DECIMALS)
else:
self._attr_native_value = None
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is still needed to get the initial value (on start).
Otherwise I think it will get set as "unknown" until the first data update.

In which case, if you want to remove duplicate code, maybe you can add a function _update_attributes that gets called in both __init__ and update

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 thought to call self.update() at the end of the __init__ of the EmonCmsSensor class
is it OK for you ?

For an upcoming PR, I think also :

  • there is not benefit to have feedtag and feedname in the identifier,
  • there is one to have feed tag in the friendly name (self._attr_name) as users will identify more easily the feeds
  • the code in the __init__ which fixes self._attr_name could be in the update method, because if a user modify a tag or a feed name, it will be reported in home assistant, which is not the case with the actual implementation

Copy link
Contributor

Choose a reason for hiding this comment

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

You shouldn't call self.update() inside __init__, as it has already been called once in setup_platform
This is why I think you should split get data and set attributes to avoid duplicate code

Copy link
Contributor Author

@alexandrecuer alexandrecuer Jun 3, 2024

Choose a reason for hiding this comment

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

in setup_platform, it is the update method of the EmonCmsData class which is called, that's why I thought it could be possible to call the update method of EmonCmsSensor in its __init__

when adding entities, update is not called by default to what I understand :

class AddEntitiesCallback(Protocol):

but no problem to refactor as you suggest, it is probably more flexible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@borpin
Copy link

borpin commented Jun 3, 2024

@epenet - is it acceptable for there to be a 'timeout' before an entity is set to Unknown?

The way emoncms works, using a timeseries database, a NULL return is acceptable, especially for a single value (if the incoming value misses the timeseries window). However, an unknown value, say for a temperature sensor does cause issues within HA (in automation and templates).

Ideally, a 'timeout' whereby it is not immediately assumed the value is UNKNOWN, but just does not get updated if the incoming value is NULL.

@alexandrecuer - perhaps the answer is not to update the sensor if the value is NULL. It is then clear to the user when it was last updated, but the value remains valid.

@epenet
Copy link
Contributor

epenet commented Jun 3, 2024

@epenet - is it acceptable for there to be a 'timeout' before an entity is set to Unknown?

The way emoncms works, using a timeseries database, a NULL return is acceptable, especially for a single value (if the incoming value misses the timeseries window). However, an unknown value, say for a temperature sensor does cause issues within HA (in automation and templates).

Ideally, a 'timeout' whereby it is not immediately assumed the value is UNKNOWN, but just does not get updated if the incoming value is NULL.

@alexandrecuer - perhaps the answer is not to update the sensor if the value is NULL. It is then clear to the user when it was last updated, but the value remains valid.

That's a question for another PR.
This PR should be only for refactoring (no change in behavior)

@alexandrecuer alexandrecuer changed the title Remove emoncms duplicate & property extra_state_attributes Remove emoncms duplicate Jun 3, 2024
@alexandrecuer alexandrecuer requested a review from epenet June 3, 2024 14:38
homeassistant/components/emoncms/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/emoncms/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/emoncms/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/emoncms/sensor.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft June 4, 2024 08:40
@home-assistant
Copy link

home-assistant bot commented Jun 4, 2024

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.

@epenet epenet changed the title Remove emoncms duplicate Remove duplicate code in emoncms Jun 4, 2024
alexandrecuer and others added 4 commits June 4, 2024 10:48
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Copy link
Contributor

@epenet epenet left a comment

Choose a reason for hiding this comment

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

LGTM 👍
Can be merged once marked as ready

@alexandrecuer alexandrecuer marked this pull request as ready for review June 4, 2024 08:55
@home-assistant home-assistant bot requested a review from epenet June 4, 2024 08:55
@epenet epenet merged commit a1e4d4d into home-assistant:dev Jun 4, 2024
20 checks passed
dgomes pushed a commit to dgomes/home-assistant that referenced this pull request Jun 4, 2024
* Remove duplicate & property extra_state_attributes

* Add methods _update_attributes and _update_value

* correction in _update_value

* Update homeassistant/components/emoncms/sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/emoncms/sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/emoncms/sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/emoncms/sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
@alexandrecuer alexandrecuer deleted the 2024-06-01-01 branch June 4, 2024 14:15
@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 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.

3 participants