-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Device registry store config entry #16152
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
Changes from 8 commits
a065271
936c14c
f141039
b8adc43
4ab222b
11475f2
2f34d27
e2d88fb
a269164
728a72c
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 |
|---|---|---|
|
|
@@ -23,8 +23,9 @@ | |
| class DeviceEntry: | ||
| """Device Registry Entry.""" | ||
|
|
||
| connection = attr.ib(type=list) | ||
| identifiers = attr.ib(type=list) | ||
| config_entries = attr.ib(type=set, converter=set) | ||
| connections = attr.ib(type=set, converter=set) | ||
| identifiers = attr.ib(type=set, converter=set) | ||
| manufacturer = attr.ib(type=str) | ||
| model = attr.ib(type=str) | ||
| name = attr.ib(type=str, default=None) | ||
|
|
@@ -46,29 +47,33 @@ def async_get_device(self, identifiers: str, connections: tuple): | |
| """Check if device is registered.""" | ||
| for device in self.devices: | ||
| if any(iden in device.identifiers for iden in identifiers) or \ | ||
| any(conn in device.connection for conn in connections): | ||
| any(conn in device.connections for conn in connections): | ||
| return device | ||
| return None | ||
|
|
||
| @callback | ||
| def async_get_or_create(self, *, connection, identifiers, manufacturer, | ||
| model, name=None, sw_version=None): | ||
| def async_get_or_create(self, *, config_entry, connections, identifiers, | ||
| manufacturer, model, name=None, sw_version=None): | ||
| """Get device. Create if it doesn't exist.""" | ||
| device = self.async_get_device(identifiers, connection) | ||
| device = self.async_get_device(identifiers, connections) | ||
|
|
||
| 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 | ||
|
|
||
| device = DeviceEntry( | ||
|
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. We should not create a new device if it has no connections or identifiers, as it can never be retrieved again
Member
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. Should identifiers be mandatory or should it check and fail for any of them?
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. I guess you're right that at least one is mandatory, I am fine with ignoring it when it doesn't match and just print a warning |
||
| connection=connection, | ||
| config_entries=[config_entry], | ||
| connections=connections, | ||
| identifiers=identifiers, | ||
| manufacturer=manufacturer, | ||
| model=model, | ||
| name=name, | ||
| sw_version=sw_version | ||
| ) | ||
|
|
||
| self.devices.append(device) | ||
|
|
||
| self.async_schedule_save() | ||
|
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. We should only save if the device has changed.
Member
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. Allright |
||
|
|
||
| return device | ||
|
|
@@ -81,7 +86,16 @@ async def async_load(self): | |
| self.devices = [] | ||
| return | ||
|
|
||
| self.devices = [DeviceEntry(**device) for device in devices['devices']] | ||
| self.devices = [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']] | ||
|
|
||
| @callback | ||
| def async_schedule_save(self): | ||
|
|
@@ -95,13 +109,14 @@ def _data_to_save(self): | |
|
|
||
| data['devices'] = [ | ||
| { | ||
| 'id': entry.id, | ||
| 'connection': entry.connection, | ||
| 'identifiers': entry.identifiers, | ||
| 'config_entries': list(entry.config_entries), | ||
| 'connections': list(entry.connections), | ||
| 'identifiers': list(entry.identifiers), | ||
| 'manufacturer': entry.manufacturer, | ||
| 'model': entry.model, | ||
| 'name': entry.name, | ||
| 'sw_version': entry.sw_version, | ||
| 'id': entry.id, | ||
| } for entry in self.devices | ||
| ] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,15 +272,16 @@ async def _async_add_entity(self, entity, update_before_add, | |
| else: | ||
| config_entry_id = None | ||
|
|
||
| device = entity.device | ||
| if device is not None: | ||
| device_info = entity.device_info | ||
| if config_entry_id is not None and device_info is not None: | ||
| device = device_registry.async_get_or_create( | ||
| connection=device['connection'], | ||
| identifiers=device['identifiers'], | ||
| manufacturer=device['manufacturer'], | ||
| model=device['model'], | ||
| name=device.get('name'), | ||
| sw_version=device.get('sw_version')) | ||
| config_entry=config_entry_id, | ||
| connections=device_info['connections'], | ||
|
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. This will blow up if the key We should probably be defensive for all values and use
Member
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. Yes, we already have an issue report on this it seems :) |
||
| identifiers=device_info['identifiers'], | ||
| manufacturer=device_info['manufacturer'], | ||
| model=device_info['model'], | ||
| name=device_info.get('name'), | ||
| sw_version=device_info.get('sw_version')) | ||
| device_id = device.id | ||
| else: | ||
| device_id = None | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Trigger save if it didn't contain the config entry.