Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the dwd_weather_warnings integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from dwdwfsapi import DwdWeatherWarningsAPI
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.helpers import entity_registry as er
13 from homeassistant.helpers.selector import EntitySelector, EntitySelectorConfig
14 
15 from .const import CONF_REGION_DEVICE_TRACKER, CONF_REGION_IDENTIFIER, DOMAIN
16 from .exceptions import EntityNotFoundError
17 from .util import get_position_data
18 
19 EXCLUSIVE_OPTIONS = (CONF_REGION_IDENTIFIER, CONF_REGION_DEVICE_TRACKER)
20 
21 
23  """Handle the config flow for the dwd_weather_warnings integration."""
24 
25  VERSION = 1
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Handle the initial step."""
31  errors: dict = {}
32 
33  if user_input is not None:
34  # Check, if either CONF_REGION_IDENTIFIER or CONF_GPS_TRACKER has been set.
35  if all(k not in user_input for k in EXCLUSIVE_OPTIONS):
36  errors["base"] = "no_identifier"
37  elif all(k in user_input for k in EXCLUSIVE_OPTIONS):
38  errors["base"] = "ambiguous_identifier"
39  elif CONF_REGION_IDENTIFIER in user_input:
40  # Validate region identifier using the API
41  identifier = user_input[CONF_REGION_IDENTIFIER]
42 
43  if not await self.hass.async_add_executor_job(
44  DwdWeatherWarningsAPI, identifier
45  ):
46  errors["base"] = "invalid_identifier"
47 
48  if not errors:
49  # Set the unique ID for this config entry.
50  await self.async_set_unique_idasync_set_unique_id(identifier)
51  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
52 
53  return self.async_create_entryasync_create_entryasync_create_entry(title=identifier, data=user_input)
54  else: # CONF_REGION_DEVICE_TRACKER
55  device_tracker = user_input[CONF_REGION_DEVICE_TRACKER]
56  registry = er.async_get(self.hass)
57  entity_entry = registry.async_get(device_tracker)
58 
59  if entity_entry is None:
60  errors["base"] = "entity_not_found"
61  else:
62  try:
63  position = get_position_data(self.hass, entity_entry.id)
64  except EntityNotFoundError:
65  errors["base"] = "entity_not_found"
66  except AttributeError:
67  errors["base"] = "attribute_not_found"
68  else:
69  # Validate position using the API
70  if not await self.hass.async_add_executor_job(
71  DwdWeatherWarningsAPI, position
72  ):
73  errors["base"] = "invalid_identifier"
74 
75  # Position is valid here, because the API call was successful.
76  if not errors and position is not None and entity_entry is not None:
77  # Set the unique ID for this config entry.
78  await self.async_set_unique_idasync_set_unique_id(entity_entry.id)
79  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
80 
81  # Replace entity ID with registry ID for more stability.
82  user_input[CONF_REGION_DEVICE_TRACKER] = entity_entry.id
83 
84  return self.async_create_entryasync_create_entryasync_create_entry(
85  title=device_tracker.removeprefix("device_tracker."),
86  data=user_input,
87  )
88 
89  return self.async_show_formasync_show_formasync_show_form(
90  step_id="user",
91  errors=errors,
92  data_schema=vol.Schema(
93  {
94  vol.Optional(CONF_REGION_IDENTIFIER): cv.string,
95  vol.Optional(CONF_REGION_DEVICE_TRACKER): EntitySelector(
96  EntitySelectorConfig(domain="device_tracker")
97  ),
98  }
99  ),
100  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
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)
tuple[float, float]|None get_position_data(HomeAssistant hass, str registry_id)
Definition: util.py:14