-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Normalize unique ID in WLED #157901
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
Normalize unique ID in WLED #157901
Changes from 12 commits
2c66613
3d8b579
163441e
88de98d
f305b0d
16f4552
ccc5a48
fc4c492
b23dcda
10f8189
acae2fe
dbe9cc0
57b3f42
5a3b79e
30c1906
85ed198
cc71388
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,10 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
|
|
||
| from homeassistant.config_entries import SOURCE_IGNORE | ||
| from homeassistant.const import Platform | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import config_validation as cv | ||
|
|
@@ -13,8 +17,11 @@ | |
| WLEDConfigEntry, | ||
| WLEDDataUpdateCoordinator, | ||
| WLEDReleasesDataUpdateCoordinator, | ||
| normalize_mac_address, | ||
| ) | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| PLATFORMS = ( | ||
| Platform.BUTTON, | ||
| Platform.LIGHT, | ||
|
|
@@ -63,3 +70,70 @@ async def async_unload_entry(hass: HomeAssistant, entry: WLEDConfigEntry) -> boo | |
| coordinator.unsub() | ||
|
|
||
| return unload_ok | ||
|
|
||
|
|
||
| async def async_migrate_entry( | ||
| hass: HomeAssistant, config_entry: WLEDConfigEntry | ||
| ) -> bool: | ||
| """Migrate old entry.""" | ||
| _LOGGER.debug( | ||
| "Migrating configuration from version %s.%s", | ||
| config_entry.version, | ||
| config_entry.minor_version, | ||
| ) | ||
|
|
||
| if config_entry.version > 1: | ||
| # The user has downgraded from a future version | ||
| return False | ||
|
|
||
| if config_entry.version == 1: | ||
| if config_entry.minor_version < 2: | ||
| if config_entry.unique_id is None: | ||
| _LOGGER.warning( | ||
| "Config entry is missing unique ID, cannot migrate to version 1.2" | ||
| ) | ||
| return False | ||
| normalized_mac_address = normalize_mac_address(config_entry.unique_id) | ||
| duplicate_entries = [ | ||
| entry | ||
| for entry in hass.config_entries.async_entries(DOMAIN) | ||
| if entry.unique_id | ||
| and normalize_mac_address(entry.unique_id) == normalized_mac_address | ||
| ] | ||
| ignored_entries = [ | ||
| entry | ||
| for entry in duplicate_entries | ||
| if entry.entry_id != config_entry.entry_id | ||
| and entry.source == SOURCE_IGNORE | ||
| ] | ||
| if ignored_entries: | ||
| _LOGGER.info( | ||
| "Found %d ignored WLED config entries with the same MAC address, removing them", | ||
| len(ignored_entries), | ||
| ) | ||
| await asyncio.gather( | ||
| *[ | ||
| hass.config_entries.async_remove(entry.entry_id) | ||
| for entry in ignored_entries | ||
| ] | ||
| ) | ||
| if len(duplicate_entries) - len(ignored_entries) > 1: | ||
| _LOGGER.warning( | ||
| "Found multiple WLED config entries with the same MAC address, cannot migrate to version 1.2" | ||
| ) | ||
| return False | ||
|
Comment on lines
+120
to
+124
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would the other ones be? mDNS? Maybe disabled? Which ones are ones we don't know what to do with?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is with those entries that were created with an incorrect unique ID, e.g. |
||
|
|
||
| hass.config_entries.async_update_entry( | ||
| config_entry, | ||
| unique_id=normalized_mac_address, | ||
| version=1, | ||
| minor_version=2, | ||
| ) | ||
|
|
||
| _LOGGER.debug( | ||
| "Migration to configuration version %s.%s successful", | ||
| config_entry.version, | ||
| config_entry.minor_version, | ||
| ) | ||
|
|
||
| return True | ||
Uh oh!
There was an error while loading. Please reload this page.