Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -5,33 +5,33 @@ import pytest
|
||||
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.components.unifi.const import (
|
||||
CONF_CONTROLLER, CONF_SITE_ID, UNIFI_CONFIG)
|
||||
CONF_CONTROLLER,
|
||||
CONF_SITE_ID,
|
||||
UNIFI_CONFIG,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_VERIFY_SSL)
|
||||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.components.unifi import controller, errors
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
CONTROLLER_SITES = {
|
||||
'site1': {
|
||||
'desc': 'nice name',
|
||||
'name': 'site',
|
||||
'role': 'admin'
|
||||
}
|
||||
}
|
||||
CONTROLLER_SITES = {"site1": {"desc": "nice name", "name": "site", "role": "admin"}}
|
||||
|
||||
CONTROLLER_DATA = {
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
CONF_PORT: 1234,
|
||||
CONF_SITE_ID: 'site',
|
||||
CONF_VERIFY_SSL: True
|
||||
CONF_SITE_ID: "site",
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
CONF_CONTROLLER: CONTROLLER_DATA
|
||||
}
|
||||
ENTRY_CONFIG = {CONF_CONTROLLER: CONTROLLER_DATA}
|
||||
|
||||
|
||||
async def test_controller_setup():
|
||||
@@ -46,16 +46,19 @@ async def test_controller_setup():
|
||||
|
||||
unifi_controller = controller.UniFiController(hass, entry)
|
||||
|
||||
with patch.object(controller, 'get_controller',
|
||||
return_value=mock_coro(api)):
|
||||
with patch.object(controller, "get_controller", return_value=mock_coro(api)):
|
||||
assert await unifi_controller.async_setup() is True
|
||||
|
||||
assert unifi_controller.api is api
|
||||
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 2
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == \
|
||||
(entry, 'device_tracker')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[1][1] == \
|
||||
(entry, 'switch')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == (
|
||||
entry,
|
||||
"device_tracker",
|
||||
)
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[1][1] == (
|
||||
entry,
|
||||
"switch",
|
||||
)
|
||||
|
||||
|
||||
async def test_controller_host():
|
||||
@@ -87,20 +90,19 @@ async def test_controller_mac():
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
client = Mock()
|
||||
client.ip = '1.2.3.4'
|
||||
client.mac = '00:11:22:33:44:55'
|
||||
client.ip = "1.2.3.4"
|
||||
client.mac = "00:11:22:33:44:55"
|
||||
api = Mock()
|
||||
api.initialize.return_value = mock_coro(True)
|
||||
api.clients = {'client1': client}
|
||||
api.clients = {"client1": client}
|
||||
api.sites.return_value = mock_coro(CONTROLLER_SITES)
|
||||
|
||||
unifi_controller = controller.UniFiController(hass, entry)
|
||||
|
||||
with patch.object(controller, 'get_controller',
|
||||
return_value=mock_coro(api)):
|
||||
with patch.object(controller, "get_controller", return_value=mock_coro(api)):
|
||||
assert await unifi_controller.async_setup() is True
|
||||
|
||||
assert unifi_controller.mac == '00:11:22:33:44:55'
|
||||
assert unifi_controller.mac == "00:11:22:33:44:55"
|
||||
|
||||
|
||||
async def test_controller_no_mac():
|
||||
@@ -110,16 +112,15 @@ async def test_controller_no_mac():
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
client = Mock()
|
||||
client.ip = '5.6.7.8'
|
||||
client.ip = "5.6.7.8"
|
||||
api = Mock()
|
||||
api.initialize.return_value = mock_coro(True)
|
||||
api.clients = {'client1': client}
|
||||
api.clients = {"client1": client}
|
||||
api.sites.return_value = mock_coro(CONTROLLER_SITES)
|
||||
|
||||
unifi_controller = controller.UniFiController(hass, entry)
|
||||
|
||||
with patch.object(controller, 'get_controller',
|
||||
return_value=mock_coro(api)):
|
||||
with patch.object(controller, "get_controller", return_value=mock_coro(api)):
|
||||
assert await unifi_controller.async_setup() is True
|
||||
|
||||
assert unifi_controller.mac is None
|
||||
@@ -136,7 +137,7 @@ async def test_controller_not_accessible():
|
||||
unifi_controller = controller.UniFiController(hass, entry)
|
||||
|
||||
with patch.object(
|
||||
controller, 'get_controller', side_effect=errors.CannotConnect
|
||||
controller, "get_controller", side_effect=errors.CannotConnect
|
||||
), pytest.raises(ConfigEntryNotReady):
|
||||
await unifi_controller.async_setup()
|
||||
|
||||
@@ -151,7 +152,7 @@ async def test_controller_unknown_error():
|
||||
|
||||
unifi_controller = controller.UniFiController(hass, entry)
|
||||
|
||||
with patch.object(controller, 'get_controller', side_effect=Exception):
|
||||
with patch.object(controller, "get_controller", side_effect=Exception):
|
||||
assert await unifi_controller.async_setup() is False
|
||||
|
||||
assert not hass.helpers.event.async_call_later.mock_calls
|
||||
@@ -165,8 +166,9 @@ async def test_reset_if_entry_had_wrong_auth():
|
||||
|
||||
unifi_controller = controller.UniFiController(hass, entry)
|
||||
|
||||
with patch.object(controller, 'get_controller',
|
||||
side_effect=errors.AuthenticationRequired):
|
||||
with patch.object(
|
||||
controller, "get_controller", side_effect=errors.AuthenticationRequired
|
||||
):
|
||||
assert await unifi_controller.async_setup() is False
|
||||
|
||||
assert not hass.async_add_job.mock_calls
|
||||
@@ -186,14 +188,12 @@ async def test_reset_unloads_entry_if_setup():
|
||||
|
||||
unifi_controller = controller.UniFiController(hass, entry)
|
||||
|
||||
with patch.object(controller, 'get_controller',
|
||||
return_value=mock_coro(api)):
|
||||
with patch.object(controller, "get_controller", return_value=mock_coro(api)):
|
||||
assert await unifi_controller.async_setup() is True
|
||||
|
||||
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 2
|
||||
|
||||
hass.config_entries.async_forward_entry_unload.return_value = \
|
||||
mock_coro(True)
|
||||
hass.config_entries.async_forward_entry_unload.return_value = mock_coro(True)
|
||||
assert await unifi_controller.async_reset()
|
||||
|
||||
assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 2
|
||||
@@ -201,7 +201,7 @@ async def test_reset_unloads_entry_if_setup():
|
||||
|
||||
async def test_get_controller(hass):
|
||||
"""Successful call."""
|
||||
with patch('aiounifi.Controller.login', return_value=mock_coro()):
|
||||
with patch("aiounifi.Controller.login", return_value=mock_coro()):
|
||||
assert await controller.get_controller(hass, **CONTROLLER_DATA)
|
||||
|
||||
|
||||
@@ -209,15 +209,16 @@ async def test_get_controller_verify_ssl_false(hass):
|
||||
"""Successful call with verify ssl set to false."""
|
||||
controller_data = dict(CONTROLLER_DATA)
|
||||
controller_data[CONF_VERIFY_SSL] = False
|
||||
with patch('aiounifi.Controller.login', return_value=mock_coro()):
|
||||
with patch("aiounifi.Controller.login", return_value=mock_coro()):
|
||||
assert await controller.get_controller(hass, **controller_data)
|
||||
|
||||
|
||||
async def test_get_controller_login_failed(hass):
|
||||
"""Check that get_controller can handle a failed login."""
|
||||
import aiounifi
|
||||
|
||||
result = None
|
||||
with patch('aiounifi.Controller.login', side_effect=aiounifi.Unauthorized):
|
||||
with patch("aiounifi.Controller.login", side_effect=aiounifi.Unauthorized):
|
||||
try:
|
||||
result = await controller.get_controller(hass, **CONTROLLER_DATA)
|
||||
except errors.AuthenticationRequired:
|
||||
@@ -228,9 +229,9 @@ async def test_get_controller_login_failed(hass):
|
||||
async def test_get_controller_controller_unavailable(hass):
|
||||
"""Check that get_controller can handle controller being unavailable."""
|
||||
import aiounifi
|
||||
|
||||
result = None
|
||||
with patch('aiounifi.Controller.login',
|
||||
side_effect=aiounifi.RequestError):
|
||||
with patch("aiounifi.Controller.login", side_effect=aiounifi.RequestError):
|
||||
try:
|
||||
result = await controller.get_controller(hass, **CONTROLLER_DATA)
|
||||
except errors.CannotConnect:
|
||||
@@ -241,9 +242,9 @@ async def test_get_controller_controller_unavailable(hass):
|
||||
async def test_get_controller_unknown_error(hass):
|
||||
"""Check that get_controller can handle unkown errors."""
|
||||
import aiounifi
|
||||
|
||||
result = None
|
||||
with patch('aiounifi.Controller.login',
|
||||
side_effect=aiounifi.AiounifiException):
|
||||
with patch("aiounifi.Controller.login", side_effect=aiounifi.AiounifiException):
|
||||
try:
|
||||
result = await controller.get_controller(hass, **CONTROLLER_DATA)
|
||||
except errors.AuthenticationRequired:
|
||||
|
||||
@@ -12,9 +12,17 @@ from aiounifi.devices import Devices
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import unifi
|
||||
from homeassistant.components.unifi.const import (
|
||||
CONF_CONTROLLER, CONF_SITE_ID, UNIFI_CONFIG)
|
||||
CONF_CONTROLLER,
|
||||
CONF_SITE_ID,
|
||||
UNIFI_CONFIG,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_VERIFY_SSL)
|
||||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -25,60 +33,58 @@ import homeassistant.util.dt as dt_util
|
||||
DEFAULT_DETECTION_TIME = timedelta(seconds=300)
|
||||
|
||||
CLIENT_1 = {
|
||||
'essid': 'ssid',
|
||||
'hostname': 'client_1',
|
||||
'ip': '10.0.0.1',
|
||||
'is_wired': False,
|
||||
'last_seen': 1562600145,
|
||||
'mac': '00:00:00:00:00:01',
|
||||
"essid": "ssid",
|
||||
"hostname": "client_1",
|
||||
"ip": "10.0.0.1",
|
||||
"is_wired": False,
|
||||
"last_seen": 1562600145,
|
||||
"mac": "00:00:00:00:00:01",
|
||||
}
|
||||
CLIENT_2 = {
|
||||
'hostname': 'client_2',
|
||||
'ip': '10.0.0.2',
|
||||
'is_wired': True,
|
||||
'last_seen': 1562600145,
|
||||
'mac': '00:00:00:00:00:02',
|
||||
'name': 'Wired Client',
|
||||
"hostname": "client_2",
|
||||
"ip": "10.0.0.2",
|
||||
"is_wired": True,
|
||||
"last_seen": 1562600145,
|
||||
"mac": "00:00:00:00:00:02",
|
||||
"name": "Wired Client",
|
||||
}
|
||||
CLIENT_3 = {
|
||||
'essid': 'ssid2',
|
||||
'hostname': 'client_3',
|
||||
'ip': '10.0.0.3',
|
||||
'is_wired': False,
|
||||
'last_seen': 1562600145,
|
||||
'mac': '00:00:00:00:00:03',
|
||||
"essid": "ssid2",
|
||||
"hostname": "client_3",
|
||||
"ip": "10.0.0.3",
|
||||
"is_wired": False,
|
||||
"last_seen": 1562600145,
|
||||
"mac": "00:00:00:00:00:03",
|
||||
}
|
||||
|
||||
DEVICE_1 = {
|
||||
'board_rev': 3,
|
||||
'device_id': 'mock-id',
|
||||
'has_fan': True,
|
||||
'fan_level': 0,
|
||||
'ip': '10.0.1.1',
|
||||
'last_seen': 1562600145,
|
||||
'mac': '00:00:00:00:01:01',
|
||||
'model': 'US16P150',
|
||||
'name': 'device_1',
|
||||
'overheating': False,
|
||||
'type': 'usw',
|
||||
'upgradable': False,
|
||||
'version': '4.0.42.10433',
|
||||
"board_rev": 3,
|
||||
"device_id": "mock-id",
|
||||
"has_fan": True,
|
||||
"fan_level": 0,
|
||||
"ip": "10.0.1.1",
|
||||
"last_seen": 1562600145,
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"model": "US16P150",
|
||||
"name": "device_1",
|
||||
"overheating": False,
|
||||
"type": "usw",
|
||||
"upgradable": False,
|
||||
"version": "4.0.42.10433",
|
||||
}
|
||||
|
||||
CONTROLLER_DATA = {
|
||||
CONF_HOST: 'mock-host',
|
||||
CONF_USERNAME: 'mock-user',
|
||||
CONF_PASSWORD: 'mock-pswd',
|
||||
CONF_HOST: "mock-host",
|
||||
CONF_USERNAME: "mock-user",
|
||||
CONF_PASSWORD: "mock-pswd",
|
||||
CONF_PORT: 1234,
|
||||
CONF_SITE_ID: 'mock-site',
|
||||
CONF_VERIFY_SSL: True
|
||||
CONF_SITE_ID: "mock-site",
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
CONF_CONTROLLER: CONTROLLER_DATA
|
||||
}
|
||||
ENTRY_CONFIG = {CONF_CONTROLLER: CONTROLLER_DATA}
|
||||
|
||||
CONTROLLER_ID = unifi.CONTROLLER_ID.format(host='mock-host', site='mock-site')
|
||||
CONTROLLER_ID = unifi.CONTROLLER_ID.format(host="mock-host", site="mock-site")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -95,14 +101,14 @@ def mock_controller(hass):
|
||||
controller.mock_client_all_responses = deque()
|
||||
|
||||
async def mock_request(method, path, **kwargs):
|
||||
kwargs['method'] = method
|
||||
kwargs['path'] = path
|
||||
kwargs["method"] = method
|
||||
kwargs["path"] = path
|
||||
controller.mock_requests.append(kwargs)
|
||||
if path == 's/{site}/stat/sta':
|
||||
if path == "s/{site}/stat/sta":
|
||||
return controller.mock_client_responses.popleft()
|
||||
if path == 's/{site}/stat/device':
|
||||
if path == "s/{site}/stat/device":
|
||||
return controller.mock_device_responses.popleft()
|
||||
if path == 's/{site}/rest/user':
|
||||
if path == "s/{site}/rest/user":
|
||||
return controller.mock_client_all_responses.popleft()
|
||||
return None
|
||||
|
||||
@@ -118,24 +124,32 @@ async def setup_controller(hass, mock_controller):
|
||||
hass.config.components.add(unifi.DOMAIN)
|
||||
hass.data[unifi.DOMAIN] = {CONTROLLER_ID: mock_controller}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, unifi.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_POLL, entry_id=1)
|
||||
1,
|
||||
unifi.DOMAIN,
|
||||
"Mock Title",
|
||||
ENTRY_CONFIG,
|
||||
"test",
|
||||
config_entries.CONN_CLASS_LOCAL_POLL,
|
||||
entry_id=1,
|
||||
)
|
||||
mock_controller.config_entry = config_entry
|
||||
|
||||
await mock_controller.async_update()
|
||||
await hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, device_tracker.DOMAIN)
|
||||
config_entry, device_tracker.DOMAIN
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that we do not discover anything or try to set up a bridge."""
|
||||
assert await async_setup_component(hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
'platform': 'unifi'
|
||||
}
|
||||
}) is True
|
||||
assert (
|
||||
await async_setup_component(
|
||||
hass, device_tracker.DOMAIN, {device_tracker.DOMAIN: {"platform": "unifi"}}
|
||||
)
|
||||
is True
|
||||
)
|
||||
assert unifi.DOMAIN not in hass.data
|
||||
|
||||
|
||||
@@ -151,44 +165,43 @@ async def test_no_clients(hass, mock_controller):
|
||||
|
||||
async def test_tracked_devices(hass, mock_controller):
|
||||
"""Test the update_items function with some clients."""
|
||||
mock_controller.mock_client_responses.append(
|
||||
[CLIENT_1, CLIENT_2, CLIENT_3])
|
||||
mock_controller.mock_client_responses.append([CLIENT_1, CLIENT_2, CLIENT_3])
|
||||
mock_controller.mock_device_responses.append([DEVICE_1])
|
||||
mock_controller.unifi_config = {unifi_dt.CONF_SSID_FILTER: ['ssid']}
|
||||
mock_controller.unifi_config = {unifi_dt.CONF_SSID_FILTER: ["ssid"]}
|
||||
|
||||
await setup_controller(hass, mock_controller)
|
||||
assert len(mock_controller.mock_requests) == 2
|
||||
assert len(hass.states.async_all()) == 5
|
||||
|
||||
client_1 = hass.states.get('device_tracker.client_1')
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1 is not None
|
||||
assert client_1.state == 'not_home'
|
||||
assert client_1.state == "not_home"
|
||||
|
||||
client_2 = hass.states.get('device_tracker.wired_client')
|
||||
client_2 = hass.states.get("device_tracker.wired_client")
|
||||
assert client_2 is not None
|
||||
assert client_2.state == 'not_home'
|
||||
assert client_2.state == "not_home"
|
||||
|
||||
client_3 = hass.states.get('device_tracker.client_3')
|
||||
client_3 = hass.states.get("device_tracker.client_3")
|
||||
assert client_3 is None
|
||||
|
||||
device_1 = hass.states.get('device_tracker.device_1')
|
||||
device_1 = hass.states.get("device_tracker.device_1")
|
||||
assert device_1 is not None
|
||||
assert device_1.state == 'not_home'
|
||||
assert device_1.state == "not_home"
|
||||
|
||||
client_1_copy = copy(CLIENT_1)
|
||||
client_1_copy['last_seen'] = dt_util.as_timestamp(dt_util.utcnow())
|
||||
client_1_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
||||
device_1_copy = copy(DEVICE_1)
|
||||
device_1_copy['last_seen'] = dt_util.as_timestamp(dt_util.utcnow())
|
||||
device_1_copy["last_seen"] = dt_util.as_timestamp(dt_util.utcnow())
|
||||
mock_controller.mock_client_responses.append([client_1_copy])
|
||||
mock_controller.mock_device_responses.append([device_1_copy])
|
||||
await mock_controller.async_update()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
client_1 = hass.states.get('device_tracker.client_1')
|
||||
assert client_1.state == 'home'
|
||||
client_1 = hass.states.get("device_tracker.client_1")
|
||||
assert client_1.state == "home"
|
||||
|
||||
device_1 = hass.states.get('device_tracker.device_1')
|
||||
assert device_1.state == 'home'
|
||||
device_1 = hass.states.get("device_tracker.device_1")
|
||||
assert device_1.state == "home"
|
||||
|
||||
|
||||
async def test_restoring_client(hass, mock_controller):
|
||||
@@ -196,23 +209,27 @@ async def test_restoring_client(hass, mock_controller):
|
||||
mock_controller.mock_client_responses.append([CLIENT_2])
|
||||
mock_controller.mock_device_responses.append({})
|
||||
mock_controller.mock_client_all_responses.append([CLIENT_1])
|
||||
mock_controller.unifi_config = {
|
||||
unifi.CONF_BLOCK_CLIENT: True
|
||||
}
|
||||
mock_controller.unifi_config = {unifi.CONF_BLOCK_CLIENT: True}
|
||||
|
||||
registry = await entity_registry.async_get_registry(hass)
|
||||
registry.async_get_or_create(
|
||||
device_tracker.DOMAIN, unifi_dt.UNIFI_DOMAIN,
|
||||
'{}-mock-site'.format(CLIENT_1['mac']),
|
||||
suggested_object_id=CLIENT_1['hostname'], config_entry_id=1)
|
||||
device_tracker.DOMAIN,
|
||||
unifi_dt.UNIFI_DOMAIN,
|
||||
"{}-mock-site".format(CLIENT_1["mac"]),
|
||||
suggested_object_id=CLIENT_1["hostname"],
|
||||
config_entry_id=1,
|
||||
)
|
||||
registry.async_get_or_create(
|
||||
device_tracker.DOMAIN, unifi_dt.UNIFI_DOMAIN,
|
||||
'{}-mock-site'.format(CLIENT_2['mac']),
|
||||
suggested_object_id=CLIENT_2['hostname'], config_entry_id=1)
|
||||
device_tracker.DOMAIN,
|
||||
unifi_dt.UNIFI_DOMAIN,
|
||||
"{}-mock-site".format(CLIENT_2["mac"]),
|
||||
suggested_object_id=CLIENT_2["hostname"],
|
||||
config_entry_id=1,
|
||||
)
|
||||
|
||||
await setup_controller(hass, mock_controller)
|
||||
assert len(mock_controller.mock_requests) == 3
|
||||
assert len(hass.states.async_all()) == 4
|
||||
|
||||
device_1 = hass.states.get('device_tracker.client_1')
|
||||
device_1 = hass.states.get("device_tracker.client_1")
|
||||
assert device_1 is not None
|
||||
+171
-153
@@ -7,7 +7,12 @@ from homeassistant.components.unifi import config_flow
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.unifi.const import CONF_CONTROLLER, CONF_SITE_ID
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_VERIFY_SSL)
|
||||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
|
||||
from tests.common import mock_coro, MockConfigEntry
|
||||
|
||||
@@ -24,45 +29,51 @@ async def test_setup_with_config(hass):
|
||||
config = {
|
||||
unifi.DOMAIN: {
|
||||
unifi.CONF_CONTROLLERS: {
|
||||
unifi.CONF_HOST: '1.2.3.4',
|
||||
unifi.CONF_SITE_ID: 'My site',
|
||||
unifi.CONF_BLOCK_CLIENT: ['12:34:56:78:90:AB'],
|
||||
unifi.CONF_HOST: "1.2.3.4",
|
||||
unifi.CONF_SITE_ID: "My site",
|
||||
unifi.CONF_BLOCK_CLIENT: ["12:34:56:78:90:AB"],
|
||||
unifi.CONF_DETECTION_TIME: 3,
|
||||
unifi.CONF_SSID_FILTER: ['ssid']
|
||||
unifi.CONF_SSID_FILTER: ["ssid"],
|
||||
}
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, unifi.DOMAIN, config) is True
|
||||
assert unifi.DOMAIN not in hass.data
|
||||
assert hass.data[unifi.UNIFI_CONFIG] == [{
|
||||
unifi.CONF_HOST: '1.2.3.4',
|
||||
unifi.CONF_SITE_ID: 'My site',
|
||||
unifi.CONF_BLOCK_CLIENT: ['12:34:56:78:90:AB'],
|
||||
unifi.CONF_DETECTION_TIME: timedelta(seconds=3),
|
||||
unifi.CONF_SSID_FILTER: ['ssid']
|
||||
}]
|
||||
assert hass.data[unifi.UNIFI_CONFIG] == [
|
||||
{
|
||||
unifi.CONF_HOST: "1.2.3.4",
|
||||
unifi.CONF_SITE_ID: "My site",
|
||||
unifi.CONF_BLOCK_CLIENT: ["12:34:56:78:90:AB"],
|
||||
unifi.CONF_DETECTION_TIME: timedelta(seconds=3),
|
||||
unifi.CONF_SSID_FILTER: ["ssid"],
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_successful_config_entry(hass):
|
||||
"""Test that configured options for a host are loaded via config entry."""
|
||||
entry = MockConfigEntry(domain=unifi.DOMAIN, data={
|
||||
'controller': {
|
||||
'host': '0.0.0.0',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'port': 80,
|
||||
'site': 'default',
|
||||
'verify_ssl': True
|
||||
entry = MockConfigEntry(
|
||||
domain=unifi.DOMAIN,
|
||||
data={
|
||||
"controller": {
|
||||
"host": "0.0.0.0",
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"port": 80,
|
||||
"site": "default",
|
||||
"verify_ssl": True,
|
||||
},
|
||||
"poe_control": True,
|
||||
},
|
||||
'poe_control': True
|
||||
})
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
mock_registry = Mock()
|
||||
with patch.object(unifi, 'UniFiController') as mock_controller, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(mock_registry)):
|
||||
with patch.object(unifi, "UniFiController") as mock_controller, patch(
|
||||
"homeassistant.helpers.device_registry.async_get_registry",
|
||||
return_value=mock_coro(mock_registry),
|
||||
):
|
||||
mock_controller.return_value.async_setup.return_value = mock_coro(True)
|
||||
mock_controller.return_value.mac = '00:11:22:33:44:55'
|
||||
mock_controller.return_value.mac = "00:11:22:33:44:55"
|
||||
assert await unifi.async_setup_entry(hass, entry) is True
|
||||
|
||||
assert len(mock_controller.mock_calls) == 2
|
||||
@@ -73,59 +84,62 @@ async def test_successful_config_entry(hass):
|
||||
|
||||
assert len(mock_registry.mock_calls) == 1
|
||||
assert mock_registry.mock_calls[0][2] == {
|
||||
'config_entry_id': entry.entry_id,
|
||||
'connections': {
|
||||
('mac', '00:11:22:33:44:55')
|
||||
},
|
||||
'manufacturer': unifi.ATTR_MANUFACTURER,
|
||||
'model': "UniFi Controller",
|
||||
'name': "UniFi Controller",
|
||||
"config_entry_id": entry.entry_id,
|
||||
"connections": {("mac", "00:11:22:33:44:55")},
|
||||
"manufacturer": unifi.ATTR_MANUFACTURER,
|
||||
"model": "UniFi Controller",
|
||||
"name": "UniFi Controller",
|
||||
}
|
||||
|
||||
|
||||
async def test_controller_fail_setup(hass):
|
||||
"""Test that a failed setup still stores controller."""
|
||||
entry = MockConfigEntry(domain=unifi.DOMAIN, data={
|
||||
'controller': {
|
||||
'host': '0.0.0.0',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'port': 80,
|
||||
'site': 'default',
|
||||
'verify_ssl': True
|
||||
entry = MockConfigEntry(
|
||||
domain=unifi.DOMAIN,
|
||||
data={
|
||||
"controller": {
|
||||
"host": "0.0.0.0",
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"port": 80,
|
||||
"site": "default",
|
||||
"verify_ssl": True,
|
||||
},
|
||||
"poe_control": True,
|
||||
},
|
||||
'poe_control': True
|
||||
})
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch.object(unifi, 'UniFiController') as mock_cntrlr:
|
||||
with patch.object(unifi, "UniFiController") as mock_cntrlr:
|
||||
mock_cntrlr.return_value.async_setup.return_value = mock_coro(False)
|
||||
assert await unifi.async_setup_entry(hass, entry) is False
|
||||
|
||||
controller_id = unifi.CONTROLLER_ID.format(
|
||||
host='0.0.0.0', site='default'
|
||||
)
|
||||
controller_id = unifi.CONTROLLER_ID.format(host="0.0.0.0", site="default")
|
||||
assert controller_id in hass.data[unifi.DOMAIN]
|
||||
|
||||
|
||||
async def test_controller_no_mac(hass):
|
||||
"""Test that configured options for a host are loaded via config entry."""
|
||||
entry = MockConfigEntry(domain=unifi.DOMAIN, data={
|
||||
'controller': {
|
||||
'host': '0.0.0.0',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'port': 80,
|
||||
'site': 'default',
|
||||
'verify_ssl': True
|
||||
entry = MockConfigEntry(
|
||||
domain=unifi.DOMAIN,
|
||||
data={
|
||||
"controller": {
|
||||
"host": "0.0.0.0",
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"port": 80,
|
||||
"site": "default",
|
||||
"verify_ssl": True,
|
||||
},
|
||||
"poe_control": True,
|
||||
},
|
||||
'poe_control': True
|
||||
})
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
mock_registry = Mock()
|
||||
with patch.object(unifi, 'UniFiController') as mock_controller, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(mock_registry)):
|
||||
with patch.object(unifi, "UniFiController") as mock_controller, patch(
|
||||
"homeassistant.helpers.device_registry.async_get_registry",
|
||||
return_value=mock_coro(mock_registry),
|
||||
):
|
||||
mock_controller.return_value.async_setup.return_value = mock_coro(True)
|
||||
mock_controller.return_value.mac = None
|
||||
assert await unifi.async_setup_entry(hass, entry) is True
|
||||
@@ -137,24 +151,28 @@ async def test_controller_no_mac(hass):
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test being able to unload an entry."""
|
||||
entry = MockConfigEntry(domain=unifi.DOMAIN, data={
|
||||
'controller': {
|
||||
'host': '0.0.0.0',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'port': 80,
|
||||
'site': 'default',
|
||||
'verify_ssl': True
|
||||
entry = MockConfigEntry(
|
||||
domain=unifi.DOMAIN,
|
||||
data={
|
||||
"controller": {
|
||||
"host": "0.0.0.0",
|
||||
"username": "user",
|
||||
"password": "pass",
|
||||
"port": 80,
|
||||
"site": "default",
|
||||
"verify_ssl": True,
|
||||
},
|
||||
"poe_control": True,
|
||||
},
|
||||
'poe_control': True
|
||||
})
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch.object(unifi, 'UniFiController') as mock_controller, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(Mock())):
|
||||
with patch.object(unifi, "UniFiController") as mock_controller, patch(
|
||||
"homeassistant.helpers.device_registry.async_get_registry",
|
||||
return_value=mock_coro(Mock()),
|
||||
):
|
||||
mock_controller.return_value.async_setup.return_value = mock_coro(True)
|
||||
mock_controller.return_value.mac = '00:11:22:33:44:55'
|
||||
mock_controller.return_value.mac = "00:11:22:33:44:55"
|
||||
assert await unifi.async_setup_entry(hass, entry) is True
|
||||
|
||||
assert len(mock_controller.return_value.mock_calls) == 1
|
||||
@@ -170,9 +188,11 @@ async def test_flow_works(hass, aioclient_mock):
|
||||
flow = config_flow.UnifiFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch('aiounifi.Controller') as mock_controller:
|
||||
with patch("aiounifi.Controller") as mock_controller:
|
||||
|
||||
def mock_constructor(
|
||||
host, username, password, port, site, websession, sslcontext):
|
||||
host, username, password, port, site, websession, sslcontext
|
||||
):
|
||||
"""Fake the controller constructor."""
|
||||
mock_controller.host = host
|
||||
mock_controller.username = username
|
||||
@@ -183,34 +203,36 @@ async def test_flow_works(hass, aioclient_mock):
|
||||
|
||||
mock_controller.side_effect = mock_constructor
|
||||
mock_controller.login.return_value = mock_coro()
|
||||
mock_controller.sites.return_value = mock_coro({
|
||||
'site1': {'name': 'default', 'role': 'admin', 'desc': 'site name'}
|
||||
})
|
||||
mock_controller.sites.return_value = mock_coro(
|
||||
{"site1": {"name": "default", "role": "admin", "desc": "site name"}}
|
||||
)
|
||||
|
||||
await flow.async_step_user(user_input={
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
CONF_PORT: 1234,
|
||||
CONF_VERIFY_SSL: True
|
||||
})
|
||||
await flow.async_step_user(
|
||||
user_input={
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
CONF_PORT: 1234,
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
)
|
||||
|
||||
result = await flow.async_step_site(user_input={})
|
||||
|
||||
assert mock_controller.host == '1.2.3.4'
|
||||
assert mock_controller.host == "1.2.3.4"
|
||||
assert len(mock_controller.login.mock_calls) == 1
|
||||
assert len(mock_controller.sites.mock_calls) == 1
|
||||
|
||||
assert result['type'] == 'create_entry'
|
||||
assert result['title'] == 'site name'
|
||||
assert result['data'] == {
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "site name"
|
||||
assert result["data"] == {
|
||||
CONF_CONTROLLER: {
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
CONF_PORT: 1234,
|
||||
CONF_SITE_ID: 'default',
|
||||
CONF_VERIFY_SSL: True
|
||||
CONF_SITE_ID: "default",
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,26 +243,22 @@ async def test_controller_multiple_sites(hass):
|
||||
flow.hass = hass
|
||||
|
||||
flow.config = {
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
}
|
||||
flow.sites = {
|
||||
'site1': {
|
||||
'name': 'default', 'role': 'admin', 'desc': 'site name'
|
||||
},
|
||||
'site2': {
|
||||
'name': 'site2', 'role': 'admin', 'desc': 'site2 name'
|
||||
}
|
||||
"site1": {"name": "default", "role": "admin", "desc": "site name"},
|
||||
"site2": {"name": "site2", "role": "admin", "desc": "site2 name"},
|
||||
}
|
||||
|
||||
result = await flow.async_step_site()
|
||||
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'site'
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "site"
|
||||
|
||||
assert result['data_schema']({'site': 'site name'})
|
||||
assert result['data_schema']({'site': 'site2 name'})
|
||||
assert result["data_schema"]({"site": "site name"})
|
||||
assert result["data_schema"]({"site": "site2 name"})
|
||||
|
||||
|
||||
async def test_controller_site_already_configured(hass):
|
||||
@@ -248,29 +266,22 @@ async def test_controller_site_already_configured(hass):
|
||||
flow = config_flow.UnifiFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
entry = MockConfigEntry(domain=unifi.DOMAIN, data={
|
||||
'controller': {
|
||||
'host': '1.2.3.4',
|
||||
'site': 'default',
|
||||
}
|
||||
})
|
||||
entry = MockConfigEntry(
|
||||
domain=unifi.DOMAIN, data={"controller": {"host": "1.2.3.4", "site": "default"}}
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
flow.config = {
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
}
|
||||
flow.desc = 'site name'
|
||||
flow.sites = {
|
||||
'site1': {
|
||||
'name': 'default', 'role': 'admin', 'desc': 'site name'
|
||||
}
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
}
|
||||
flow.desc = "site name"
|
||||
flow.sites = {"site1": {"name": "default", "role": "admin", "desc": "site name"}}
|
||||
|
||||
result = await flow.async_step_site()
|
||||
|
||||
assert result['type'] == 'abort'
|
||||
assert result["type"] == "abort"
|
||||
|
||||
|
||||
async def test_user_credentials_faulty(hass, aioclient_mock):
|
||||
@@ -278,17 +289,20 @@ async def test_user_credentials_faulty(hass, aioclient_mock):
|
||||
flow = config_flow.UnifiFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch.object(config_flow, 'get_controller',
|
||||
side_effect=unifi.errors.AuthenticationRequired):
|
||||
result = await flow.async_step_user({
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
CONF_SITE_ID: 'default',
|
||||
})
|
||||
with patch.object(
|
||||
config_flow, "get_controller", side_effect=unifi.errors.AuthenticationRequired
|
||||
):
|
||||
result = await flow.async_step_user(
|
||||
{
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
CONF_SITE_ID: "default",
|
||||
}
|
||||
)
|
||||
|
||||
assert result['type'] == 'form'
|
||||
assert result['errors'] == {'base': 'faulty_credentials'}
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {"base": "faulty_credentials"}
|
||||
|
||||
|
||||
async def test_controller_is_unavailable(hass, aioclient_mock):
|
||||
@@ -296,17 +310,20 @@ async def test_controller_is_unavailable(hass, aioclient_mock):
|
||||
flow = config_flow.UnifiFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch.object(config_flow, 'get_controller',
|
||||
side_effect=unifi.errors.CannotConnect):
|
||||
result = await flow.async_step_user({
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
CONF_SITE_ID: 'default',
|
||||
})
|
||||
with patch.object(
|
||||
config_flow, "get_controller", side_effect=unifi.errors.CannotConnect
|
||||
):
|
||||
result = await flow.async_step_user(
|
||||
{
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
CONF_SITE_ID: "default",
|
||||
}
|
||||
)
|
||||
|
||||
assert result['type'] == 'form'
|
||||
assert result['errors'] == {'base': 'service_unavailable'}
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {"base": "service_unavailable"}
|
||||
|
||||
|
||||
async def test_controller_unkown_problem(hass, aioclient_mock):
|
||||
@@ -314,13 +331,14 @@ async def test_controller_unkown_problem(hass, aioclient_mock):
|
||||
flow = config_flow.UnifiFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch.object(config_flow, 'get_controller',
|
||||
side_effect=Exception):
|
||||
result = await flow.async_step_user({
|
||||
CONF_HOST: '1.2.3.4',
|
||||
CONF_USERNAME: 'username',
|
||||
CONF_PASSWORD: 'password',
|
||||
CONF_SITE_ID: 'default',
|
||||
})
|
||||
with patch.object(config_flow, "get_controller", side_effect=Exception):
|
||||
result = await flow.async_step_user(
|
||||
{
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_USERNAME: "username",
|
||||
CONF_PASSWORD: "password",
|
||||
CONF_SITE_ID: "default",
|
||||
}
|
||||
)
|
||||
|
||||
assert result['type'] == 'abort'
|
||||
assert result["type"] == "abort"
|
||||
@@ -13,201 +13,207 @@ from aiounifi.devices import Devices
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import unifi
|
||||
from homeassistant.components.unifi.const import (
|
||||
CONF_CONTROLLER, CONF_SITE_ID, UNIFI_CONFIG)
|
||||
CONF_CONTROLLER,
|
||||
CONF_SITE_ID,
|
||||
UNIFI_CONFIG,
|
||||
)
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_VERIFY_SSL)
|
||||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
|
||||
import homeassistant.components.switch as switch
|
||||
|
||||
CLIENT_1 = {
|
||||
'hostname': 'client_1',
|
||||
'ip': '10.0.0.1',
|
||||
'is_wired': True,
|
||||
'mac': '00:00:00:00:00:01',
|
||||
'name': 'POE Client 1',
|
||||
'oui': 'Producer',
|
||||
'sw_mac': '00:00:00:00:01:01',
|
||||
'sw_port': 1,
|
||||
'wired-rx_bytes': 1234000000,
|
||||
'wired-tx_bytes': 5678000000
|
||||
"hostname": "client_1",
|
||||
"ip": "10.0.0.1",
|
||||
"is_wired": True,
|
||||
"mac": "00:00:00:00:00:01",
|
||||
"name": "POE Client 1",
|
||||
"oui": "Producer",
|
||||
"sw_mac": "00:00:00:00:01:01",
|
||||
"sw_port": 1,
|
||||
"wired-rx_bytes": 1234000000,
|
||||
"wired-tx_bytes": 5678000000,
|
||||
}
|
||||
CLIENT_2 = {
|
||||
'hostname': 'client_2',
|
||||
'ip': '10.0.0.2',
|
||||
'is_wired': True,
|
||||
'mac': '00:00:00:00:00:02',
|
||||
'name': 'POE Client 2',
|
||||
'oui': 'Producer',
|
||||
'sw_mac': '00:00:00:00:01:01',
|
||||
'sw_port': 2,
|
||||
'wired-rx_bytes': 1234000000,
|
||||
'wired-tx_bytes': 5678000000
|
||||
"hostname": "client_2",
|
||||
"ip": "10.0.0.2",
|
||||
"is_wired": True,
|
||||
"mac": "00:00:00:00:00:02",
|
||||
"name": "POE Client 2",
|
||||
"oui": "Producer",
|
||||
"sw_mac": "00:00:00:00:01:01",
|
||||
"sw_port": 2,
|
||||
"wired-rx_bytes": 1234000000,
|
||||
"wired-tx_bytes": 5678000000,
|
||||
}
|
||||
CLIENT_3 = {
|
||||
'hostname': 'client_3',
|
||||
'ip': '10.0.0.3',
|
||||
'is_wired': True,
|
||||
'mac': '00:00:00:00:00:03',
|
||||
'name': 'Non-POE Client 3',
|
||||
'oui': 'Producer',
|
||||
'sw_mac': '00:00:00:00:01:01',
|
||||
'sw_port': 3,
|
||||
'wired-rx_bytes': 1234000000,
|
||||
'wired-tx_bytes': 5678000000
|
||||
"hostname": "client_3",
|
||||
"ip": "10.0.0.3",
|
||||
"is_wired": True,
|
||||
"mac": "00:00:00:00:00:03",
|
||||
"name": "Non-POE Client 3",
|
||||
"oui": "Producer",
|
||||
"sw_mac": "00:00:00:00:01:01",
|
||||
"sw_port": 3,
|
||||
"wired-rx_bytes": 1234000000,
|
||||
"wired-tx_bytes": 5678000000,
|
||||
}
|
||||
CLIENT_4 = {
|
||||
'hostname': 'client_4',
|
||||
'ip': '10.0.0.4',
|
||||
'is_wired': True,
|
||||
'mac': '00:00:00:00:00:04',
|
||||
'name': 'Non-POE Client 4',
|
||||
'oui': 'Producer',
|
||||
'sw_mac': '00:00:00:00:01:01',
|
||||
'sw_port': 4,
|
||||
'wired-rx_bytes': 1234000000,
|
||||
'wired-tx_bytes': 5678000000
|
||||
"hostname": "client_4",
|
||||
"ip": "10.0.0.4",
|
||||
"is_wired": True,
|
||||
"mac": "00:00:00:00:00:04",
|
||||
"name": "Non-POE Client 4",
|
||||
"oui": "Producer",
|
||||
"sw_mac": "00:00:00:00:01:01",
|
||||
"sw_port": 4,
|
||||
"wired-rx_bytes": 1234000000,
|
||||
"wired-tx_bytes": 5678000000,
|
||||
}
|
||||
CLOUDKEY = {
|
||||
'hostname': 'client_1',
|
||||
'ip': 'mock-host',
|
||||
'is_wired': True,
|
||||
'mac': '10:00:00:00:00:01',
|
||||
'name': 'Cloud key',
|
||||
'oui': 'Producer',
|
||||
'sw_mac': '00:00:00:00:01:01',
|
||||
'sw_port': 1,
|
||||
'wired-rx_bytes': 1234000000,
|
||||
'wired-tx_bytes': 5678000000
|
||||
"hostname": "client_1",
|
||||
"ip": "mock-host",
|
||||
"is_wired": True,
|
||||
"mac": "10:00:00:00:00:01",
|
||||
"name": "Cloud key",
|
||||
"oui": "Producer",
|
||||
"sw_mac": "00:00:00:00:01:01",
|
||||
"sw_port": 1,
|
||||
"wired-rx_bytes": 1234000000,
|
||||
"wired-tx_bytes": 5678000000,
|
||||
}
|
||||
POE_SWITCH_CLIENTS = [
|
||||
{
|
||||
'hostname': 'client_1',
|
||||
'ip': '10.0.0.1',
|
||||
'is_wired': True,
|
||||
'mac': '00:00:00:00:00:01',
|
||||
'name': 'POE Client 1',
|
||||
'oui': 'Producer',
|
||||
'sw_mac': '00:00:00:00:01:01',
|
||||
'sw_port': 1,
|
||||
'wired-rx_bytes': 1234000000,
|
||||
'wired-tx_bytes': 5678000000
|
||||
"hostname": "client_1",
|
||||
"ip": "10.0.0.1",
|
||||
"is_wired": True,
|
||||
"mac": "00:00:00:00:00:01",
|
||||
"name": "POE Client 1",
|
||||
"oui": "Producer",
|
||||
"sw_mac": "00:00:00:00:01:01",
|
||||
"sw_port": 1,
|
||||
"wired-rx_bytes": 1234000000,
|
||||
"wired-tx_bytes": 5678000000,
|
||||
},
|
||||
{
|
||||
'hostname': 'client_2',
|
||||
'ip': '10.0.0.2',
|
||||
'is_wired': True,
|
||||
'mac': '00:00:00:00:00:02',
|
||||
'name': 'POE Client 2',
|
||||
'oui': 'Producer',
|
||||
'sw_mac': '00:00:00:00:01:01',
|
||||
'sw_port': 1,
|
||||
'wired-rx_bytes': 1234000000,
|
||||
'wired-tx_bytes': 5678000000
|
||||
}
|
||||
"hostname": "client_2",
|
||||
"ip": "10.0.0.2",
|
||||
"is_wired": True,
|
||||
"mac": "00:00:00:00:00:02",
|
||||
"name": "POE Client 2",
|
||||
"oui": "Producer",
|
||||
"sw_mac": "00:00:00:00:01:01",
|
||||
"sw_port": 1,
|
||||
"wired-rx_bytes": 1234000000,
|
||||
"wired-tx_bytes": 5678000000,
|
||||
},
|
||||
]
|
||||
|
||||
DEVICE_1 = {
|
||||
'device_id': 'mock-id',
|
||||
'ip': '10.0.1.1',
|
||||
'mac': '00:00:00:00:01:01',
|
||||
'type': 'usw',
|
||||
'name': 'mock-name',
|
||||
'port_overrides': [],
|
||||
'port_table': [
|
||||
"device_id": "mock-id",
|
||||
"ip": "10.0.1.1",
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"type": "usw",
|
||||
"name": "mock-name",
|
||||
"port_overrides": [],
|
||||
"port_table": [
|
||||
{
|
||||
'media': 'GE',
|
||||
'name': 'Port 1',
|
||||
'port_idx': 1,
|
||||
'poe_class': 'Class 4',
|
||||
'poe_enable': True,
|
||||
'poe_mode': 'auto',
|
||||
'poe_power': '2.56',
|
||||
'poe_voltage': '53.40',
|
||||
'portconf_id': '1a1',
|
||||
'port_poe': True,
|
||||
'up': True
|
||||
"media": "GE",
|
||||
"name": "Port 1",
|
||||
"port_idx": 1,
|
||||
"poe_class": "Class 4",
|
||||
"poe_enable": True,
|
||||
"poe_mode": "auto",
|
||||
"poe_power": "2.56",
|
||||
"poe_voltage": "53.40",
|
||||
"portconf_id": "1a1",
|
||||
"port_poe": True,
|
||||
"up": True,
|
||||
},
|
||||
{
|
||||
'media': 'GE',
|
||||
'name': 'Port 2',
|
||||
'port_idx': 2,
|
||||
'poe_class': 'Class 4',
|
||||
'poe_enable': True,
|
||||
'poe_mode': 'auto',
|
||||
'poe_power': '2.56',
|
||||
'poe_voltage': '53.40',
|
||||
'portconf_id': '1a2',
|
||||
'port_poe': True,
|
||||
'up': True
|
||||
"media": "GE",
|
||||
"name": "Port 2",
|
||||
"port_idx": 2,
|
||||
"poe_class": "Class 4",
|
||||
"poe_enable": True,
|
||||
"poe_mode": "auto",
|
||||
"poe_power": "2.56",
|
||||
"poe_voltage": "53.40",
|
||||
"portconf_id": "1a2",
|
||||
"port_poe": True,
|
||||
"up": True,
|
||||
},
|
||||
{
|
||||
'media': 'GE',
|
||||
'name': 'Port 3',
|
||||
'port_idx': 3,
|
||||
'poe_class': 'Unknown',
|
||||
'poe_enable': False,
|
||||
'poe_mode': 'off',
|
||||
'poe_power': '0.00',
|
||||
'poe_voltage': '0.00',
|
||||
'portconf_id': '1a3',
|
||||
'port_poe': False,
|
||||
'up': True
|
||||
"media": "GE",
|
||||
"name": "Port 3",
|
||||
"port_idx": 3,
|
||||
"poe_class": "Unknown",
|
||||
"poe_enable": False,
|
||||
"poe_mode": "off",
|
||||
"poe_power": "0.00",
|
||||
"poe_voltage": "0.00",
|
||||
"portconf_id": "1a3",
|
||||
"port_poe": False,
|
||||
"up": True,
|
||||
},
|
||||
{
|
||||
'media': 'GE',
|
||||
'name': 'Port 4',
|
||||
'port_idx': 4,
|
||||
'poe_class': 'Unknown',
|
||||
'poe_enable': False,
|
||||
'poe_mode': 'auto',
|
||||
'poe_power': '0.00',
|
||||
'poe_voltage': '0.00',
|
||||
'portconf_id': '1a4',
|
||||
'port_poe': True,
|
||||
'up': True
|
||||
}
|
||||
]
|
||||
"media": "GE",
|
||||
"name": "Port 4",
|
||||
"port_idx": 4,
|
||||
"poe_class": "Unknown",
|
||||
"poe_enable": False,
|
||||
"poe_mode": "auto",
|
||||
"poe_power": "0.00",
|
||||
"poe_voltage": "0.00",
|
||||
"portconf_id": "1a4",
|
||||
"port_poe": True,
|
||||
"up": True,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
BLOCKED = {
|
||||
'blocked': True,
|
||||
'hostname': 'block_client_1',
|
||||
'ip': '10.0.0.1',
|
||||
'is_guest': False,
|
||||
'is_wired': False,
|
||||
'mac': '00:00:00:00:01:01',
|
||||
'name': 'Block Client 1',
|
||||
'noted': True,
|
||||
'oui': 'Producer',
|
||||
"blocked": True,
|
||||
"hostname": "block_client_1",
|
||||
"ip": "10.0.0.1",
|
||||
"is_guest": False,
|
||||
"is_wired": False,
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"name": "Block Client 1",
|
||||
"noted": True,
|
||||
"oui": "Producer",
|
||||
}
|
||||
UNBLOCKED = {
|
||||
'blocked': False,
|
||||
'hostname': 'block_client_2',
|
||||
'ip': '10.0.0.2',
|
||||
'is_guest': False,
|
||||
'is_wired': True,
|
||||
'mac': '00:00:00:00:01:02',
|
||||
'name': 'Block Client 2',
|
||||
'noted': True,
|
||||
'oui': 'Producer',
|
||||
"blocked": False,
|
||||
"hostname": "block_client_2",
|
||||
"ip": "10.0.0.2",
|
||||
"is_guest": False,
|
||||
"is_wired": True,
|
||||
"mac": "00:00:00:00:01:02",
|
||||
"name": "Block Client 2",
|
||||
"noted": True,
|
||||
"oui": "Producer",
|
||||
}
|
||||
|
||||
CONTROLLER_DATA = {
|
||||
CONF_HOST: 'mock-host',
|
||||
CONF_USERNAME: 'mock-user',
|
||||
CONF_PASSWORD: 'mock-pswd',
|
||||
CONF_HOST: "mock-host",
|
||||
CONF_USERNAME: "mock-user",
|
||||
CONF_PASSWORD: "mock-pswd",
|
||||
CONF_PORT: 1234,
|
||||
CONF_SITE_ID: 'mock-site',
|
||||
CONF_VERIFY_SSL: True
|
||||
CONF_SITE_ID: "mock-site",
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
CONF_CONTROLLER: CONTROLLER_DATA
|
||||
}
|
||||
ENTRY_CONFIG = {CONF_CONTROLLER: CONTROLLER_DATA}
|
||||
|
||||
CONTROLLER_ID = unifi.CONTROLLER_ID.format(host='mock-host', site='mock-site')
|
||||
CONTROLLER_ID = unifi.CONTROLLER_ID.format(host="mock-host", site="mock-site")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -216,7 +222,7 @@ def mock_controller(hass):
|
||||
hass.data[UNIFI_CONFIG] = {}
|
||||
controller = unifi.UniFiController(hass, None)
|
||||
|
||||
controller._site_role = 'admin'
|
||||
controller._site_role = "admin"
|
||||
|
||||
controller.api = Mock()
|
||||
controller.mock_requests = []
|
||||
@@ -226,14 +232,14 @@ def mock_controller(hass):
|
||||
controller.mock_client_all_responses = deque()
|
||||
|
||||
async def mock_request(method, path, **kwargs):
|
||||
kwargs['method'] = method
|
||||
kwargs['path'] = path
|
||||
kwargs["method"] = method
|
||||
kwargs["path"] = path
|
||||
controller.mock_requests.append(kwargs)
|
||||
if path == 's/{site}/stat/sta':
|
||||
if path == "s/{site}/stat/sta":
|
||||
return controller.mock_client_responses.popleft()
|
||||
if path == 's/{site}/stat/device':
|
||||
if path == "s/{site}/stat/device":
|
||||
return controller.mock_device_responses.popleft()
|
||||
if path == 's/{site}/rest/user':
|
||||
if path == "s/{site}/rest/user":
|
||||
return controller.mock_client_all_responses.popleft()
|
||||
return None
|
||||
|
||||
@@ -249,23 +255,30 @@ async def setup_controller(hass, mock_controller):
|
||||
hass.config.components.add(unifi.DOMAIN)
|
||||
hass.data[unifi.DOMAIN] = {CONTROLLER_ID: mock_controller}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, unifi.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_POLL, entry_id=1)
|
||||
1,
|
||||
unifi.DOMAIN,
|
||||
"Mock Title",
|
||||
ENTRY_CONFIG,
|
||||
"test",
|
||||
config_entries.CONN_CLASS_LOCAL_POLL,
|
||||
entry_id=1,
|
||||
)
|
||||
mock_controller.config_entry = config_entry
|
||||
|
||||
await mock_controller.async_update()
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'switch')
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, "switch")
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that we do not discover anything or try to set up a bridge."""
|
||||
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'unifi'
|
||||
}
|
||||
}) is True
|
||||
assert (
|
||||
await async_setup_component(
|
||||
hass, switch.DOMAIN, {"switch": {"platform": "unifi"}}
|
||||
)
|
||||
is True
|
||||
)
|
||||
assert unifi.DOMAIN not in hass.data
|
||||
|
||||
|
||||
@@ -285,7 +298,7 @@ async def test_controller_not_client(hass, mock_controller):
|
||||
await setup_controller(hass, mock_controller)
|
||||
assert len(mock_controller.mock_requests) == 2
|
||||
assert not hass.states.async_all()
|
||||
cloudkey = hass.states.get('switch.cloud_key')
|
||||
cloudkey = hass.states.get("switch.cloud_key")
|
||||
assert cloudkey is None
|
||||
|
||||
|
||||
@@ -294,7 +307,7 @@ async def test_not_admin(hass, mock_controller):
|
||||
mock_controller.mock_client_responses.append([CLIENT_1])
|
||||
mock_controller.mock_device_responses.append([])
|
||||
|
||||
mock_controller._site_role = 'viewer'
|
||||
mock_controller._site_role = "viewer"
|
||||
|
||||
await setup_controller(hass, mock_controller)
|
||||
assert len(mock_controller.mock_requests) == 2
|
||||
@@ -305,36 +318,35 @@ async def test_switches(hass, mock_controller):
|
||||
"""Test the update_items function with some clients."""
|
||||
mock_controller.mock_client_responses.append([CLIENT_1, CLIENT_4])
|
||||
mock_controller.mock_device_responses.append([DEVICE_1])
|
||||
mock_controller.mock_client_all_responses.append(
|
||||
[BLOCKED, UNBLOCKED, CLIENT_1])
|
||||
mock_controller.mock_client_all_responses.append([BLOCKED, UNBLOCKED, CLIENT_1])
|
||||
mock_controller.unifi_config = {
|
||||
unifi.CONF_BLOCK_CLIENT: [BLOCKED['mac'], UNBLOCKED['mac']]
|
||||
unifi.CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]]
|
||||
}
|
||||
|
||||
await setup_controller(hass, mock_controller)
|
||||
assert len(mock_controller.mock_requests) == 3
|
||||
assert len(hass.states.async_all()) == 5
|
||||
|
||||
switch_1 = hass.states.get('switch.poe_client_1')
|
||||
switch_1 = hass.states.get("switch.poe_client_1")
|
||||
assert switch_1 is not None
|
||||
assert switch_1.state == 'on'
|
||||
assert switch_1.attributes['power'] == '2.56'
|
||||
assert switch_1.attributes['received'] == 1234
|
||||
assert switch_1.attributes['sent'] == 5678
|
||||
assert switch_1.attributes['switch'] == '00:00:00:00:01:01'
|
||||
assert switch_1.attributes['port'] == 1
|
||||
assert switch_1.attributes['poe_mode'] == 'auto'
|
||||
assert switch_1.state == "on"
|
||||
assert switch_1.attributes["power"] == "2.56"
|
||||
assert switch_1.attributes["received"] == 1234
|
||||
assert switch_1.attributes["sent"] == 5678
|
||||
assert switch_1.attributes["switch"] == "00:00:00:00:01:01"
|
||||
assert switch_1.attributes["port"] == 1
|
||||
assert switch_1.attributes["poe_mode"] == "auto"
|
||||
|
||||
switch_4 = hass.states.get('switch.poe_client_4')
|
||||
switch_4 = hass.states.get("switch.poe_client_4")
|
||||
assert switch_4 is None
|
||||
|
||||
blocked = hass.states.get('switch.block_client_1')
|
||||
blocked = hass.states.get("switch.block_client_1")
|
||||
assert blocked is not None
|
||||
assert blocked.state == 'on'
|
||||
assert blocked.state == "on"
|
||||
|
||||
unblocked = hass.states.get('switch.block_client_2')
|
||||
unblocked = hass.states.get("switch.block_client_2")
|
||||
assert unblocked is not None
|
||||
assert unblocked.state == 'off'
|
||||
assert unblocked.state == "off"
|
||||
|
||||
|
||||
async def test_new_client_discovered(hass, mock_controller):
|
||||
@@ -350,40 +362,36 @@ async def test_new_client_discovered(hass, mock_controller):
|
||||
mock_controller.mock_device_responses.append([DEVICE_1])
|
||||
|
||||
# Calling a service will trigger the updates to run
|
||||
await hass.services.async_call('switch', 'turn_off', {
|
||||
'entity_id': 'switch.poe_client_1'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_off", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
)
|
||||
assert len(mock_controller.mock_requests) == 5
|
||||
assert len(hass.states.async_all()) == 3
|
||||
assert mock_controller.mock_requests[2] == {
|
||||
'json': {
|
||||
'port_overrides': [{
|
||||
'port_idx': 1,
|
||||
'portconf_id': '1a1',
|
||||
'poe_mode': 'off'}]
|
||||
"json": {
|
||||
"port_overrides": [{"port_idx": 1, "portconf_id": "1a1", "poe_mode": "off"}]
|
||||
},
|
||||
'method': 'put',
|
||||
'path': 's/{site}/rest/device/mock-id'
|
||||
"method": "put",
|
||||
"path": "s/{site}/rest/device/mock-id",
|
||||
}
|
||||
|
||||
await hass.services.async_call('switch', 'turn_on', {
|
||||
'entity_id': 'switch.poe_client_1'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_on", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
)
|
||||
assert len(mock_controller.mock_requests) == 7
|
||||
assert mock_controller.mock_requests[5] == {
|
||||
'json': {
|
||||
'port_overrides': [{
|
||||
'port_idx': 1,
|
||||
'portconf_id': '1a1',
|
||||
'poe_mode': 'auto'}]
|
||||
"json": {
|
||||
"port_overrides": [
|
||||
{"port_idx": 1, "portconf_id": "1a1", "poe_mode": "auto"}
|
||||
]
|
||||
},
|
||||
'method': 'put',
|
||||
'path': 's/{site}/rest/device/mock-id'
|
||||
"method": "put",
|
||||
"path": "s/{site}/rest/device/mock-id",
|
||||
}
|
||||
|
||||
switch_2 = hass.states.get('switch.poe_client_2')
|
||||
switch_2 = hass.states.get("switch.poe_client_2")
|
||||
assert switch_2 is not None
|
||||
assert switch_2.state == 'on'
|
||||
assert switch_2.state == "on"
|
||||
|
||||
|
||||
async def test_failed_update_successful_login(hass, mock_controller):
|
||||
@@ -424,9 +432,9 @@ async def test_failed_update_unreachable_controller(hass, mock_controller):
|
||||
mock_controller.api.clients.update.side_effect = aiounifi.AiounifiException
|
||||
|
||||
# Calling a service will trigger the updates to run
|
||||
await hass.services.async_call('switch', 'turn_off', {
|
||||
'entity_id': 'switch.poe_client_1'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_off", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
)
|
||||
|
||||
assert len(mock_controller.mock_requests) == 3
|
||||
assert len(hass.states.async_all()) == 3
|
||||
@@ -447,8 +455,8 @@ async def test_ignore_multiple_poe_clients_on_same_port(hass, mock_controller):
|
||||
# 1 All Lights group, 2 lights
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
switch_1 = hass.states.get('switch.poe_client_1')
|
||||
switch_2 = hass.states.get('switch.poe_client_2')
|
||||
switch_1 = hass.states.get("switch.poe_client_1")
|
||||
switch_2 = hass.states.get("switch.poe_client_2")
|
||||
assert switch_1 is None
|
||||
assert switch_2 is None
|
||||
|
||||
@@ -458,23 +466,27 @@ async def test_restoring_client(hass, mock_controller):
|
||||
mock_controller.mock_client_responses.append([CLIENT_2])
|
||||
mock_controller.mock_device_responses.append([DEVICE_1])
|
||||
mock_controller.mock_client_all_responses.append([CLIENT_1])
|
||||
mock_controller.unifi_config = {
|
||||
unifi.CONF_BLOCK_CLIENT: ['random mac']
|
||||
}
|
||||
mock_controller.unifi_config = {unifi.CONF_BLOCK_CLIENT: ["random mac"]}
|
||||
|
||||
registry = await entity_registry.async_get_registry(hass)
|
||||
registry.async_get_or_create(
|
||||
switch.DOMAIN, unifi.DOMAIN,
|
||||
'poe-{}'.format(CLIENT_1['mac']),
|
||||
suggested_object_id=CLIENT_1['hostname'], config_entry_id=1)
|
||||
switch.DOMAIN,
|
||||
unifi.DOMAIN,
|
||||
"poe-{}".format(CLIENT_1["mac"]),
|
||||
suggested_object_id=CLIENT_1["hostname"],
|
||||
config_entry_id=1,
|
||||
)
|
||||
registry.async_get_or_create(
|
||||
switch.DOMAIN, unifi.DOMAIN,
|
||||
'poe-{}'.format(CLIENT_2['mac']),
|
||||
suggested_object_id=CLIENT_2['hostname'], config_entry_id=1)
|
||||
switch.DOMAIN,
|
||||
unifi.DOMAIN,
|
||||
"poe-{}".format(CLIENT_2["mac"]),
|
||||
suggested_object_id=CLIENT_2["hostname"],
|
||||
config_entry_id=1,
|
||||
)
|
||||
|
||||
await setup_controller(hass, mock_controller)
|
||||
assert len(mock_controller.mock_requests) == 3
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
device_1 = hass.states.get('switch.client_1')
|
||||
device_1 = hass.states.get("switch.client_1")
|
||||
assert device_1 is not None
|
||||
Reference in new issue
Block a user