2019-07-14 10:30:23 +00:00
|
|
|
"""Config flow to configure the Twente Milieu integration."""
|
2021-03-05 23:33:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2019-07-14 10:30:23 +00:00
|
|
|
from twentemilieu import (
|
|
|
|
TwenteMilieu,
|
|
|
|
TwenteMilieuAddressError,
|
|
|
|
TwenteMilieuConnectionError,
|
|
|
|
)
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.config_entries import ConfigFlow
|
|
|
|
from homeassistant.const import CONF_ID
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2021-03-05 23:33:26 +00:00
|
|
|
from .const import CONF_HOUSE_LETTER, CONF_HOUSE_NUMBER, CONF_POST_CODE
|
|
|
|
from .const import DOMAIN # pylint: disable=unused-import
|
|
|
|
|
2019-07-14 10:30:23 +00:00
|
|
|
|
2021-03-05 23:33:26 +00:00
|
|
|
class TwenteMilieuFlowHandler(ConfigFlow, domain=DOMAIN):
|
2019-07-14 10:30:23 +00:00
|
|
|
"""Handle a Twente Milieu config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
|
|
|
|
2021-03-05 23:33:26 +00:00
|
|
|
async def _show_setup_form(
|
|
|
|
self, errors: dict[str, str] | None = None
|
|
|
|
) -> dict[str, Any]:
|
2019-07-14 10:30:23 +00:00
|
|
|
"""Show the setup form to the user."""
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_POST_CODE): str,
|
|
|
|
vol.Required(CONF_HOUSE_NUMBER): str,
|
|
|
|
vol.Optional(CONF_HOUSE_LETTER): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors or {},
|
|
|
|
)
|
|
|
|
|
2021-03-05 23:33:26 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> dict[str, Any]:
|
2019-07-14 10:30:23 +00:00
|
|
|
"""Handle a flow initiated by the user."""
|
|
|
|
if user_input is None:
|
|
|
|
return await self._show_setup_form(user_input)
|
|
|
|
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
session = async_get_clientsession(self.hass)
|
|
|
|
|
|
|
|
twentemilieu = TwenteMilieu(
|
|
|
|
post_code=user_input[CONF_POST_CODE],
|
|
|
|
house_number=user_input[CONF_HOUSE_NUMBER],
|
|
|
|
house_letter=user_input.get(CONF_HOUSE_LETTER),
|
|
|
|
session=session,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
unique_id = await twentemilieu.unique_id()
|
|
|
|
except TwenteMilieuConnectionError:
|
2020-10-11 12:54:08 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
2019-07-14 10:30:23 +00:00
|
|
|
return await self._show_setup_form(errors)
|
|
|
|
except TwenteMilieuAddressError:
|
|
|
|
errors["base"] = "invalid_address"
|
|
|
|
return await self._show_setup_form(errors)
|
|
|
|
|
|
|
|
entries = self._async_current_entries()
|
|
|
|
for entry in entries:
|
|
|
|
if entry.data[CONF_ID] == unique_id:
|
2020-10-11 12:54:08 +00:00
|
|
|
return self.async_abort(reason="already_configured")
|
2019-07-14 10:30:23 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
2021-03-05 23:33:26 +00:00
|
|
|
title=str(unique_id),
|
2019-07-14 10:30:23 +00:00
|
|
|
data={
|
|
|
|
CONF_ID: unique_id,
|
|
|
|
CONF_POST_CODE: user_input[CONF_POST_CODE],
|
|
|
|
CONF_HOUSE_NUMBER: user_input[CONF_HOUSE_NUMBER],
|
|
|
|
CONF_HOUSE_LETTER: user_input.get(CONF_HOUSE_LETTER),
|
|
|
|
},
|
|
|
|
)
|