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.
This commit is contained in:
Jordan Keith 2016-07-05 01:20:00 +10:00 committed by Paulus Schoutsen
parent 2cdef7fb2f
commit 83a72ab4dc
2 changed files with 6 additions and 3 deletions

View File

@ -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

View File

@ -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')