mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix and test for prefixed MAC addresses. (#5052)
* Fix and test for prefixed MAC addresses. * Fix style. * Don't commit old code. * Fix style.
This commit is contained in:
parent
e5dfcf7310
commit
43e5d28643
@ -485,13 +485,18 @@ class Device(Entity):
|
|||||||
if not self.mac:
|
if not self.mac:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if '_' in self.mac:
|
||||||
|
_, mac = self.mac.split('_', 1)
|
||||||
|
else:
|
||||||
|
mac = self.mac
|
||||||
|
|
||||||
# prevent lookup of invalid macs
|
# prevent lookup of invalid macs
|
||||||
if not len(self.mac.split(':')) == 6:
|
if not len(mac.split(':')) == 6:
|
||||||
return 'unknown'
|
return 'unknown'
|
||||||
|
|
||||||
# we only need the first 3 bytes of the mac for a lookup
|
# we only need the first 3 bytes of the mac for a lookup
|
||||||
# this improves somewhat on privacy
|
# this improves somewhat on privacy
|
||||||
oui_bytes = self.mac.split(':')[0:3]
|
oui_bytes = mac.split(':')[0:3]
|
||||||
# bytes like 00 get truncates to 0, API needs full bytes
|
# bytes like 00 get truncates to 0, API needs full bytes
|
||||||
oui = '{:02x}:{:02x}:{:02x}'.format(*[int(b, 16) for b in oui_bytes])
|
oui = '{:02x}:{:02x}:{:02x}'.format(*[int(b, 16) for b in oui_bytes])
|
||||||
url = 'http://api.macvendors.com/' + oui
|
url = 'http://api.macvendors.com/' + oui
|
||||||
|
@ -210,6 +210,40 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(device.vendor, vendor_string)
|
self.assertEqual(device.vendor, vendor_string)
|
||||||
|
|
||||||
|
def test_mac_vendor_mac_formats(self):
|
||||||
|
"""Verify all variations of MAC addresses are handled correctly."""
|
||||||
|
vendor_string = 'Raspberry Pi Foundation'
|
||||||
|
|
||||||
|
with mock_aiohttp_client() as aioclient_mock:
|
||||||
|
aioclient_mock.get('http://api.macvendors.com/b8:27:eb',
|
||||||
|
text=vendor_string)
|
||||||
|
aioclient_mock.get('http://api.macvendors.com/00:27:eb',
|
||||||
|
text=vendor_string)
|
||||||
|
|
||||||
|
mac = 'B8:27:EB:00:00:00'
|
||||||
|
device = device_tracker.Device(
|
||||||
|
self.hass, timedelta(seconds=180),
|
||||||
|
True, 'test', mac, 'Test name')
|
||||||
|
run_coroutine_threadsafe(device.set_vendor_for_mac(),
|
||||||
|
self.hass.loop).result()
|
||||||
|
self.assertEqual(device.vendor, vendor_string)
|
||||||
|
|
||||||
|
mac = '0:27:EB:00:00:00'
|
||||||
|
device = device_tracker.Device(
|
||||||
|
self.hass, timedelta(seconds=180),
|
||||||
|
True, 'test', mac, 'Test name')
|
||||||
|
run_coroutine_threadsafe(device.set_vendor_for_mac(),
|
||||||
|
self.hass.loop).result()
|
||||||
|
self.assertEqual(device.vendor, vendor_string)
|
||||||
|
|
||||||
|
mac = 'PREFIXED_B8:27:EB:00:00:00'
|
||||||
|
device = device_tracker.Device(
|
||||||
|
self.hass, timedelta(seconds=180),
|
||||||
|
True, 'test', mac, 'Test name')
|
||||||
|
run_coroutine_threadsafe(device.set_vendor_for_mac(),
|
||||||
|
self.hass.loop).result()
|
||||||
|
self.assertEqual(device.vendor, vendor_string)
|
||||||
|
|
||||||
def test_mac_vendor_lookup_unknown(self):
|
def test_mac_vendor_lookup_unknown(self):
|
||||||
"""Prevent another mac vendor lookup if was not found first time."""
|
"""Prevent another mac vendor lookup if was not found first time."""
|
||||||
mac = 'B8:27:EB:00:00:00'
|
mac = 'B8:27:EB:00:00:00'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user