-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Clean up device update, add via-hub #16659
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from homeassistant.loader import bind_hass | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| _UNDEF = object() | ||
|
|
||
| DATA_REGISTRY = 'device_registry' | ||
|
|
||
|
|
@@ -32,6 +33,7 @@ class DeviceEntry: | |
| model = attr.ib(type=str) | ||
| name = attr.ib(type=str, default=None) | ||
| sw_version = attr.ib(type=str, default=None) | ||
| hub_device_id = attr.ib(type=str, default=None) | ||
| id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex)) | ||
|
|
||
|
|
||
|
|
@@ -54,53 +56,99 @@ def async_get_device(self, identifiers: set, connections: set): | |
| return None | ||
|
|
||
| @callback | ||
| def async_get_or_create(self, *, config_entry, connections, identifiers, | ||
| manufacturer, model, name=None, sw_version=None): | ||
| def async_get_or_create(self, *, config_entry_id, connections, identifiers, | ||
| manufacturer, model, name=None, sw_version=None, | ||
| via_hub=None): | ||
| """Get device. Create if it doesn't exist.""" | ||
| if not identifiers and not connections: | ||
| return None | ||
|
|
||
| device = self.async_get_device(identifiers, connections) | ||
|
|
||
| if via_hub is not None: | ||
| hub_device = self.async_get_device({via_hub}, set()) | ||
| hub_device_id = hub_device.id if hub_device else None | ||
| else: | ||
| hub_device_id = None | ||
|
|
||
| if device is not None: | ||
| if config_entry not in device.config_entries: | ||
| device.config_entries.add(config_entry) | ||
| self.async_schedule_save() | ||
| return device | ||
| return self._async_update_device( | ||
| device.id, config_entry_id=config_entry_id, | ||
| hub_device_id=hub_device_id | ||
| ) | ||
|
|
||
| device = DeviceEntry( | ||
| config_entries=[config_entry], | ||
| config_entries={config_entry_id}, | ||
| connections=connections, | ||
| identifiers=identifiers, | ||
| manufacturer=manufacturer, | ||
| model=model, | ||
| name=name, | ||
| sw_version=sw_version | ||
| sw_version=sw_version, | ||
| hub_device_id=hub_device_id | ||
| ) | ||
| self.devices[device.id] = device | ||
|
|
||
| self.async_schedule_save() | ||
|
|
||
| return device | ||
|
|
||
| @callback | ||
| def _async_update_device(self, device_id, *, config_entry_id=_UNDEF, | ||
| remove_config_entry_id=_UNDEF, | ||
| hub_device_id=_UNDEF): | ||
| """Update device attributes.""" | ||
| old = self.devices[device_id] | ||
|
|
||
| changes = {} | ||
|
|
||
| config_entries = old.config_entries | ||
|
|
||
| if (config_entry_id is not _UNDEF and | ||
| config_entry_id not in old.config_entries): | ||
| config_entries = old.config_entries | {config_entry_id} | ||
|
|
||
| if (remove_config_entry_id is not _UNDEF and | ||
| remove_config_entry_id in config_entries): | ||
| config_entries = set(config_entries) | ||
| config_entries.remove(remove_config_entry_id) | ||
|
|
||
| if config_entries is not old.config_entries: | ||
| changes['config_entries'] = config_entries | ||
|
|
||
| if (hub_device_id is not _UNDEF and | ||
| hub_device_id != old.hub_device_id): | ||
| changes['hub_device_id'] = hub_device_id | ||
|
|
||
| if not changes: | ||
| return old | ||
|
|
||
| new = self.devices[device_id] = attr.evolve(old, **changes) | ||
| self.async_schedule_save() | ||
| return new | ||
|
|
||
| async def async_load(self): | ||
| """Load the device registry.""" | ||
| devices = await self._store.async_load() | ||
|
|
||
| if devices is None: | ||
| self.devices = OrderedDict() | ||
| return | ||
|
|
||
| self.devices = {device['id']: DeviceEntry( | ||
| config_entries=device['config_entries'], | ||
| connections={tuple(conn) for conn in device['connections']}, | ||
| identifiers={tuple(iden) for iden in device['identifiers']}, | ||
| manufacturer=device['manufacturer'], | ||
| model=device['model'], | ||
| name=device['name'], | ||
| sw_version=device['sw_version'], | ||
| id=device['id'], | ||
| ) for device in devices['devices']} | ||
| data = await self._store.async_load() | ||
|
|
||
| devices = OrderedDict() | ||
|
|
||
| if data is not None: | ||
| for device in data['devices']: | ||
| devices[device['id']] = DeviceEntry( | ||
| config_entries=set(device['config_entries']), | ||
| connections={tuple(conn) for conn in device['connections']}, | ||
| identifiers={tuple(iden) for iden in device['identifiers']}, | ||
|
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. line too long (80 > 79 characters) |
||
| manufacturer=device['manufacturer'], | ||
| model=device['model'], | ||
| name=device['name'], | ||
| sw_version=device['sw_version'], | ||
| id=device['id'], | ||
| # Introduced in 0.79 | ||
| hub_device_id=device.get('hub_device_id'), | ||
| ) | ||
|
|
||
| self.devices = devices | ||
|
|
||
| @callback | ||
| def async_schedule_save(self): | ||
|
|
@@ -122,18 +170,19 @@ def _data_to_save(self): | |
| 'name': entry.name, | ||
| 'sw_version': entry.sw_version, | ||
| 'id': entry.id, | ||
| 'hub_device_id': entry.hub_device_id, | ||
| } for entry in self.devices.values() | ||
| ] | ||
|
|
||
| return data | ||
|
|
||
| @callback | ||
| def async_clear_config_entry(self, config_entry): | ||
| def async_clear_config_entry(self, config_entry_id): | ||
| """Clear config entry from registry entries.""" | ||
| for device in self.devices.values(): | ||
| if config_entry in device.config_entries: | ||
| device.config_entries.remove(config_entry) | ||
| self.async_schedule_save() | ||
| for dev_id, device in self.devices.items(): | ||
| if config_entry_id in device.config_entries: | ||
| self._async_update_device( | ||
| dev_id, remove_config_entry_id=config_entry_id) | ||
|
|
||
|
|
||
| @bind_hass | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
line too long (80 > 79 characters)