2018-11-06 09:34:24 +00:00
|
|
|
"""Test deCONZ gateway."""
|
2019-09-19 21:44:09 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
from asynctest import Mock, patch
|
2019-12-09 11:25:35 +00:00
|
|
|
import pydeconz
|
2019-02-14 04:36:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
from homeassistant import config_entries
|
2019-12-09 11:25:35 +00:00
|
|
|
from homeassistant.components import deconz, ssdp
|
2019-02-14 04:36:06 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-09-19 21:44:09 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
BRIDGEID = "0123456789"
|
2019-03-24 18:27:32 +00:00
|
|
|
|
2018-11-06 09:34:24 +00:00
|
|
|
ENTRY_CONFIG = {
|
2019-09-19 21:44:09 +00:00
|
|
|
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
|
|
|
deconz.config_flow.CONF_BRIDGEID: BRIDGEID,
|
|
|
|
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
|
|
|
deconz.config_flow.CONF_PORT: 80,
|
2019-09-25 16:56:31 +00:00
|
|
|
deconz.config_flow.CONF_UUID: "456DEF",
|
2018-11-06 09:34:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
DECONZ_CONFIG = {
|
|
|
|
"bridgeid": BRIDGEID,
|
|
|
|
"ipaddress": "1.2.3.4",
|
|
|
|
"mac": "00:11:22:33:44:55",
|
|
|
|
"modelid": "deCONZ",
|
|
|
|
"name": "deCONZ mock gateway",
|
|
|
|
"sw_version": "2.05.69",
|
|
|
|
"uuid": "1234",
|
|
|
|
"websocketport": 1234,
|
|
|
|
}
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
DECONZ_WEB_REQUEST = {"config": DECONZ_CONFIG}
|
|
|
|
|
|
|
|
|
|
|
|
async def setup_deconz_integration(hass, config, options, get_state_response):
|
|
|
|
"""Create the deCONZ gateway."""
|
|
|
|
config_entry = config_entries.ConfigEntry(
|
|
|
|
version=1,
|
|
|
|
domain=deconz.DOMAIN,
|
|
|
|
title="Mock Title",
|
|
|
|
data=config,
|
|
|
|
source="test",
|
|
|
|
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
|
|
|
|
system_options={},
|
|
|
|
options=options,
|
|
|
|
entry_id="1",
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-12-08 15:53:34 +00:00
|
|
|
for resource in ("groups", "lights", "sensors"):
|
|
|
|
if resource not in get_state_response:
|
|
|
|
get_state_response[resource] = {}
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
with patch(
|
2019-12-08 15:53:34 +00:00
|
|
|
"pydeconz.DeconzSession.request", return_value=get_state_response
|
2019-09-19 21:44:09 +00:00
|
|
|
), patch("pydeconz.DeconzSession.start", return_value=True):
|
|
|
|
await deconz.async_setup_entry(hass, config_entry)
|
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
hass.config_entries._entries.append(config_entry)
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
return hass.data[deconz.DOMAIN].get(config[deconz.CONF_BRIDGEID])
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
async def test_gateway_setup(hass):
|
|
|
|
"""Successful setup."""
|
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setup",
|
|
|
|
return_value=True,
|
|
|
|
) as forward_entry_setup:
|
|
|
|
gateway = await setup_deconz_integration(
|
|
|
|
hass, ENTRY_CONFIG, options={}, get_state_response=data
|
|
|
|
)
|
|
|
|
assert gateway.bridgeid == BRIDGEID
|
|
|
|
assert gateway.master is True
|
|
|
|
assert gateway.option_allow_clip_sensor is False
|
|
|
|
assert gateway.option_allow_deconz_groups is True
|
|
|
|
|
|
|
|
assert len(gateway.deconz_ids) == 0
|
|
|
|
assert len(hass.states.async_all()) == 0
|
|
|
|
|
|
|
|
entry = gateway.config_entry
|
|
|
|
assert forward_entry_setup.mock_calls[0][1] == (entry, "binary_sensor")
|
|
|
|
assert forward_entry_setup.mock_calls[1][1] == (entry, "climate")
|
|
|
|
assert forward_entry_setup.mock_calls[2][1] == (entry, "cover")
|
|
|
|
assert forward_entry_setup.mock_calls[3][1] == (entry, "light")
|
|
|
|
assert forward_entry_setup.mock_calls[4][1] == (entry, "scene")
|
|
|
|
assert forward_entry_setup.mock_calls[5][1] == (entry, "sensor")
|
|
|
|
assert forward_entry_setup.mock_calls[6][1] == (entry, "switch")
|
|
|
|
|
|
|
|
|
|
|
|
async def test_gateway_retry(hass):
|
2019-03-24 18:27:32 +00:00
|
|
|
"""Retry setup."""
|
2019-09-19 21:44:09 +00:00
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.deconz.gateway.get_gateway",
|
|
|
|
side_effect=deconz.errors.CannotConnect,
|
|
|
|
), pytest.raises(ConfigEntryNotReady):
|
|
|
|
await setup_deconz_integration(
|
|
|
|
hass, ENTRY_CONFIG, options={}, get_state_response=data
|
|
|
|
)
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
async def test_gateway_setup_fails(hass):
|
|
|
|
"""Retry setup."""
|
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.deconz.gateway.get_gateway", side_effect=Exception
|
|
|
|
):
|
|
|
|
gateway = await setup_deconz_integration(
|
|
|
|
hass, ENTRY_CONFIG, options={}, get_state_response=data
|
|
|
|
)
|
|
|
|
assert gateway is None
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
async def test_connection_status_signalling(hass):
|
2018-11-06 09:34:24 +00:00
|
|
|
"""Make sure that connection status triggers a dispatcher send."""
|
2019-09-19 21:44:09 +00:00
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
gateway = await setup_deconz_integration(
|
|
|
|
hass, ENTRY_CONFIG, options={}, get_state_response=data
|
|
|
|
)
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
event_call = Mock()
|
|
|
|
unsub = async_dispatcher_connect(hass, gateway.signal_reachable, event_call)
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
gateway.async_connection_status_callback(False)
|
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
assert gateway.available is False
|
|
|
|
assert len(event_call.mock_calls) == 1
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
unsub()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
async def test_update_address(hass):
|
|
|
|
"""Make sure that connection status triggers a dispatcher send."""
|
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
gateway = await setup_deconz_integration(
|
|
|
|
hass, ENTRY_CONFIG, options={}, get_state_response=data
|
|
|
|
)
|
|
|
|
assert gateway.api.host == "1.2.3.4"
|
|
|
|
|
|
|
|
await hass.config_entries.flow.async_init(
|
|
|
|
deconz.config_flow.DOMAIN,
|
|
|
|
data={
|
2019-12-19 17:28:03 +00:00
|
|
|
ssdp.ATTR_SSDP_LOCATION: "http://2.3.4.5:80/",
|
|
|
|
ssdp.ATTR_UPNP_MANUFACTURER_URL: deconz.config_flow.DECONZ_MANUFACTURERURL,
|
|
|
|
ssdp.ATTR_UPNP_SERIAL: BRIDGEID,
|
|
|
|
ssdp.ATTR_UPNP_UDN: "uuid:456DEF",
|
2019-09-19 21:44:09 +00:00
|
|
|
},
|
|
|
|
context={"source": "ssdp"},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
assert gateway.api.host == "2.3.4.5"
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
async def test_reset_after_successful_setup(hass):
|
|
|
|
"""Make sure that connection status triggers a dispatcher send."""
|
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
gateway = await setup_deconz_integration(
|
|
|
|
hass, ENTRY_CONFIG, options={}, get_state_response=data
|
|
|
|
)
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
result = await gateway.async_reset()
|
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
assert result is True
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_gateway(hass):
|
|
|
|
"""Successful call."""
|
2019-12-08 15:53:34 +00:00
|
|
|
with patch("pydeconz.DeconzSession.initialize", return_value=True):
|
2019-09-19 21:44:09 +00:00
|
|
|
assert await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
async def test_get_gateway_fails_unauthorized(hass):
|
|
|
|
"""Failed call."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-12-08 15:53:34 +00:00
|
|
|
"pydeconz.DeconzSession.initialize", side_effect=pydeconz.errors.Unauthorized,
|
2019-09-19 21:44:09 +00:00
|
|
|
), pytest.raises(deconz.errors.AuthenticationRequired):
|
|
|
|
assert (
|
|
|
|
await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
|
|
|
is False
|
|
|
|
)
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_gateway_fails_cannot_connect(hass):
|
2018-11-06 09:34:24 +00:00
|
|
|
"""Failed call."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-12-08 15:53:34 +00:00
|
|
|
"pydeconz.DeconzSession.initialize", side_effect=pydeconz.errors.RequestError,
|
2019-09-19 21:44:09 +00:00
|
|
|
), pytest.raises(deconz.errors.CannotConnect):
|
|
|
|
assert (
|
|
|
|
await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
|
|
|
is False
|
|
|
|
)
|