Fix racy homekit_controller platform setup caused by #22368 #22655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
#22194 introduced some new homekit_controller tests and @balloob and @awarecan pointed out at least one of them was flaky. This doesn't happen in my config entry branch and i've now traced its root cause to #22368.
Essentially,
HKDevice.__init__
on dev has for some time triggered thesetup_platform
for its sub-domains like light/climate/etc. But there is a race where this code hasn't run yet:If the setup_platform runs before that happens you get the error in the test. This fix moves where we store the current HKDevice into hass.data so that it is always set before any further
setup_platform
is called.This race is probably a real bug, not just a flaky test. So if #22368 is already in a tag or in an rc branch it might be worth applying the fix there too.
This is something of a temporary band aid - the buggy code is actually removed in my config entry branch and doesn't have this problem. The config entry branch won't trigger side effects in
HKDevice.__init__
any more. So the config entry work will be the 'real' fix, but this will do until then.**Related issue (if applicable): #22650
Checklist:
tox
. Your PR cannot be merged unless tests pass