deCONZ - retry if setup fails (#17772)
* Make component retry if setup fails * Improve overall test coverage
This commit is contained in:
1 parent
145677ed75
commit
a9140dc8f5
19 files changed
+823
-475
No files matched your search
@@ -4,6 +4,9 @@ from unittest.mock import Mock, patch
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.binary_sensor as binary_sensor
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -14,7 +17,8 @@ SENSOR = {
|
||||
"name": "Sensor 1 name",
|
||||
"type": "ZHAPresence",
|
||||
"state": {"presence": False},
|
||||
"config": {}
|
||||
"config": {},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Sensor 2 id",
|
||||
@@ -26,70 +30,105 @@ SENSOR = {
|
||||
}
|
||||
|
||||
|
||||
async def setup_bridge(hass, data, allow_clip_sensor=True):
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data, allow_clip_sensor=True):
|
||||
"""Load the deCONZ binary sensor platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
bridge = DeconzSession(loop, session, **entry.data)
|
||||
bridge.config = Mock()
|
||||
|
||||
ENTRY_CONFIG[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await bridge.async_load_parameters()
|
||||
hass.data[deconz.DOMAIN] = bridge
|
||||
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title',
|
||||
{'host': 'mock-host', 'allow_clip_sensor': allow_clip_sensor}, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
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()
|
||||
|
||||
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that we do not discover anything or try to set up a gateway."""
|
||||
assert await async_setup_component(hass, binary_sensor.DOMAIN, {
|
||||
'binary_sensor': {
|
||||
'platform': deconz.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_binary_sensors(hass):
|
||||
"""Test that no sensors in deconz results in no sensor entities."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
await setup_gateway(hass, data)
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_binary_sensors(hass):
|
||||
"""Test successful creation of binary sensor entities."""
|
||||
data = {"sensors": SENSOR}
|
||||
await setup_bridge(hass, data)
|
||||
assert "binary_sensor.sensor_1_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
await setup_gateway(hass, data)
|
||||
assert "binary_sensor.sensor_1_name" in \
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "binary_sensor.sensor_2_name" not in \
|
||||
hass.data[deconz.DATA_DECONZ_ID]
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
hass.data[deconz.DOMAIN].api.sensors['1'].async_update(
|
||||
{'state': {'on': False}})
|
||||
|
||||
|
||||
async def test_add_new_sensor(hass):
|
||||
"""Test successful creation of sensor entities."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
await setup_gateway(hass, data)
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'ZHAPresence'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert "binary_sensor.name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "binary_sensor.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_do_not_allow_clip_sensor(hass):
|
||||
"""Test that clip sensors can be ignored."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data, allow_clip_sensor=False)
|
||||
await setup_gateway(hass, data, allow_clip_sensor=False)
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'CLIPPresence'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
|
||||
|
||||
async def test_unload_switch(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
data = {"sensors": SENSOR}
|
||||
await setup_gateway(hass, data)
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
@@ -5,6 +5,9 @@ from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.components.deconz.const import COVER_TYPES
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.cover as cover
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -13,14 +16,15 @@ SUPPORTED_COVERS = {
|
||||
"id": "Cover 1 id",
|
||||
"name": "Cover 1 name",
|
||||
"type": "Level controllable output",
|
||||
"state": {},
|
||||
"modelid": "Not zigbee spec"
|
||||
"state": {"bri": 255, "reachable": True},
|
||||
"modelid": "Not zigbee spec",
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Cover 2 id",
|
||||
"name": "Cover 2 name",
|
||||
"type": "Window covering device",
|
||||
"state": {},
|
||||
"state": {"bri": 255, "reachable": True},
|
||||
"modelid": "lumi.curtain"
|
||||
}
|
||||
}
|
||||
@@ -35,58 +39,109 @@ UNSUPPORTED_COVER = {
|
||||
}
|
||||
|
||||
|
||||
async def setup_bridge(hass, data):
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data):
|
||||
"""Load the deCONZ cover platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
bridge = DeconzSession(loop, session, **entry.data)
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await bridge.async_load_parameters()
|
||||
hass.data[deconz.DOMAIN] = bridge
|
||||
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', {'host': 'mock-host'}, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'cover')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_no_switches(hass):
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that we do not discover anything or try to set up a gateway."""
|
||||
assert await async_setup_component(hass, cover.DOMAIN, {
|
||||
'cover': {
|
||||
'platform': deconz.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_covers(hass):
|
||||
"""Test that no cover entities are created."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_cover(hass):
|
||||
"""Test that all supported cover entities are created."""
|
||||
await setup_bridge(hass, {"lights": SUPPORTED_COVERS})
|
||||
assert "cover.cover_1_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_COVERS})
|
||||
assert "cover.cover_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(SUPPORTED_COVERS) == len(COVER_TYPES)
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
cover_1 = hass.states.get('cover.cover_1_name')
|
||||
assert cover_1 is not None
|
||||
assert cover_1.state == 'closed'
|
||||
|
||||
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
|
||||
|
||||
await hass.services.async_call('cover', 'open_cover', {
|
||||
'entity_id': 'cover.cover_1_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('cover', 'close_cover', {
|
||||
'entity_id': 'cover.cover_1_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('cover', 'stop_cover', {
|
||||
'entity_id': 'cover.cover_1_name'
|
||||
}, blocking=True)
|
||||
|
||||
await hass.services.async_call('cover', 'close_cover', {
|
||||
'entity_id': 'cover.cover_2_name'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_add_new_cover(hass):
|
||||
"""Test successful creation of cover entity."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
await setup_gateway(hass, data)
|
||||
cover = Mock()
|
||||
cover.name = 'name'
|
||||
cover.type = "Level controllable output"
|
||||
cover.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_light', [cover])
|
||||
await hass.async_block_till_done()
|
||||
assert "cover.name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "cover.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_unsupported_cover(hass):
|
||||
"""Test that unsupported covers are not created."""
|
||||
await setup_bridge(hass, {"lights": UNSUPPORTED_COVER})
|
||||
await setup_gateway(hass, {"lights": UNSUPPORTED_COVER})
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_unload_cover(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_COVERS})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
@@ -20,7 +20,7 @@ async def test_flow_works(hass, aioclient_mock):
|
||||
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
await flow.async_step_init()
|
||||
await flow.async_step_user()
|
||||
await flow.async_step_link(user_input={})
|
||||
result = await flow.async_step_options(
|
||||
user_input={'allow_clip_sensor': True, 'allow_deconz_groups': True})
|
||||
@@ -45,7 +45,7 @@ async def test_flow_already_registered_bridge(hass):
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_init()
|
||||
result = await flow.async_step_user()
|
||||
assert result['type'] == 'abort'
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ async def test_flow_no_discovered_bridges(hass, aioclient_mock):
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_init()
|
||||
result = await flow.async_step_user()
|
||||
assert result['type'] == 'abort'
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ async def test_flow_one_bridge_discovered(hass, aioclient_mock):
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_init()
|
||||
result = await flow.async_step_user()
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'link'
|
||||
|
||||
@@ -81,9 +81,9 @@ async def test_flow_two_bridges_discovered(hass, aioclient_mock):
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_init()
|
||||
result = await flow.async_step_user()
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'init'
|
||||
assert result['step_id'] == 'user'
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
assert result['data_schema']({'host': '0.0.0.0'})
|
||||
@@ -92,6 +92,21 @@ async def test_flow_two_bridges_discovered(hass, aioclient_mock):
|
||||
result['data_schema']({'host': '5.6.7.8'})
|
||||
|
||||
|
||||
async def test_flow_two_bridges_selection(hass, aioclient_mock):
|
||||
"""Test config flow selection of one of two bridges."""
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.bridges = [
|
||||
{'bridgeid': 'id1', 'host': '1.2.3.4', 'port': 80},
|
||||
{'bridgeid': 'id2', 'host': '5.6.7.8', 'port': 80}
|
||||
]
|
||||
|
||||
result = await flow.async_step_user(user_input={'host': '1.2.3.4'})
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'link'
|
||||
assert flow.deconz_config['host'] == '1.2.3.4'
|
||||
|
||||
|
||||
async def test_link_no_api_key(hass, aioclient_mock):
|
||||
"""Test config flow should abort if no API key was possible to retrieve."""
|
||||
aioclient_mock.post('http://1.2.3.4:80/api', json=[])
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
"""Test deCONZ component setup process."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.components.deconz import DATA_DECONZ_ID
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
from tests.common import mock_coro
|
||||
from homeassistant.components import deconz
|
||||
|
||||
from tests.common import mock_coro, MockConfigEntry
|
||||
|
||||
|
||||
CONFIG = {
|
||||
"config": {
|
||||
@@ -99,173 +99,113 @@ async def test_setup_entry_no_available_bridge(hass):
|
||||
|
||||
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'}
|
||||
with patch.object(hass, 'async_create_task') as mock_add_job, \
|
||||
patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True), \
|
||||
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
|
||||
'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
|
||||
})
|
||||
entry.add_to_hass(hass)
|
||||
mock_registry = Mock()
|
||||
with patch.object(deconz, 'DeconzGateway') as mock_gateway, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(Mock())):
|
||||
return_value=mock_coro(mock_registry)):
|
||||
mock_gateway.return_value.async_setup.return_value = mock_coro(True)
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
assert hass.data[deconz.DOMAIN]
|
||||
assert hass.data[deconz.DATA_DECONZ_ID] == {}
|
||||
assert len(hass.data[deconz.DATA_DECONZ_UNSUB]) == 1
|
||||
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)
|
||||
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] == \
|
||||
(entry, 'cover')
|
||||
assert mock_config_entries.async_forward_entry_setup.mock_calls[2][1] == \
|
||||
(entry, 'light')
|
||||
assert mock_config_entries.async_forward_entry_setup.mock_calls[3][1] == \
|
||||
(entry, 'scene')
|
||||
assert mock_config_entries.async_forward_entry_setup.mock_calls[4][1] == \
|
||||
(entry, 'sensor')
|
||||
assert mock_config_entries.async_forward_entry_setup.mock_calls[5][1] == \
|
||||
(entry, 'switch')
|
||||
|
||||
|
||||
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'}
|
||||
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):
|
||||
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
|
||||
'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
|
||||
})
|
||||
entry.add_to_hass(hass)
|
||||
mock_registry = Mock()
|
||||
with patch.object(deconz, 'DeconzGateway') as mock_gateway, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(mock_registry)):
|
||||
mock_gateway.return_value.async_setup.return_value = mock_coro(True)
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
|
||||
assert deconz.DATA_DECONZ_EVENT in hass.data
|
||||
|
||||
hass.data[deconz.DATA_DECONZ_EVENT].append(Mock())
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {'id': 'deconzid'}
|
||||
mock_gateway.return_value.async_reset.return_value = mock_coro(True)
|
||||
assert await deconz.async_unload_entry(hass, entry)
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
assert len(hass.data[deconz.DATA_DECONZ_UNSUB]) == 0
|
||||
assert len(hass.data[deconz.DATA_DECONZ_EVENT]) == 0
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
|
||||
|
||||
async def test_add_new_device(hass):
|
||||
"""Test adding a new device generates a signal for platforms."""
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80,
|
||||
'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False}
|
||||
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, \
|
||||
patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True):
|
||||
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()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80,
|
||||
'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False}
|
||||
remote = Mock()
|
||||
remote.name = 'name'
|
||||
remote.type = 'ZHASwitch'
|
||||
remote.register_async_callback = Mock()
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True):
|
||||
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
|
||||
|
||||
|
||||
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()
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(CONFIG)), \
|
||||
patch('pydeconz.DeconzSession.start', return_value=True):
|
||||
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
|
||||
|
||||
|
||||
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), \
|
||||
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
|
||||
'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
|
||||
})
|
||||
entry.add_to_hass(hass)
|
||||
mock_registry = Mock()
|
||||
with patch.object(deconz, 'DeconzGateway') as mock_gateway, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(Mock())):
|
||||
return_value=mock_coro(mock_registry)):
|
||||
mock_gateway.return_value.async_setup.return_value = mock_coro(True)
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
|
||||
hass.data[DATA_DECONZ_ID] = {
|
||||
hass.data[deconz.DOMAIN].deconz_ids = {
|
||||
'light.test': '/light/1'
|
||||
}
|
||||
data = {'on': True, 'attr1': 10, 'attr2': 20}
|
||||
|
||||
# only field
|
||||
with patch('pydeconz.DeconzSession.async_put_state') as async_put_state:
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
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:
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
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:
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
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:
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
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:
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
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()
|
||||
|
||||
|
||||
async def test_service_refresh_devices(hass):
|
||||
"""Test that service can refresh devices."""
|
||||
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
|
||||
'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
|
||||
})
|
||||
entry.add_to_hass(hass)
|
||||
mock_registry = Mock()
|
||||
with patch.object(deconz, 'DeconzGateway') as mock_gateway, \
|
||||
patch('homeassistant.helpers.device_registry.async_get_registry',
|
||||
return_value=mock_coro(mock_registry)):
|
||||
mock_gateway.return_value.async_setup.return_value = mock_coro(True)
|
||||
assert await deconz.async_setup_entry(hass, entry) is True
|
||||
|
||||
with patch.object(hass.data[deconz.DOMAIN].api, 'async_load_parameters',
|
||||
return_value=mock_coro(True)):
|
||||
await hass.services.async_call(
|
||||
'deconz', 'device_refresh', service_data={})
|
||||
await hass.async_block_till_done()
|
||||
with patch.object(hass.data[deconz.DOMAIN].api, 'async_load_parameters',
|
||||
return_value=mock_coro(False)):
|
||||
await hass.services.async_call(
|
||||
'deconz', 'device_refresh', service_data={})
|
||||
await hass.async_block_till_done()
|
||||
@@ -4,6 +4,9 @@ from unittest.mock import Mock, patch
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.light as light
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -12,7 +15,18 @@ LIGHT = {
|
||||
"1": {
|
||||
"id": "Light 1 id",
|
||||
"name": "Light 1 name",
|
||||
"state": {}
|
||||
"state": {
|
||||
"on": True, "bri": 255, "colormode": "xy", "xy": (500, 500),
|
||||
"reachable": True
|
||||
},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Light 2 id",
|
||||
"name": "Light 2 name",
|
||||
"state": {
|
||||
"on": True, "colormode": "ct", "ct": 2500, "reachable": True
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +34,7 @@ GROUP = {
|
||||
"1": {
|
||||
"id": "Group 1 id",
|
||||
"name": "Group 1 name",
|
||||
"type": "LightGroup",
|
||||
"state": {},
|
||||
"action": {},
|
||||
"scenes": [],
|
||||
@@ -47,85 +62,152 @@ SWITCH = {
|
||||
}
|
||||
|
||||
|
||||
async def setup_bridge(hass, data, allow_deconz_groups=True):
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data, allow_deconz_groups=True):
|
||||
"""Load the deCONZ light platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
bridge = DeconzSession(loop, session, **entry.data)
|
||||
bridge.config = Mock()
|
||||
|
||||
ENTRY_CONFIG[deconz.const.CONF_ALLOW_DECONZ_GROUPS] = allow_deconz_groups
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await bridge.async_load_parameters()
|
||||
hass.data[deconz.DOMAIN] = bridge
|
||||
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title',
|
||||
{'host': 'mock-host', 'allow_deconz_groups': allow_deconz_groups},
|
||||
'test', config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'light')
|
||||
# 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 gateway."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
'light': {
|
||||
'platform': deconz.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_lights_or_groups(hass):
|
||||
"""Test that no lights or groups entities are created."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_lights_and_groups(hass):
|
||||
"""Test that lights or groups entities are created."""
|
||||
await setup_bridge(hass, {"lights": LIGHT, "groups": GROUP})
|
||||
assert "light.light_1_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "light.group_1_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "light.group_2_name" not in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert len(hass.states.async_all()) == 3
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"lights": LIGHT, "groups": GROUP})
|
||||
assert "light.light_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "light.light_2_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "light.group_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "light.group_2_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 4
|
||||
|
||||
lamp_1 = hass.states.get('light.light_1_name')
|
||||
assert lamp_1 is not None
|
||||
assert lamp_1.state == 'on'
|
||||
assert lamp_1.attributes['brightness'] == 255
|
||||
assert lamp_1.attributes['hs_color'] == (224.235, 100.0)
|
||||
|
||||
light_2 = hass.states.get('light.light_2_name')
|
||||
assert light_2 is not None
|
||||
assert light_2.state == 'on'
|
||||
assert light_2.attributes['color_temp'] == 2500
|
||||
|
||||
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
|
||||
|
||||
await hass.services.async_call('light', 'turn_on', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'color_temp': 2500,
|
||||
'brightness': 200,
|
||||
'transition': 5,
|
||||
'flash': 'short',
|
||||
'effect': 'colorloop'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('light', 'turn_on', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'hs_color': (20, 30),
|
||||
'flash': 'long',
|
||||
'effect': 'None'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('light', 'turn_off', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'transition': 5,
|
||||
'flash': 'short'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('light', 'turn_off', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'flash': 'long'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_add_new_light(hass):
|
||||
"""Test successful creation of light entities."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
await setup_gateway(hass, {})
|
||||
light = Mock()
|
||||
light.name = 'name'
|
||||
light.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_light', [light])
|
||||
await hass.async_block_till_done()
|
||||
assert "light.name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "light.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_add_new_group(hass):
|
||||
"""Test successful creation of group entities."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
await setup_gateway(hass, {})
|
||||
group = Mock()
|
||||
group.name = 'name'
|
||||
group.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_group', [group])
|
||||
await hass.async_block_till_done()
|
||||
assert "light.name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "light.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_do_not_add_deconz_groups(hass):
|
||||
"""Test that clip sensors can be ignored."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data, allow_deconz_groups=False)
|
||||
await setup_gateway(hass, {}, allow_deconz_groups=False)
|
||||
group = Mock()
|
||||
group.name = 'name'
|
||||
group.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_group', [group])
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
|
||||
|
||||
async def test_no_switch(hass):
|
||||
"""Test that a switch doesn't get created as a light entity."""
|
||||
await setup_bridge(hass, {"lights": SWITCH})
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
await setup_gateway(hass, {"lights": SWITCH})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_unload_light(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
await setup_gateway(hass, {"lights": LIGHT, "groups": GROUP})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
# Group.all_lights will not be removed
|
||||
assert len(hass.states.async_all()) == 1
|
||||
@@ -1,8 +1,11 @@
|
||||
"""deCONZ scenes platform tests."""
|
||||
"""deCONZ scene platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.scene as scene
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -21,39 +24,73 @@ GROUP = {
|
||||
}
|
||||
|
||||
|
||||
async def setup_bridge(hass, data):
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data):
|
||||
"""Load the deCONZ scene platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
bridge = DeconzSession(loop, session, **entry.data)
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await bridge.async_load_parameters()
|
||||
hass.data[deconz.DOMAIN] = bridge
|
||||
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', {'host': 'mock-host'}, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'scene')
|
||||
# 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 gateway."""
|
||||
assert await async_setup_component(hass, scene.DOMAIN, {
|
||||
'scene': {
|
||||
'platform': deconz.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_scenes(hass):
|
||||
"""Test the update_lights function with some lights."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
"""Test that scenes can be loaded without scenes being available."""
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_scenes(hass):
|
||||
"""Test the update_lights function with some lights."""
|
||||
data = {"groups": GROUP}
|
||||
await setup_bridge(hass, data)
|
||||
assert "scene.group_1_name_scene_1" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
"""Test that scenes works."""
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"groups": GROUP})
|
||||
assert "scene.group_1_name_scene_1" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
await hass.services.async_call('scene', 'turn_on', {
|
||||
'entity_id': 'scene.group_1_name_scene_1'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_unload_scene(hass):
|
||||
"""Test that it works to unload scene entities."""
|
||||
await setup_gateway(hass, {"groups": GROUP})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
@@ -1,10 +1,12 @@
|
||||
"""deCONZ sensor platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.sensor as sensor
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -13,9 +15,10 @@ SENSOR = {
|
||||
"1": {
|
||||
"id": "Sensor 1 id",
|
||||
"name": "Sensor 1 name",
|
||||
"type": "ZHATemperature",
|
||||
"state": {"temperature": False},
|
||||
"config": {}
|
||||
"type": "ZHALightLevel",
|
||||
"state": {"lightlevel": 30000, "dark": False},
|
||||
"config": {"reachable": True},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Sensor 2 id",
|
||||
@@ -36,80 +39,134 @@ SENSOR = {
|
||||
"name": "Sensor 4 name",
|
||||
"type": "ZHASwitch",
|
||||
"state": {"buttonevent": 1000},
|
||||
"config": {"battery": 100}
|
||||
"config": {"battery": 100},
|
||||
"uniqueid": "00:00:00:00:00:00:00:01-00"
|
||||
},
|
||||
"5": {
|
||||
"id": "Sensor 5 id",
|
||||
"name": "Sensor 5 name",
|
||||
"type": "ZHASwitch",
|
||||
"state": {"buttonevent": 1000},
|
||||
"config": {"battery": 100},
|
||||
"uniqueid": "00:00:00:00:00:00:00:02:00-00"
|
||||
},
|
||||
"6": {
|
||||
"id": "Sensor 6 id",
|
||||
"name": "Sensor 6 name",
|
||||
"type": "Daylight",
|
||||
"state": {"daylight": True},
|
||||
"config": {}
|
||||
},
|
||||
"7": {
|
||||
"id": "Sensor 7 id",
|
||||
"name": "Sensor 7 name",
|
||||
"type": "ZHAPower",
|
||||
"state": {"current": 2, "power": 6, "voltage": 3},
|
||||
"config": {"reachable": True}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async def setup_bridge(hass, data, allow_clip_sensor=True):
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data, allow_clip_sensor=True):
|
||||
"""Load the deCONZ sensor platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
bridge = DeconzSession(loop, session, **entry.data)
|
||||
bridge.config = Mock()
|
||||
|
||||
ENTRY_CONFIG[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await bridge.async_load_parameters()
|
||||
hass.data[deconz.DOMAIN] = bridge
|
||||
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
||||
hass.data[deconz.DATA_DECONZ_EVENT] = []
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title',
|
||||
{'host': 'mock-host', 'allow_clip_sensor': allow_clip_sensor}, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'sensor')
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, 'sensor')
|
||||
# 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 gateway."""
|
||||
assert await async_setup_component(hass, sensor.DOMAIN, {
|
||||
'sensor': {
|
||||
'platform': deconz.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_sensors(hass):
|
||||
"""Test that no sensors in deconz results in no sensor entities."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_sensors(hass):
|
||||
"""Test successful creation of sensor entities."""
|
||||
data = {"sensors": SENSOR}
|
||||
await setup_bridge(hass, data)
|
||||
assert "sensor.sensor_1_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "sensor.sensor_2_name" not in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "sensor.sensor_3_name" not in hass.data[deconz.DATA_DECONZ_ID]
|
||||
await setup_gateway(hass, {"sensors": SENSOR})
|
||||
assert "sensor.sensor_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_2_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_3_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_3_name_battery_level" not in \
|
||||
hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "sensor.sensor_4_name" not in hass.data[deconz.DATA_DECONZ_ID]
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_4_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_4_name_battery_level" in \
|
||||
hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert len(hass.states.async_all()) == 2
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 5
|
||||
|
||||
hass.data[deconz.DOMAIN].api.sensors['1'].async_update(
|
||||
{'state': {'on': False}})
|
||||
hass.data[deconz.DOMAIN].api.sensors['4'].async_update(
|
||||
{'config': {'battery': 75}})
|
||||
|
||||
|
||||
async def test_add_new_sensor(hass):
|
||||
"""Test successful creation of sensor entities."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
await setup_gateway(hass, {})
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'ZHATemperature'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert "sensor.name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "sensor.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_do_not_allow_clipsensor(hass):
|
||||
"""Test that clip sensors can be ignored."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data, allow_clip_sensor=False)
|
||||
await setup_gateway(hass, {}, allow_clip_sensor=False)
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'CLIPTemperature'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
|
||||
|
||||
async def test_unload_sensor(hass):
|
||||
"""Test that it works to unload sensor entities."""
|
||||
await setup_gateway(hass, {"sensors": SENSOR})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
@@ -5,6 +5,9 @@ from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.components.deconz.const import SWITCH_TYPES
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.switch as switch
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -13,19 +16,20 @@ SUPPORTED_SWITCHES = {
|
||||
"id": "Switch 1 id",
|
||||
"name": "Switch 1 name",
|
||||
"type": "On/Off plug-in unit",
|
||||
"state": {}
|
||||
"state": {"on": True, "reachable": True},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Switch 2 id",
|
||||
"name": "Switch 2 name",
|
||||
"type": "Smart plug",
|
||||
"state": {}
|
||||
"state": {"on": True, "reachable": True}
|
||||
},
|
||||
"3": {
|
||||
"id": "Switch 3 id",
|
||||
"name": "Switch 3 name",
|
||||
"type": "Warning device",
|
||||
"state": {}
|
||||
"state": {"alert": "lselect", "reachable": True}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,61 +43,113 @@ UNSUPPORTED_SWITCH = {
|
||||
}
|
||||
|
||||
|
||||
async def setup_bridge(hass, data):
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data):
|
||||
"""Load the deCONZ switch platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
entry = Mock()
|
||||
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
|
||||
bridge = DeconzSession(loop, session, **entry.data)
|
||||
bridge.config = Mock()
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await bridge.async_load_parameters()
|
||||
hass.data[deconz.DOMAIN] = bridge
|
||||
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
||||
hass.data[deconz.DATA_DECONZ_ID] = {}
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', {'host': 'mock-host'}, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
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 gateway."""
|
||||
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': deconz.DOMAIN
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_switches(hass):
|
||||
"""Test that no switch entities are created."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_switch(hass):
|
||||
async def test_switches(hass):
|
||||
"""Test that all supported switch entities are created."""
|
||||
await setup_bridge(hass, {"lights": SUPPORTED_SWITCHES})
|
||||
assert "switch.switch_1_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "switch.switch_2_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "switch.switch_3_name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
|
||||
assert "switch.switch_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "switch.switch_2_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "switch.switch_3_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(SUPPORTED_SWITCHES) == len(SWITCH_TYPES)
|
||||
assert len(hass.states.async_all()) == 4
|
||||
|
||||
switch_1 = hass.states.get('switch.switch_1_name')
|
||||
assert switch_1 is not None
|
||||
assert switch_1.state == 'on'
|
||||
switch_3 = hass.states.get('switch.switch_3_name')
|
||||
assert switch_3 is not None
|
||||
assert switch_3.state == 'on'
|
||||
|
||||
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
|
||||
|
||||
await hass.services.async_call('switch', 'turn_on', {
|
||||
'entity_id': 'switch.switch_1_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('switch', 'turn_off', {
|
||||
'entity_id': 'switch.switch_1_name'
|
||||
}, blocking=True)
|
||||
|
||||
await hass.services.async_call('switch', 'turn_on', {
|
||||
'entity_id': 'switch.switch_3_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('switch', 'turn_off', {
|
||||
'entity_id': 'switch.switch_3_name'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_add_new_switch(hass):
|
||||
"""Test successful creation of switch entity."""
|
||||
data = {}
|
||||
await setup_bridge(hass, data)
|
||||
await setup_gateway(hass, {})
|
||||
switch = Mock()
|
||||
switch.name = 'name'
|
||||
switch.type = "Smart plug"
|
||||
switch.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_light', [switch])
|
||||
await hass.async_block_till_done()
|
||||
assert "switch.name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||
assert "switch.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_unsupported_switch(hass):
|
||||
"""Test that unsupported switches are not created."""
|
||||
await setup_bridge(hass, {"lights": UNSUPPORTED_SWITCH})
|
||||
await setup_gateway(hass, {"lights": UNSUPPORTED_SWITCH})
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_unload_switch(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
Reference in new issue
Block a user