2021-08-04 20:20:03 +00:00
|
|
|
"""Config flow for Uptime Robot integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-05 12:58:29 +00:00
|
|
|
from pyuptimerobot import (
|
|
|
|
UptimeRobot,
|
|
|
|
UptimeRobotAccount,
|
|
|
|
UptimeRobotApiError,
|
|
|
|
UptimeRobotApiResponse,
|
|
|
|
UptimeRobotAuthenticationException,
|
|
|
|
UptimeRobotException,
|
|
|
|
)
|
2021-08-04 20:20:03 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_API_KEY
|
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2021-08-05 10:13:47 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2021-08-04 20:20:03 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2021-08-05 10:13:47 +00:00
|
|
|
from .const import API_ATTR_OK, DOMAIN, LOGGER
|
2021-08-04 20:20:03 +00:00
|
|
|
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str})
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for Uptime Robot."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2021-08-05 12:58:29 +00:00
|
|
|
async def _validate_input(
|
|
|
|
self, data: ConfigType
|
|
|
|
) -> tuple[dict[str, str], UptimeRobotAccount | None]:
|
|
|
|
"""Validate the user input allows us to connect."""
|
|
|
|
errors: dict[str, str] = {}
|
|
|
|
response: UptimeRobotApiResponse | UptimeRobotApiError | None = None
|
|
|
|
uptime_robot_api = UptimeRobot(
|
|
|
|
data[CONF_API_KEY], async_get_clientsession(self.hass)
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = await uptime_robot_api.async_get_account_details()
|
|
|
|
except UptimeRobotAuthenticationException as exception:
|
|
|
|
LOGGER.error(exception)
|
|
|
|
errors["base"] = "invalid_api_key"
|
|
|
|
except UptimeRobotException as exception:
|
|
|
|
LOGGER.error(exception)
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except Exception as exception: # pylint: disable=broad-except
|
|
|
|
LOGGER.exception(exception)
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
else:
|
|
|
|
if response.status != API_ATTR_OK:
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
LOGGER.error(response.error.message)
|
|
|
|
|
|
|
|
account: UptimeRobotAccount | None = (
|
|
|
|
response.data
|
|
|
|
if response and response.data and response.data.email
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
if account:
|
|
|
|
await self.async_set_unique_id(str(account.user_id))
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
|
|
|
return errors, account
|
|
|
|
|
2021-08-04 20:20:03 +00:00
|
|
|
async def async_step_user(self, user_input: ConfigType | None = None) -> FlowResult:
|
|
|
|
"""Handle the initial step."""
|
|
|
|
errors: dict[str, str] = {}
|
|
|
|
if user_input is None:
|
|
|
|
return self.async_show_form(
|
2021-08-05 12:58:29 +00:00
|
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA
|
2021-08-04 20:20:03 +00:00
|
|
|
)
|
|
|
|
|
2021-08-05 12:58:29 +00:00
|
|
|
errors, account = await self._validate_input(user_input)
|
|
|
|
if account:
|
2021-08-05 10:13:47 +00:00
|
|
|
return self.async_create_entry(title=account.email, data=user_input)
|
2021-08-04 20:20:03 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_import(self, import_config: ConfigType) -> FlowResult:
|
|
|
|
"""Import a config entry from configuration.yaml."""
|
|
|
|
for entry in self._async_current_entries():
|
|
|
|
if entry.data[CONF_API_KEY] == import_config[CONF_API_KEY]:
|
|
|
|
LOGGER.warning(
|
|
|
|
"Already configured. This YAML configuration has already been imported. Please remove it"
|
|
|
|
)
|
|
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
|
2021-08-05 10:13:47 +00:00
|
|
|
imported_config = {CONF_API_KEY: import_config[CONF_API_KEY]}
|
|
|
|
|
2021-08-05 12:58:29 +00:00
|
|
|
_, account = await self._validate_input(imported_config)
|
|
|
|
if account:
|
|
|
|
return self.async_create_entry(title=account.email, data=imported_config)
|
|
|
|
return self.async_abort(reason="unknown")
|