Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for azure_event_hub integration."""
2 
3 from __future__ import annotations
4 
5 from copy import deepcopy
6 import logging
7 from typing import Any
8 
9 from azure.eventhub.exceptions import EventHubError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
13 from homeassistant.core import callback
15  SchemaFlowFormStep,
16  SchemaOptionsFlowHandler,
17 )
18 
19 from .client import AzureEventHubClient
20 from .const import (
21  CONF_EVENT_HUB_CON_STRING,
22  CONF_EVENT_HUB_INSTANCE_NAME,
23  CONF_EVENT_HUB_NAMESPACE,
24  CONF_EVENT_HUB_SAS_KEY,
25  CONF_EVENT_HUB_SAS_POLICY,
26  CONF_MAX_DELAY,
27  CONF_SEND_INTERVAL,
28  CONF_USE_CONN_STRING,
29  DEFAULT_OPTIONS,
30  DOMAIN,
31  STEP_CONN_STRING,
32  STEP_SAS,
33  STEP_USER,
34 )
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 BASE_SCHEMA = vol.Schema(
39  {
40  vol.Required(CONF_EVENT_HUB_INSTANCE_NAME): str,
41  vol.Optional(CONF_USE_CONN_STRING, default=False): bool,
42  }
43 )
44 
45 CONN_STRING_SCHEMA = vol.Schema(
46  {
47  vol.Required(CONF_EVENT_HUB_CON_STRING): str,
48  }
49 )
50 
51 SAS_SCHEMA = vol.Schema(
52  {
53  vol.Required(CONF_EVENT_HUB_NAMESPACE): str,
54  vol.Required(CONF_EVENT_HUB_SAS_POLICY): str,
55  vol.Required(CONF_EVENT_HUB_SAS_KEY): str,
56  }
57 )
58 
59 OPTIONS_SCHEMA = vol.Schema(
60  {
61  vol.Required(CONF_SEND_INTERVAL): int,
62  }
63 )
64 OPTIONS_FLOW = {
65  "init": SchemaFlowFormStep(OPTIONS_SCHEMA),
66 }
67 
68 
69 async def validate_data(data: dict[str, Any]) -> dict[str, str] | None:
70  """Validate the input."""
71  client = AzureEventHubClient.from_input(**data)
72  try:
73  await client.test_connection()
74  except EventHubError:
75  return {"base": "cannot_connect"}
76  except Exception:
77  _LOGGER.exception("Unknown error")
78  return {"base": "unknown"}
79  return None
80 
81 
82 class AEHConfigFlow(ConfigFlow, domain=DOMAIN):
83  """Handle a config flow for azure event hub."""
84 
85  VERSION: int = 1
86 
87  def __init__(self) -> None:
88  """Initialize the config flow."""
89  self._data_data: dict[str, Any] = {}
90  self._options: dict[str, Any] = deepcopy(DEFAULT_OPTIONS)
91  self._conn_string_conn_string: bool | None = None
92 
93  @staticmethod
94  @callback
96  config_entry: ConfigEntry,
97  ) -> SchemaOptionsFlowHandler:
98  """Get the options flow for this handler."""
99  return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
100 
101  async def async_step_user(
102  self, user_input: dict[str, Any] | None = None
103  ) -> ConfigFlowResult:
104  """Handle the initial user step."""
105  if user_input is None:
106  return self.async_show_formasync_show_formasync_show_form(step_id=STEP_USER, data_schema=BASE_SCHEMA)
107 
108  self._conn_string_conn_string = user_input.pop(CONF_USE_CONN_STRING)
109  self._data_data = user_input
110 
111  if self._conn_string_conn_string:
112  return await self.async_step_conn_stringasync_step_conn_string()
113  return await self.async_step_sasasync_step_sas()
114 
116  self, user_input: dict[str, Any] | None = None
117  ) -> ConfigFlowResult:
118  """Handle the connection string steps."""
119  errors = await self.async_update_and_validate_dataasync_update_and_validate_data(user_input)
120  if user_input is None or errors is not None:
121  return self.async_show_formasync_show_formasync_show_form(
122  step_id=STEP_CONN_STRING,
123  data_schema=CONN_STRING_SCHEMA,
124  errors=errors,
125  description_placeholders={
126  "event_hub_instance_name": self._data_data[CONF_EVENT_HUB_INSTANCE_NAME]
127  },
128  last_step=True,
129  )
130 
131  return self.async_create_entryasync_create_entryasync_create_entry(
132  title=self._data_data[CONF_EVENT_HUB_INSTANCE_NAME],
133  data=self._data_data,
134  options=self._options,
135  )
136 
137  async def async_step_sas(
138  self, user_input: dict[str, Any] | None = None
139  ) -> ConfigFlowResult:
140  """Handle the sas steps."""
141  errors = await self.async_update_and_validate_dataasync_update_and_validate_data(user_input)
142  if user_input is None or errors is not None:
143  return self.async_show_formasync_show_formasync_show_form(
144  step_id=STEP_SAS,
145  data_schema=SAS_SCHEMA,
146  errors=errors,
147  description_placeholders={
148  "event_hub_instance_name": self._data_data[CONF_EVENT_HUB_INSTANCE_NAME]
149  },
150  last_step=True,
151  )
152 
153  return self.async_create_entryasync_create_entryasync_create_entry(
154  title=self._data_data[CONF_EVENT_HUB_INSTANCE_NAME],
155  data=self._data_data,
156  options=self._options,
157  )
158 
159  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
160  """Import config from configuration.yaml."""
161  if CONF_SEND_INTERVAL in import_data:
162  self._options[CONF_SEND_INTERVAL] = import_data.pop(CONF_SEND_INTERVAL)
163  if CONF_MAX_DELAY in import_data:
164  self._options[CONF_MAX_DELAY] = import_data.pop(CONF_MAX_DELAY)
165  self._data_data = import_data
166  errors = await validate_data(self._data_data)
167  if errors:
168  return self.async_abortasync_abortasync_abort(reason=errors["base"])
169  return self.async_create_entryasync_create_entryasync_create_entry(
170  title=self._data_data[CONF_EVENT_HUB_INSTANCE_NAME],
171  data=self._data_data,
172  options=self._options,
173  )
174 
176  self, user_input: dict[str, Any] | None
177  ) -> dict[str, str] | None:
178  """Validate the input."""
179  if user_input is None:
180  return None
181  self._data_data.update(user_input)
182  return await validate_data(self._data_data)
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:159
dict[str, str]|None async_update_and_validate_data(self, dict[str, Any]|None user_input)
Definition: config_flow.py:177
ConfigFlowResult async_step_conn_string(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:117
ConfigFlowResult async_step_sas(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:139
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:103
SchemaOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:97
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_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)
_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)
dict[str, str]|None validate_data(dict[str, Any] data)
Definition: config_flow.py:69
IssData update(pyiss.ISS iss)
Definition: __init__.py:33