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.config_entries import ConfigFlow
|
|
|
|
from homeassistant.const import CONF_ID
|
2021-04-29 11:40:51 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2019-07-14 10:30:23 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2021-03-30 04:02:56 +00:00
|
|
|
from .const import CONF_HOUSE_LETTER, CONF_HOUSE_NUMBER, CONF_POST_CODE, DOMAIN
|
2021-03-05 23:33:26 +00:00
|
|
|
|
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
|
|
|
|
|
2021-03-05 23:33:26 +00:00
|
|
|
async def _show_setup_form(
|
|
|
|
self, errors: dict[str, str] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
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
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
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],
|
2021-11-13 11:22:07 +00:00
|
|
|
house_letter=user_input.get(CONF_HOUSE_LETTER, ""),
|
2019-07-14 10:30:23 +00:00
|
|
|
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)
|
|
|
|
|
2021-11-13 14:34:09 +00:00
|
|
|
await self.async_set_unique_id(str(unique_id))
|
|
|
|
self._abort_if_unique_id_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),
|
|
|
|
},
|
|
|
|
)
|