Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Meteoclimatic integration."""
2 
3 import logging
4 from typing import Any
5 
6 from meteoclimatic import MeteoclimaticClient
7 from meteoclimatic.exceptions import MeteoclimaticError, StationNotFound
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 
12 from .const import CONF_STATION_CODE, DOMAIN
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
17 class MeteoclimaticFlowHandler(ConfigFlow, domain=DOMAIN):
18  """Handle a Meteoclimatic config flow."""
19 
20  VERSION = 1
21 
22  def _show_setup_form(self, user_input=None, errors=None):
23  """Show the setup form to the user."""
24  if user_input is None:
25  user_input = {}
26 
27  return self.async_show_formasync_show_formasync_show_form(
28  step_id="user",
29  data_schema=vol.Schema(
30  {
31  vol.Required(
32  CONF_STATION_CODE, default=user_input.get(CONF_STATION_CODE, "")
33  ): str
34  }
35  ),
36  errors=errors or {},
37  )
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle a flow initiated by the user."""
43  errors: dict[str, str] = {}
44 
45  if user_input is None:
46  return self._show_setup_form_show_setup_form(user_input, errors)
47 
48  station_code = user_input[CONF_STATION_CODE]
49  client = MeteoclimaticClient()
50 
51  try:
52  weather = await self.hass.async_add_executor_job(
53  client.weather_at_station, station_code
54  )
55  except StationNotFound as exp:
56  _LOGGER.error("Station not found: %s", exp)
57  errors["base"] = "not_found"
58  return self._show_setup_form_show_setup_form(user_input, errors)
59  except MeteoclimaticError as exp:
60  _LOGGER.error("Error when obtaining Meteoclimatic weather: %s", exp)
61  return self.async_abortasync_abortasync_abort(reason="unknown")
62 
63  # Check if already configured
64  await self.async_set_unique_idasync_set_unique_id(station_code, raise_on_progress=False)
65 
66  return self.async_create_entryasync_create_entryasync_create_entry(
67  title=weather.station.name, data={CONF_STATION_CODE: station_code}
68  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)