2018-08-21 19:25:16 +00:00
|
|
|
"""Config flow to configure the HomematicIP Cloud component."""
|
2019-11-25 13:17:14 +00:00
|
|
|
from typing import Any, Dict, Set
|
2019-04-25 22:13:07 +00:00
|
|
|
|
2018-07-06 21:05:34 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
from homeassistant import config_entries
|
2019-10-23 16:21:49 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2018-07-06 21:05:34 +00:00
|
|
|
|
2019-02-14 15:01:46 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER,
|
|
|
|
DOMAIN as HMIPC_DOMAIN,
|
|
|
|
HMIPC_AUTHTOKEN,
|
|
|
|
HMIPC_HAPID,
|
|
|
|
HMIPC_NAME,
|
|
|
|
HMIPC_PIN,
|
|
|
|
)
|
2018-07-06 21:05:34 +00:00
|
|
|
from .hap import HomematicipAuth
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2019-10-23 16:21:49 +00:00
|
|
|
def configured_haps(hass: HomeAssistantType) -> Set[str]:
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Return a set of the configured access points."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return set(
|
|
|
|
entry.data[HMIPC_HAPID]
|
|
|
|
for entry in hass.config_entries.async_entries(HMIPC_DOMAIN)
|
|
|
|
)
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@config_entries.HANDLERS.register(HMIPC_DOMAIN)
|
2018-09-17 08:12:46 +00:00
|
|
|
class HomematicipCloudFlowHandler(config_entries.ConfigFlow):
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Config flow for the HomematicIP Cloud component."""
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
2018-09-17 08:12:46 +00:00
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH
|
2018-07-06 21:05:34 +00:00
|
|
|
|
2019-11-25 13:17:14 +00:00
|
|
|
def __init__(self) -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Initialize HomematicIP Cloud config flow."""
|
|
|
|
self.auth = None
|
|
|
|
|
2019-11-25 13:17:14 +00:00
|
|
|
async def async_step_user(self, user_input=None) -> Dict[str, Any]:
|
2018-08-09 11:24:14 +00:00
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
return await self.async_step_init(user_input)
|
|
|
|
|
2019-11-25 13:17:14 +00:00
|
|
|
async def async_step_init(self, user_input=None) -> Dict[str, Any]:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Handle a flow start."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
user_input[HMIPC_HAPID] = user_input[HMIPC_HAPID].replace("-", "").upper()
|
2018-07-06 21:05:34 +00:00
|
|
|
if user_input[HMIPC_HAPID] in configured_haps(self.hass):
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="already_configured")
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
self.auth = HomematicipAuth(self.hass, user_input)
|
|
|
|
connected = await self.auth.async_setup()
|
|
|
|
if connected:
|
2018-08-21 19:25:16 +00:00
|
|
|
_LOGGER.info("Connection to HomematicIP Cloud established")
|
2018-07-06 21:05:34 +00:00
|
|
|
return await self.async_step_link()
|
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="init",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(HMIPC_HAPID): str,
|
|
|
|
vol.Optional(HMIPC_NAME): str,
|
|
|
|
vol.Optional(HMIPC_PIN): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors,
|
2018-07-06 21:05:34 +00:00
|
|
|
)
|
|
|
|
|
2019-11-25 13:17:14 +00:00
|
|
|
async def async_step_link(self, user_input=None) -> Dict[str, Any]:
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Attempt to link with the HomematicIP Cloud access point."""
|
2018-07-06 21:05:34 +00:00
|
|
|
errors = {}
|
|
|
|
|
|
|
|
pressed = await self.auth.async_checkbutton()
|
|
|
|
if pressed:
|
|
|
|
authtoken = await self.auth.async_register()
|
|
|
|
if authtoken:
|
2018-08-21 19:25:16 +00:00
|
|
|
_LOGGER.info("Write config entry for HomematicIP Cloud")
|
2018-07-06 21:05:34 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title=self.auth.config.get(HMIPC_HAPID),
|
|
|
|
data={
|
|
|
|
HMIPC_HAPID: self.auth.config.get(HMIPC_HAPID),
|
|
|
|
HMIPC_AUTHTOKEN: authtoken,
|
2019-07-31 19:25:30 +00:00
|
|
|
HMIPC_NAME: self.auth.config.get(HMIPC_NAME),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return self.async_abort(reason="connection_aborted")
|
|
|
|
errors["base"] = "press_the_button"
|
2018-07-06 21:05:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="link", errors=errors)
|
2018-07-06 21:05:34 +00:00
|
|
|
|
2019-11-25 13:17:14 +00:00
|
|
|
async def async_step_import(self, import_info) -> Dict[str, Any]:
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Import a new access point as a config entry."""
|
2018-07-06 21:05:34 +00:00
|
|
|
hapid = import_info[HMIPC_HAPID]
|
|
|
|
authtoken = import_info[HMIPC_AUTHTOKEN]
|
|
|
|
name = import_info[HMIPC_NAME]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hapid = hapid.replace("-", "").upper()
|
2018-07-06 21:05:34 +00:00
|
|
|
if hapid in configured_haps(self.hass):
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="already_configured")
|
2018-07-06 21:05:34 +00:00
|
|
|
|
2018-08-21 19:25:16 +00:00
|
|
|
_LOGGER.info("Imported authentication for %s", hapid)
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=hapid,
|
2019-07-31 19:25:30 +00:00
|
|
|
data={HMIPC_AUTHTOKEN: authtoken, HMIPC_HAPID: hapid, HMIPC_NAME: name},
|
2018-07-06 21:05:34 +00:00
|
|
|
)
|