core/homeassistant/components/homematicip_cloud/config_flow.py

98 lines
3.3 KiB
Python
Raw Normal View History

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
2018-07-06 21:05:34 +00:00
"""Config flow to configure HomematicIP Cloud."""
import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.core import callback
from .const import (
DOMAIN as HMIPC_DOMAIN, _LOGGER,
HMIPC_HAPID, HMIPC_AUTHTOKEN, HMIPC_PIN, HMIPC_NAME)
from .hap import HomematicipAuth
@callback
def configured_haps(hass):
"""Return a set of the configured accesspoints."""
return set(entry.data[HMIPC_HAPID] for entry
in hass.config_entries.async_entries(HMIPC_DOMAIN))
@config_entries.HANDLERS.register(HMIPC_DOMAIN)
class HomematicipCloudFlowHandler(data_entry_flow.FlowHandler):
"""Config flow HomematicIP Cloud."""
VERSION = 1
def __init__(self):
"""Initialize HomematicIP Cloud config flow."""
self.auth = None
async def async_step_init(self, user_input=None):
"""Handle a flow start."""
errors = {}
if user_input is not None:
user_input[HMIPC_HAPID] = \
user_input[HMIPC_HAPID].replace('-', '').upper()
if user_input[HMIPC_HAPID] in configured_haps(self.hass):
return self.async_abort(reason='already_configured')
self.auth = HomematicipAuth(self.hass, user_input)
connected = await self.auth.async_setup()
if connected:
_LOGGER.info("Connection established")
return await self.async_step_link()
return self.async_show_form(
step_id='init',
data_schema=vol.Schema({
vol.Required(HMIPC_HAPID): str,
vol.Optional(HMIPC_PIN): str,
vol.Optional(HMIPC_NAME): str,
}),
errors=errors
)
async def async_step_link(self, user_input=None):
"""Attempt to link with the HomematicIP Cloud accesspoint."""
errors = {}
pressed = await self.auth.async_checkbutton()
if pressed:
authtoken = await self.auth.async_register()
if authtoken:
_LOGGER.info("Write config entry")
return self.async_create_entry(
title=self.auth.config.get(HMIPC_HAPID),
data={
HMIPC_HAPID: self.auth.config.get(HMIPC_HAPID),
HMIPC_AUTHTOKEN: authtoken,
HMIPC_NAME: self.auth.config.get(HMIPC_NAME)
})
return self.async_abort(reason='conection_aborted')
else:
errors['base'] = 'press_the_button'
return self.async_show_form(step_id='link', errors=errors)
async def async_step_import(self, import_info):
"""Import a new bridge as a config entry."""
hapid = import_info[HMIPC_HAPID]
authtoken = import_info[HMIPC_AUTHTOKEN]
name = import_info[HMIPC_NAME]
hapid = hapid.replace('-', '').upper()
if hapid in configured_haps(self.hass):
return self.async_abort(reason='already_configured')
_LOGGER.info('Imported authentication for %s', hapid)
return self.async_create_entry(
title=hapid,
data={
HMIPC_HAPID: hapid,
HMIPC_AUTHTOKEN: authtoken,
HMIPC_NAME: name
}
)