Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the GeoNet NZ Quakes 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 (
18  CONF_MINIMUM_MAGNITUDE,
19  CONF_MMI,
20  DEFAULT_MINIMUM_MAGNITUDE,
21  DEFAULT_MMI,
22  DEFAULT_RADIUS,
23  DEFAULT_SCAN_INTERVAL,
24  DOMAIN,
25 )
26 
27 DATA_SCHEMA = vol.Schema(
28  {
29  vol.Optional(CONF_MMI, default=DEFAULT_MMI): vol.All(
30  vol.Coerce(int), vol.Range(min=-1, max=8)
31  ),
32  vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): cv.positive_int,
33  }
34 )
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 
39 class GeonetnzQuakesFlowHandler(ConfigFlow, domain=DOMAIN):
40  """Handle a GeoNet NZ Quakes config flow."""
41 
42  async def _show_form(self, errors=None):
43  """Show the form to the user."""
44  return self.async_show_formasync_show_formasync_show_form(
45  step_id="user", data_schema=DATA_SCHEMA, errors=errors or {}
46  )
47 
48  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
49  """Import a config entry from configuration.yaml."""
50  return await self.async_step_userasync_step_user(import_data)
51 
52  async def async_step_user(
53  self, user_input: dict[str, Any] | None = None
54  ) -> ConfigFlowResult:
55  """Handle the start of the config flow."""
56  _LOGGER.debug("User input: %s", user_input)
57  if not user_input:
58  return await self._show_form_show_form()
59 
60  latitude = user_input.get(CONF_LATITUDE, self.hass.config.latitude)
61  user_input[CONF_LATITUDE] = latitude
62  longitude = user_input.get(CONF_LONGITUDE, self.hass.config.longitude)
63  user_input[CONF_LONGITUDE] = longitude
64 
65  identifier = f"{user_input[CONF_LATITUDE]}, {user_input[CONF_LONGITUDE]}"
66 
67  await self.async_set_unique_idasync_set_unique_id(identifier)
68  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
69 
70  scan_interval = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
71  user_input[CONF_SCAN_INTERVAL] = scan_interval.total_seconds()
72 
73  minimum_magnitude = user_input.get(
74  CONF_MINIMUM_MAGNITUDE, DEFAULT_MINIMUM_MAGNITUDE
75  )
76  user_input[CONF_MINIMUM_MAGNITUDE] = minimum_magnitude
77 
78  return self.async_create_entryasync_create_entryasync_create_entry(title=identifier, data=user_input)
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)
config_entries.ConfigFlowResult async_step_import(self, dict[str, Any]|None _)