Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for Brottsplatskartan integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 import uuid
7 
8 from brottsplatskartan import AREAS
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE
13 from homeassistant.helpers import selector
14 
15 from .const import CONF_APP_ID, CONF_AREA, DEFAULT_NAME, DOMAIN
16 
17 DATA_SCHEMA = vol.Schema(
18  {
19  vol.Optional(CONF_LOCATION): selector.LocationSelector(
20  selector.LocationSelectorConfig(radius=False, icon="")
21  ),
22  vol.Optional(CONF_AREA): selector.SelectSelector(
23  selector.SelectSelectorConfig(
24  options=AREAS,
25  mode=selector.SelectSelectorMode.DROPDOWN,
26  )
27  ),
28  }
29 )
30 
31 
32 class BPKConfigFlow(ConfigFlow, domain=DOMAIN):
33  """Handle a config flow for Brottsplatskartan integration."""
34 
35  VERSION = 1
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle the user step."""
41  errors: dict[str, str] = {}
42 
43  if user_input is not None:
44  latitude: float | None = None
45  longitude: float | None = None
46  area: str | None = user_input.get(CONF_AREA)
47 
48  if area:
49  name = f"{DEFAULT_NAME} {area}"
50  elif location := user_input.get(CONF_LOCATION):
51  lat: float = location[CONF_LATITUDE]
52  long: float = location[CONF_LONGITUDE]
53  latitude = lat
54  longitude = long
55  name = f"{DEFAULT_NAME} {round(latitude, 2)}, {round(longitude, 2)}"
56  else:
57  latitude = self.hass.config.latitude
58  longitude = self.hass.config.longitude
59  name = f"{DEFAULT_NAME} HOME"
60 
61  app = f"ha-{uuid.getnode()}"
62 
63  self._async_abort_entries_match_async_abort_entries_match(
64  {CONF_AREA: area, CONF_LATITUDE: latitude, CONF_LONGITUDE: longitude}
65  )
66  return self.async_create_entryasync_create_entryasync_create_entry(
67  title=name,
68  data={
69  CONF_LATITUDE: latitude,
70  CONF_LONGITUDE: longitude,
71  CONF_AREA: area,
72  CONF_APP_ID: app,
73  },
74  )
75 
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="user",
78  data_schema=DATA_SCHEMA,
79  errors=errors,
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)
_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)