Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Canary."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any, Final
7 
8 from canary.api import Api
9 from requests.exceptions import ConnectTimeout, HTTPError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import (
13  ConfigEntry,
14  ConfigFlow,
15  ConfigFlowResult,
16  OptionsFlow,
17 )
18 from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
19 from homeassistant.core import HomeAssistant, callback
20 
21 from .const import (
22  CONF_FFMPEG_ARGUMENTS,
23  DEFAULT_FFMPEG_ARGUMENTS,
24  DEFAULT_TIMEOUT,
25  DOMAIN,
26 )
27 
28 _LOGGER: Final = logging.getLogger(__name__)
29 
30 
31 def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> bool:
32  """Validate the user input allows us to connect.
33 
34  Data has the keys from DATA_SCHEMA with values provided by the user.
35  """
36  # constructor does login call
37  Api(
38  data[CONF_USERNAME],
39  data[CONF_PASSWORD],
40  data.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
41  )
42 
43  return True
44 
45 
46 class CanaryConfigFlow(ConfigFlow, domain=DOMAIN):
47  """Handle a config flow for Canary."""
48 
49  VERSION = 1
50 
51  @staticmethod
52  @callback
53  def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
54  """Get the options flow for this handler."""
56 
57  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
58  """Handle a flow initiated by configuration file."""
59  return await self.async_step_userasync_step_userasync_step_user(import_data)
60 
61  async def async_step_user(
62  self, user_input: dict[str, Any] | None = None
63  ) -> ConfigFlowResult:
64  """Handle a flow initiated by the user."""
65  errors = {}
66  default_username = ""
67 
68  if user_input is not None:
69  if CONF_TIMEOUT not in user_input:
70  user_input[CONF_TIMEOUT] = DEFAULT_TIMEOUT
71 
72  default_username = user_input[CONF_USERNAME]
73 
74  try:
75  await self.hass.async_add_executor_job(
76  validate_input, self.hass, user_input
77  )
78  except (ConnectTimeout, HTTPError):
79  errors["base"] = "cannot_connect"
80  except Exception:
81  _LOGGER.exception("Unexpected exception")
82  return self.async_abortasync_abortasync_abort(reason="unknown")
83  else:
84  return self.async_create_entryasync_create_entryasync_create_entry(
85  title=user_input[CONF_USERNAME],
86  data=user_input,
87  )
88 
89  data_schema = {
90  vol.Required(CONF_USERNAME, default=default_username): str,
91  vol.Required(CONF_PASSWORD): str,
92  }
93 
94  return self.async_show_formasync_show_formasync_show_form(
95  step_id="user",
96  data_schema=vol.Schema(data_schema),
97  errors=errors or {},
98  )
99 
100 
102  """Handle Canary client options."""
103 
104  async def async_step_init(
105  self, user_input: dict[str, Any] | None = None
106  ) -> ConfigFlowResult:
107  """Manage Canary options."""
108  if user_input is not None:
109  return self.async_create_entryasync_create_entry(title="", data=user_input)
110 
111  options = {
112  vol.Optional(
113  CONF_FFMPEG_ARGUMENTS,
114  default=self.config_entryconfig_entryconfig_entry.options.get(
115  CONF_FFMPEG_ARGUMENTS, DEFAULT_FFMPEG_ARGUMENTS
116  ),
117  ): str,
118  vol.Optional(
119  CONF_TIMEOUT,
120  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
121  ): int,
122  }
123 
124  return self.async_show_formasync_show_form(step_id="init", data_schema=vol.Schema(options))
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:53
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:57
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:63
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:106
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_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
None config_entry(self, ConfigEntry value)
_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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
bool validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:31