Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the GDACS integration."""
2 
3 import logging
4 from typing import Any
5 
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import (
10  CONF_LATITUDE,
11  CONF_LONGITUDE,
12  CONF_RADIUS,
13  CONF_SCAN_INTERVAL,
14 )
15 from homeassistant.helpers import config_validation as cv
16 
17 from .const import CONF_CATEGORIES, DEFAULT_RADIUS, DEFAULT_SCAN_INTERVAL, DOMAIN
18 
19 DATA_SCHEMA = vol.Schema(
20  {vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): cv.positive_int}
21 )
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 class GdacsFlowHandler(ConfigFlow, domain=DOMAIN):
27  """Handle a GDACS config flow."""
28 
29  async def _show_form(
30  self, errors: dict[str, str] | None = None
31  ) -> ConfigFlowResult:
32  """Show the form to the user."""
33  return self.async_show_formasync_show_formasync_show_form(
34  step_id="user", data_schema=DATA_SCHEMA, errors=errors or {}
35  )
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle the start of the config flow."""
41  _LOGGER.debug("User input: %s", user_input)
42  if not user_input:
43  return await self._show_form_show_form()
44 
45  latitude = user_input.get(CONF_LATITUDE, self.hass.config.latitude)
46  user_input[CONF_LATITUDE] = latitude
47  longitude = user_input.get(CONF_LONGITUDE, self.hass.config.longitude)
48  user_input[CONF_LONGITUDE] = longitude
49 
50  identifier = f"{user_input[CONF_LATITUDE]}, {user_input[CONF_LONGITUDE]}"
51 
52  await self.async_set_unique_idasync_set_unique_id(identifier)
53  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
54 
55  scan_interval = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
56  user_input[CONF_SCAN_INTERVAL] = scan_interval.total_seconds()
57 
58  categories = user_input.get(CONF_CATEGORIES, [])
59  user_input[CONF_CATEGORIES] = categories
60 
61  return self.async_create_entryasync_create_entryasync_create_entry(title=identifier, data=user_input)
ConfigFlowResult _show_form(self, dict[str, str]|None errors=None)
Definition: config_flow.py:31
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_step_user(self, dict[str, Any]|None user_input=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)