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

Fix ZHA creating unnecessary "summ received" entity after upgrade #109268

Conversation

TheJulianJES
Copy link
Contributor

@TheJulianJES TheJulianJES commented Feb 1, 2024

Proposed change

Do not create the ZHA current_summ_received entity until initialized once to avoid unnecessary/ghost entities.

The current_summ_received attribute only started to be initialized in HA 2024.2.0.
Due to ZHA attribute initialization happening in the background for existing devices (to read the attribute and see if it's supported/unsupported), this entity would always get created directly after the upgrade for all power-measuring devices.
If HA is then restarted, the entity would turn into a ghost entity, as ZHA then detected that it's unsupported.

To avoid creating a lot of unnecessary entities for existing devices, this PR waits until that specific attribute was properly initialized once for now before creating the entity.
We can't do this for all (sensor) entities, as we want some entities to be created, even if no report was sent yet. Our test cases also test for that behavior.

This is relevant for HA Core 2024.2.0 (betas), so also marked this for 2024.2.0 to avoid the issue of creating that entity unnecessarily.
For anyone affected on current betas, the ghost entity can be removed using the UI.

Tests

Due to this entity differing from our other "normal" (non-diagnostic) entities (which are always created, even if the attribute isn't initialized), I've had to adapt the tests for this new behavior.
More details are provided in a commit message where the tests are updated.

The entities are removed from the ZHA devices list, as the attributes are all None there by default, so this entity isn't created in that case.

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

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 (modified) 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:

The attribute isn't initialized for these devices in the test (which our check now expects it to be), hence we need to remove them from this list.
…ntity

The attribute needs to be initialized for it to be created which we do by plugging the attribute read.
The test expects the initial state to be "unknown", but hence we plugged the attribute (to create the entity), the state is whatever we plug the attribute read as.
@home-assistant
Copy link

home-assistant bot commented Feb 1, 2024

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

Code owner commands

Code owners of zha 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 zha 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.

@TheJulianJES TheJulianJES changed the title Fix ZHA creating unnecessary current_summ_received after upgrade Fix ZHA creating unnecessary "summ received" entity after upgrade Feb 1, 2024
@TheJulianJES TheJulianJES marked this pull request as ready for review February 5, 2024 23:54
@TheJulianJES TheJulianJES added this to the 2024.2.0 milestone Feb 5, 2024
@dmulcahey dmulcahey merged commit 965f31a into home-assistant:dev Feb 6, 2024
23 checks passed
frenck pushed a commit that referenced this pull request Feb 6, 2024
…09268)

* Do not create `current_summ_received` entity until initialized once

* Update zha_devices_list.py to not expect summation received entities

The attribute isn't initialized for these devices in the test (which our check now expects it to be), hence we need to remove them from this list.

* Update sensor tests to have initial state for current_summ_received entity

The attribute needs to be initialized for it to be created which we do by plugging the attribute read.
The test expects the initial state to be "unknown", but hence we plugged the attribute (to create the entity), the state is whatever we plug the attribute read as.

* Update sensor tests to expect not updating current_summ_received entity if it doesn't exist
@github-actions github-actions bot locked and limited conversation to collaborators Feb 7, 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.

None yet

6 participants