Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Livisi Home Assistant."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 from typing import Any
7 
8 from aiohttp import ClientConnectorError
9 from aiolivisi import AioLivisi, errors as livisi_errors
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_PASSWORD
14 from homeassistant.helpers import aiohttp_client
15 
16 from .const import DOMAIN, LOGGER
17 
18 
19 class LivisiFlowHandler(ConfigFlow, domain=DOMAIN):
20  """Handle a Livisi Smart Home config flow."""
21 
22  def __init__(self) -> None:
23  """Create the configuration file."""
24  self.aio_livisiaio_livisi: AioLivisi = None
25  self.data_schemadata_schema = vol.Schema(
26  {
27  vol.Required(CONF_HOST): str,
28  vol.Required(CONF_PASSWORD): str,
29  }
30  )
31 
32  async def async_step_user(
33  self, user_input: dict[str, str] | None = None
34  ) -> ConfigFlowResult:
35  """Handle the initial step."""
36  if user_input is None:
37  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=self.data_schemadata_schema)
38 
39  errors = {}
40  try:
41  await self._login_login(user_input)
42  except livisi_errors.WrongCredentialException:
43  errors["base"] = "wrong_password"
44  except livisi_errors.ShcUnreachableException:
45  errors["base"] = "cannot_connect"
46  except livisi_errors.IncorrectIpAddressException:
47  errors["base"] = "wrong_ip_address"
48  else:
49  controller_info: dict[str, Any] = {}
50  with suppress(ClientConnectorError):
51  controller_info = await self.aio_livisiaio_livisi.async_get_controller()
52  if controller_info:
53  return await self.create_entitycreate_entity(user_input, controller_info)
54  errors["base"] = "cannot_connect"
55 
56  return self.async_show_formasync_show_formasync_show_form(
57  step_id="user", data_schema=self.data_schemadata_schema, errors=errors
58  )
59 
60  async def _login(self, user_input: dict[str, str]) -> None:
61  """Login into Livisi Smart Home."""
62  web_session = aiohttp_client.async_get_clientsession(self.hass)
63  self.aio_livisiaio_livisi = AioLivisi(web_session)
64  livisi_connection_data = {
65  "ip_address": user_input[CONF_HOST],
66  "password": user_input[CONF_PASSWORD],
67  }
68 
69  await self.aio_livisiaio_livisi.async_set_token(livisi_connection_data)
70 
71  async def create_entity(
72  self, user_input: dict[str, str], controller_info: dict[str, Any]
73  ) -> ConfigFlowResult:
74  """Create LIVISI entity."""
75  if (controller_data := controller_info.get("gateway")) is None:
76  controller_data = controller_info
77  controller_type = controller_data["controllerType"]
78  LOGGER.debug(
79  "Integrating SHC %s with serial number: %s",
80  controller_type,
81  controller_data["serialNumber"],
82  )
83 
84  return self.async_create_entryasync_create_entryasync_create_entry(
85  title=f"SHC {controller_type}",
86  data={
87  **user_input,
88  },
89  )
ConfigFlowResult create_entity(self, dict[str, str] user_input, dict[str, Any] controller_info)
Definition: config_flow.py:73
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:34
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
Controller|None async_get_controller(HomeAssistant hass, str ip_address, str password, int port, bool ssl)
Definition: config_flow.py:41