Axis config flow (#18543)
* Initial draft * Add tests for init Fix hound comments * Add tests for device Change parameter handling to make device easier to test * Remove superfluous functionality per Martins request * Fix hound comments * Embedded platforms * Fix device import * Config flow retry * Options default values will be set automatically to options in config entry before component can be used * Clean up init Add populate options Fix small issues in config flow Add tests covering init * Improve device tests * Add config flow tests * Fix hound comments * Rebase miss * Initial tests for binary sensors * Clean up More binary sensor tests * Hound comments * Add camera tests * Fix initial state of sensors * Bump dependency to v17 * Fix pylint and flake8 * Fix comments
This commit is contained in:
1 parent
9214934d47
commit
6988fe783c
22 files changed
+1288
-324
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for the Axis component."""
|
||||
@@ -0,0 +1,102 @@
|
||||
"""Axis binary sensor platform tests."""
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import axis
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.binary_sensor as binary_sensor
|
||||
|
||||
EVENTS = [
|
||||
{
|
||||
'operation': 'Initialized',
|
||||
'topic': 'tns1:Device/tnsaxis:Sensor/PIR',
|
||||
'source': 'sensor',
|
||||
'source_idx': '0',
|
||||
'type': 'state',
|
||||
'value': '0'
|
||||
},
|
||||
{
|
||||
'operation': 'Initialized',
|
||||
'topic': 'tnsaxis:CameraApplicationPlatform/VMD/Camera1Profile1',
|
||||
'type': 'active',
|
||||
'value': '1'
|
||||
}
|
||||
]
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
axis.CONF_DEVICE: {
|
||||
axis.config_flow.CONF_HOST: '1.2.3.4',
|
||||
axis.config_flow.CONF_USERNAME: 'user',
|
||||
axis.config_flow.CONF_PASSWORD: 'pass',
|
||||
axis.config_flow.CONF_PORT: 80
|
||||
},
|
||||
axis.config_flow.CONF_MAC: '1234ABCD',
|
||||
axis.config_flow.CONF_MODEL: 'model',
|
||||
axis.config_flow.CONF_NAME: 'model 0'
|
||||
}
|
||||
|
||||
ENTRY_OPTIONS = {
|
||||
axis.CONF_CAMERA: False,
|
||||
axis.CONF_EVENTS: True,
|
||||
axis.CONF_TRIGGER_TIME: 0
|
||||
}
|
||||
|
||||
|
||||
async def setup_device(hass):
|
||||
"""Load the Axis binary sensor platform."""
|
||||
from axis import AxisDevice
|
||||
loop = Mock()
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, axis.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH, options=ENTRY_OPTIONS)
|
||||
device = axis.AxisNetworkDevice(hass, config_entry)
|
||||
device.api = AxisDevice(loop=loop, **config_entry.data[axis.CONF_DEVICE],
|
||||
signal=device.async_signal_callback)
|
||||
hass.data[axis.DOMAIN] = {device.serial: device}
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, 'binary_sensor')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return device
|
||||
|
||||
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that nothing happens when platform is manually configured."""
|
||||
assert await async_setup_component(hass, binary_sensor.DOMAIN, {
|
||||
'binary_sensor': {
|
||||
'platform': axis.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
|
||||
assert axis.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_binary_sensors(hass):
|
||||
"""Test that no sensors in Axis results in no sensor entities."""
|
||||
await setup_device(hass)
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_binary_sensors(hass):
|
||||
"""Test that sensors are loaded properly."""
|
||||
device = await setup_device(hass)
|
||||
|
||||
for event in EVENTS:
|
||||
device.api.stream.event.manage_event(event)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 2
|
||||
|
||||
pir = hass.states.get('binary_sensor.model_0_pir_0')
|
||||
assert pir.state == 'off'
|
||||
assert pir.name == 'model 0 PIR 0'
|
||||
|
||||
vmd4 = hass.states.get('binary_sensor.model_0_vmd4_camera1profile1')
|
||||
assert vmd4.state == 'on'
|
||||
assert vmd4.name == 'model 0 VMD4 Camera1Profile1'
|
||||
@@ -0,0 +1,73 @@
|
||||
"""Axis camera platform tests."""
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import axis
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.camera as camera
|
||||
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
axis.CONF_DEVICE: {
|
||||
axis.config_flow.CONF_HOST: '1.2.3.4',
|
||||
axis.config_flow.CONF_USERNAME: 'user',
|
||||
axis.config_flow.CONF_PASSWORD: 'pass',
|
||||
axis.config_flow.CONF_PORT: 80
|
||||
},
|
||||
axis.config_flow.CONF_MAC: '1234ABCD',
|
||||
axis.config_flow.CONF_MODEL: 'model',
|
||||
axis.config_flow.CONF_NAME: 'model 0'
|
||||
}
|
||||
|
||||
ENTRY_OPTIONS = {
|
||||
axis.CONF_CAMERA: False,
|
||||
axis.CONF_EVENTS: True,
|
||||
axis.CONF_TRIGGER_TIME: 0
|
||||
}
|
||||
|
||||
|
||||
async def setup_device(hass):
|
||||
"""Load the Axis binary sensor platform."""
|
||||
from axis import AxisDevice
|
||||
loop = Mock()
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, axis.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH, options=ENTRY_OPTIONS)
|
||||
device = axis.AxisNetworkDevice(hass, config_entry)
|
||||
device.api = AxisDevice(loop=loop, **config_entry.data[axis.CONF_DEVICE],
|
||||
signal=device.async_signal_callback)
|
||||
hass.data[axis.DOMAIN] = {device.serial: device}
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, 'camera')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return device
|
||||
|
||||
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that nothing happens when platform is manually configured."""
|
||||
assert await async_setup_component(hass, camera.DOMAIN, {
|
||||
'camera': {
|
||||
'platform': axis.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
|
||||
assert axis.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_camera(hass):
|
||||
"""Test that Axis camera platform is loaded properly."""
|
||||
await setup_device(hass)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
cam = hass.states.get('camera.model_0')
|
||||
assert cam.state == 'idle'
|
||||
assert cam.name == 'model 0'
|
||||
@@ -0,0 +1,319 @@
|
||||
"""Test Axis config flow."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant.components import axis
|
||||
from homeassistant.components.axis import config_flow
|
||||
|
||||
from tests.common import mock_coro, MockConfigEntry
|
||||
|
||||
import axis as axis_lib
|
||||
|
||||
|
||||
async def test_configured_devices(hass):
|
||||
"""Test that configured devices works as expected."""
|
||||
result = config_flow.configured_devices(hass)
|
||||
|
||||
assert not result
|
||||
|
||||
entry = MockConfigEntry(domain=axis.DOMAIN,
|
||||
data={axis.CONF_DEVICE: {axis.CONF_HOST: ''}})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = config_flow.configured_devices(hass)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
async def test_flow_works(hass):
|
||||
"""Test that config flow works."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch('axis.AxisDevice') as mock_device:
|
||||
def mock_constructor(
|
||||
loop, host, username, password, port, web_proto, event_types,
|
||||
signal):
|
||||
"""Fake the controller constructor."""
|
||||
mock_device.loop = loop
|
||||
mock_device.host = host
|
||||
mock_device.username = username
|
||||
mock_device.password = password
|
||||
mock_device.port = port
|
||||
return mock_device
|
||||
|
||||
def mock_get_param(param):
|
||||
"""Fake get param method."""
|
||||
return param
|
||||
|
||||
mock_device.side_effect = mock_constructor
|
||||
mock_device.vapix.load_params.return_value = Mock()
|
||||
mock_device.vapix.get_param.side_effect = mock_get_param
|
||||
|
||||
result = await flow.async_step_user(user_input={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81
|
||||
})
|
||||
|
||||
assert result['type'] == 'create_entry'
|
||||
assert result['title'] == '{} - {}'.format(
|
||||
axis_lib.vapix.VAPIX_MODEL_ID, axis_lib.vapix.VAPIX_SERIAL_NUMBER)
|
||||
assert result['data'] == {
|
||||
axis.CONF_DEVICE: {
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81
|
||||
},
|
||||
config_flow.CONF_MAC: axis_lib.vapix.VAPIX_SERIAL_NUMBER,
|
||||
config_flow.CONF_MODEL: axis_lib.vapix.VAPIX_MODEL_ID,
|
||||
config_flow.CONF_NAME: 'Brand.ProdNbr 0'
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_fails_already_configured(hass):
|
||||
"""Test that config flow fails on already configured device."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
entry = MockConfigEntry(domain=axis.DOMAIN, data={axis.CONF_DEVICE: {
|
||||
axis.CONF_HOST: '1.2.3.4'
|
||||
}})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await flow.async_step_user(user_input={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81
|
||||
})
|
||||
|
||||
assert result['errors'] == {'base': 'already_configured'}
|
||||
|
||||
|
||||
async def test_flow_fails_faulty_credentials(hass):
|
||||
"""Test that config flow fails on faulty credentials."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch('homeassistant.components.axis.config_flow.get_device',
|
||||
side_effect=config_flow.AuthenticationRequired):
|
||||
result = await flow.async_step_user(user_input={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81
|
||||
})
|
||||
|
||||
assert result['errors'] == {'base': 'faulty_credentials'}
|
||||
|
||||
|
||||
async def test_flow_fails_device_unavailable(hass):
|
||||
"""Test that config flow fails on device unavailable."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch('homeassistant.components.axis.config_flow.get_device',
|
||||
side_effect=config_flow.CannotConnect):
|
||||
result = await flow.async_step_user(user_input={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81
|
||||
})
|
||||
|
||||
assert result['errors'] == {'base': 'device_unavailable'}
|
||||
|
||||
|
||||
async def test_flow_create_entry(hass):
|
||||
"""Test that create entry can generate a name without other entries."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.model = 'model'
|
||||
|
||||
result = await flow._create_entry()
|
||||
|
||||
assert result['data'][config_flow.CONF_NAME] == 'model 0'
|
||||
|
||||
|
||||
async def test_flow_create_entry_more_entries(hass):
|
||||
"""Test that create entry can generate a name with other entries."""
|
||||
entry = MockConfigEntry(
|
||||
domain=axis.DOMAIN, data={config_flow.CONF_NAME: 'model 0',
|
||||
config_flow.CONF_MODEL: 'model'})
|
||||
entry.add_to_hass(hass)
|
||||
entry2 = MockConfigEntry(
|
||||
domain=axis.DOMAIN, data={config_flow.CONF_NAME: 'model 1',
|
||||
config_flow.CONF_MODEL: 'model'})
|
||||
entry2.add_to_hass(hass)
|
||||
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.model = 'model'
|
||||
|
||||
result = await flow._create_entry()
|
||||
|
||||
assert result['data'][config_flow.CONF_NAME] == 'model 2'
|
||||
|
||||
|
||||
async def test_discovery_flow(hass):
|
||||
"""Test that discovery for new devices work."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch.object(axis, 'get_device', return_value=mock_coro(Mock())):
|
||||
result = await flow.async_step_discovery(discovery_info={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_PORT: 80,
|
||||
'properties': {'macaddress': '1234'}
|
||||
})
|
||||
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'user'
|
||||
|
||||
|
||||
async def test_discovery_flow_known_device(hass):
|
||||
"""Test that discovery for known devices work.
|
||||
|
||||
This is legacy support from devices registered with configurator.
|
||||
"""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch('homeassistant.components.axis.config_flow.load_json',
|
||||
return_value={'1234ABCD': {
|
||||
config_flow.CONF_HOST: '2.3.4.5',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 80}}), \
|
||||
patch('axis.AxisDevice') as mock_device:
|
||||
def mock_constructor(
|
||||
loop, host, username, password, port, web_proto, event_types,
|
||||
signal):
|
||||
"""Fake the controller constructor."""
|
||||
mock_device.loop = loop
|
||||
mock_device.host = host
|
||||
mock_device.username = username
|
||||
mock_device.password = password
|
||||
mock_device.port = port
|
||||
return mock_device
|
||||
|
||||
def mock_get_param(param):
|
||||
"""Fake get param method."""
|
||||
return param
|
||||
|
||||
mock_device.side_effect = mock_constructor
|
||||
mock_device.vapix.load_params.return_value = Mock()
|
||||
mock_device.vapix.get_param.side_effect = mock_get_param
|
||||
|
||||
result = await flow.async_step_discovery(discovery_info={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_PORT: 80,
|
||||
'hostname': 'name',
|
||||
'properties': {'macaddress': '1234ABCD'}
|
||||
})
|
||||
|
||||
assert result['type'] == 'create_entry'
|
||||
|
||||
|
||||
async def test_discovery_flow_already_configured(hass):
|
||||
"""Test that discovery doesn't setup already configured devices."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
entry = MockConfigEntry(domain=axis.DOMAIN, data={axis.CONF_DEVICE: {
|
||||
axis.CONF_HOST: '1.2.3.4'
|
||||
}})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await flow.async_step_discovery(discovery_info={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81
|
||||
})
|
||||
print(result)
|
||||
assert result['type'] == 'abort'
|
||||
|
||||
|
||||
async def test_discovery_flow_link_local_address(hass):
|
||||
"""Test that discovery doesn't setup devices with link local addresses."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_discovery(discovery_info={
|
||||
config_flow.CONF_HOST: '169.254.3.4'
|
||||
})
|
||||
|
||||
assert result['type'] == 'abort'
|
||||
|
||||
|
||||
async def test_discovery_flow_bad_config_file(hass):
|
||||
"""Test that discovery with bad config files abort."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch('homeassistant.components.axis.config_flow.load_json',
|
||||
return_value={'1234ABCD': {
|
||||
config_flow.CONF_HOST: '2.3.4.5',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 80}}), \
|
||||
patch('homeassistant.components.axis.config_flow.DEVICE_SCHEMA',
|
||||
side_effect=config_flow.vol.Invalid('')):
|
||||
result = await flow.async_step_discovery(discovery_info={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
'properties': {'macaddress': '1234ABCD'}
|
||||
})
|
||||
|
||||
assert result['type'] == 'abort'
|
||||
|
||||
|
||||
async def test_import_flow_works(hass):
|
||||
"""Test that import flow works."""
|
||||
flow = config_flow.AxisFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
with patch('axis.AxisDevice') as mock_device:
|
||||
def mock_constructor(
|
||||
loop, host, username, password, port, web_proto, event_types,
|
||||
signal):
|
||||
"""Fake the controller constructor."""
|
||||
mock_device.loop = loop
|
||||
mock_device.host = host
|
||||
mock_device.username = username
|
||||
mock_device.password = password
|
||||
mock_device.port = port
|
||||
return mock_device
|
||||
|
||||
def mock_get_param(param):
|
||||
"""Fake get param method."""
|
||||
return param
|
||||
|
||||
mock_device.side_effect = mock_constructor
|
||||
mock_device.vapix.load_params.return_value = Mock()
|
||||
mock_device.vapix.get_param.side_effect = mock_get_param
|
||||
|
||||
result = await flow.async_step_import(import_config={
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81,
|
||||
config_flow.CONF_NAME: 'name'
|
||||
})
|
||||
|
||||
assert result['type'] == 'create_entry'
|
||||
assert result['title'] == '{} - {}'.format(
|
||||
axis_lib.vapix.VAPIX_MODEL_ID, axis_lib.vapix.VAPIX_SERIAL_NUMBER)
|
||||
assert result['data'] == {
|
||||
axis.CONF_DEVICE: {
|
||||
config_flow.CONF_HOST: '1.2.3.4',
|
||||
config_flow.CONF_USERNAME: 'user',
|
||||
config_flow.CONF_PASSWORD: 'pass',
|
||||
config_flow.CONF_PORT: 81
|
||||
},
|
||||
config_flow.CONF_MAC: axis_lib.vapix.VAPIX_SERIAL_NUMBER,
|
||||
config_flow.CONF_MODEL: axis_lib.vapix.VAPIX_MODEL_ID,
|
||||
config_flow.CONF_NAME: 'name'
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
"""Test Axis device."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
from homeassistant.components.axis import device, errors
|
||||
|
||||
DEVICE_DATA = {
|
||||
device.CONF_HOST: '1.2.3.4',
|
||||
device.CONF_USERNAME: 'username',
|
||||
device.CONF_PASSWORD: 'password',
|
||||
device.CONF_PORT: 1234
|
||||
}
|
||||
|
||||
ENTRY_OPTIONS = {
|
||||
device.CONF_CAMERA: True,
|
||||
device.CONF_EVENTS: ['pir'],
|
||||
}
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
device.CONF_DEVICE: DEVICE_DATA,
|
||||
device.CONF_MAC: 'mac',
|
||||
device.CONF_MODEL: 'model',
|
||||
device.CONF_NAME: 'name'
|
||||
}
|
||||
|
||||
|
||||
async def test_device_setup():
|
||||
"""Successful setup."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
entry.options = ENTRY_OPTIONS
|
||||
api = Mock()
|
||||
|
||||
axis_device = device.AxisNetworkDevice(hass, entry)
|
||||
|
||||
assert axis_device.host == DEVICE_DATA[device.CONF_HOST]
|
||||
assert axis_device.model == ENTRY_CONFIG[device.CONF_MODEL]
|
||||
assert axis_device.name == ENTRY_CONFIG[device.CONF_NAME]
|
||||
assert axis_device.serial == ENTRY_CONFIG[device.CONF_MAC]
|
||||
|
||||
with patch.object(device, 'get_device', return_value=mock_coro(api)):
|
||||
assert await axis_device.async_setup() is True
|
||||
|
||||
assert axis_device.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, 'camera')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[1][1] == \
|
||||
(entry, 'binary_sensor')
|
||||
|
||||
|
||||
async def test_device_not_accessible():
|
||||
"""Failed setup schedules a retry of setup."""
|
||||
hass = Mock()
|
||||
hass.data = dict()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
entry.options = ENTRY_OPTIONS
|
||||
|
||||
axis_device = device.AxisNetworkDevice(hass, entry)
|
||||
|
||||
with patch.object(device, 'get_device',
|
||||
side_effect=errors.CannotConnect), \
|
||||
pytest.raises(device.ConfigEntryNotReady):
|
||||
await axis_device.async_setup()
|
||||
|
||||
assert not hass.helpers.event.async_call_later.mock_calls
|
||||
|
||||
|
||||
async def test_device_unknown_error():
|
||||
"""Unknown errors are handled."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
entry.options = ENTRY_OPTIONS
|
||||
|
||||
axis_device = device.AxisNetworkDevice(hass, entry)
|
||||
|
||||
with patch.object(device, 'get_device', side_effect=Exception):
|
||||
assert await axis_device.async_setup() is False
|
||||
|
||||
assert not hass.helpers.event.async_call_later.mock_calls
|
||||
|
||||
|
||||
async def test_new_event_sends_signal(hass):
|
||||
"""Make sure that new event send signal."""
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
axis_device = device.AxisNetworkDevice(hass, entry)
|
||||
|
||||
with patch.object(device, 'async_dispatcher_send') as mock_dispatch_send:
|
||||
axis_device.async_signal_callback(action='add', event='event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_dispatch_send.mock_calls) == 1
|
||||
assert len(mock_dispatch_send.mock_calls[0]) == 3
|
||||
|
||||
|
||||
async def test_shutdown():
|
||||
"""Successful shutdown."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
axis_device = device.AxisNetworkDevice(hass, entry)
|
||||
axis_device.api = Mock()
|
||||
|
||||
axis_device.shutdown(None)
|
||||
|
||||
assert len(axis_device.api.stop.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_get_device(hass):
|
||||
"""Successful call."""
|
||||
with patch('axis.vapix.Vapix.load_params',
|
||||
return_value=mock_coro()):
|
||||
assert await device.get_device(hass, DEVICE_DATA)
|
||||
|
||||
|
||||
async def test_get_device_fails(hass):
|
||||
"""Device unauthorized yields authentication required error."""
|
||||
import axis
|
||||
|
||||
with patch('axis.vapix.Vapix.load_params',
|
||||
side_effect=axis.Unauthorized), \
|
||||
pytest.raises(errors.AuthenticationRequired):
|
||||
await device.get_device(hass, DEVICE_DATA)
|
||||
|
||||
|
||||
async def test_get_device_device_unavailable(hass):
|
||||
"""Device unavailable yields cannot connect error."""
|
||||
import axis
|
||||
|
||||
with patch('axis.vapix.Vapix.load_params',
|
||||
side_effect=axis.RequestError), \
|
||||
pytest.raises(errors.CannotConnect):
|
||||
await device.get_device(hass, DEVICE_DATA)
|
||||
|
||||
|
||||
async def test_get_device_unknown_error(hass):
|
||||
"""Device yield unknown error."""
|
||||
import axis
|
||||
|
||||
with patch('axis.vapix.Vapix.load_params',
|
||||
side_effect=axis.AxisException), \
|
||||
pytest.raises(errors.AuthenticationRequired):
|
||||
await device.get_device(hass, DEVICE_DATA)
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Test Axis component setup process."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components import axis
|
||||
|
||||
from tests.common import mock_coro, MockConfigEntry
|
||||
|
||||
|
||||
async def test_setup(hass):
|
||||
"""Test configured options for a device are loaded via config entry."""
|
||||
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch.object(axis, 'configured_devices', return_value={}):
|
||||
|
||||
assert await async_setup_component(hass, axis.DOMAIN, {
|
||||
axis.DOMAIN: {
|
||||
'device_name': {
|
||||
axis.CONF_HOST: '1.2.3.4',
|
||||
axis.config_flow.CONF_PORT: 80,
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
assert len(mock_config_entries.flow.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_device_already_configured(hass):
|
||||
"""Test already configured device does not configure a second."""
|
||||
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch.object(axis, 'configured_devices', return_value={'1.2.3.4'}):
|
||||
|
||||
assert await async_setup_component(hass, axis.DOMAIN, {
|
||||
axis.DOMAIN: {
|
||||
'device_name': {
|
||||
axis.CONF_HOST: '1.2.3.4'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
assert not mock_config_entries.flow.mock_calls
|
||||
|
||||
|
||||
async def test_setup_no_config(hass):
|
||||
"""Test setup without configuration."""
|
||||
assert await async_setup_component(hass, axis.DOMAIN, {})
|
||||
assert axis.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_setup_entry(hass):
|
||||
"""Test successful setup of entry."""
|
||||
entry = MockConfigEntry(
|
||||
domain=axis.DOMAIN, data={axis.device.CONF_MAC: '0123'})
|
||||
|
||||
mock_device = Mock()
|
||||
mock_device.async_setup.return_value = mock_coro(True)
|
||||
mock_device.serial.return_value = '1'
|
||||
|
||||
with patch.object(axis, 'AxisNetworkDevice') as mock_device_class, \
|
||||
patch.object(
|
||||
axis, 'async_populate_options', return_value=mock_coro(True)):
|
||||
mock_device_class.return_value = mock_device
|
||||
|
||||
assert await axis.async_setup_entry(hass, entry)
|
||||
|
||||
assert len(hass.data[axis.DOMAIN]) == 1
|
||||
|
||||
|
||||
async def test_setup_entry_fails(hass):
|
||||
"""Test successful setup of entry."""
|
||||
entry = MockConfigEntry(
|
||||
domain=axis.DOMAIN, data={axis.device.CONF_MAC: '0123'}, options=True)
|
||||
|
||||
mock_device = Mock()
|
||||
mock_device.async_setup.return_value = mock_coro(False)
|
||||
|
||||
with patch.object(axis, 'AxisNetworkDevice') as mock_device_class:
|
||||
mock_device_class.return_value = mock_device
|
||||
|
||||
assert not await axis.async_setup_entry(hass, entry)
|
||||
|
||||
assert not hass.data[axis.DOMAIN]
|
||||
|
||||
|
||||
async def test_populate_options(hass):
|
||||
"""Test successful populate options."""
|
||||
entry = MockConfigEntry(domain=axis.DOMAIN, data={'device': {}})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch.object(axis, 'get_device', return_value=mock_coro(Mock())):
|
||||
|
||||
await axis.async_populate_options(hass, entry)
|
||||
|
||||
assert entry.options == {
|
||||
axis.CONF_CAMERA: True,
|
||||
axis.CONF_EVENTS: True,
|
||||
axis.CONF_TRIGGER_TIME: axis.DEFAULT_TRIGGER_TIME
|
||||
}
|
||||
Reference in new issue
Block a user