Compare commits
2 commits
dev
...
dev-reg-fo
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d925068770 | ||
![]() |
d4c94f0cb2 |
3 changed files with 116 additions and 15 deletions
|
@ -38,6 +38,17 @@ class DeviceEntry:
|
||||||
id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex))
|
id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex))
|
||||||
|
|
||||||
|
|
||||||
|
def format_mac(mac):
|
||||||
|
"""Format the mac address string for entry into dev reg."""
|
||||||
|
lower_mac = mac.lower()
|
||||||
|
if len(lower_mac) == 12:
|
||||||
|
# no : included
|
||||||
|
return ':'.join(lower_mac[i:i + 2] for i in range(0, 12, 2))
|
||||||
|
|
||||||
|
# Either with ':' included or not sure how formatted
|
||||||
|
return lower_mac
|
||||||
|
|
||||||
|
|
||||||
class DeviceRegistry:
|
class DeviceRegistry:
|
||||||
"""Class to hold a registry of devices."""
|
"""Class to hold a registry of devices."""
|
||||||
|
|
||||||
|
@ -71,6 +82,12 @@ class DeviceRegistry:
|
||||||
if connections is None:
|
if connections is None:
|
||||||
connections = set()
|
connections = set()
|
||||||
|
|
||||||
|
connections = {
|
||||||
|
(key, format_mac(value)) if key == CONNECTION_NETWORK_MAC
|
||||||
|
else (key, value)
|
||||||
|
for key, value in connections
|
||||||
|
}
|
||||||
|
|
||||||
device = self.async_get_device(identifiers, connections)
|
device = self.async_get_device(identifiers, connections)
|
||||||
|
|
||||||
if device is None:
|
if device is None:
|
||||||
|
|
|
@ -136,6 +136,7 @@ async def test_unload_entry(hass):
|
||||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||||
entry.async_unload.return_value = mock_coro(True)
|
entry.async_unload.return_value = mock_coro(True)
|
||||||
deconzmock = Mock()
|
deconzmock = Mock()
|
||||||
|
deconzmock.config.mac = 'mock-mac'
|
||||||
deconzmock.async_load_parameters.return_value = mock_coro(True)
|
deconzmock.async_load_parameters.return_value = mock_coro(True)
|
||||||
deconzmock.sensors = {}
|
deconzmock.sensors = {}
|
||||||
with patch('pydeconz.DeconzSession', return_value=deconzmock):
|
with patch('pydeconz.DeconzSession', return_value=deconzmock):
|
||||||
|
|
|
@ -15,7 +15,9 @@ async def test_get_or_create_returns_same_entry(registry):
|
||||||
"""Make sure we do not duplicate entries."""
|
"""Make sure we do not duplicate entries."""
|
||||||
entry = registry.async_get_or_create(
|
entry = registry.async_get_or_create(
|
||||||
config_entry_id='1234',
|
config_entry_id='1234',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '0123')},
|
identifiers={('bridgeid', '0123')},
|
||||||
sw_version='sw-version',
|
sw_version='sw-version',
|
||||||
name='name',
|
name='name',
|
||||||
|
@ -23,12 +25,16 @@ async def test_get_or_create_returns_same_entry(registry):
|
||||||
model='model')
|
model='model')
|
||||||
entry2 = registry.async_get_or_create(
|
entry2 = registry.async_get_or_create(
|
||||||
config_entry_id='1234',
|
config_entry_id='1234',
|
||||||
connections={('ethernet', '11:22:33:44:55:66:77:88')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '11:22:33:66:77:88')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '0123')},
|
identifiers={('bridgeid', '0123')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
entry3 = registry.async_get_or_create(
|
entry3 = registry.async_get_or_create(
|
||||||
config_entry_id='1234',
|
config_entry_id='1234',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')}
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(registry.devices) == 1
|
assert len(registry.devices) == 1
|
||||||
|
@ -46,7 +52,9 @@ async def test_requirement_for_identifier_or_connection(registry):
|
||||||
"""Make sure we do require some descriptor of device."""
|
"""Make sure we do require some descriptor of device."""
|
||||||
entry = registry.async_get_or_create(
|
entry = registry.async_get_or_create(
|
||||||
config_entry_id='1234',
|
config_entry_id='1234',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers=set(),
|
identifiers=set(),
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
entry2 = registry.async_get_or_create(
|
entry2 = registry.async_get_or_create(
|
||||||
|
@ -70,17 +78,23 @@ async def test_multiple_config_entries(registry):
|
||||||
"""Make sure we do not get duplicate entries."""
|
"""Make sure we do not get duplicate entries."""
|
||||||
entry = registry.async_get_or_create(
|
entry = registry.async_get_or_create(
|
||||||
config_entry_id='123',
|
config_entry_id='123',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '0123')},
|
identifiers={('bridgeid', '0123')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
entry2 = registry.async_get_or_create(
|
entry2 = registry.async_get_or_create(
|
||||||
config_entry_id='456',
|
config_entry_id='456',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '0123')},
|
identifiers={('bridgeid', '0123')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
entry3 = registry.async_get_or_create(
|
entry3 = registry.async_get_or_create(
|
||||||
config_entry_id='123',
|
config_entry_id='123',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '0123')},
|
identifiers={('bridgeid', '0123')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
|
|
||||||
|
@ -110,7 +124,7 @@ async def test_loading_from_storage(hass, hass_storage):
|
||||||
'identifiers': [
|
'identifiers': [
|
||||||
[
|
[
|
||||||
'serial',
|
'serial',
|
||||||
'12:34:56:78:90:AB:CD:EF'
|
'12:34:56:AB:CD:EF'
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
'manufacturer': 'manufacturer',
|
'manufacturer': 'manufacturer',
|
||||||
|
@ -127,7 +141,7 @@ async def test_loading_from_storage(hass, hass_storage):
|
||||||
entry = registry.async_get_or_create(
|
entry = registry.async_get_or_create(
|
||||||
config_entry_id='1234',
|
config_entry_id='1234',
|
||||||
connections={('Zigbee', '01.23.45.67.89')},
|
connections={('Zigbee', '01.23.45.67.89')},
|
||||||
identifiers={('serial', '12:34:56:78:90:AB:CD:EF')},
|
identifiers={('serial', '12:34:56:AB:CD:EF')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
assert entry.id == 'abcdefghijklm'
|
assert entry.id == 'abcdefghijklm'
|
||||||
assert isinstance(entry.config_entries, set)
|
assert isinstance(entry.config_entries, set)
|
||||||
|
@ -137,17 +151,23 @@ async def test_removing_config_entries(registry):
|
||||||
"""Make sure we do not get duplicate entries."""
|
"""Make sure we do not get duplicate entries."""
|
||||||
entry = registry.async_get_or_create(
|
entry = registry.async_get_or_create(
|
||||||
config_entry_id='123',
|
config_entry_id='123',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '0123')},
|
identifiers={('bridgeid', '0123')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
entry2 = registry.async_get_or_create(
|
entry2 = registry.async_get_or_create(
|
||||||
config_entry_id='456',
|
config_entry_id='456',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '0123')},
|
identifiers={('bridgeid', '0123')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
entry3 = registry.async_get_or_create(
|
entry3 = registry.async_get_or_create(
|
||||||
config_entry_id='123',
|
config_entry_id='123',
|
||||||
connections={('ethernet', '34:56:78:90:AB:CD:EF:12')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '34:56:78:CD:EF:12')
|
||||||
|
},
|
||||||
identifiers={('bridgeid', '4567')},
|
identifiers={('bridgeid', '4567')},
|
||||||
manufacturer='manufacturer', model='model')
|
manufacturer='manufacturer', model='model')
|
||||||
|
|
||||||
|
@ -168,7 +188,9 @@ async def test_specifying_hub_device_create(registry):
|
||||||
"""Test specifying a hub and updating."""
|
"""Test specifying a hub and updating."""
|
||||||
hub = registry.async_get_or_create(
|
hub = registry.async_get_or_create(
|
||||||
config_entry_id='123',
|
config_entry_id='123',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('hue', '0123')},
|
identifiers={('hue', '0123')},
|
||||||
manufacturer='manufacturer', model='hub')
|
manufacturer='manufacturer', model='hub')
|
||||||
|
|
||||||
|
@ -195,7 +217,9 @@ async def test_specifying_hub_device_update(registry):
|
||||||
|
|
||||||
hub = registry.async_get_or_create(
|
hub = registry.async_get_or_create(
|
||||||
config_entry_id='123',
|
config_entry_id='123',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('hue', '0123')},
|
identifiers={('hue', '0123')},
|
||||||
manufacturer='manufacturer', model='hub')
|
manufacturer='manufacturer', model='hub')
|
||||||
|
|
||||||
|
@ -213,7 +237,9 @@ async def test_loading_saving_data(hass, registry):
|
||||||
"""Test that we load/save data correctly."""
|
"""Test that we load/save data correctly."""
|
||||||
orig_hub = registry.async_get_or_create(
|
orig_hub = registry.async_get_or_create(
|
||||||
config_entry_id='123',
|
config_entry_id='123',
|
||||||
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
identifiers={('hue', '0123')},
|
identifiers={('hue', '0123')},
|
||||||
manufacturer='manufacturer', model='hub')
|
manufacturer='manufacturer', model='hub')
|
||||||
|
|
||||||
|
@ -239,3 +265,60 @@ async def test_loading_saving_data(hass, registry):
|
||||||
|
|
||||||
assert orig_hub == new_hub
|
assert orig_hub == new_hub
|
||||||
assert orig_light == new_light
|
assert orig_light == new_light
|
||||||
|
|
||||||
|
|
||||||
|
async def test_format_mac(registry):
|
||||||
|
"""Make sure we normalize mac addresses."""
|
||||||
|
entry = registry.async_get_or_create(
|
||||||
|
config_entry_id='1234',
|
||||||
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
|
||||||
|
},
|
||||||
|
)
|
||||||
|
entry2 = registry.async_get_or_create(
|
||||||
|
config_entry_id='1234',
|
||||||
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '123456ABCDEF')
|
||||||
|
},
|
||||||
|
)
|
||||||
|
entry3 = registry.async_get_or_create(
|
||||||
|
config_entry_id='1234',
|
||||||
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '123456abcdef')
|
||||||
|
},
|
||||||
|
)
|
||||||
|
entry4 = registry.async_get_or_create(
|
||||||
|
config_entry_id='1234',
|
||||||
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:ab:cd:ef')
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert entry.connections == {
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:ab:cd:ef')
|
||||||
|
}
|
||||||
|
|
||||||
|
assert entry.id == entry2.id
|
||||||
|
assert entry2.connections == {
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:ab:cd:ef')
|
||||||
|
}
|
||||||
|
|
||||||
|
assert entry.id == entry3.id
|
||||||
|
assert entry2.connections == {
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:ab:cd:ef')
|
||||||
|
}
|
||||||
|
|
||||||
|
assert entry.id == entry4.id
|
||||||
|
assert entry4.connections == {
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:ab:cd:ef')
|
||||||
|
}
|
||||||
|
|
||||||
|
# This should not raise
|
||||||
|
invalid_mac_entry = registry.async_get_or_create(
|
||||||
|
config_entry_id='1234',
|
||||||
|
connections={
|
||||||
|
(device_registry.CONNECTION_NETWORK_MAC, 'invalid_mac')
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert list(invalid_mac_entry.connections)[0][1] == 'invalid_mac'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue