mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 01:46:11 -04:00
deCONZ - manual input fallback in config flow (#18116)
* Add config flow step for manual input Remove support for loading discovery config from json file * Small cleanup Fix all translations to step user instead of step init * Revert to using step_init * Small cleanup Add test_gateway that was forgotten in a previous PR * Fix hound comment * Fix empty pydocstring
This commit is contained in:
committed by
Paulus Schoutsen
parent
24c110ad3c
commit
c5d0440041
@@ -1,5 +1,4 @@
|
||||
"""Tests for deCONZ config flow."""
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
|
||||
import voluptuous as vol
|
||||
@@ -45,7 +44,7 @@ async def test_flow_already_registered_bridge(hass):
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_user()
|
||||
result = await flow.async_step_init()
|
||||
assert result['type'] == 'abort'
|
||||
|
||||
|
||||
@@ -55,8 +54,9 @@ async def test_flow_no_discovered_bridges(hass, aioclient_mock):
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_user()
|
||||
assert result['type'] == 'abort'
|
||||
result = await flow.async_step_init()
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'user'
|
||||
|
||||
|
||||
async def test_flow_one_bridge_discovered(hass, aioclient_mock):
|
||||
@@ -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_user()
|
||||
result = await flow.async_step_init()
|
||||
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_user()
|
||||
result = await flow.async_step_init()
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'user'
|
||||
assert result['step_id'] == 'init'
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
assert result['data_schema']({'host': '0.0.0.0'})
|
||||
@@ -101,12 +101,26 @@ async def test_flow_two_bridges_selection(hass, aioclient_mock):
|
||||
{'bridgeid': 'id2', 'host': '5.6.7.8', 'port': 80}
|
||||
]
|
||||
|
||||
result = await flow.async_step_user(user_input={'host': '1.2.3.4'})
|
||||
result = await flow.async_step_init(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_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)
|
||||
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):
|
||||
"""Test config flow should abort if no API key was possible to retrieve."""
|
||||
aioclient_mock.post('http://1.2.3.4:80/api', json=[])
|
||||
@@ -138,57 +152,14 @@ async def test_link_already_registered_bridge(hass):
|
||||
|
||||
|
||||
async def test_bridge_discovery(hass):
|
||||
"""Test a bridge being discovered with no additional config file."""
|
||||
"""Test a bridge being discovered."""
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
with patch.object(config_flow, 'load_json', return_value={}):
|
||||
result = await flow.async_step_discovery({
|
||||
'host': '1.2.3.4',
|
||||
'port': 80,
|
||||
'serial': 'id'
|
||||
})
|
||||
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'link'
|
||||
|
||||
|
||||
async def test_bridge_discovery_config_file(hass):
|
||||
"""Test a bridge being discovered with a corresponding config file."""
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
with patch.object(config_flow, 'load_json',
|
||||
return_value={'host': '1.2.3.4',
|
||||
'port': 8080,
|
||||
'api_key': '1234567890ABCDEF'}):
|
||||
result = await flow.async_step_discovery({
|
||||
'host': '1.2.3.4',
|
||||
'port': 80,
|
||||
'serial': 'id'
|
||||
})
|
||||
|
||||
assert result['type'] == 'create_entry'
|
||||
assert result['title'] == 'deCONZ-id'
|
||||
assert result['data'] == {
|
||||
'bridgeid': 'id',
|
||||
result = await flow.async_step_discovery({
|
||||
'host': '1.2.3.4',
|
||||
'port': 80,
|
||||
'api_key': '1234567890ABCDEF',
|
||||
'allow_clip_sensor': True,
|
||||
'allow_deconz_groups': True
|
||||
}
|
||||
|
||||
|
||||
async def test_bridge_discovery_other_config_file(hass):
|
||||
"""Test a bridge being discovered with another bridges config file."""
|
||||
flow = config_flow.DeconzFlowHandler()
|
||||
flow.hass = hass
|
||||
with patch.object(config_flow, 'load_json',
|
||||
return_value={'host': '5.6.7.8', 'api_key': '5678'}):
|
||||
result = await flow.async_step_discovery({
|
||||
'host': '1.2.3.4',
|
||||
'port': 80,
|
||||
'serial': 'id'
|
||||
})
|
||||
'serial': 'id'
|
||||
})
|
||||
|
||||
assert result['type'] == 'form'
|
||||
assert result['step_id'] == 'link'
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
"""Test deCONZ gateway."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant.components.deconz import gateway
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
"host": "1.2.3.4",
|
||||
"port": 80,
|
||||
"api_key": "1234567890ABCDEF",
|
||||
"bridgeid": "0123456789ABCDEF",
|
||||
"allow_clip_sensor": True,
|
||||
"allow_deconz_groups": True,
|
||||
}
|
||||
|
||||
|
||||
async def test_gateway_setup():
|
||||
"""Successful setup."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
api = Mock()
|
||||
api.async_add_remote.return_value = Mock()
|
||||
api.sensors = {}
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
|
||||
with patch.object(gateway, 'get_gateway', return_value=mock_coro(api)), \
|
||||
patch.object(
|
||||
gateway, 'async_dispatcher_connect', return_value=Mock()):
|
||||
assert await deconz_gateway.async_setup() is True
|
||||
|
||||
assert deconz_gateway.api is api
|
||||
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 6
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == \
|
||||
(entry, 'binary_sensor')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[1][1] == \
|
||||
(entry, 'cover')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[2][1] == \
|
||||
(entry, 'light')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[3][1] == \
|
||||
(entry, 'scene')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[4][1] == \
|
||||
(entry, 'sensor')
|
||||
assert hass.config_entries.async_forward_entry_setup.mock_calls[5][1] == \
|
||||
(entry, 'switch')
|
||||
assert len(api.start.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_gateway_retry():
|
||||
"""Retry setup."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
|
||||
with patch.object(gateway, 'get_gateway', return_value=mock_coro(False)):
|
||||
assert await deconz_gateway.async_setup() is False
|
||||
|
||||
|
||||
async def test_connection_status(hass):
|
||||
"""Make sure that connection status triggers a dispatcher send."""
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
with patch.object(gateway, 'async_dispatcher_send') as mock_dispatch_send:
|
||||
deconz_gateway.async_connection_status_callback(True)
|
||||
|
||||
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_device(hass):
|
||||
"""Successful retry setup."""
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
with patch.object(gateway, 'async_dispatcher_send') as mock_dispatch_send:
|
||||
deconz_gateway.async_add_device_callback('sensor', Mock())
|
||||
|
||||
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_remote():
|
||||
"""Successful add remote."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
remote = Mock()
|
||||
remote.name = 'name'
|
||||
remote.type = 'ZHASwitch'
|
||||
remote.register_async_callback = Mock()
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
deconz_gateway.async_add_remote([remote])
|
||||
|
||||
assert len(deconz_gateway.events) == 1
|
||||
|
||||
|
||||
async def test_shutdown():
|
||||
"""Successful shutdown."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
deconz_gateway.api = Mock()
|
||||
deconz_gateway.shutdown(None)
|
||||
|
||||
assert len(deconz_gateway.api.close.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reset_cancel_retry():
|
||||
"""Verify async reset can handle a scheduled retry."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
|
||||
with patch.object(gateway, 'get_gateway', return_value=mock_coro(False)):
|
||||
assert await deconz_gateway.async_setup() is False
|
||||
|
||||
assert deconz_gateway._cancel_retry_setup is not None
|
||||
|
||||
assert await deconz_gateway.async_reset() is True
|
||||
|
||||
|
||||
async def test_reset_after_successful_setup():
|
||||
"""Verify that reset works on a setup component."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = ENTRY_CONFIG
|
||||
api = Mock()
|
||||
api.async_add_remote.return_value = Mock()
|
||||
api.sensors = {}
|
||||
|
||||
deconz_gateway = gateway.DeconzGateway(hass, entry)
|
||||
|
||||
with patch.object(gateway, 'get_gateway', return_value=mock_coro(api)), \
|
||||
patch.object(
|
||||
gateway, 'async_dispatcher_connect', return_value=Mock()):
|
||||
assert await deconz_gateway.async_setup() is True
|
||||
|
||||
listener = Mock()
|
||||
deconz_gateway.listeners = [listener]
|
||||
event = Mock()
|
||||
event.async_will_remove_from_hass = Mock()
|
||||
deconz_gateway.events = [event]
|
||||
deconz_gateway.deconz_ids = {'key': 'value'}
|
||||
|
||||
hass.config_entries.async_forward_entry_unload.return_value = \
|
||||
mock_coro(True)
|
||||
assert await deconz_gateway.async_reset() is True
|
||||
|
||||
assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 6
|
||||
|
||||
assert len(listener.mock_calls) == 1
|
||||
assert len(deconz_gateway.listeners) == 0
|
||||
|
||||
assert len(event.async_will_remove_from_hass.mock_calls) == 1
|
||||
assert len(deconz_gateway.events) == 0
|
||||
|
||||
assert len(deconz_gateway.deconz_ids) == 0
|
||||
|
||||
|
||||
async def test_get_gateway(hass):
|
||||
"""Successful call."""
|
||||
with patch('pydeconz.DeconzSession.async_load_parameters',
|
||||
return_value=mock_coro(True)):
|
||||
assert await gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
||||
|
||||
|
||||
async def test_get_gateway_fails(hass):
|
||||
"""Failed call."""
|
||||
with patch('pydeconz.DeconzSession.async_load_parameters',
|
||||
return_value=mock_coro(False)):
|
||||
assert await gateway.get_gateway(
|
||||
hass, ENTRY_CONFIG, Mock(), Mock()) is False
|
||||
|
||||
|
||||
async def test_create_event():
|
||||
"""Successfully created a deCONZ event."""
|
||||
hass = Mock()
|
||||
remote = Mock()
|
||||
remote.name = 'Name'
|
||||
|
||||
event = gateway.DeconzEvent(hass, remote)
|
||||
|
||||
assert event._id == 'name'
|
||||
|
||||
|
||||
async def test_update_event():
|
||||
"""Successfully update a deCONZ event."""
|
||||
hass = Mock()
|
||||
remote = Mock()
|
||||
remote.name = 'Name'
|
||||
|
||||
event = gateway.DeconzEvent(hass, remote)
|
||||
event.async_update_callback({'state': True})
|
||||
|
||||
assert len(hass.bus.async_fire.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_remove_event():
|
||||
"""Successfully update a deCONZ event."""
|
||||
hass = Mock()
|
||||
remote = Mock()
|
||||
remote.name = 'Name'
|
||||
|
||||
event = gateway.DeconzEvent(hass, remote)
|
||||
event.async_will_remove_from_hass()
|
||||
|
||||
assert event._device is None
|
||||
@@ -21,8 +21,7 @@ CONFIG = {
|
||||
async def test_config_with_host_passed_to_config_entry(hass):
|
||||
"""Test that configured options for a host are loaded via config entry."""
|
||||
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch.object(deconz, 'configured_hosts', return_value=[]), \
|
||||
patch.object(deconz, 'load_json', return_value={}):
|
||||
patch.object(deconz, 'configured_hosts', return_value=[]):
|
||||
assert await async_setup_component(hass, deconz.DOMAIN, {
|
||||
deconz.DOMAIN: {
|
||||
deconz.CONF_HOST: '1.2.3.4',
|
||||
@@ -33,24 +32,10 @@ async def test_config_with_host_passed_to_config_entry(hass):
|
||||
assert len(mock_config_entries.flow.mock_calls) == 2
|
||||
|
||||
|
||||
async def test_config_file_passed_to_config_entry(hass):
|
||||
"""Test that configuration file for a host are loaded via config entry."""
|
||||
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch.object(deconz, 'configured_hosts', return_value=[]), \
|
||||
patch.object(deconz, 'load_json',
|
||||
return_value={'host': '1.2.3.4'}):
|
||||
assert await async_setup_component(hass, deconz.DOMAIN, {
|
||||
deconz.DOMAIN: {}
|
||||
}) is True
|
||||
# Import flow started
|
||||
assert len(mock_config_entries.flow.mock_calls) == 2
|
||||
|
||||
|
||||
async def test_config_without_host_not_passed_to_config_entry(hass):
|
||||
"""Test that a configuration without a host does not initiate an import."""
|
||||
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch.object(deconz, 'configured_hosts', return_value=[]), \
|
||||
patch.object(deconz, 'load_json', return_value={}):
|
||||
patch.object(deconz, 'configured_hosts', return_value=[]):
|
||||
assert await async_setup_component(hass, deconz.DOMAIN, {
|
||||
deconz.DOMAIN: {}
|
||||
}) is True
|
||||
@@ -62,8 +47,7 @@ async def test_config_already_registered_not_passed_to_config_entry(hass):
|
||||
"""Test that an already registered host does not initiate an import."""
|
||||
with patch.object(hass, 'config_entries') as mock_config_entries, \
|
||||
patch.object(deconz, 'configured_hosts',
|
||||
return_value=['1.2.3.4']), \
|
||||
patch.object(deconz, 'load_json', return_value={}):
|
||||
return_value=['1.2.3.4']):
|
||||
assert await async_setup_component(hass, deconz.DOMAIN, {
|
||||
deconz.DOMAIN: {
|
||||
deconz.CONF_HOST: '1.2.3.4',
|
||||
|
||||
Reference in New Issue
Block a user