1 """Config flow to configure the OpenUV component."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from dataclasses
import dataclass
9 from pyopenuv
import Client
10 from pyopenuv.errors
import OpenUvError
11 import voluptuous
as vol
24 SchemaOptionsFlowHandler,
35 STEP_REAUTH_SCHEMA = vol.Schema(
37 vol.Required(CONF_API_KEY): str,
41 OPTIONS_SCHEMA = vol.Schema(
44 CONF_FROM_WINDOW, description={
"suggested_value": DEFAULT_FROM_WINDOW}
47 CONF_TO_WINDOW, description={
"suggested_value": DEFAULT_TO_WINDOW}
59 """Define structured OpenUV data needed to create/re-auth an entry."""
68 """Return the unique for this data."""
69 return f
"{self.latitude}, {self.longitude}"
73 """Handle an OpenUV config flow."""
83 """Return the config schema."""
86 vol.Required(CONF_API_KEY): str,
88 CONF_LATITUDE,
"coords", default=self.hass.config.latitude
91 CONF_LONGITUDE,
"coords", default=self.hass.config.longitude
94 CONF_ELEVATION, default=self.hass.config.elevation
100 self, data: OpenUvData, error_step_id: str, error_schema: vol.Schema
101 ) -> ConfigFlowResult:
102 """Verify the credentials and create/re-auth the entry."""
103 websession = aiohttp_client.async_get_clientsession(self.hass)
104 client = Client(data.api_key, 0, 0, session=websession)
107 await client.uv_index()
110 step_id=error_step_id,
111 data_schema=error_schema,
112 errors={CONF_API_KEY:
"invalid_api_key"},
113 description_placeholders={
114 CONF_LATITUDE:
str(data.latitude),
115 CONF_LONGITUDE:
str(data.longitude),
120 CONF_API_KEY: data.api_key,
121 CONF_LATITUDE: data.latitude,
122 CONF_LONGITUDE: data.longitude,
123 CONF_ELEVATION: data.elevation,
127 self.hass.config_entries.async_update_entry(existing_entry, data=entry_data)
128 self.hass.async_create_task(
129 self.hass.config_entries.async_reload(existing_entry.entry_id)
137 """Define the config flow to handle options."""
141 self, entry_data: Mapping[str, Any]
142 ) -> ConfigFlowResult:
143 """Handle configuration by re-auth."""
148 self, user_input: dict[str, Any] |
None =
None
149 ) -> ConfigFlowResult:
150 """Handle re-auth completion."""
153 step_id=
"reauth_confirm",
154 data_schema=STEP_REAUTH_SCHEMA,
155 description_placeholders={
156 CONF_LATITUDE: self.
_reauth_data_reauth_data[CONF_LATITUDE],
157 CONF_LONGITUDE: self.
_reauth_data_reauth_data[CONF_LONGITUDE],
162 user_input[CONF_API_KEY],
168 return await self.
_async_verify_async_verify(data,
"reauth_confirm", STEP_REAUTH_SCHEMA)
171 self, user_input: dict[str, Any] |
None =
None
172 ) -> ConfigFlowResult:
173 """Handle the start of the config flow."""
180 user_input[CONF_API_KEY],
181 user_input[CONF_LATITUDE],
182 user_input[CONF_LONGITUDE],
183 user_input[CONF_ELEVATION],
ConfigFlowResult _async_verify(self, OpenUvData data, str error_step_id, vol.Schema error_schema)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
vol.Schema step_user_schema(self)
SchemaOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
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_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)