-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
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 unique identifiers where multiple IKEA Tradfri gateways are in use #136060
Fix unique identifiers where multiple IKEA Tradfri gateways are in use #136060
Conversation
Added migration function to execute upon initialisation, to: a) remove the erroneously-added config)_entry added to the device (gateway B gets added as a config_entry to a device associated to gateway A), and b) swap out the non-unique identifiers for genuinely unique identifiers.
…itly executing migrate_entity_unique_ids() from __init__.py)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So wait, what do you mean the problem is? Say I have 2 bridges. can the devices be used using both bridges (as in like a mesh?)?
Or are the device_ids just single numbers or so, first one gets 0, second 1, etc
@joostlek You can't have a single device paired to more than one gateway. The problem occurs in this HA integration when you try to use 2 or more Tradfri gateways, because of a combination of the way this integration was written and the way the Tradfri gateway firmware works. In my specific example, the two gateways I've got have multiple devices associated to each of them. The pytradfri (not 100% name is correct) library responsible for polling the gateways simply responds with the list of unique identifiers that the gateway has assigned to each of its paired devices. My assumption is that because the gateways are the same hardware and firmware versions, they are using the same starting-point in the sequence used to assign unique IDs to devices as each device is paired to the gateway. Then, because of the code-issue seen (that this PR attempts to resolve), the devices are added to the device-library using the gateway's unique identifier with no other context. This has led to a situation whereby multiple devices in the device-library have identical identifiers, despite technically being different devices associated to different gateways. I have discovered that the devices will display in HA as the device associated to whichever gateway in the Tradfri integration that was most-recently (re)loaded. Hopefully this example demonstrates the problem: My guess is that when the original version of this integration was written, it was not tested with multiple gateways. My resolution is to add each gateway's own unique ID into the identifiers in the device-library, like Edit: realised I never at any point thanked you for taking a look, so - thanks! 👍 |
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
To set expectations, I'll make changes over the next few days. |
So I've attempted to upgrade the pytradfri dependency as part of this fix, and run into a bunch of issues as a result. I suspect there might be an issue with the pytradfri library: home-assistant-libs/pytradfri#986 |
For this PR, the pytradfri dependency was restored to 9.0.1. There will be a forthcoming PR to change the tradfri component to support the latest version of pytradfri, as per home-assistant-libs/pytradfri#986 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I noticed we didn't have coverage for some lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Thanks for your help on getting this to completion @MartinHjelmare 👍 |
Great work! 👍 |
#136060) * Create unique identifiers where multiple gateways are in use Resolving issue #134497 * Added migration function to __init__.py Added migration function to execute upon initialisation, to: a) remove the erroneously-added config)_entry added to the device (gateway B gets added as a config_entry to a device associated to gateway A), and b) swap out the non-unique identifiers for genuinely unique identifiers. * Added tests to simulate migration from bad data scenario (i.e. explicitly executing migrate_entity_unique_ids() from __init__.py) * Ammendments suggested in first review * Changes after second review * Rewrite of test_migrate_config_entry_and_identifiers after feedback * Converted migrate function into major version, updated tests * Finalised variable naming convention per feedback, added test to validate config entry migrated to v2 * Hopefully final changes for cosmetic / comment stucture * Further code-coverage in test_migrate_config_entry_and_identifiers() * Minor test corrections * Added test for non-tradfri identifiers
Resolving issue #134497
Proposed change
This PR fixes a bug whereby users with multiple Tradfri gateways configured in the Tradfri integration will see inconsistent / randomly changing lists of devices associated to those gateways in Home Assistant.
This PR also introduces a migration function within the init.py file, to correct installations where multiple gateways have been configured in the Tradfri integration. The migration script does two things:
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.To help with the load of incoming pull requests: