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 startup creating entities with non-unique IDs #99679

Merged

Conversation

puddly
Copy link
Contributor

@puddly puddly commented Sep 5, 2023

Proposed change

#98082 introduced a regression where startup retries caused entities to be re-created multiple times with the same IDs, triggering errors. I've reorganized startup to only create entities only once.

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 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:

@home-assistant
Copy link

home-assistant bot commented Sep 5, 2023

Hey there @dmulcahey, @Adminiuga, 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.

@puddly puddly force-pushed the puddly/zha-gateway-restartable branch from 9177624 to 2eedfbd Compare September 5, 2023 16:44
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Looks good!

@@ -134,7 +134,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
else:
_LOGGER.debug("ZHA storage file does not exist or was already removed")

zha_gateway = ZHAGateway(hass, config, config_entry)
# Re-use the gateway object between ZHA reloads
if (zha_gateway := zha_data.get(DATA_ZHA_GATEWAY)) is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Not for this PR, but zha_data should be a dataclass.

Comment on lines +155 to +156
self.ha_device_registry = dr.async_get(self._hass)
self.ha_entity_registry = er.async_get(self._hass)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also not for this PR, but this looks like a remnant from the olden days when getting the registries required await-ing. I'd suggest to just remove it.

@emontnemery emontnemery merged commit 7ab1913 into home-assistant:dev Sep 5, 2023
21 checks passed
@@ -134,7 +134,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
else:
_LOGGER.debug("ZHA storage file does not exist or was already removed")

zha_gateway = ZHAGateway(hass, config, config_entry)
# Re-use the gateway object between ZHA reloads
if (zha_gateway := zha_data.get(DATA_ZHA_GATEWAY)) is None:
Copy link
Member

Choose a reason for hiding this comment

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

Reusing the gateway instance requires special care in the gateway to manage the instance state and life cycle. Have we reviewed all the instance attributes of the gateway and documented that all instance attributes and future changes need to take this into consideration?

Copy link
Contributor

Choose a reason for hiding this comment

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

what about this instead? #99694

@dmulcahey dmulcahey mentioned this pull request Sep 5, 2023
20 tasks
bramkragten pushed a commit that referenced this pull request Sep 6, 2023
* Make the ZHAGateway initialization restartable so entities are unique

* Add a unit test
@github-actions github-actions bot locked and limited conversation to collaborators Sep 7, 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.

Platform zha does not generate unique IDs - ZHA/SkyConnect/Multipan
6 participants