Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for AccuWeather."""
2 
3 from __future__ import annotations
4 
5 from asyncio import timeout
6 from typing import Any
7 
8 from accuweather import AccuWeather, ApiError, InvalidApiKeyError, RequestsExceededError
9 from aiohttp import ClientError
10 from aiohttp.client_exceptions import ClientConnectorError
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 
18 from .const import DOMAIN
19 
20 
21 class AccuWeatherFlowHandler(ConfigFlow, domain=DOMAIN):
22  """Config flow for AccuWeather."""
23 
24  VERSION = 1
25 
26  async def async_step_user(
27  self, user_input: dict[str, Any] | None = None
28  ) -> ConfigFlowResult:
29  """Handle a flow initialized by the user."""
30  errors = {}
31 
32  if user_input is not None:
33  websession = async_get_clientsession(self.hass)
34  try:
35  async with timeout(10):
36  accuweather = AccuWeather(
37  user_input[CONF_API_KEY],
38  websession,
39  latitude=user_input[CONF_LATITUDE],
40  longitude=user_input[CONF_LONGITUDE],
41  )
42  await accuweather.async_get_location()
43  except (ApiError, ClientConnectorError, TimeoutError, ClientError):
44  errors["base"] = "cannot_connect"
45  except InvalidApiKeyError:
46  errors[CONF_API_KEY] = "invalid_api_key"
47  except RequestsExceededError:
48  errors[CONF_API_KEY] = "requests_exceeded"
49  else:
50  await self.async_set_unique_idasync_set_unique_id(
51  accuweather.location_key, raise_on_progress=False
52  )
53 
54  return self.async_create_entryasync_create_entryasync_create_entry(
55  title=user_input[CONF_NAME], data=user_input
56  )
57 
58  return self.async_show_formasync_show_formasync_show_form(
59  step_id="user",
60  data_schema=vol.Schema(
61  {
62  vol.Required(CONF_API_KEY): str,
63  vol.Optional(
64  CONF_LATITUDE, default=self.hass.config.latitude
65  ): cv.latitude,
66  vol.Optional(
67  CONF_LONGITUDE, default=self.hass.config.longitude
68  ): cv.longitude,
69  vol.Optional(
70  CONF_NAME, default=self.hass.config.location_name
71  ): str,
72  }
73  ),
74  errors=errors,
75  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:28
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)
_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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)