mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 17:57:55 +00:00
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:
parent
2cdef7fb2f
commit
83a72ab4dc
@ -16,6 +16,7 @@ REQUIREMENTS = ['urllib3', 'unifi==1.2.5']
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
CONF_PORT = 'port'
|
CONF_PORT = 'port'
|
||||||
|
CONF_SITE_ID = 'site_id'
|
||||||
|
|
||||||
|
|
||||||
def get_scanner(hass, config):
|
def get_scanner(hass, config):
|
||||||
@ -32,6 +33,7 @@ def get_scanner(hass, config):
|
|||||||
host = this_config.get(CONF_HOST, 'localhost')
|
host = this_config.get(CONF_HOST, 'localhost')
|
||||||
username = this_config.get(CONF_USERNAME)
|
username = this_config.get(CONF_USERNAME)
|
||||||
password = this_config.get(CONF_PASSWORD)
|
password = this_config.get(CONF_PASSWORD)
|
||||||
|
site_id = this_config.get(CONF_SITE_ID, 'default')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
port = int(this_config.get(CONF_PORT, 8443))
|
port = int(this_config.get(CONF_PORT, 8443))
|
||||||
@ -40,7 +42,7 @@ def get_scanner(hass, config):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ctrl = Controller(host, username, password, port, 'v4')
|
ctrl = Controller(host, username, password, port, 'v4', site_id)
|
||||||
except urllib.error.HTTPError as ex:
|
except urllib.error.HTTPError as ex:
|
||||||
_LOGGER.error('Failed to connect to unifi: %s', ex)
|
_LOGGER.error('Failed to connect to unifi: %s', ex)
|
||||||
return False
|
return False
|
||||||
|
@ -24,7 +24,7 @@ class TestUnifiScanner(unittest.TestCase):
|
|||||||
result = unifi.get_scanner(None, config)
|
result = unifi.get_scanner(None, config)
|
||||||
self.assertEqual(unifi.UnifiScanner.return_value, result)
|
self.assertEqual(unifi.UnifiScanner.return_value, result)
|
||||||
mock_ctrl.assert_called_once_with('localhost', 'foo', 'password',
|
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_scanner.assert_called_once_with(mock_ctrl.return_value)
|
||||||
|
|
||||||
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
||||||
@ -37,12 +37,13 @@ class TestUnifiScanner(unittest.TestCase):
|
|||||||
CONF_PASSWORD: 'password',
|
CONF_PASSWORD: 'password',
|
||||||
CONF_HOST: 'myhost',
|
CONF_HOST: 'myhost',
|
||||||
'port': 123,
|
'port': 123,
|
||||||
|
'site_id': 'abcdef01',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = unifi.get_scanner(None, config)
|
result = unifi.get_scanner(None, config)
|
||||||
self.assertEqual(unifi.UnifiScanner.return_value, result)
|
self.assertEqual(unifi.UnifiScanner.return_value, result)
|
||||||
mock_ctrl.assert_called_once_with('myhost', 'foo', 'password',
|
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_scanner.assert_called_once_with(mock_ctrl.return_value)
|
||||||
|
|
||||||
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user