From 83a72ab4dc188ee7a6229762fe151a1dacad41ed Mon Sep 17 00:00:00 2001 From: Jordan Keith Date: Tue, 5 Jul 2016 01:20:00 +1000 Subject: [PATCH] Update unifi.py to support sites (#2434) * Update unifi.py Add support for a site that is not the default within the Unifi Controller. i.e. A controller with multiple sites: - Home - Friends - Parents (default) Supplying the identifier for 'Home' now means that the devices tracked will be associated with 'Home'. * Update test_unifi.py Fix test modules as well. --- homeassistant/components/device_tracker/unifi.py | 4 +++- tests/components/device_tracker/test_unifi.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/device_tracker/unifi.py b/homeassistant/components/device_tracker/unifi.py index 3bff25edc5e..2ae3f76e5e6 100644 --- a/homeassistant/components/device_tracker/unifi.py +++ b/homeassistant/components/device_tracker/unifi.py @@ -16,6 +16,7 @@ REQUIREMENTS = ['urllib3', 'unifi==1.2.5'] _LOGGER = logging.getLogger(__name__) CONF_PORT = 'port' +CONF_SITE_ID = 'site_id' def get_scanner(hass, config): @@ -32,6 +33,7 @@ def get_scanner(hass, config): host = this_config.get(CONF_HOST, 'localhost') username = this_config.get(CONF_USERNAME) password = this_config.get(CONF_PASSWORD) + site_id = this_config.get(CONF_SITE_ID, 'default') try: port = int(this_config.get(CONF_PORT, 8443)) @@ -40,7 +42,7 @@ def get_scanner(hass, config): return False try: - ctrl = Controller(host, username, password, port, 'v4') + ctrl = Controller(host, username, password, port, 'v4', site_id) except urllib.error.HTTPError as ex: _LOGGER.error('Failed to connect to unifi: %s', ex) return False diff --git a/tests/components/device_tracker/test_unifi.py b/tests/components/device_tracker/test_unifi.py index 913097d17f1..e3f64cc84c3 100644 --- a/tests/components/device_tracker/test_unifi.py +++ b/tests/components/device_tracker/test_unifi.py @@ -24,7 +24,7 @@ class TestUnifiScanner(unittest.TestCase): result = unifi.get_scanner(None, config) self.assertEqual(unifi.UnifiScanner.return_value, result) mock_ctrl.assert_called_once_with('localhost', 'foo', 'password', - 8443, 'v4') + 8443, 'v4', 'default') mock_scanner.assert_called_once_with(mock_ctrl.return_value) @mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner') @@ -37,12 +37,13 @@ class TestUnifiScanner(unittest.TestCase): CONF_PASSWORD: 'password', CONF_HOST: 'myhost', 'port': 123, + 'site_id': 'abcdef01', } } result = unifi.get_scanner(None, config) self.assertEqual(unifi.UnifiScanner.return_value, result) mock_ctrl.assert_called_once_with('myhost', 'foo', 'password', - 123, 'v4') + 123, 'v4', 'abcdef01') mock_scanner.assert_called_once_with(mock_ctrl.return_value) @mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')