Support multiple deCONZ gateways (#22449)

* Store gateways inside a dict in deconz domain

* Make reachable events gateway specific

* Gateway shall always exist

* Adapt new device signalling to support multiple gateways

* Services follow gateway master

* Working on unload entry

* Make unload and master handover work
Improve tests for init

* Fix config flow

* Fix linting

* Clean up init tests

* Clean up hassio discovery to fit with the rest

* Store gateways inside a dict in deconz domain

* Make reachable events gateway specific

* Gateway shall always exist

* Adapt new device signalling to support multiple gateways

* Services follow gateway master

* Working on unload entry

* Make unload and master handover work
Improve tests for init

* Fix config flow

* Fix linting

* Clean up init tests

* Clean up hassio discovery to fit with the rest

* Add support for services to specify bridgeid
This commit is contained in:
Robert Svensson authored and Jason Hu committed 2019-04-04 17:48:24 -07:00
1 parent b9ec623ad9
commit b50afec5f1
22 files changed
+535 -426

No files matched your search

+18 -17
View File
@@ -54,7 +54,7 @@ async def setup_gateway(hass, data, allow_clip_sensor=True):
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = gateway
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch('pydeconz.DeconzSession.async_get_state',
return_value=mock_coro(data)):
@@ -64,6 +64,7 @@ async def setup_gateway(hass, data, allow_clip_sensor=True):
config_entry, 'binary_sensor')
# To flush out the service call to update the group
await hass.async_block_till_done()
return gateway
async def test_platform_manually_configured(hass):
@@ -79,56 +80,56 @@ async def test_platform_manually_configured(hass):
async def test_no_binary_sensors(hass):
"""Test that no sensors in deconz results in no sensor entities."""
data = {}
await setup_gateway(hass, data)
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
gateway = await setup_gateway(hass, data)
assert len(hass.data[deconz.DOMAIN][gateway.bridgeid].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_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.DOMAIN].deconz_ids
gateway = await setup_gateway(hass, data)
assert "binary_sensor.sensor_1_name" in gateway.deconz_ids
assert "binary_sensor.sensor_2_name" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 1
hass.data[deconz.DOMAIN].api.sensors['1'].async_update(
hass.data[deconz.DOMAIN][gateway.bridgeid].api.sensors['1'].async_update(
{'state': {'on': False}})
async def test_add_new_sensor(hass):
"""Test successful creation of sensor entities."""
data = {}
await setup_gateway(hass, data)
gateway = 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])
async_dispatcher_send(
hass, gateway.async_event_new_device('sensor'), [sensor])
await hass.async_block_till_done()
assert "binary_sensor.name" in hass.data[deconz.DOMAIN].deconz_ids
assert "binary_sensor.name" in gateway.deconz_ids
async def test_do_not_allow_clip_sensor(hass):
"""Test that clip sensors can be ignored."""
data = {}
await setup_gateway(hass, data, allow_clip_sensor=False)
gateway = 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])
async_dispatcher_send(
hass, gateway.async_event_new_device('sensor'), [sensor])
await hass.async_block_till_done()
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
assert len(gateway.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)
gateway = await setup_gateway(hass, data)
await hass.data[deconz.DOMAIN].async_reset()
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
+25 -22
View File
@@ -65,7 +65,7 @@ async def setup_gateway(hass, data, allow_clip_sensor=True):
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(hass.loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = gateway
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch('pydeconz.DeconzSession.async_get_state',
return_value=mock_coro(data)):
@@ -75,6 +75,7 @@ async def setup_gateway(hass, data, allow_clip_sensor=True):
config_entry, 'climate')
# To flush out the service call to update the group
await hass.async_block_till_done()
return gateway
async def test_platform_manually_configured(hass):
@@ -89,26 +90,26 @@ async def test_platform_manually_configured(hass):
async def test_no_sensors(hass):
"""Test that no sensors in deconz results in no climate entities."""
await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN].deconz_ids
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert not hass.states.async_all()
async def test_climate_devices(hass):
"""Test successful creation of sensor entities."""
await setup_gateway(hass, {"sensors": SENSOR})
assert "climate.climate_1_name" in hass.data[deconz.DOMAIN].deconz_ids
assert "sensor.sensor_2_name" not in hass.data[deconz.DOMAIN].deconz_ids
gateway = await setup_gateway(hass, {"sensors": SENSOR})
assert "climate.climate_1_name" in gateway.deconz_ids
assert "sensor.sensor_2_name" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 1
hass.data[deconz.DOMAIN].api.sensors['1'].async_update(
gateway.api.sensors['1'].async_update(
{'state': {'on': False}})
await hass.services.async_call(
'climate', 'turn_on', {'entity_id': 'climate.climate_1_name'},
blocking=True
)
hass.data[deconz.DOMAIN].api.session.put.assert_called_with(
gateway.api.session.put.assert_called_with(
'http://1.2.3.4:80/api/ABCDEF/sensors/1/config',
data='{"mode": "auto"}'
)
@@ -117,7 +118,7 @@ async def test_climate_devices(hass):
'climate', 'turn_off', {'entity_id': 'climate.climate_1_name'},
blocking=True
)
hass.data[deconz.DOMAIN].api.session.put.assert_called_with(
gateway.api.session.put.assert_called_with(
'http://1.2.3.4:80/api/ABCDEF/sensors/1/config',
data='{"mode": "off"}'
)
@@ -127,18 +128,18 @@ async def test_climate_devices(hass):
{'entity_id': 'climate.climate_1_name', 'temperature': 20},
blocking=True
)
hass.data[deconz.DOMAIN].api.session.put.assert_called_with(
gateway.api.session.put.assert_called_with(
'http://1.2.3.4:80/api/ABCDEF/sensors/1/config',
data='{"heatsetpoint": 2000.0}'
)
assert len(hass.data[deconz.DOMAIN].api.session.put.mock_calls) == 3
assert len(gateway.api.session.put.mock_calls) == 3
async def test_verify_state_update(hass):
"""Test that state update properly."""
await setup_gateway(hass, {"sensors": SENSOR})
assert "climate.climate_1_name" in hass.data[deconz.DOMAIN].deconz_ids
gateway = await setup_gateway(hass, {"sensors": SENSOR})
assert "climate.climate_1_name" in gateway.deconz_ids
thermostat = hass.states.get('climate.climate_1_name')
assert thermostat.state == 'on'
@@ -150,7 +151,7 @@ async def test_verify_state_update(hass):
"id": "1",
"config": {"on": False}
}
hass.data[deconz.DOMAIN].api.async_event_handler(state_update)
gateway.api.async_event_handler(state_update)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1
@@ -161,32 +162,34 @@ async def test_verify_state_update(hass):
async def test_add_new_climate_device(hass):
"""Test successful creation of climate entities."""
await setup_gateway(hass, {})
gateway = await setup_gateway(hass, {})
sensor = Mock()
sensor.name = 'name'
sensor.type = 'ZHAThermostat'
sensor.register_async_callback = Mock()
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
async_dispatcher_send(
hass, gateway.async_event_new_device('sensor'), [sensor])
await hass.async_block_till_done()
assert "climate.name" in hass.data[deconz.DOMAIN].deconz_ids
assert "climate.name" in gateway.deconz_ids
async def test_do_not_allow_clipsensor(hass):
"""Test that clip sensors can be ignored."""
await setup_gateway(hass, {}, allow_clip_sensor=False)
gateway = await setup_gateway(hass, {}, allow_clip_sensor=False)
sensor = Mock()
sensor.name = 'name'
sensor.type = 'CLIPThermostat'
sensor.register_async_callback = Mock()
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
async_dispatcher_send(
hass, gateway.async_event_new_device('sensor'), [sensor])
await hass.async_block_till_done()
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
assert len(gateway.deconz_ids) == 0
async def test_unload_sensor(hass):
"""Test that it works to unload sensor entities."""
await setup_gateway(hass, {"sensors": SENSOR})
gateway = await setup_gateway(hass, {"sensors": SENSOR})
await hass.data[deconz.DOMAIN].async_reset()
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
+14 -59
View File
@@ -22,10 +22,7 @@ async def test_flow_works(hass, aioclient_mock):
flow.hass = hass
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})
result = await flow.async_step_link(user_input={})
assert result['type'] == 'create_entry'
assert result['title'] == 'deCONZ-id'
@@ -33,25 +30,10 @@ async def test_flow_works(hass, aioclient_mock):
'bridgeid': 'id',
'host': '1.2.3.4',
'port': 80,
'api_key': '1234567890ABCDEF',
'allow_clip_sensor': True,
'allow_deconz_groups': True
'api_key': '1234567890ABCDEF'
}
async def test_flow_already_registered_bridge(hass):
"""Test config flow don't allow more than one bridge to be registered."""
MockConfigEntry(domain='deconz', data={
'host': '1.2.3.4'
}).add_to_hass(hass)
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
result = await flow.async_step_user()
assert result['type'] == 'abort'
async def test_flow_bridge_discovery_fails(hass, aioclient_mock):
"""Test config flow works when discovery fails."""
flow = config_flow.DeconzFlowHandler()
@@ -153,24 +135,6 @@ async def test_link_no_api_key(hass):
assert result['errors'] == {'base': 'no_key'}
async def test_link_already_registered_bridge(hass):
"""Test that link verifies to only allow one config entry to complete.
This is possible with discovery which will allow the user to complete
a second config entry and then complete the discovered config entry.
"""
MockConfigEntry(domain='deconz', data={
'host': '1.2.3.4'
}).add_to_hass(hass)
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
flow.deconz_config = {'host': '1.2.3.4', 'port': 80}
result = await flow.async_step_link(user_input={})
assert result['type'] == 'abort'
async def test_bridge_discovery(hass):
"""Test a bridge being discovered."""
flow = config_flow.DeconzFlowHandler()
@@ -197,6 +161,7 @@ async def test_bridge_discovery_already_configured(hass):
result = await flow.async_step_discovery({
'host': '1.2.3.4',
'port': 80,
'serial': 'id'
})
@@ -234,14 +199,12 @@ async def test_import_with_api_key(hass):
'bridgeid': 'id',
'host': '1.2.3.4',
'port': 80,
'api_key': '1234567890ABCDEF',
'allow_clip_sensor': True,
'allow_deconz_groups': True
'api_key': '1234567890ABCDEF'
}
async def test_options(hass, aioclient_mock):
"""Test that options work and that bridgeid can be requested."""
async def test_create_entry(hass, aioclient_mock):
"""Test that _create_entry work and that bridgeid can be requested."""
aioclient_mock.get('http://1.2.3.4:80/api/1234567890ABCDEF/config',
json={"bridgeid": "id"},
headers={'content-type': 'application/json'})
@@ -252,8 +215,7 @@ async def test_options(hass, aioclient_mock):
'port': 80,
'api_key': '1234567890ABCDEF'}
result = await flow.async_step_options(
user_input={'allow_clip_sensor': False, 'allow_deconz_groups': False})
result = await flow._create_entry()
assert result['type'] == 'create_entry'
assert result['title'] == 'deCONZ-id'
@@ -261,9 +223,7 @@ async def test_options(hass, aioclient_mock):
'bridgeid': 'id',
'host': '1.2.3.4',
'port': 80,
'api_key': '1234567890ABCDEF',
'allow_clip_sensor': False,
'allow_deconz_groups': False
'api_key': '1234567890ABCDEF'
}
@@ -286,8 +246,8 @@ async def test_hassio_confirm(hass):
data={
'addon': 'Mock Addon',
'host': 'mock-deconz',
'port': 8080,
'serial': 'aa:bb',
'port': 80,
'serial': 'id',
'api_key': '1234567890ABCDEF',
},
context={'source': 'hassio'}
@@ -299,18 +259,13 @@ async def test_hassio_confirm(hass):
}
result = await hass.config_entries.flow.async_configure(
result['flow_id'], {
'allow_clip_sensor': True,
'allow_deconz_groups': True,
}
result['flow_id'], user_input={}
)
assert result['type'] == 'create_entry'
assert result['result'].data == {
'host': 'mock-deconz',
'port': 8080,
'bridgeid': 'aa:bb',
'api_key': '1234567890ABCDEF',
'allow_clip_sensor': True,
'allow_deconz_groups': True,
'port': 80,
'bridgeid': 'id',
'api_key': '1234567890ABCDEF'
}
+13 -11
View File
@@ -61,7 +61,7 @@ async def setup_gateway(hass, data):
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = gateway
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch('pydeconz.DeconzSession.async_get_state',
return_value=mock_coro(data)):
@@ -70,6 +70,7 @@ async def setup_gateway(hass, data):
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()
return gateway
async def test_platform_manually_configured(hass):
@@ -84,8 +85,8 @@ async def test_platform_manually_configured(hass):
async def test_no_covers(hass):
"""Test that no cover entities are created."""
await setup_gateway(hass, {})
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert len(hass.states.async_all()) == 0
@@ -93,8 +94,8 @@ async def test_cover(hass):
"""Test that all supported cover entities are created."""
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
gateway = await setup_gateway(hass, {"lights": SUPPORTED_COVERS})
assert "cover.cover_1_name" in gateway.deconz_ids
assert len(SUPPORTED_COVERS) == len(COVER_TYPES)
assert len(hass.states.async_all()) == 3
@@ -102,7 +103,7 @@ async def test_cover(hass):
assert cover_1 is not None
assert cover_1.state == 'closed'
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
gateway.api.lights['1'].async_update({})
await hass.services.async_call('cover', 'open_cover', {
'entity_id': 'cover.cover_1_name'
@@ -122,14 +123,15 @@ async def test_cover(hass):
async def test_add_new_cover(hass):
"""Test successful creation of cover entity."""
data = {}
await setup_gateway(hass, data)
gateway = 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])
async_dispatcher_send(
hass, gateway.async_event_new_device('light'), [cover])
await hass.async_block_till_done()
assert "cover.name" in hass.data[deconz.DOMAIN].deconz_ids
assert "cover.name" in gateway.deconz_ids
async def test_unsupported_cover(hass):
@@ -140,8 +142,8 @@ async def test_unsupported_cover(hass):
async def test_unload_cover(hass):
"""Test that it works to unload switch entities."""
await setup_gateway(hass, {"lights": SUPPORTED_COVERS})
gateway = await setup_gateway(hass, {"lights": SUPPORTED_COVERS})
await hass.data[deconz.DOMAIN].async_reset()
await gateway.async_reset()
assert len(hass.states.async_all()) == 1
+135 -73
View File
@@ -11,56 +11,62 @@ from homeassistant.components import deconz
from tests.common import mock_coro, MockConfigEntry
ENTRY1_HOST = '1.2.3.4'
ENTRY1_PORT = 80
ENTRY1_API_KEY = '1234567890ABCDEF'
ENTRY1_BRIDGEID = '12345ABC'
CONFIG = {
"config": {
"bridgeid": "0123456789ABCDEF",
"mac": "12:34:56:78:90:ab",
"modelid": "deCONZ",
"name": "Phoscon",
"swversion": "2.05.35"
}
}
ENTRY2_HOST = '2.3.4.5'
ENTRY2_PORT = 80
ENTRY2_API_KEY = '1234567890ABCDEF'
ENTRY2_BRIDGEID = '23456DEF'
async def setup_entry(hass, entry):
"""Test that setup entry works."""
with patch.object(deconz.DeconzGateway, 'async_setup',
return_value=mock_coro(True)), \
patch.object(deconz.DeconzGateway, 'async_update_device_registry',
return_value=mock_coro(True)):
assert await deconz.async_setup_entry(hass, entry) is True
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=[]):
with patch.object(hass.config_entries, 'flow') as mock_config_flow:
assert await async_setup_component(hass, deconz.DOMAIN, {
deconz.DOMAIN: {
deconz.CONF_HOST: '1.2.3.4',
deconz.CONF_PORT: 80
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT
}
}) is True
# Import flow started
assert len(mock_config_entries.flow.mock_calls) == 2
assert len(mock_config_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=[]):
MockConfigEntry(domain=deconz.DOMAIN, data={}).add_to_hass(hass)
with patch.object(hass.config_entries, 'flow') as mock_config_flow:
assert await async_setup_component(hass, deconz.DOMAIN, {
deconz.DOMAIN: {}
}) is True
# No flow started
assert len(mock_config_entries.flow.mock_calls) == 0
assert len(mock_config_flow.mock_calls) == 0
async def test_config_already_registered_not_passed_to_config_entry(hass):
async def test_config_import_entry_fails_when_entries_exist(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']):
MockConfigEntry(domain=deconz.DOMAIN, data={}).add_to_hass(hass)
with patch.object(hass.config_entries, 'flow') as mock_config_flow:
assert await async_setup_component(hass, deconz.DOMAIN, {
deconz.DOMAIN: {
deconz.CONF_HOST: '1.2.3.4',
deconz.CONF_PORT: 80
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT
}
}) is True
# No flow started
assert len(mock_config_entries.flow.mock_calls) == 0
assert len(mock_config_flow.mock_calls) == 0
async def test_config_discovery(hass):
@@ -71,16 +77,14 @@ async def test_config_discovery(hass):
assert len(mock_config_entries.flow.mock_calls) == 0
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_fails(hass):
"""Test setup entry fails if deCONZ is not available."""
entry = Mock()
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
entry.data = {
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY
}
with patch('pydeconz.DeconzSession.async_load_parameters',
side_effect=Exception):
await deconz.async_setup_entry(hass, entry)
@@ -89,61 +93,121 @@ async def test_setup_entry_fails(hass):
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',
side_effect=asyncio.TimeoutError
), pytest.raises(ConfigEntryNotReady):
entry.data = {
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY
}
with patch('pydeconz.DeconzSession.async_load_parameters',
side_effect=asyncio.TimeoutError),\
pytest.raises(ConfigEntryNotReady):
await deconz.async_setup_entry(hass, entry)
async def test_setup_entry_successful(hass):
"""Test setup entry is successful."""
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID
})
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 hass.data[deconz.DOMAIN]
await setup_entry(hass, entry)
assert ENTRY1_BRIDGEID in hass.data[deconz.DOMAIN]
assert hass.data[deconz.DOMAIN][ENTRY1_BRIDGEID].master
async def test_setup_entry_multiple_gateways(hass):
"""Test setup entry is successful with multiple gateways."""
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID
})
entry.add_to_hass(hass)
entry2 = MockConfigEntry(domain=deconz.DOMAIN, data={
deconz.CONF_HOST: ENTRY2_HOST,
deconz.CONF_PORT: ENTRY2_PORT,
deconz.CONF_API_KEY: ENTRY2_API_KEY,
deconz.CONF_BRIDGEID: ENTRY2_BRIDGEID
})
entry2.add_to_hass(hass)
await setup_entry(hass, entry)
await setup_entry(hass, entry2)
assert ENTRY1_BRIDGEID in hass.data[deconz.DOMAIN]
assert hass.data[deconz.DOMAIN][ENTRY1_BRIDGEID].master
assert ENTRY2_BRIDGEID in hass.data[deconz.DOMAIN]
assert not hass.data[deconz.DOMAIN][ENTRY2_BRIDGEID].master
async def test_unload_entry(hass):
"""Test being able to unload an entry."""
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID
})
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
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
await setup_entry(hass, entry)
with patch.object(deconz.DeconzGateway, 'async_reset',
return_value=mock_coro(True)):
assert await deconz.async_unload_entry(hass, entry)
assert not hass.data[deconz.DOMAIN]
async def test_unload_entry_multiple_gateways(hass):
"""Test being able to unload an entry and master gateway gets moved."""
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID
})
entry.add_to_hass(hass)
entry2 = MockConfigEntry(domain=deconz.DOMAIN, data={
deconz.CONF_HOST: ENTRY2_HOST,
deconz.CONF_PORT: ENTRY2_PORT,
deconz.CONF_API_KEY: ENTRY2_API_KEY,
deconz.CONF_BRIDGEID: ENTRY2_BRIDGEID
})
entry2.add_to_hass(hass)
await setup_entry(hass, entry)
await setup_entry(hass, entry2)
with patch.object(deconz.DeconzGateway, 'async_reset',
return_value=mock_coro(True)):
assert await deconz.async_unload_entry(hass, entry)
assert ENTRY2_BRIDGEID in hass.data[deconz.DOMAIN]
assert hass.data[deconz.DOMAIN][ENTRY2_BRIDGEID].master
async def test_service_configure(hass):
"""Test that service invokes pydeconz with the correct path and data."""
entry = MockConfigEntry(domain=deconz.DOMAIN, data={
'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID
})
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
hass.data[deconz.DOMAIN].deconz_ids = {
await setup_entry(hass, entry)
hass.data[deconz.DOMAIN][ENTRY1_BRIDGEID].deconz_ids = {
'light.test': '/light/1'
}
data = {'on': True, 'attr1': 10, 'attr2': 20}
@@ -191,25 +255,23 @@ async def test_service_configure(hass):
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'
deconz.CONF_HOST: ENTRY1_HOST,
deconz.CONF_PORT: ENTRY1_PORT,
deconz.CONF_API_KEY: ENTRY1_API_KEY,
deconz.CONF_BRIDGEID: ENTRY1_BRIDGEID
})
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
await setup_entry(hass, entry)
with patch.object(hass.data[deconz.DOMAIN].api, 'async_load_parameters',
return_value=mock_coro(True)):
with patch('pydeconz.DeconzSession.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)):
with patch('pydeconz.DeconzSession.async_load_parameters',
return_value=mock_coro(False)):
await hass.services.async_call(
'deconz', 'device_refresh', service_data={})
await hass.async_block_till_done()
+27 -22
View File
@@ -87,7 +87,7 @@ async def setup_gateway(hass, data, allow_deconz_groups=True):
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = gateway
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch('pydeconz.DeconzSession.async_get_state',
return_value=mock_coro(data)):
@@ -96,6 +96,7 @@ async def setup_gateway(hass, data, allow_deconz_groups=True):
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()
return gateway
async def test_platform_manually_configured(hass):
@@ -110,8 +111,8 @@ async def test_platform_manually_configured(hass):
async def test_no_lights_or_groups(hass):
"""Test that no lights or groups entities are created."""
await setup_gateway(hass, {})
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert len(hass.states.async_all()) == 0
@@ -119,11 +120,12 @@ async def test_lights_and_groups(hass):
"""Test that lights or groups entities are created."""
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
gateway = await setup_gateway(
hass, {"lights": LIGHT, "groups": GROUP})
assert "light.light_1_name" in gateway.deconz_ids
assert "light.light_2_name" in gateway.deconz_ids
assert "light.group_1_name" in gateway.deconz_ids
assert "light.group_2_name" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 4
lamp_1 = hass.states.get('light.light_1_name')
@@ -137,7 +139,7 @@ async def test_lights_and_groups(hass):
assert light_2.state == 'on'
assert light_2.attributes['color_temp'] == 2500
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
gateway.api.lights['1'].async_update({})
await hass.services.async_call('light', 'turn_on', {
'entity_id': 'light.light_1_name',
@@ -166,49 +168,52 @@ async def test_lights_and_groups(hass):
async def test_add_new_light(hass):
"""Test successful creation of light entities."""
await setup_gateway(hass, {})
gateway = await setup_gateway(hass, {})
light = Mock()
light.name = 'name'
light.register_async_callback = Mock()
async_dispatcher_send(hass, 'deconz_new_light', [light])
async_dispatcher_send(
hass, gateway.async_event_new_device('light'), [light])
await hass.async_block_till_done()
assert "light.name" in hass.data[deconz.DOMAIN].deconz_ids
assert "light.name" in gateway.deconz_ids
async def test_add_new_group(hass):
"""Test successful creation of group entities."""
await setup_gateway(hass, {})
gateway = await setup_gateway(hass, {})
group = Mock()
group.name = 'name'
group.register_async_callback = Mock()
async_dispatcher_send(hass, 'deconz_new_group', [group])
async_dispatcher_send(
hass, gateway.async_event_new_device('group'), [group])
await hass.async_block_till_done()
assert "light.name" in hass.data[deconz.DOMAIN].deconz_ids
assert "light.name" in gateway.deconz_ids
async def test_do_not_add_deconz_groups(hass):
"""Test that clip sensors can be ignored."""
await setup_gateway(hass, {}, allow_deconz_groups=False)
gateway = 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])
async_dispatcher_send(
hass, gateway.async_event_new_device('group'), [group])
await hass.async_block_till_done()
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
assert len(gateway.deconz_ids) == 0
async def test_no_switch(hass):
"""Test that a switch doesn't get created as a light entity."""
await setup_gateway(hass, {"lights": SWITCH})
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
gateway = await setup_gateway(hass, {"lights": SWITCH})
assert len(gateway.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})
gateway = await setup_gateway(hass, {"lights": LIGHT, "groups": GROUP})
await hass.data[deconz.DOMAIN].async_reset()
await gateway.async_reset()
# Group.all_lights will not be removed
assert len(hass.states.async_all()) == 1
+8 -7
View File
@@ -47,7 +47,7 @@ async def setup_gateway(hass, data):
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = gateway
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch('pydeconz.DeconzSession.async_get_state',
return_value=mock_coro(data)):
@@ -56,6 +56,7 @@ async def setup_gateway(hass, data):
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()
return gateway
async def test_platform_manually_configured(hass):
@@ -70,8 +71,8 @@ async def test_platform_manually_configured(hass):
async def test_no_scenes(hass):
"""Test that scenes can be loaded without scenes being available."""
await setup_gateway(hass, {})
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert len(hass.states.async_all()) == 0
@@ -79,8 +80,8 @@ async def test_scenes(hass):
"""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
gateway = await setup_gateway(hass, {"groups": GROUP})
assert "scene.group_1_name_scene_1" in gateway.deconz_ids
assert len(hass.states.async_all()) == 1
await hass.services.async_call('scene', 'turn_on', {
@@ -90,8 +91,8 @@ async def test_scenes(hass):
async def test_unload_scene(hass):
"""Test that it works to unload scene entities."""
await setup_gateway(hass, {"groups": GROUP})
gateway = await setup_gateway(hass, {"groups": GROUP})
await hass.data[deconz.DOMAIN].async_reset()
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
+23 -24
View File
@@ -91,7 +91,7 @@ async def setup_gateway(hass, data, allow_clip_sensor=True):
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = gateway
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch('pydeconz.DeconzSession.async_get_state',
return_value=mock_coro(data)):
@@ -101,6 +101,7 @@ async def setup_gateway(hass, data, allow_clip_sensor=True):
config_entry, 'sensor')
# To flush out the service call to update the group
await hass.async_block_till_done()
return gateway
async def test_platform_manually_configured(hass):
@@ -115,58 +116,56 @@ async def test_platform_manually_configured(hass):
async def test_no_sensors(hass):
"""Test that no sensors in deconz results in no sensor entities."""
await setup_gateway(hass, {})
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert len(hass.states.async_all()) == 0
async def test_sensors(hass):
"""Test successful creation of sensor entities."""
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.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.DOMAIN].deconz_ids
gateway = await setup_gateway(hass, {"sensors": SENSOR})
assert "sensor.sensor_1_name" in gateway.deconz_ids
assert "sensor.sensor_2_name" not in gateway.deconz_ids
assert "sensor.sensor_3_name" not in gateway.deconz_ids
assert "sensor.sensor_3_name_battery_level" not in gateway.deconz_ids
assert "sensor.sensor_4_name" not in gateway.deconz_ids
assert "sensor.sensor_4_name_battery_level" in gateway.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}})
gateway.api.sensors['1'].async_update({'state': {'on': False}})
gateway.api.sensors['4'].async_update({'config': {'battery': 75}})
async def test_add_new_sensor(hass):
"""Test successful creation of sensor entities."""
await setup_gateway(hass, {})
gateway = 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])
async_dispatcher_send(
hass, gateway.async_event_new_device('sensor'), [sensor])
await hass.async_block_till_done()
assert "sensor.name" in hass.data[deconz.DOMAIN].deconz_ids
assert "sensor.name" in gateway.deconz_ids
async def test_do_not_allow_clipsensor(hass):
"""Test that clip sensors can be ignored."""
await setup_gateway(hass, {}, allow_clip_sensor=False)
gateway = 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])
async_dispatcher_send(
hass, gateway.async_event_new_device('sensor'), [sensor])
await hass.async_block_till_done()
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
assert len(gateway.deconz_ids) == 0
async def test_unload_sensor(hass):
"""Test that it works to unload sensor entities."""
await setup_gateway(hass, {"sensors": SENSOR})
gateway = await setup_gateway(hass, {"sensors": SENSOR})
await hass.data[deconz.DOMAIN].async_reset()
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
+15 -13
View File
@@ -65,7 +65,7 @@ async def setup_gateway(hass, data):
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = gateway
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch('pydeconz.DeconzSession.async_get_state',
return_value=mock_coro(data)):
@@ -74,6 +74,7 @@ async def setup_gateway(hass, data):
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()
return gateway
async def test_platform_manually_configured(hass):
@@ -88,8 +89,8 @@ async def test_platform_manually_configured(hass):
async def test_no_switches(hass):
"""Test that no switch entities are created."""
await setup_gateway(hass, {})
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert len(hass.states.async_all()) == 0
@@ -97,10 +98,10 @@ async def test_switches(hass):
"""Test that all supported switch entities are created."""
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
gateway = await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
assert "switch.switch_1_name" in gateway.deconz_ids
assert "switch.switch_2_name" in gateway.deconz_ids
assert "switch.switch_3_name" in gateway.deconz_ids
assert len(SUPPORTED_SWITCHES) == len(SWITCH_TYPES)
assert len(hass.states.async_all()) == 4
@@ -111,7 +112,7 @@ async def test_switches(hass):
assert switch_3 is not None
assert switch_3.state == 'on'
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
gateway.api.lights['1'].async_update({})
await hass.services.async_call('switch', 'turn_on', {
'entity_id': 'switch.switch_1_name'
@@ -130,14 +131,15 @@ async def test_switches(hass):
async def test_add_new_switch(hass):
"""Test successful creation of switch entity."""
await setup_gateway(hass, {})
gateway = 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])
async_dispatcher_send(
hass, gateway.async_event_new_device('light'), [switch])
await hass.async_block_till_done()
assert "switch.name" in hass.data[deconz.DOMAIN].deconz_ids
assert "switch.name" in gateway.deconz_ids
async def test_unsupported_switch(hass):
@@ -148,8 +150,8 @@ async def test_unsupported_switch(hass):
async def test_unload_switch(hass):
"""Test that it works to unload switch entities."""
await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
gateway = await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
await hass.data[deconz.DOMAIN].async_reset()
await gateway.async_reset()
assert len(hass.states.async_all()) == 1