Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for OpenWeatherMap."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
8  ConfigEntry,
9  ConfigFlow,
10  ConfigFlowResult,
11  OptionsFlow,
12 )
13 from homeassistant.const import (
14  CONF_API_KEY,
15  CONF_LANGUAGE,
16  CONF_LATITUDE,
17  CONF_LONGITUDE,
18  CONF_MODE,
19  CONF_NAME,
20 )
21 from homeassistant.core import callback
23 
24 from .const import (
25  CONFIG_FLOW_VERSION,
26  DEFAULT_LANGUAGE,
27  DEFAULT_NAME,
28  DEFAULT_OWM_MODE,
29  DOMAIN,
30  LANGUAGES,
31  OWM_MODES,
32 )
33 from .utils import build_data_and_options, validate_api_key
34 
35 
36 class OpenWeatherMapConfigFlow(ConfigFlow, domain=DOMAIN):
37  """Config flow for OpenWeatherMap."""
38 
39  VERSION = CONFIG_FLOW_VERSION
40 
41  @staticmethod
42  @callback
44  config_entry: ConfigEntry,
45  ) -> OpenWeatherMapOptionsFlow:
46  """Get the options flow for this handler."""
48 
49  async def async_step_user(self, user_input=None) -> ConfigFlowResult:
50  """Handle a flow initialized by the user."""
51  errors = {}
52  description_placeholders = {}
53 
54  if user_input is not None:
55  latitude = user_input[CONF_LATITUDE]
56  longitude = user_input[CONF_LONGITUDE]
57  mode = user_input[CONF_MODE]
58 
59  await self.async_set_unique_idasync_set_unique_id(f"{latitude}-{longitude}")
60  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
61 
62  errors, description_placeholders = await validate_api_key(
63  user_input[CONF_API_KEY], mode
64  )
65 
66  if not errors:
67  data, options = build_data_and_options(user_input)
68  return self.async_create_entryasync_create_entryasync_create_entry(
69  title=user_input[CONF_NAME], data=data, options=options
70  )
71 
72  schema = vol.Schema(
73  {
74  vol.Required(CONF_API_KEY): str,
75  vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
76  vol.Optional(
77  CONF_LATITUDE, default=self.hass.config.latitude
78  ): cv.latitude,
79  vol.Optional(
80  CONF_LONGITUDE, default=self.hass.config.longitude
81  ): cv.longitude,
82  vol.Optional(CONF_MODE, default=DEFAULT_OWM_MODE): vol.In(OWM_MODES),
83  vol.Optional(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): vol.In(
84  LANGUAGES
85  ),
86  }
87  )
88 
89  return self.async_show_formasync_show_formasync_show_form(
90  step_id="user",
91  data_schema=schema,
92  errors=errors,
93  description_placeholders=description_placeholders,
94  )
95 
96 
98  """Handle options."""
99 
100  async def async_step_init(self, user_input: dict | None = None) -> ConfigFlowResult:
101  """Manage the options."""
102  if user_input is not None:
103  return self.async_create_entryasync_create_entry(title="", data=user_input)
104 
105  return self.async_show_formasync_show_form(
106  step_id="init",
107  data_schema=self._get_options_schema_get_options_schema(),
108  )
109 
111  return vol.Schema(
112  {
113  vol.Optional(
114  CONF_MODE,
115  default=self.config_entryconfig_entryconfig_entry.options.get(
116  CONF_MODE,
117  self.config_entryconfig_entryconfig_entry.data.get(CONF_MODE, DEFAULT_OWM_MODE),
118  ),
119  ): vol.In(OWM_MODES),
120  vol.Optional(
121  CONF_LANGUAGE,
122  default=self.config_entryconfig_entryconfig_entry.options.get(
123  CONF_LANGUAGE,
124  self.config_entryconfig_entryconfig_entry.data.get(CONF_LANGUAGE, DEFAULT_LANGUAGE),
125  ),
126  ): vol.In(LANGUAGES),
127  }
128  )
OpenWeatherMapOptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:45
ConfigFlowResult async_step_init(self, dict|None user_input=None)
Definition: config_flow.py:100
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_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)
None config_entry(self, ConfigEntry value)
_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)
tuple[dict[str, Any], dict[str, Any]] build_data_and_options(dict[str, Any] combined_data)
Definition: utils.py:33