1 """Adds config flow for WeatherKit."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
8 from apple_weatherkit.client
import (
10 WeatherKitApiClientAuthenticationError,
11 WeatherKitApiClientCommunicationError,
12 WeatherKitApiClientError,
14 import voluptuous
as vol
21 LocationSelectorConfig,
35 DATA_SCHEMA = vol.Schema(
41 vol.Required(CONF_KEY_ID): str,
42 vol.Required(CONF_SERVICE_ID): str,
43 vol.Required(CONF_TEAM_ID): str,
54 """Error to indicate a location is unsupported."""
58 """Config flow for WeatherKit."""
64 user_input: dict[str, Any] |
None =
None,
65 ) -> ConfigFlowResult:
66 """Handle a flow initialized by the user."""
68 if user_input
is not None:
70 user_input[CONF_KEY_PEM] = self.
_fix_key_input_fix_key_input(user_input[CONF_KEY_PEM])
72 except WeatherKitUnsupportedLocationError
as exception:
73 LOGGER.error(exception)
74 errors[
"base"] =
"unsupported_location"
75 except WeatherKitApiClientAuthenticationError
as exception:
76 LOGGER.warning(exception)
77 errors[
"base"] =
"invalid_auth"
78 except WeatherKitApiClientCommunicationError
as exception:
79 LOGGER.error(exception)
80 errors[
"base"] =
"cannot_connect"
81 except WeatherKitApiClientError
as exception:
82 LOGGER.exception(exception)
83 errors[
"base"] =
"unknown"
86 location = user_input.pop(CONF_LOCATION)
87 user_input[CONF_LATITUDE] = location[CONF_LATITUDE]
88 user_input[CONF_LONGITUDE] = location[CONF_LONGITUDE]
91 title=f
"{user_input[CONF_LATITUDE]}, {user_input[CONF_LONGITUDE]}",
95 suggested_values: Mapping[str, Any] = {
97 CONF_LATITUDE: self.hass.config.latitude,
98 CONF_LONGITUDE: self.hass.config.longitude,
105 data_schema=data_schema,
110 """Fix common user errors with the key input."""
112 key_input = key_input.replace(
"—",
"--")
115 key_input = key_input.strip()
118 header =
"-----BEGIN PRIVATE KEY-----"
119 if not key_input.startswith(header):
120 key_input = f
"{header}\n{key_input}"
122 footer =
"-----END PRIVATE KEY-----"
123 if not key_input.endswith(footer):
124 key_input += f
"\n{footer}"
129 """Validate credentials."""
130 client = WeatherKitApiClient(
131 key_id=user_input[CONF_KEY_ID],
132 service_id=user_input[CONF_SERVICE_ID],
133 team_id=user_input[CONF_TEAM_ID],
134 key_pem=user_input[CONF_KEY_PEM],
138 location = user_input[CONF_LOCATION]
139 availability = await client.get_availability(
140 location[CONF_LATITUDE],
141 location[CONF_LONGITUDE],
146 "API does not support this location"
str _fix_key_input(self, str key_input)
None _test_config(self, dict[str, Any] user_input)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)