1 """Config flow for UptimeRobot integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
8 from pyuptimerobot
import (
12 UptimeRobotApiResponse,
13 UptimeRobotAuthenticationException,
16 import voluptuous
as vol
22 from .const
import API_ATTR_OK, DOMAIN, LOGGER
24 STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str})
28 """Handle a config flow for UptimeRobot."""
33 self, data: dict[str, Any]
34 ) -> tuple[dict[str, str], UptimeRobotAccount |
None]:
35 """Validate the user input allows us to connect."""
36 errors: dict[str, str] = {}
37 response: UptimeRobotApiResponse | UptimeRobotApiError |
None =
None
38 key: str = data[CONF_API_KEY]
39 if key.startswith((
"ur",
"m")):
40 LOGGER.error(
"Wrong API key type detected, use the 'main' API key")
41 errors[
"base"] =
"not_main_key"
46 response = await uptime_robot_api.async_get_account_details()
47 except UptimeRobotAuthenticationException
as exception:
48 LOGGER.error(exception)
49 errors[
"base"] =
"invalid_api_key"
50 except UptimeRobotException
as exception:
51 LOGGER.error(exception)
52 errors[
"base"] =
"cannot_connect"
53 except Exception
as exception:
54 LOGGER.exception(exception)
55 errors[
"base"] =
"unknown"
57 if response.status != API_ATTR_OK:
58 errors[
"base"] =
"unknown"
59 LOGGER.error(response.error.message)
61 account: UptimeRobotAccount |
None = (
63 if response
and response.data
and response.data.email
67 return errors, account
70 self, user_input: dict[str, Any] |
None =
None
71 ) -> ConfigFlowResult:
72 """Handle the initial step."""
73 if user_input
is None:
75 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA
85 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
89 self, entry_data: Mapping[str, Any]
90 ) -> ConfigFlowResult:
91 """Return the reauth confirm step."""
95 self, user_input: dict[str, Any] |
None =
None
96 ) -> ConfigFlowResult:
97 """Dialog that informs the user that reauth is required."""
98 if user_input
is None:
100 step_id=
"reauth_confirm", data_schema=STEP_USER_DATA_SCHEMA
102 errors, account = await self.
_validate_input_validate_input(user_input)
104 if self.context.
get(
"unique_id")
and self.context[
"unique_id"] !=
str(
107 errors[
"base"] =
"reauth_failed_matching_account"
111 self.hass.config_entries.async_update_entry(
112 existing_entry, data=user_input
114 await self.hass.config_entries.async_reload(existing_entry.entry_id)
119 step_id=
"reauth_confirm", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
tuple[dict[str, str], UptimeRobotAccount|None] _validate_input(self, dict[str, Any] data)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(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)
web.Response get(self, web.Request request, str config_key)
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)