1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Normalize MAC addresses (#16916)

* Normalize MAC addresses

* Handle all mac formats
This commit is contained in:
Paulus Schoutsen
2018-11-06 16:33:31 +01:00
committed by Pascal Vizeli
parent 42fea4fb97
commit bde02afe4f
2 changed files with 109 additions and 15 deletions

View File

@@ -38,6 +38,25 @@ class DeviceEntry:
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."""
to_test = mac
if len(to_test) == 17 and to_test.count(':') == 5:
return to_test.lower()
elif len(to_test) == 17 and to_test.count('-') == 5:
to_test = to_test.replace('-', '')
elif len(to_test) == 14 and to_test.count('.') == 2:
to_test = to_test.replace('.', '')
if len(to_test) == 12:
# no : included
return ':'.join(to_test.lower()[i:i + 2] for i in range(0, 12, 2))
# Not sure how formatted, return original
return mac
class DeviceRegistry:
"""Class to hold a registry of devices."""
@@ -71,6 +90,12 @@ class DeviceRegistry:
if connections is None:
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)
if device is None: