Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Rainforest Eagle integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST, CONF_TYPE
12 
13 from .const import CONF_CLOUD_ID, CONF_HARDWARE_ADDRESS, CONF_INSTALL_CODE, DOMAIN
14 from .data import CannotConnect, InvalidAuth, async_get_type
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
19 def create_schema(user_input: dict[str, Any] | None) -> vol.Schema:
20  """Create user schema with passed in defaults if available."""
21  if user_input is None:
22  user_input = {}
23  return vol.Schema(
24  {
25  vol.Required(CONF_HOST, default=user_input.get(CONF_HOST)): str,
26  vol.Required(CONF_CLOUD_ID, default=user_input.get(CONF_CLOUD_ID)): str,
27  vol.Required(
28  CONF_INSTALL_CODE, default=user_input.get(CONF_INSTALL_CODE)
29  ): str,
30  }
31  )
32 
33 
34 class RainforestEagleConfigFlow(ConfigFlow, domain=DOMAIN):
35  """Handle a config flow for Rainforest Eagle."""
36 
37  VERSION = 1
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle the initial step."""
43  if user_input is None:
44  return self.async_show_formasync_show_formasync_show_form(
45  step_id="user", data_schema=create_schema(user_input)
46  )
47 
48  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_CLOUD_ID])
49  errors = {}
50 
51  try:
52  eagle_type, hardware_address = await async_get_type(
53  self.hass,
54  user_input[CONF_CLOUD_ID],
55  user_input[CONF_INSTALL_CODE],
56  user_input[CONF_HOST],
57  )
58  except CannotConnect:
59  errors["base"] = "cannot_connect"
60  except InvalidAuth:
61  errors["base"] = "invalid_auth"
62  except Exception:
63  _LOGGER.exception("Unexpected exception")
64  errors["base"] = "unknown"
65  else:
66  user_input[CONF_TYPE] = eagle_type
67  user_input[CONF_HARDWARE_ADDRESS] = hardware_address
68  return self.async_create_entryasync_create_entryasync_create_entry(
69  title=user_input[CONF_CLOUD_ID], data=user_input
70  )
71 
72  return self.async_show_formasync_show_formasync_show_form(
73  step_id="user", data_schema=create_schema(user_input), errors=errors
74  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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)
vol.Schema create_schema(dict[str, Any]|None user_input)
Definition: config_flow.py:19
def async_get_type(hass, cloud_id, install_code, host)
Definition: data.py:35