Add HomematicIP Cloud Config Flow and Entries loading (#14861)

* Add HomematicIP Cloud to config flow

* Inititial trial for config_flow

* Integrations text files

* Load and write config_flow and init homematicip_cloud

* Split into dedicated files

* Ceanup of text messages

* Working config_flow

* Move imports inside a function

* Enable laoding even no accesspoints are defined

* Revert unnecassary changes in CONFIG_SCHEMA

* Better error handling

* fix flask8

* Migration to async for token generation

* A few fixes

* Simplify config_flow

* Bump version to 9.6 with renamed package

* Requirements file

* First fixes after review

* Implement async_step_import

* Cleanup for Config Flow

* First tests for homematicip_cloud setup

* Remove config_flow tests

* Really remove all things

* Fix comment

* Update picture

* Add support for async_setup_entry to switch and climate platform

* Update path of the config_flow picture

* Refactoring for better tesability

* Further tests implemented

* Move 3th party lib inside function

* Fix lint

* Update requirments_test_all.txt file

* UPdate of requirments_test_all.txt did not work

* Furder cleanup in websocket connection

* Remove a test for the hap

* Revert "Remove a test for the hap"

This reverts commit 968d58cba108e0f371022c7ab540374aa2ab13f4.

* First tests implemented for config_flow

* Fix lint

* Rework of client registration process

* Implemented tests for config_flow 100% coverage

* Cleanup

* Cleanup comments and code

* Try to fix import problem

* Add homematicip to the test env requirements
This commit is contained in:
Mattias Welponer authored and Paulus Schoutsen committed 2018-07-06 17:05:34 -04:00
1 parent 0f1bcfd63b
commit 9970965718
23 files changed
+1020 -293

No files matched your search

@@ -0,0 +1 @@
"""Tests for the HomematicIP Cloud component."""
@@ -0,0 +1,150 @@
"""Tests for HomematicIP Cloud config flow."""
from unittest.mock import patch
from homeassistant.components.homematicip_cloud import hap as hmipc
from homeassistant.components.homematicip_cloud import config_flow, const
from tests.common import MockConfigEntry, mock_coro
async def test_flow_works(hass):
"""Test config flow works."""
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
hap = hmipc.HomematicipAuth(hass, config)
with patch.object(hap, 'get_auth', return_value=mock_coro()), \
patch.object(hmipc.HomematicipAuth, 'async_checkbutton',
return_value=mock_coro(True)), \
patch.object(hmipc.HomematicipAuth, 'async_register',
return_value=mock_coro(True)):
hap.authtoken = 'ABC'
result = await flow.async_step_init(user_input=config)
assert hap.authtoken == 'ABC'
assert result['type'] == 'create_entry'
async def test_flow_init_connection_error(hass):
"""Test config flow with accesspoint connection error."""
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
with patch.object(hmipc.HomematicipAuth, 'async_setup',
return_value=mock_coro(False)):
result = await flow.async_step_init(user_input=config)
assert result['type'] == 'form'
async def test_flow_link_connection_error(hass):
"""Test config flow client registration connection error."""
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
with patch.object(hmipc.HomematicipAuth, 'async_setup',
return_value=mock_coro(True)), \
patch.object(hmipc.HomematicipAuth, 'async_checkbutton',
return_value=mock_coro(True)), \
patch.object(hmipc.HomematicipAuth, 'async_register',
return_value=mock_coro(False)):
result = await flow.async_step_init(user_input=config)
assert result['type'] == 'abort'
async def test_flow_link_press_button(hass):
"""Test config flow ask for pressing the blue button."""
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
with patch.object(hmipc.HomematicipAuth, 'async_setup',
return_value=mock_coro(True)), \
patch.object(hmipc.HomematicipAuth, 'async_checkbutton',
return_value=mock_coro(False)):
result = await flow.async_step_init(user_input=config)
assert result['type'] == 'form'
assert result['errors'] == {'base': 'press_the_button'}
async def test_init_flow_show_form(hass):
"""Test config flow shows up with a form."""
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
result = await flow.async_step_init(user_input=None)
assert result['type'] == 'form'
async def test_init_already_configured(hass):
"""Test accesspoint is already configured."""
MockConfigEntry(domain=const.DOMAIN, data={
const.HMIPC_HAPID: 'ABC123',
}).add_to_hass(hass)
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
result = await flow.async_step_init(user_input=config)
assert result['type'] == 'abort'
async def test_import_config(hass):
"""Test importing a host with an existing config file."""
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
result = await flow.async_step_import({
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip'
})
assert result['type'] == 'create_entry'
assert result['title'] == 'ABC123'
assert result['data'] == {
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip'
}
async def test_import_existing_config(hass):
"""Test abort of an existing accesspoint from config."""
flow = config_flow.HomematicipCloudFlowHandler()
flow.hass = hass
MockConfigEntry(domain=const.DOMAIN, data={
hmipc.HMIPC_HAPID: 'ABC123',
}).add_to_hass(hass)
result = await flow.async_step_import({
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip'
})
assert result['type'] == 'abort'
@@ -0,0 +1,113 @@
"""Test HomematicIP Cloud accesspoint."""
from unittest.mock import Mock, patch
from homeassistant.components.homematicip_cloud import hap as hmipc
from homeassistant.components.homematicip_cloud import const, errors
from tests.common import mock_coro
async def test_auth_setup(hass):
"""Test auth setup for client registration."""
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
hap = hmipc.HomematicipAuth(hass, config)
with patch.object(hap, 'get_auth', return_value=mock_coro()):
assert await hap.async_setup() is True
async def test_auth_setup_connection_error(hass):
"""Test auth setup connection error behaviour."""
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
hap = hmipc.HomematicipAuth(hass, config)
with patch.object(hap, 'get_auth',
side_effect=errors.HmipcConnectionError):
assert await hap.async_setup() is False
async def test_auth_auth_check_and_register(hass):
"""Test auth client registration."""
config = {
const.HMIPC_HAPID: 'ABC123',
const.HMIPC_PIN: '123',
const.HMIPC_NAME: 'hmip',
}
hap = hmipc.HomematicipAuth(hass, config)
hap.auth = Mock()
with patch.object(hap.auth, 'isRequestAcknowledged',
return_value=mock_coro()), \
patch.object(hap.auth, 'requestAuthToken',
return_value=mock_coro('ABC')), \
patch.object(hap.auth, 'confirmAuthToken',
return_value=mock_coro()):
assert await hap.async_checkbutton() is True
assert await hap.async_register() == 'ABC'
async def test_hap_setup_works(aioclient_mock):
"""Test a successful setup of a accesspoint."""
hass = Mock()
entry = Mock()
home = Mock()
entry.data = {
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip',
}
hap = hmipc.HomematicipHAP(hass, entry)
with patch.object(hap, 'get_hap', return_value=mock_coro(home)):
assert await hap.async_setup() is True
assert hap.home is home
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 5
assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == \
(entry, 'binary_sensor')
async def test_hap_setup_connection_error():
"""Test a failed accesspoint setup."""
hass = Mock()
entry = Mock()
entry.data = {
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip',
}
hap = hmipc.HomematicipHAP(hass, entry)
with patch.object(hap, 'get_hap',
side_effect=errors.HmipcConnectionError):
assert await hap.async_setup() is False
assert len(hass.async_add_job.mock_calls) == 0
assert len(hass.config_entries.flow.async_init.mock_calls) == 0
async def test_hap_reset_unloads_entry_if_setup():
"""Test calling reset while the entry has been setup."""
hass = Mock()
entry = Mock()
home = Mock()
entry.data = {
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip',
}
hap = hmipc.HomematicipHAP(hass, entry)
with patch.object(hap, 'get_hap', return_value=mock_coro(home)):
assert await hap.async_setup() is True
assert hap.home is home
assert len(hass.services.async_register.mock_calls) == 0
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 5
hass.config_entries.async_forward_entry_unload.return_value = \
mock_coro(True)
await hap.async_reset()
assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 5
@@ -0,0 +1,103 @@
"""Test HomematicIP Cloud setup process."""
from unittest.mock import patch
from homeassistant.setup import async_setup_component
from homeassistant.components import homematicip_cloud as hmipc
from tests.common import mock_coro, MockConfigEntry
async def test_config_with_accesspoint_passed_to_config_entry(hass):
"""Test that config for a accesspoint are loaded via config entry."""
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hmipc, 'configured_haps', return_value=[]):
assert await async_setup_component(hass, hmipc.DOMAIN, {
hmipc.DOMAIN: {
hmipc.CONF_ACCESSPOINT: 'ABC123',
hmipc.CONF_AUTHTOKEN: '123',
hmipc.CONF_NAME: 'name',
}
}) is True
# Flow started for the access point
assert len(mock_config_entries.flow.mock_calls) == 2
async def test_config_already_registered_not_passed_to_config_entry(hass):
"""Test that an already registered accesspoint does not get imported."""
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hmipc, 'configured_haps', return_value=['ABC123']):
assert await async_setup_component(hass, hmipc.DOMAIN, {
hmipc.DOMAIN: {
hmipc.CONF_ACCESSPOINT: 'ABC123',
hmipc.CONF_AUTHTOKEN: '123',
hmipc.CONF_NAME: 'name',
}
}) is True
# No flow started
assert len(mock_config_entries.flow.mock_calls) == 0
async def test_setup_entry_successful(hass):
"""Test setup entry is successful."""
entry = MockConfigEntry(domain=hmipc.DOMAIN, data={
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip',
})
entry.add_to_hass(hass)
with patch.object(hmipc, 'HomematicipHAP') as mock_hap:
mock_hap.return_value.async_setup.return_value = mock_coro(True)
assert await async_setup_component(hass, hmipc.DOMAIN, {
hmipc.DOMAIN: {
hmipc.CONF_ACCESSPOINT: 'ABC123',
hmipc.CONF_AUTHTOKEN: '123',
hmipc.CONF_NAME: 'hmip',
}
}) is True
assert len(mock_hap.mock_calls) == 2
async def test_setup_defined_accesspoint(hass):
"""Test we initiate config entry for the accesspoint."""
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(hmipc, 'configured_haps', return_value=[]):
mock_config_entries.flow.async_init.return_value = mock_coro()
assert await async_setup_component(hass, hmipc.DOMAIN, {
hmipc.DOMAIN: {
hmipc.CONF_ACCESSPOINT: 'ABC123',
hmipc.CONF_AUTHTOKEN: '123',
hmipc.CONF_NAME: 'hmip',
}
}) is True
assert len(mock_config_entries.flow.mock_calls) == 1
assert mock_config_entries.flow.mock_calls[0][2]['data'] == {
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip',
}
async def test_unload_entry(hass):
"""Test being able to unload an entry."""
entry = MockConfigEntry(domain=hmipc.DOMAIN, data={
hmipc.HMIPC_HAPID: 'ABC123',
hmipc.HMIPC_AUTHTOKEN: '123',
hmipc.HMIPC_NAME: 'hmip',
})
entry.add_to_hass(hass)
with patch.object(hmipc, 'HomematicipHAP') as mock_hap:
mock_hap.return_value.async_setup.return_value = mock_coro(True)
assert await async_setup_component(hass, hmipc.DOMAIN, {}) is True
assert len(mock_hap.return_value.mock_calls) == 1
mock_hap.return_value.async_reset.return_value = mock_coro(True)
assert await hmipc.async_unload_entry(hass, entry)
assert len(mock_hap.return_value.async_reset.mock_calls) == 1
assert hass.data[hmipc.DOMAIN] == {}