* Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
269 lines
9.1 KiB
Python
269 lines
9.1 KiB
Python
"""The tests for the Unifi WAP device tracker platform."""
|
|
from unittest import mock
|
|
from datetime import datetime, timedelta
|
|
from pyunifi.controller import APIError
|
|
|
|
|
|
import pytest
|
|
import voluptuous as vol
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
from homeassistant.components.device_tracker import DOMAIN
|
|
import homeassistant.components.unifi.device_tracker as unifi
|
|
from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
|
|
CONF_PLATFORM, CONF_VERIFY_SSL,
|
|
CONF_MONITORED_CONDITIONS)
|
|
DEFAULT_DETECTION_TIME = timedelta(seconds=300)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_ctrl():
|
|
"""Mock pyunifi."""
|
|
with mock.patch('pyunifi.controller.Controller') as mock_control:
|
|
yield mock_control
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_scanner():
|
|
"""Mock UnifyScanner."""
|
|
with mock.patch('homeassistant.components.unifi.device_tracker'
|
|
'.UnifiScanner') as scanner:
|
|
yield scanner
|
|
|
|
|
|
@mock.patch('os.access', return_value=True)
|
|
@mock.patch('os.path.isfile', mock.Mock(return_value=True))
|
|
def test_config_valid_verify_ssl(hass, mock_scanner, mock_ctrl):
|
|
"""Test the setup with a string for ssl_verify.
|
|
|
|
Representing the absolute path to a CA certificate bundle.
|
|
"""
|
|
config = {
|
|
DOMAIN: unifi.PLATFORM_SCHEMA({
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
|
CONF_USERNAME: 'foo',
|
|
CONF_PASSWORD: 'password',
|
|
CONF_VERIFY_SSL: "/tmp/unifi.crt"
|
|
})
|
|
}
|
|
result = unifi.get_scanner(hass, config)
|
|
assert mock_scanner.return_value == result
|
|
assert mock_ctrl.call_count == 1
|
|
assert mock_ctrl.mock_calls[0] == \
|
|
mock.call('localhost', 'foo', 'password', 8443,
|
|
version='v4', site_id='default', ssl_verify="/tmp/unifi.crt")
|
|
|
|
assert mock_scanner.call_count == 1
|
|
assert mock_scanner.call_args == mock.call(mock_ctrl.return_value,
|
|
DEFAULT_DETECTION_TIME,
|
|
None, None)
|
|
|
|
|
|
def test_config_minimal(hass, mock_scanner, mock_ctrl):
|
|
"""Test the setup with minimal configuration."""
|
|
config = {
|
|
DOMAIN: unifi.PLATFORM_SCHEMA({
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
|
CONF_USERNAME: 'foo',
|
|
CONF_PASSWORD: 'password',
|
|
})
|
|
}
|
|
result = unifi.get_scanner(hass, config)
|
|
assert mock_scanner.return_value == result
|
|
assert mock_ctrl.call_count == 1
|
|
assert mock_ctrl.mock_calls[0] == \
|
|
mock.call('localhost', 'foo', 'password', 8443,
|
|
version='v4', site_id='default', ssl_verify=True)
|
|
|
|
assert mock_scanner.call_count == 1
|
|
assert mock_scanner.call_args == mock.call(mock_ctrl.return_value,
|
|
DEFAULT_DETECTION_TIME,
|
|
None, None)
|
|
|
|
|
|
def test_config_full(hass, mock_scanner, mock_ctrl):
|
|
"""Test the setup with full configuration."""
|
|
config = {
|
|
DOMAIN: unifi.PLATFORM_SCHEMA({
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
|
CONF_USERNAME: 'foo',
|
|
CONF_PASSWORD: 'password',
|
|
CONF_HOST: 'myhost',
|
|
CONF_VERIFY_SSL: False,
|
|
CONF_MONITORED_CONDITIONS: ['essid', 'signal'],
|
|
'port': 123,
|
|
'site_id': 'abcdef01',
|
|
'detection_time': 300,
|
|
})
|
|
}
|
|
result = unifi.get_scanner(hass, config)
|
|
assert mock_scanner.return_value == result
|
|
assert mock_ctrl.call_count == 1
|
|
assert mock_ctrl.call_args == \
|
|
mock.call('myhost', 'foo', 'password', 123,
|
|
version='v4', site_id='abcdef01', ssl_verify=False)
|
|
|
|
assert mock_scanner.call_count == 1
|
|
assert mock_scanner.call_args == mock.call(
|
|
mock_ctrl.return_value,
|
|
DEFAULT_DETECTION_TIME,
|
|
None,
|
|
config[DOMAIN][CONF_MONITORED_CONDITIONS])
|
|
|
|
|
|
def test_config_error():
|
|
"""Test for configuration errors."""
|
|
with pytest.raises(vol.Invalid):
|
|
unifi.PLATFORM_SCHEMA({
|
|
# no username
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
|
CONF_HOST: 'myhost',
|
|
'port': 123,
|
|
})
|
|
with pytest.raises(vol.Invalid):
|
|
unifi.PLATFORM_SCHEMA({
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
|
CONF_USERNAME: 'foo',
|
|
CONF_PASSWORD: 'password',
|
|
CONF_HOST: 'myhost',
|
|
'port': 'foo', # bad port!
|
|
})
|
|
with pytest.raises(vol.Invalid):
|
|
unifi.PLATFORM_SCHEMA({
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
|
CONF_USERNAME: 'foo',
|
|
CONF_PASSWORD: 'password',
|
|
CONF_VERIFY_SSL: "dfdsfsdfsd", # Invalid ssl_verify (no file)
|
|
})
|
|
|
|
|
|
def test_config_controller_failed(hass, mock_ctrl, mock_scanner):
|
|
"""Test for controller failure."""
|
|
config = {
|
|
'device_tracker': {
|
|
CONF_PLATFORM: unifi.DOMAIN,
|
|
CONF_USERNAME: 'foo',
|
|
CONF_PASSWORD: 'password',
|
|
}
|
|
}
|
|
mock_ctrl.side_effect = APIError(
|
|
'/', 500, 'foo', {}, None)
|
|
result = unifi.get_scanner(hass, config)
|
|
assert result is False
|
|
|
|
|
|
def test_scanner_update():
|
|
"""Test the scanner update."""
|
|
ctrl = mock.MagicMock()
|
|
fake_clients = [
|
|
{'mac': '123', 'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '234', 'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
]
|
|
ctrl.get_clients.return_value = fake_clients
|
|
unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None, None)
|
|
assert ctrl.get_clients.call_count == 1
|
|
assert ctrl.get_clients.call_args == mock.call()
|
|
|
|
|
|
def test_scanner_update_error():
|
|
"""Test the scanner update for error."""
|
|
ctrl = mock.MagicMock()
|
|
ctrl.get_clients.side_effect = APIError(
|
|
'/', 500, 'foo', {}, None)
|
|
unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None, None)
|
|
|
|
|
|
def test_scan_devices():
|
|
"""Test the scanning for devices."""
|
|
ctrl = mock.MagicMock()
|
|
fake_clients = [
|
|
{'mac': '123', 'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '234', 'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
]
|
|
ctrl.get_clients.return_value = fake_clients
|
|
scanner = unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None, None)
|
|
assert set(scanner.scan_devices()) == set(['123', '234'])
|
|
|
|
|
|
def test_scan_devices_filtered():
|
|
"""Test the scanning for devices based on SSID."""
|
|
ctrl = mock.MagicMock()
|
|
fake_clients = [
|
|
{'mac': '123', 'essid': 'foonet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '234', 'essid': 'foonet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '567', 'essid': 'notnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '890', 'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
]
|
|
|
|
ssid_filter = ['foonet', 'barnet']
|
|
ctrl.get_clients.return_value = fake_clients
|
|
scanner = unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, ssid_filter,
|
|
None)
|
|
assert set(scanner.scan_devices()) == set(['123', '234', '890'])
|
|
|
|
|
|
def test_get_device_name():
|
|
"""Test the getting of device names."""
|
|
ctrl = mock.MagicMock()
|
|
fake_clients = [
|
|
{'mac': '123',
|
|
'hostname': 'foobar',
|
|
'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '234',
|
|
'name': 'Nice Name',
|
|
'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '456',
|
|
'essid': 'barnet',
|
|
'last_seen': '1504786810'},
|
|
]
|
|
ctrl.get_clients.return_value = fake_clients
|
|
scanner = unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None, None)
|
|
assert scanner.get_device_name('123') == 'foobar'
|
|
assert scanner.get_device_name('234') == 'Nice Name'
|
|
assert scanner.get_device_name('456') is None
|
|
assert scanner.get_device_name('unknown') is None
|
|
|
|
|
|
def test_monitored_conditions():
|
|
"""Test the filtering of attributes."""
|
|
ctrl = mock.MagicMock()
|
|
fake_clients = [
|
|
{'mac': '123',
|
|
'hostname': 'foobar',
|
|
'essid': 'barnet',
|
|
'signal': -60,
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow()),
|
|
'latest_assoc_time': 946684800.0},
|
|
{'mac': '234',
|
|
'name': 'Nice Name',
|
|
'essid': 'barnet',
|
|
'signal': -42,
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
{'mac': '456',
|
|
'hostname': 'wired',
|
|
'essid': 'barnet',
|
|
'last_seen': dt_util.as_timestamp(dt_util.utcnow())},
|
|
]
|
|
ctrl.get_clients.return_value = fake_clients
|
|
scanner = unifi.UnifiScanner(ctrl, DEFAULT_DETECTION_TIME, None,
|
|
['essid', 'signal', 'latest_assoc_time'])
|
|
assert scanner.get_extra_attributes('123') == {
|
|
'essid': 'barnet',
|
|
'signal': -60,
|
|
'latest_assoc_time': datetime(2000, 1, 1, 0, 0, tzinfo=dt_util.UTC)
|
|
}
|
|
assert scanner.get_extra_attributes('234') == {
|
|
'essid': 'barnet',
|
|
'signal': -42
|
|
}
|
|
assert scanner.get_extra_attributes('456') == {'essid': 'barnet'}
|