core/tests/components/device_tracker/test_unifi.py

158 lines
5.7 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Unifi WAP device tracker platform."""
import unittest
from unittest import mock
import urllib
from pyunifi import controller
2016-09-04 08:06:16 +00:00
import voluptuous as vol
from tests.common import get_test_home_assistant
2016-09-04 08:06:16 +00:00
from homeassistant.components.device_tracker import DOMAIN, unifi as unifi
from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
CONF_PLATFORM, CONF_VERIFY_SSL)
class TestUnifiScanner(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the Unifiy platform."""
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
@mock.patch.object(controller, 'Controller')
def test_config_minimal(self, mock_ctrl, mock_scanner):
2016-03-09 09:25:50 +00:00
"""Test the setup with minimal configuration."""
config = {
2016-09-04 08:06:16 +00:00
DOMAIN: unifi.PLATFORM_SCHEMA({
CONF_PLATFORM: unifi.DOMAIN,
CONF_USERNAME: 'foo',
CONF_PASSWORD: 'password',
2016-09-04 08:06:16 +00:00
})
}
result = unifi.get_scanner(self.hass, config)
2016-09-04 08:06:16 +00:00
self.assertEqual(mock_scanner.return_value, result)
self.assertEqual(mock_ctrl.call_count, 1)
self.assertEqual(
mock_ctrl.call_args,
mock.call('localhost', 'foo', 'password', 8443,
version='v4', site_id='default', ssl_verify=True)
)
self.assertEqual(mock_scanner.call_count, 1)
self.assertEqual(
mock_scanner.call_args,
mock.call(mock_ctrl.return_value)
)
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
@mock.patch.object(controller, 'Controller')
def test_config_full(self, mock_ctrl, mock_scanner):
2016-03-09 09:25:50 +00:00
"""Test the setup with full configuration."""
config = {
2016-09-04 08:06:16 +00:00
DOMAIN: unifi.PLATFORM_SCHEMA({
CONF_PLATFORM: unifi.DOMAIN,
CONF_USERNAME: 'foo',
CONF_PASSWORD: 'password',
CONF_HOST: 'myhost',
CONF_VERIFY_SSL: False,
'port': 123,
'site_id': 'abcdef01',
2016-09-04 08:06:16 +00:00
})
}
result = unifi.get_scanner(self.hass, config)
2016-09-04 08:06:16 +00:00
self.assertEqual(mock_scanner.return_value, result)
self.assertEqual(mock_ctrl.call_count, 1)
self.assertEqual(
mock_ctrl.call_args,
mock.call('myhost', 'foo', 'password', 123,
version='v4', site_id='abcdef01', ssl_verify=False)
)
self.assertEqual(mock_scanner.call_count, 1)
self.assertEqual(
mock_scanner.call_args,
mock.call(mock_ctrl.return_value)
)
2016-09-04 08:06:16 +00:00
def test_config_error(self):
2016-03-09 09:25:50 +00:00
"""Test for configuration errors."""
2016-09-04 08:06:16 +00:00
with self.assertRaises(vol.Invalid):
unifi.PLATFORM_SCHEMA({
# no username
CONF_PLATFORM: unifi.DOMAIN,
CONF_HOST: 'myhost',
'port': 123,
2016-09-04 08:06:16 +00:00
})
with self.assertRaises(vol.Invalid):
unifi.PLATFORM_SCHEMA({
CONF_PLATFORM: unifi.DOMAIN,
CONF_USERNAME: 'foo',
CONF_PASSWORD: 'password',
CONF_HOST: 'myhost',
2016-09-04 08:06:16 +00:00
'port': 'foo', # bad port!
})
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
@mock.patch.object(controller, 'Controller')
def test_config_controller_failed(self, mock_ctrl, mock_scanner):
2016-03-09 09:25:50 +00:00
"""Test for controller failure."""
config = {
'device_tracker': {
2016-09-04 08:06:16 +00:00
CONF_PLATFORM: unifi.DOMAIN,
CONF_USERNAME: 'foo',
CONF_PASSWORD: 'password',
}
}
mock_ctrl.side_effect = urllib.error.HTTPError(
'/', 500, 'foo', {}, None)
result = unifi.get_scanner(self.hass, config)
self.assertFalse(result)
2016-09-04 08:06:16 +00:00
def test_scanner_update(self): # pylint: disable=no-self-use
2016-03-09 09:25:50 +00:00
"""Test the scanner update."""
ctrl = mock.MagicMock()
fake_clients = [
{'mac': '123'},
{'mac': '234'},
]
ctrl.get_clients.return_value = fake_clients
unifi.UnifiScanner(ctrl)
self.assertEqual(ctrl.get_clients.call_count, 1)
self.assertEqual(ctrl.get_clients.call_args, mock.call())
2016-09-04 08:06:16 +00:00
def test_scanner_update_error(self): # pylint: disable=no-self-use
2016-03-09 09:25:50 +00:00
"""Test the scanner update for error."""
ctrl = mock.MagicMock()
ctrl.get_clients.side_effect = urllib.error.HTTPError(
'/', 500, 'foo', {}, None)
unifi.UnifiScanner(ctrl)
def test_scan_devices(self):
2016-03-09 09:25:50 +00:00
"""Test the scanning for devices."""
ctrl = mock.MagicMock()
fake_clients = [
{'mac': '123'},
{'mac': '234'},
]
ctrl.get_clients.return_value = fake_clients
scanner = unifi.UnifiScanner(ctrl)
self.assertEqual(set(['123', '234']), set(scanner.scan_devices()))
def test_get_device_name(self):
2016-03-09 09:25:50 +00:00
"""Test the getting of device names."""
ctrl = mock.MagicMock()
fake_clients = [
{'mac': '123', 'hostname': 'foobar'},
{'mac': '234', 'name': 'Nice Name'},
{'mac': '456'},
]
ctrl.get_clients.return_value = fake_clients
scanner = unifi.UnifiScanner(ctrl)
self.assertEqual('foobar', scanner.get_device_name('123'))
self.assertEqual('Nice Name', scanner.get_device_name('234'))
self.assertEqual(None, scanner.get_device_name('456'))
self.assertEqual(None, scanner.get_device_name('unknown'))