Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Azure Data Explorer integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from azure.kusto.data.exceptions import KustoAuthenticationError, KustoServiceError
9 import voluptuous as vol
10 
11 from homeassistant import config_entries
12 from homeassistant.config_entries import ConfigFlowResult
13 from homeassistant.helpers.selector import BooleanSelector
14 
15 from . import AzureDataExplorerClient
16 from .const import (
17  CONF_ADX_CLUSTER_INGEST_URI,
18  CONF_ADX_DATABASE_NAME,
19  CONF_ADX_TABLE_NAME,
20  CONF_APP_REG_ID,
21  CONF_APP_REG_SECRET,
22  CONF_AUTHORITY_ID,
23  CONF_USE_QUEUED_CLIENT,
24  DEFAULT_OPTIONS,
25  DOMAIN,
26 )
27 
28 _LOGGER = logging.getLogger(__name__)
29 
30 STEP_USER_DATA_SCHEMA = vol.Schema(
31  {
32  vol.Required(CONF_ADX_CLUSTER_INGEST_URI): str,
33  vol.Required(CONF_ADX_DATABASE_NAME): str,
34  vol.Required(CONF_ADX_TABLE_NAME): str,
35  vol.Required(CONF_APP_REG_ID): str,
36  vol.Required(CONF_APP_REG_SECRET): str,
37  vol.Required(CONF_AUTHORITY_ID): str,
38  vol.Required(CONF_USE_QUEUED_CLIENT, default=False): BooleanSelector(),
39  }
40 )
41 
42 
44  """Handle a config flow for Azure Data Explorer."""
45 
46  VERSION = 1
47 
48  async def validate_input(self, data: dict[str, Any]) -> dict[str, Any] | None:
49  """Validate the user input allows us to connect.
50 
51  Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
52  """
53  client = AzureDataExplorerClient(data)
54 
55  try:
56  await self.hass.async_add_executor_job(client.test_connection)
57 
58  except KustoAuthenticationError as exp:
59  _LOGGER.error(exp)
60  return {"base": "invalid_auth"}
61 
62  except KustoServiceError as exp:
63  _LOGGER.error(exp)
64  return {"base": "cannot_connect"}
65 
66  return None
67 
68  async def async_step_user(
69  self, user_input: dict[str, Any] | None = None
70  ) -> ConfigFlowResult:
71  """Handle the initial step."""
72 
73  errors: dict = {}
74  if user_input:
75  errors = await self.validate_inputvalidate_input(user_input) # type: ignore[assignment]
76  if not errors:
77  return self.async_create_entryasync_create_entryasync_create_entry(
78  data=user_input,
79  title=user_input[CONF_ADX_CLUSTER_INGEST_URI].replace(
80  "https://", ""
81  ),
82  options=DEFAULT_OPTIONS,
83  )
84  return self.async_show_formasync_show_formasync_show_form(
85  step_id="user",
86  data_schema=STEP_USER_DATA_SCHEMA,
87  errors=errors,
88  last_step=True,
89  )
dict[str, Any]|None validate_input(self, dict[str, Any] data)
Definition: config_flow.py:48
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:70
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)