2018-04-18 16:27:44 +02:00
|
|
|
"""Test deCONZ component setup process."""
|
2018-04-23 18:00:16 +02:00
|
|
|
from unittest.mock import Mock, patch
|
2018-04-18 16:27:44 +02:00
|
|
|
|
2018-10-26 09:15:26 +02:00
|
|
|
from homeassistant.components import deconz
|
|
|
|
from homeassistant.components.deconz import DATA_DECONZ_ID
|
2018-05-05 16:11:00 +02:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2018-04-18 16:27:44 +02:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-04-23 18:00:16 +02:00
|
|
|
from tests.common import mock_coro
|
|
|
|
|
2018-08-24 19:37:22 +02:00
|
|
|
CONFIG = {
|
|
|
|
"config": {
|
|
|
|
"bridgeid": "0123456789ABCDEF",
|
|
|
|
"mac": "12:34:56:78:90:ab",
|
|
|
|
"modelid": "deCONZ",
|
|
|
|
"name": "Phoscon",
|
|
|
|
"swversion": "2.05.35"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:27:44 +02:00
|
|
|
|
|
|
|
async def test_config_with_host_passed_to_config_entry(hass):
|
|
|
|
"""Test that configured options for a host are loaded via config entry."""
|
|
|
|
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
|
|
|
patch.object(deconz, 'configured_hosts', return_value=[]), \
|
|
|
|
patch.object(deconz, 'load_json', return_value={}):
|
|
|
|
assert await async_setup_component(hass, deconz.DOMAIN, {
|
|
|
|
deconz.DOMAIN: {
|
|
|
|
deconz.CONF_HOST: '1.2.3.4',
|
|
|
|
deconz.CONF_PORT: 80
|
|
|
|
}
|
|
|
|
}) is True
|
|
|
|
# Import flow started
|
|
|
|
assert len(mock_config_entries.flow.mock_calls) == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_file_passed_to_config_entry(hass):
|
|
|
|
"""Test that configuration file for a host are loaded via config entry."""
|
|
|
|
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
|
|
|
patch.object(deconz, 'configured_hosts', return_value=[]), \
|
|
|
|
patch.object(deconz, 'load_json',
|
|
|
|
return_value={'host': '1.2.3.4'}):
|
|
|
|
assert await async_setup_component(hass, deconz.DOMAIN, {
|
|
|
|
deconz.DOMAIN: {}
|
|
|
|
}) is True
|
|
|
|
# Import flow started
|
|
|
|
assert len(mock_config_entries.flow.mock_calls) == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_without_host_not_passed_to_config_entry(hass):
|
|
|
|
"""Test that a configuration without a host does not initiate an import."""
|
|
|
|
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
|
|
|
patch.object(deconz, 'configured_hosts', return_value=[]), \
|
|
|
|
patch.object(deconz, 'load_json', return_value={}):
|
|
|
|
assert await async_setup_component(hass, deconz.DOMAIN, {
|
|
|
|
deconz.DOMAIN: {}
|
|
|
|
}) is True
|
|
|
|
# No flow started
|
|
|
|
assert len(mock_config_entries.flow.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_already_registered_not_passed_to_config_entry(hass):
|
|
|
|
"""Test that an already registered host does not initiate an import."""
|
|
|
|
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
|
|
|
patch.object(deconz, 'configured_hosts',
|
|
|
|
return_value=['1.2.3.4']), \
|
|
|
|
patch.object(deconz, 'load_json', return_value={}):
|
|
|
|
assert await async_setup_component(hass, deconz.DOMAIN, {
|
|
|
|
deconz.DOMAIN: {
|
|
|
|
deconz.CONF_HOST: '1.2.3.4',
|
|
|
|
deconz.CONF_PORT: 80
|
|
|
|
}
|
|
|
|
}) is True
|
|
|
|
# No flow started
|
|
|
|
assert len(mock_config_entries.flow.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_discovery(hass):
|
|
|
|
"""Test that a discovered bridge does not initiate an import."""
|
|
|
|
with patch.object(hass, 'config_entries') as mock_config_entries:
|
|
|
|
assert await async_setup_component(hass, deconz.DOMAIN, {}) is True
|
|
|
|
# No flow started
|
|
|
|
assert len(mock_config_entries.flow.mock_calls) == 0
|
2018-04-23 18:00:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_already_registered_bridge(hass):
|
|
|
|
"""Test setup entry doesn't allow more than one instance of deCONZ."""
|
|
|
|
hass.data[deconz.DOMAIN] = True
|
|
|
|
assert await deconz.async_setup_entry(hass, {}) is False
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_no_available_bridge(hass):
|
|
|
|
"""Test setup entry fails if deCONZ is not available."""
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
|
|
|
with patch('pydeconz.DeconzSession.async_load_parameters',
|
|
|
|
return_value=mock_coro(False)):
|
|
|
|
assert await deconz.async_setup_entry(hass, entry) is False
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_successful(hass):
|
|
|
|
"""Test setup entry is successful."""
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
2018-07-23 14:05:38 +02:00
|
|
|
with patch.object(hass, 'async_create_task') as mock_add_job, \
|
2018-04-23 18:00:16 +02:00
|
|
|
patch.object(hass, 'config_entries') as mock_config_entries, \
|
2018-08-24 19:37:22 +02:00
|
|
|
patch('pydeconz.DeconzSession.async_get_state',
|
|
|
|
return_value=mock_coro(CONFIG)), \
|
|
|
|
patch('pydeconz.DeconzSession.start', return_value=True), \
|
|
|
|
patch('homeassistant.helpers.device_registry.async_get_registry',
|
|
|
|
return_value=mock_coro(Mock())):
|
2018-04-23 18:00:16 +02:00
|
|
|
assert await deconz.async_setup_entry(hass, entry) is True
|
|
|
|
assert hass.data[deconz.DOMAIN]
|
|
|
|
assert hass.data[deconz.DATA_DECONZ_ID] == {}
|
2018-05-05 16:11:00 +02:00
|
|
|
assert len(hass.data[deconz.DATA_DECONZ_UNSUB]) == 1
|
2018-09-21 19:59:20 +02:00
|
|
|
assert len(mock_add_job.mock_calls) == \
|
|
|
|
len(deconz.SUPPORTED_PLATFORMS)
|
|
|
|
assert len(mock_config_entries.async_forward_entry_setup.mock_calls) == \
|
|
|
|
len(deconz.SUPPORTED_PLATFORMS)
|
2018-04-23 18:00:16 +02:00
|
|
|
assert mock_config_entries.async_forward_entry_setup.mock_calls[0][1] == \
|
|
|
|
(entry, 'binary_sensor')
|
|
|
|
assert mock_config_entries.async_forward_entry_setup.mock_calls[1][1] == \
|
2018-09-21 19:59:20 +02:00
|
|
|
(entry, 'cover')
|
2018-04-23 18:00:16 +02:00
|
|
|
assert mock_config_entries.async_forward_entry_setup.mock_calls[2][1] == \
|
2018-09-21 19:59:20 +02:00
|
|
|
(entry, 'light')
|
2018-04-23 18:00:16 +02:00
|
|
|
assert mock_config_entries.async_forward_entry_setup.mock_calls[3][1] == \
|
2018-09-21 19:59:20 +02:00
|
|
|
(entry, 'scene')
|
2018-08-01 11:03:08 +02:00
|
|
|
assert mock_config_entries.async_forward_entry_setup.mock_calls[4][1] == \
|
2018-09-21 19:59:20 +02:00
|
|
|
(entry, 'sensor')
|
|
|
|
assert mock_config_entries.async_forward_entry_setup.mock_calls[5][1] == \
|
2018-08-01 11:03:08 +02:00
|
|
|
(entry, 'switch')
|
2018-04-29 16:16:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unload_entry(hass):
|
|
|
|
"""Test being able to unload an entry."""
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
2018-08-24 19:37:22 +02:00
|
|
|
entry.async_unload.return_value = mock_coro(True)
|
|
|
|
deconzmock = Mock()
|
|
|
|
deconzmock.async_load_parameters.return_value = mock_coro(True)
|
|
|
|
deconzmock.sensors = {}
|
|
|
|
with patch('pydeconz.DeconzSession', return_value=deconzmock):
|
2018-04-29 16:16:20 +02:00
|
|
|
assert await deconz.async_setup_entry(hass, entry) is True
|
2018-08-24 19:37:22 +02:00
|
|
|
|
2018-04-29 16:16:20 +02:00
|
|
|
assert deconz.DATA_DECONZ_EVENT in hass.data
|
2018-08-24 19:37:22 +02:00
|
|
|
|
2018-04-29 16:16:20 +02:00
|
|
|
hass.data[deconz.DATA_DECONZ_EVENT].append(Mock())
|
|
|
|
hass.data[deconz.DATA_DECONZ_ID] = {'id': 'deconzid'}
|
|
|
|
assert await deconz.async_unload_entry(hass, entry)
|
|
|
|
assert deconz.DOMAIN not in hass.data
|
2018-05-05 16:11:00 +02:00
|
|
|
assert len(hass.data[deconz.DATA_DECONZ_UNSUB]) == 0
|
2018-04-29 16:16:20 +02:00
|
|
|
assert len(hass.data[deconz.DATA_DECONZ_EVENT]) == 0
|
|
|
|
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
2018-05-05 16:11:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_add_new_device(hass):
|
|
|
|
"""Test adding a new device generates a signal for platforms."""
|
2018-08-24 19:37:22 +02:00
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'port': 80,
|
|
|
|
'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False}
|
2018-05-05 16:11:00 +02:00
|
|
|
new_event = {
|
|
|
|
"t": "event",
|
|
|
|
"e": "added",
|
|
|
|
"r": "sensors",
|
|
|
|
"id": "1",
|
|
|
|
"sensor": {
|
|
|
|
"config": {
|
|
|
|
"on": "True",
|
|
|
|
"reachable": "True"
|
|
|
|
},
|
|
|
|
"name": "event",
|
|
|
|
"state": {},
|
|
|
|
"type": "ZHASwitch"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with patch.object(deconz, 'async_dispatcher_send') as mock_dispatch_send, \
|
2018-08-24 19:37:22 +02:00
|
|
|
patch('pydeconz.DeconzSession.async_get_state',
|
|
|
|
return_value=mock_coro(CONFIG)), \
|
|
|
|
patch('pydeconz.DeconzSession.start', return_value=True):
|
2018-05-05 16:11:00 +02:00
|
|
|
assert await deconz.async_setup_entry(hass, entry) is True
|
|
|
|
hass.data[deconz.DOMAIN].async_event_handler(new_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_add_new_remote(hass):
|
|
|
|
"""Test new added device creates a new remote."""
|
|
|
|
entry = Mock()
|
2018-08-24 19:37:22 +02:00
|
|
|
entry.data = {'host': '1.2.3.4', 'port': 80,
|
|
|
|
'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False}
|
2018-05-05 16:11:00 +02:00
|
|
|
remote = Mock()
|
|
|
|
remote.name = 'name'
|
|
|
|
remote.type = 'ZHASwitch'
|
|
|
|
remote.register_async_callback = Mock()
|
2018-08-24 19:37:22 +02:00
|
|
|
with patch('pydeconz.DeconzSession.async_get_state',
|
|
|
|
return_value=mock_coro(CONFIG)), \
|
|
|
|
patch('pydeconz.DeconzSession.start', return_value=True):
|
2018-05-05 16:11:00 +02:00
|
|
|
assert await deconz.async_setup_entry(hass, entry) is True
|
|
|
|
async_dispatcher_send(hass, 'deconz_new_sensor', [remote])
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.data[deconz.DATA_DECONZ_EVENT]) == 1
|
2018-05-29 16:09:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_do_not_allow_clip_sensor(hass):
|
|
|
|
"""Test that clip sensors can be ignored."""
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'port': 80,
|
|
|
|
'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False}
|
|
|
|
remote = Mock()
|
|
|
|
remote.name = 'name'
|
|
|
|
remote.type = 'CLIPSwitch'
|
|
|
|
remote.register_async_callback = Mock()
|
2018-08-24 19:37:22 +02:00
|
|
|
with patch('pydeconz.DeconzSession.async_get_state',
|
|
|
|
return_value=mock_coro(CONFIG)), \
|
|
|
|
patch('pydeconz.DeconzSession.start', return_value=True):
|
2018-05-29 16:09:53 +02:00
|
|
|
assert await deconz.async_setup_entry(hass, entry) is True
|
|
|
|
|
|
|
|
async_dispatcher_send(hass, 'deconz_new_sensor', [remote])
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.data[deconz.DATA_DECONZ_EVENT]) == 0
|
2018-10-26 09:15:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_service_configure(hass):
|
|
|
|
"""Test that service invokes pydeconz with the correct path and data."""
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
|
|
|
with patch('pydeconz.DeconzSession.async_get_state',
|
|
|
|
return_value=mock_coro(CONFIG)), \
|
|
|
|
patch('pydeconz.DeconzSession.start', return_value=True), \
|
|
|
|
patch('homeassistant.helpers.device_registry.async_get_registry',
|
|
|
|
return_value=mock_coro(Mock())):
|
|
|
|
assert await deconz.async_setup_entry(hass, entry) is True
|
|
|
|
|
|
|
|
hass.data[DATA_DECONZ_ID] = {
|
|
|
|
'light.test': '/light/1'
|
|
|
|
}
|
|
|
|
data = {'on': True, 'attr1': 10, 'attr2': 20}
|
|
|
|
|
|
|
|
# only field
|
|
|
|
with patch('pydeconz.DeconzSession.async_put_state') as async_put_state:
|
|
|
|
await hass.services.async_call('deconz', 'configure', service_data={
|
|
|
|
'field': '/light/42', 'data': data
|
|
|
|
})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_put_state.assert_called_with('/light/42', data)
|
|
|
|
# only entity
|
|
|
|
with patch('pydeconz.DeconzSession.async_put_state') as async_put_state:
|
|
|
|
await hass.services.async_call('deconz', 'configure', service_data={
|
|
|
|
'entity': 'light.test', 'data': data
|
|
|
|
})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_put_state.assert_called_with('/light/1', data)
|
|
|
|
# entity + field
|
|
|
|
with patch('pydeconz.DeconzSession.async_put_state') as async_put_state:
|
|
|
|
await hass.services.async_call('deconz', 'configure', service_data={
|
|
|
|
'entity': 'light.test', 'field': '/state', 'data': data})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_put_state.assert_called_with('/light/1/state', data)
|
|
|
|
|
|
|
|
# non-existing entity (or not from deCONZ)
|
|
|
|
with patch('pydeconz.DeconzSession.async_put_state') as async_put_state:
|
|
|
|
await hass.services.async_call('deconz', 'configure', service_data={
|
|
|
|
'entity': 'light.nonexisting', 'field': '/state', 'data': data})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_put_state.assert_not_called()
|
|
|
|
# field does not start with /
|
|
|
|
with patch('pydeconz.DeconzSession.async_put_state') as async_put_state:
|
|
|
|
await hass.services.async_call('deconz', 'configure', service_data={
|
|
|
|
'entity': 'light.test', 'field': 'state', 'data': data})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
async_put_state.assert_not_called()
|