Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the GeoJSON events integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import (
12  CONF_LATITUDE,
13  CONF_LOCATION,
14  CONF_LONGITUDE,
15  CONF_RADIUS,
16  CONF_URL,
17  UnitOfLength,
18 )
19 from homeassistant.helpers import config_validation as cv, selector
20 from homeassistant.util.unit_conversion import DistanceConverter
21 
22 from .const import DEFAULT_RADIUS_IN_M, DOMAIN
23 
24 DATA_SCHEMA = vol.Schema(
25  {
26  vol.Required(CONF_URL): cv.string,
27  vol.Required(CONF_LOCATION): selector.LocationSelector(
28  selector.LocationSelectorConfig(radius=True, icon="")
29  ),
30  }
31 )
32 
33 
34 class GeoJsonEventsFlowHandler(ConfigFlow, domain=DOMAIN):
35  """Handle a GeoJSON events config flow."""
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  if not user_input:
42  suggested_values: Mapping[str, Any] = {
43  CONF_LOCATION: {
44  CONF_LATITUDE: self.hass.config.latitude,
45  CONF_LONGITUDE: self.hass.config.longitude,
46  CONF_RADIUS: DEFAULT_RADIUS_IN_M,
47  }
48  }
49  data_schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
50  DATA_SCHEMA, suggested_values
51  )
52  return self.async_show_formasync_show_formasync_show_form(
53  step_id="user",
54  data_schema=data_schema,
55  )
56 
57  url: str = user_input[CONF_URL]
58  location: dict[str, Any] = user_input[CONF_LOCATION]
59  latitude: float = location[CONF_LATITUDE]
60  longitude: float = location[CONF_LONGITUDE]
61  self._async_abort_entries_match_async_abort_entries_match(
62  {
63  CONF_URL: url,
64  CONF_LATITUDE: latitude,
65  CONF_LONGITUDE: longitude,
66  }
67  )
68  return self.async_create_entryasync_create_entryasync_create_entry(
69  title=f"{url} ({latitude}, {longitude})",
70  data={
71  CONF_URL: url,
72  CONF_LATITUDE: latitude,
73  CONF_LONGITUDE: longitude,
74  CONF_RADIUS: DistanceConverter.convert(
75  location[CONF_RADIUS],
76  UnitOfLength.METERS,
77  UnitOfLength.KILOMETERS,
78  ),
79  },
80  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)