Add friendly name to devices in the device registry (#21318)

* add friendly name to devices in the device registry

* switch to name_by_user

* review comments
pull/21466/head
David F. Mulcahey 2019-02-26 15:20:16 -05:00 committed by Paulus Schoutsen
parent a34524febe
commit c4400be62a
4 changed files with 34 additions and 10 deletions

View File

@ -19,6 +19,7 @@ SCHEMA_WS_UPDATE = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): WS_TYPE_UPDATE,
vol.Required('device_id'): str,
vol.Optional('area_id'): vol.Any(str, None),
vol.Optional('name_by_user'): vol.Any(str, None),
})
@ -49,11 +50,13 @@ async def websocket_update_device(hass, connection, msg):
"""Handle update area websocket command."""
registry = await async_get_registry(hass)
entry = registry.async_update_device(
msg['device_id'], area_id=msg['area_id'])
msg.pop('type')
msg_id = msg.pop('id')
entry = registry.async_update_device(**msg)
connection.send_message(websocket_api.result_message(
msg['id'], _entry_dict(entry)
msg_id, _entry_dict(entry)
))
@ -70,4 +73,5 @@ def _entry_dict(entry):
'id': entry.id,
'hub_device_id': entry.hub_device_id,
'area_id': entry.area_id,
'name_by_user': entry.name_by_user,
}

View File

@ -37,6 +37,7 @@ class DeviceEntry:
sw_version = attr.ib(type=str, default=None)
hub_device_id = attr.ib(type=str, default=None)
area_id = attr.ib(type=str, default=None)
name_by_user = attr.ib(type=str, default=None)
id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex))
@ -124,9 +125,11 @@ class DeviceRegistry:
)
@callback
def async_update_device(self, device_id, *, area_id=_UNDEF):
def async_update_device(
self, device_id, *, area_id=_UNDEF, name_by_user=_UNDEF):
"""Update properties of a device."""
return self._async_update_device(device_id, area_id=area_id)
return self._async_update_device(
device_id, area_id=area_id, name_by_user=name_by_user)
@callback
def _async_update_device(self, device_id, *, add_config_entry_id=_UNDEF,
@ -138,7 +141,8 @@ class DeviceRegistry:
name=_UNDEF,
sw_version=_UNDEF,
hub_device_id=_UNDEF,
area_id=_UNDEF):
area_id=_UNDEF,
name_by_user=_UNDEF):
"""Update device attributes."""
old = self.devices[device_id]
@ -179,6 +183,10 @@ class DeviceRegistry:
if (area_id is not _UNDEF and area_id != old.area_id):
changes['area_id'] = area_id
if (name_by_user is not _UNDEF and
name_by_user != old.name_by_user):
changes['name_by_user'] = name_by_user
if not changes:
return old
@ -208,7 +216,8 @@ class DeviceRegistry:
# Introduced in 0.79
hub_device_id=device.get('hub_device_id'),
# Introduced in 0.87
area_id=device.get('area_id')
area_id=device.get('area_id'),
name_by_user=device.get('name_by_user')
)
self.devices = devices
@ -234,7 +243,8 @@ class DeviceRegistry:
'sw_version': entry.sw_version,
'id': entry.id,
'hub_device_id': entry.hub_device_id,
'area_id': entry.area_id
'area_id': entry.area_id,
'name_by_user': entry.name_by_user
} for entry in self.devices.values()
]

View File

@ -49,6 +49,7 @@ async def test_list_devices(hass, client, registry):
'sw_version': None,
'hub_device_id': None,
'area_id': None,
'name_by_user': None,
},
{
'config_entries': ['1234'],
@ -59,6 +60,7 @@ async def test_list_devices(hass, client, registry):
'sw_version': None,
'hub_device_id': dev1,
'area_id': None,
'name_by_user': None,
}
]
@ -72,11 +74,13 @@ async def test_update_device(hass, client, registry):
manufacturer='manufacturer', model='model')
assert not device.area_id
assert not device.name_by_user
await client.send_json({
'id': 1,
'device_id': device.id,
'area_id': '12345A',
'name_by_user': 'Test Friendly Name',
'type': 'config/device_registry/update',
})
@ -84,4 +88,5 @@ async def test_update_device(hass, client, registry):
assert msg['result']['id'] == device.id
assert msg['result']['area_id'] == '12345A'
assert msg['result']['name_by_user'] == 'Test Friendly Name'
assert len(registry.devices) == 1

View File

@ -133,7 +133,8 @@ async def test_loading_from_storage(hass, hass_storage):
'model': 'model',
'name': 'name',
'sw_version': 'version',
'area_id': '12345A'
'area_id': '12345A',
'name_by_user': 'Test Friendly Name'
}
]
}
@ -148,6 +149,7 @@ async def test_loading_from_storage(hass, hass_storage):
manufacturer='manufacturer', model='model')
assert entry.id == 'abcdefghijklm'
assert entry.area_id == '12345A'
assert entry.name_by_user == 'Test Friendly Name'
assert isinstance(entry.config_entries, set)
@ -360,8 +362,11 @@ async def test_update(registry):
})
assert not entry.area_id
assert not entry.name_by_user
updated_entry = registry.async_update_device(entry.id, area_id='12345A')
updated_entry = registry.async_update_device(
entry.id, area_id='12345A', name_by_user='Test Friendly Name')
assert updated_entry != entry
assert updated_entry.area_id == '12345A'
assert updated_entry.name_by_user == 'Test Friendly Name'