Support deCONZ library with exception handling (#21952)

This commit is contained in:
Robert Svensson authored and GitHub committed 2019-03-24 19:27:32 +01:00
1 parent 89f8203163
commit 8d1cf553de
10 files changed
+190 -65

No files matched your search

+8 -5
View File
@@ -46,11 +46,14 @@ async def setup_gateway(hass, data, allow_clip_sensor=True):
"""Load the deCONZ sensor platform."""
from pydeconz import DeconzSession
session = Mock(put=asynctest.CoroutineMock(
return_value=Mock(status=200,
json=asynctest.CoroutineMock(),
text=asynctest.CoroutineMock(),
)
response = Mock(
status=200, json=asynctest.CoroutineMock(),
text=asynctest.CoroutineMock())
response.content_type = 'application/json'
session = Mock(
put=asynctest.CoroutineMock(
return_value=response
)
)
+51 -26
View File
@@ -1,7 +1,8 @@
"""Tests for deCONZ config flow."""
import pytest
from unittest.mock import patch
import asyncio
import voluptuous as vol
from homeassistant.components.deconz import config_flow
from tests.common import MockConfigEntry
@@ -12,15 +13,17 @@ async def test_flow_works(hass, aioclient_mock):
"""Test that config flow works."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[
{'id': 'id', 'internalipaddress': '1.2.3.4', 'internalport': 80}
])
], headers={'content-type': 'application/json'})
aioclient_mock.post('http://1.2.3.4:80/api', json=[
{"success": {"username": "1234567890ABCDEF"}}
])
], headers={'content-type': 'application/json'})
flow = config_flow.DeconzFlowHandler()
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})
@@ -41,35 +44,53 @@ async def test_flow_already_registered_bridge(hass):
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_init()
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()
flow.hass = hass
with patch('pydeconz.utils.async_discovery',
side_effect=asyncio.TimeoutError):
result = await flow.async_step_user()
assert result['type'] == 'form'
assert result['step_id'] == 'init'
async def test_flow_no_discovered_bridges(hass, aioclient_mock):
"""Test config flow discovers no bridges."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[])
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[],
headers={'content-type': 'application/json'})
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'] == 'user'
assert result['step_id'] == 'init'
async def test_flow_one_bridge_discovered(hass, aioclient_mock):
"""Test config flow discovers one bridge."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[
{'id': 'id', 'internalipaddress': '1.2.3.4', 'internalport': 80}
])
], headers={'content-type': 'application/json'})
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'
assert flow.deconz_config['host'] == '1.2.3.4'
async def test_flow_two_bridges_discovered(hass, aioclient_mock):
@@ -77,19 +98,14 @@ async def test_flow_two_bridges_discovered(hass, aioclient_mock):
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[
{'id': 'id1', 'internalipaddress': '1.2.3.4', 'internalport': 80},
{'id': 'id2', 'internalipaddress': '5.6.7.8', 'internalport': 80}
])
], headers={'content-type': 'application/json'})
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == 'form'
assert result['step_id'] == 'init'
with pytest.raises(vol.Invalid):
assert result['data_schema']({'host': '0.0.0.0'})
result['data_schema']({'host': '1.2.3.4'})
result['data_schema']({'host': '5.6.7.8'})
result = await flow.async_step_user()
assert result['data_schema']({'host': '1.2.3.4'})
assert result['data_schema']({'host': '5.6.7.8'})
async def test_flow_two_bridges_selection(hass, aioclient_mock):
@@ -101,7 +117,7 @@ async def test_flow_two_bridges_selection(hass, aioclient_mock):
{'bridgeid': 'id2', 'host': '5.6.7.8', 'port': 80}
]
result = await flow.async_step_init(user_input={'host': '1.2.3.4'})
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'
@@ -110,25 +126,28 @@ async def test_flow_two_bridges_selection(hass, aioclient_mock):
async def test_flow_manual_configuration(hass, aioclient_mock):
"""Test config flow with manual input."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[])
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
user_input = {'host': '1.2.3.4', 'port': 80}
result = await flow.async_step_init(user_input)
result = await flow.async_step_user(user_input)
assert result['type'] == 'form'
assert result['step_id'] == 'link'
assert flow.deconz_config == user_input
async def test_link_no_api_key(hass, aioclient_mock):
async def test_link_no_api_key(hass):
"""Test config flow should abort if no API key was possible to retrieve."""
aioclient_mock.post('http://1.2.3.4:80/api', json=[])
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={})
with patch('pydeconz.utils.async_get_api_key',
side_effect=pydeconz.errors.ResponseError):
result = await flow.async_step_link(user_input={})
assert result['type'] == 'form'
assert result['step_id'] == 'link'
assert result['errors'] == {'base': 'no_key'}
@@ -143,6 +162,7 @@ async def test_link_already_registered_bridge(hass):
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}
@@ -155,6 +175,7 @@ async def test_bridge_discovery(hass):
"""Test a bridge being discovered."""
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
result = await flow.async_step_discovery({
'host': '1.2.3.4',
'port': 80,
@@ -222,14 +243,18 @@ async def test_import_with_api_key(hass):
async def test_options(hass, aioclient_mock):
"""Test that options work and that bridgeid can be requested."""
aioclient_mock.get('http://1.2.3.4:80/api/1234567890ABCDEF/config',
json={"bridgeid": "id"})
json={"bridgeid": "id"},
headers={'content-type': 'application/json'})
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
flow.deconz_config = {'host': '1.2.3.4',
'port': 80,
'api_key': '1234567890ABCDEF'}
result = await flow.async_step_options(
user_input={'allow_clip_sensor': False, 'allow_deconz_groups': False})
assert result['type'] == 'create_entry'
assert result['title'] == 'deCONZ-id'
assert result['data'] == {
+32 -5
View File
@@ -4,10 +4,13 @@ from unittest.mock import Mock, patch
import pytest
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.components.deconz import gateway
from homeassistant.components.deconz import errors, gateway
from tests.common import mock_coro
import pydeconz
ENTRY_CONFIG = {
"host": "1.2.3.4",
"port": 80,
@@ -62,11 +65,25 @@ async def test_gateway_retry():
deconz_gateway = gateway.DeconzGateway(hass, entry)
with patch.object(
gateway, 'get_gateway', return_value=mock_coro(False)
), pytest.raises(ConfigEntryNotReady):
gateway, 'get_gateway', side_effect=errors.CannotConnect), \
pytest.raises(ConfigEntryNotReady):
await deconz_gateway.async_setup()
async def test_gateway_setup_fails():
"""Retry setup."""
hass = Mock()
entry = Mock()
entry.data = ENTRY_CONFIG
deconz_gateway = gateway.DeconzGateway(hass, entry)
with patch.object(gateway, 'get_gateway', side_effect=Exception):
result = await deconz_gateway.async_setup()
assert not result
async def test_connection_status(hass):
"""Make sure that connection status triggers a dispatcher send."""
entry = Mock()
@@ -170,10 +187,20 @@ async def test_get_gateway(hass):
assert await gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
async def test_get_gateway_fails(hass):
async def test_get_gateway_fails_unauthorized(hass):
"""Failed call."""
with patch('pydeconz.DeconzSession.async_load_parameters',
return_value=mock_coro(False)):
side_effect=pydeconz.errors.Unauthorized), \
pytest.raises(errors.AuthenticationRequired):
assert await gateway.get_gateway(
hass, ENTRY_CONFIG, Mock(), Mock()) is False
async def test_get_gateway_fails_cannot_connect(hass):
"""Failed call."""
with patch('pydeconz.DeconzSession.async_load_parameters',
side_effect=pydeconz.errors.RequestError), \
pytest.raises(errors.CannotConnect):
assert await gateway.get_gateway(
hass, ENTRY_CONFIG, Mock(), Mock()) is False
+13 -1
View File
@@ -1,6 +1,7 @@
"""Test deCONZ component setup process."""
from unittest.mock import Mock, patch
import asyncio
import pytest
import voluptuous as vol
@@ -76,13 +77,22 @@ async def test_setup_entry_already_registered_bridge(hass):
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'}
with patch('pydeconz.DeconzSession.async_load_parameters',
side_effect=Exception):
await deconz.async_setup_entry(hass, entry)
async def test_setup_entry_no_available_bridge(hass):
"""Test setup entry fails if deCONZ is not available."""
entry = Mock()
entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
with patch(
'pydeconz.DeconzSession.async_load_parameters',
return_value=mock_coro(False)
side_effect=asyncio.TimeoutError
), pytest.raises(ConfigEntryNotReady):
await deconz.async_setup_entry(hass, entry)
@@ -185,6 +195,7 @@ async def test_service_refresh_devices(hass):
})
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)):
@@ -196,6 +207,7 @@ async def test_service_refresh_devices(hass):
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(