Macvendor (#4468)
* Add MAC vendor lookup for device_tracker. * Test vendor mac lookup and fix device attribute. * Generate requirements. * Style. * Use hyphen instead of underscore to satisfy 'idna'. https://github.com/kjd/idna/issues/17 * Resort imports. * Refactor macvendor to use macvendors.com API instead of netaddr library. * Test vendor lookup using macvendors.com api. * Remove debugging. * Correct description. * No longer needed. * Device tracker is now an async component. Fix ddwrt tests. * Fix linting. * Add test case for error conditions. * There is no reason to retry failes vendor loopups as they won't be saved to the file anyways at that point. * Sorry, bad assumption, this only made things worse. * Wait for async parts during setup component to complete before asserting results. * Fix linting. * Is generated when running 'coverage html'. * Undo isort. * Make aioclient_mock exception more generic. * Only lookup mac vendor string with adding new device to known_devices.yaml. * Undo isort. * Revert unneeded change. * Adjust to use new websession pattern. * Always make sure to cleanup response. * Use correct function to release response. * Fix tests.
This commit is contained in:
1 parent
f09b888a8a
commit
08f8e540e3
5 files changed
+156
-4
No files matched your search
@@ -3,6 +3,7 @@ import os
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import logging
|
||||
import re
|
||||
import requests
|
||||
import requests_mock
|
||||
|
||||
@@ -17,6 +18,8 @@ from homeassistant.util import slugify
|
||||
from tests.common import (
|
||||
get_test_home_assistant, assert_setup_component, load_fixture)
|
||||
|
||||
from ...test_util.aiohttp import mock_aiohttp_client
|
||||
|
||||
TEST_HOST = '127.0.0.1'
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -26,6 +29,13 @@ class TestDdwrt(unittest.TestCase):
|
||||
|
||||
hass = None
|
||||
|
||||
def run(self, result=None):
|
||||
"""Mock out http calls to macvendor API for whole test suite."""
|
||||
with mock_aiohttp_client() as aioclient_mock:
|
||||
macvendor_re = re.compile('http://api.macvendors.com/.*')
|
||||
aioclient_mock.get(macvendor_re, text='')
|
||||
super().run(result)
|
||||
|
||||
def setup_method(self, _):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -136,6 +146,7 @@ class TestDdwrt(unittest.TestCase):
|
||||
CONF_USERNAME: 'fake_user',
|
||||
CONF_PASSWORD: '0'
|
||||
}})
|
||||
self.hass.block_till_done()
|
||||
|
||||
path = self.hass.config.path(device_tracker.YAML_DEVICES)
|
||||
devices = config.load_yaml_config_file(path)
|
||||
@@ -164,6 +175,7 @@ class TestDdwrt(unittest.TestCase):
|
||||
CONF_USERNAME: 'fake_user',
|
||||
CONF_PASSWORD: '0'
|
||||
}})
|
||||
self.hass.block_till_done()
|
||||
|
||||
path = self.hass.config.path(device_tracker.YAML_DEVICES)
|
||||
devices = config.load_yaml_config_file(path)
|
||||
@@ -192,6 +204,7 @@ class TestDdwrt(unittest.TestCase):
|
||||
CONF_USERNAME: 'fake_user',
|
||||
CONF_PASSWORD: '0'
|
||||
}})
|
||||
self.hass.block_till_done()
|
||||
|
||||
path = self.hass.config.path(device_tracker.YAML_DEVICES)
|
||||
devices = config.load_yaml_config_file(path)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""The tests for the device tracker component."""
|
||||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import unittest
|
||||
@@ -23,6 +24,8 @@ from tests.common import (
|
||||
get_test_home_assistant, fire_time_changed, fire_service_discovered,
|
||||
patch_yaml_files, assert_setup_component)
|
||||
|
||||
from ...test_util.aiohttp import mock_aiohttp_client
|
||||
|
||||
TEST_PLATFORM = {device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -107,6 +110,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
||||
self.assertEqual(device.config_picture, config.config_picture)
|
||||
self.assertEqual(device.away_hide, config.away_hide)
|
||||
self.assertEqual(device.consider_home, config.consider_home)
|
||||
self.assertEqual(device.vendor, config.vendor)
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@patch('homeassistant.components.device_tracker._LOGGER.warning')
|
||||
@@ -154,8 +158,13 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
||||
|
||||
self.assertTrue(setup_component(self.hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
|
||||
|
||||
# wait for async calls (macvendor) to finish
|
||||
self.hass.block_till_done()
|
||||
|
||||
config = device_tracker.load_config(self.yaml_devices, self.hass,
|
||||
timedelta(seconds=0))
|
||||
|
||||
assert len(config) == 1
|
||||
assert config[0].dev_id == 'dev1'
|
||||
assert config[0].track
|
||||
@@ -181,6 +190,72 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
||||
"55502f40dc8b7c769880b10874abc9d0.jpg?s=80&d=wavatar")
|
||||
self.assertEqual(device.config_picture, gravatar_url)
|
||||
|
||||
def test_mac_vendor_lookup(self):
|
||||
"""Test if vendor string is lookup on macvendors API."""
|
||||
mac = 'B8:27:EB:00:00:00'
|
||||
vendor_string = 'Raspberry Pi Foundation'
|
||||
|
||||
device = device_tracker.Device(
|
||||
self.hass, timedelta(seconds=180), True, 'test', mac, 'Test name')
|
||||
|
||||
with mock_aiohttp_client() as aioclient_mock:
|
||||
aioclient_mock.get('http://api.macvendors.com/b8:27:eb',
|
||||
text=vendor_string)
|
||||
|
||||
run_coroutine_threadsafe(device.set_vendor_for_mac(),
|
||||
self.hass.loop).result()
|
||||
assert aioclient_mock.call_count == 1
|
||||
|
||||
self.assertEqual(device.vendor, vendor_string)
|
||||
|
||||
def test_mac_vendor_lookup_unknown(self):
|
||||
"""Prevent another mac vendor lookup if was not found first time."""
|
||||
mac = 'B8:27:EB:00:00:00'
|
||||
|
||||
device = device_tracker.Device(
|
||||
self.hass, timedelta(seconds=180), True, 'test', mac, 'Test name')
|
||||
|
||||
with mock_aiohttp_client() as aioclient_mock:
|
||||
aioclient_mock.get('http://api.macvendors.com/b8:27:eb',
|
||||
status=404)
|
||||
|
||||
run_coroutine_threadsafe(device.set_vendor_for_mac(),
|
||||
self.hass.loop).result()
|
||||
|
||||
self.assertEqual(device.vendor, 'unknown')
|
||||
|
||||
def test_mac_vendor_lookup_error(self):
|
||||
"""Prevent another lookup if failure during API call."""
|
||||
mac = 'B8:27:EB:00:00:00'
|
||||
|
||||
device = device_tracker.Device(
|
||||
self.hass, timedelta(seconds=180), True, 'test', mac, 'Test name')
|
||||
|
||||
with mock_aiohttp_client() as aioclient_mock:
|
||||
aioclient_mock.get('http://api.macvendors.com/b8:27:eb',
|
||||
status=500)
|
||||
|
||||
run_coroutine_threadsafe(device.set_vendor_for_mac(),
|
||||
self.hass.loop).result()
|
||||
|
||||
self.assertEqual(device.vendor, 'unknown')
|
||||
|
||||
def test_mac_vendor_lookup_exception(self):
|
||||
"""Prevent another lookup if exception during API call."""
|
||||
mac = 'B8:27:EB:00:00:00'
|
||||
|
||||
device = device_tracker.Device(
|
||||
self.hass, timedelta(seconds=180), True, 'test', mac, 'Test name')
|
||||
|
||||
with mock_aiohttp_client() as aioclient_mock:
|
||||
aioclient_mock.get('http://api.macvendors.com/b8:27:eb',
|
||||
exc=asyncio.TimeoutError())
|
||||
|
||||
run_coroutine_threadsafe(device.set_vendor_for_mac(),
|
||||
self.hass.loop).result()
|
||||
|
||||
self.assertEqual(device.vendor, 'unknown')
|
||||
|
||||
def test_discovery(self):
|
||||
"""Test discovery."""
|
||||
scanner = get_component('device_tracker.test').SCANNER
|
||||
|
||||
Reference in new issue
Block a user