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 Volcano integration."""
2 
3 from typing import Any
4 
5 import voluptuous as vol
6 
7 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
8 from homeassistant.const import (
9  CONF_LATITUDE,
10  CONF_LONGITUDE,
11  CONF_RADIUS,
12  CONF_SCAN_INTERVAL,
13  CONF_UNIT_SYSTEM,
14 )
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers import config_validation as cv
17 from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
18 
19 from .const import (
20  DEFAULT_RADIUS,
21  DEFAULT_SCAN_INTERVAL,
22  DOMAIN,
23  IMPERIAL_UNITS,
24  METRIC_UNITS,
25 )
26 
27 
28 @callback
29 def configured_instances(hass: HomeAssistant) -> set[str]:
30  """Return a set of configured GeoNet NZ Volcano instances."""
31  return {
32  f"{entry.data[CONF_LATITUDE]}, {entry.data[CONF_LONGITUDE]}"
33  for entry in hass.config_entries.async_entries(DOMAIN)
34  }
35 
36 
37 class GeonetnzVolcanoFlowHandler(ConfigFlow, domain=DOMAIN):
38  """Handle a GeoNet NZ Volcano config flow."""
39 
40  async def _show_form(self, errors=None):
41  """Show the form to the user."""
42  data_schema = vol.Schema(
43  {vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): cv.positive_int}
44  )
45 
46  return self.async_show_formasync_show_formasync_show_form(
47  step_id="user", data_schema=data_schema, errors=errors or {}
48  )
49 
50  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
51  """Import a config entry from configuration.yaml."""
52  return await self.async_step_userasync_step_user(import_data)
53 
54  async def async_step_user(
55  self, user_input: dict[str, Any] | None = None
56  ) -> ConfigFlowResult:
57  """Handle the start of the config flow."""
58  if not user_input:
59  return await self._show_form_show_form()
60 
61  latitude = user_input.get(CONF_LATITUDE, self.hass.config.latitude)
62  user_input[CONF_LATITUDE] = latitude
63  longitude = user_input.get(CONF_LONGITUDE, self.hass.config.longitude)
64  user_input[CONF_LONGITUDE] = longitude
65 
66  identifier = f"{user_input[CONF_LATITUDE]}, {user_input[CONF_LONGITUDE]}"
67  if identifier in configured_instances(self.hass):
68  return await self._show_form_show_form({"base": "already_configured"})
69 
70  if self.hass.config.units is US_CUSTOMARY_SYSTEM:
71  user_input[CONF_UNIT_SYSTEM] = IMPERIAL_UNITS
72  else:
73  user_input[CONF_UNIT_SYSTEM] = METRIC_UNITS
74 
75  scan_interval = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
76  user_input[CONF_SCAN_INTERVAL] = scan_interval.total_seconds()
77 
78  return self.async_create_entryasync_create_entryasync_create_entry(title=identifier, data=user_input)
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)
set[str] configured_instances(HomeAssistant hass)
Definition: config_flow.py:29
config_entries.ConfigFlowResult async_step_import(self, dict[str, Any]|None _)