2020-03-14 05:46:17 +00:00
|
|
|
"""Config flow for Rachio integration."""
|
2022-06-13 11:30:41 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-23 18:53:39 +00:00
|
|
|
from http import HTTPStatus
|
2020-03-14 05:46:17 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from rachiopy import Rachio
|
2020-10-11 00:44:49 +00:00
|
|
|
from requests.exceptions import ConnectTimeout
|
2020-03-14 05:46:17 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries, core, exceptions
|
2021-11-21 13:55:54 +00:00
|
|
|
from homeassistant.components import zeroconf
|
2021-10-23 18:53:39 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2020-03-14 05:46:17 +00:00
|
|
|
from homeassistant.core import callback
|
2021-11-21 13:55:54 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2020-03-14 05:46:17 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONF_MANUAL_RUN_MINS,
|
|
|
|
DEFAULT_MANUAL_RUN_MINS,
|
2021-03-30 04:02:56 +00:00
|
|
|
DOMAIN,
|
2020-03-14 05:46:17 +00:00
|
|
|
KEY_ID,
|
|
|
|
KEY_STATUS,
|
|
|
|
KEY_USERNAME,
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
|
|
|
async def validate_input(hass: core.HomeAssistant, data):
|
|
|
|
"""Validate the user input allows us to connect.
|
|
|
|
|
|
|
|
Data has the keys from DATA_SCHEMA with values provided by the user.
|
|
|
|
"""
|
|
|
|
rachio = Rachio(data[CONF_API_KEY])
|
|
|
|
username = None
|
|
|
|
try:
|
2020-10-11 00:44:49 +00:00
|
|
|
data = await hass.async_add_executor_job(rachio.person.info)
|
2020-03-14 05:46:17 +00:00
|
|
|
_LOGGER.debug("rachio.person.getInfo: %s", data)
|
2021-10-23 18:53:39 +00:00
|
|
|
if int(data[0][KEY_STATUS]) != HTTPStatus.OK:
|
2020-03-14 05:46:17 +00:00
|
|
|
raise InvalidAuth
|
|
|
|
|
|
|
|
rachio_id = data[1][KEY_ID]
|
|
|
|
data = await hass.async_add_executor_job(rachio.person.get, rachio_id)
|
|
|
|
_LOGGER.debug("rachio.person.get: %s", data)
|
2021-10-23 18:53:39 +00:00
|
|
|
if int(data[0][KEY_STATUS]) != HTTPStatus.OK:
|
2020-03-14 05:46:17 +00:00
|
|
|
raise CannotConnect
|
|
|
|
|
|
|
|
username = data[1][KEY_USERNAME]
|
2020-10-11 00:44:49 +00:00
|
|
|
except ConnectTimeout as error:
|
2020-03-14 05:46:17 +00:00
|
|
|
_LOGGER.error("Could not reach the Rachio API: %s", error)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise CannotConnect from error
|
2020-03-14 05:46:17 +00:00
|
|
|
|
|
|
|
# Return info that you want to store in the config entry.
|
|
|
|
return {"title": username}
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for Rachio."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle the initial step."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
2020-04-06 17:24:08 +00:00
|
|
|
await self.async_set_unique_id(user_input[CONF_API_KEY])
|
|
|
|
self._abort_if_unique_id_configured()
|
2020-03-14 05:46:17 +00:00
|
|
|
try:
|
|
|
|
info = await validate_input(self.hass, user_input)
|
2020-04-06 17:24:08 +00:00
|
|
|
return self.async_create_entry(title=info["title"], data=user_input)
|
2020-03-14 05:46:17 +00:00
|
|
|
except CannotConnect:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except InvalidAuth:
|
|
|
|
errors["base"] = "invalid_auth"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
|
|
|
)
|
|
|
|
|
2021-11-21 13:55:54 +00:00
|
|
|
async def async_step_homekit(
|
|
|
|
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
|
|
|
) -> FlowResult:
|
2020-03-14 05:46:17 +00:00
|
|
|
"""Handle HomeKit discovery."""
|
2021-05-11 20:00:12 +00:00
|
|
|
self._async_abort_entries_match()
|
2021-11-21 13:55:54 +00:00
|
|
|
await self.async_set_unique_id(
|
2021-11-30 16:02:24 +00:00
|
|
|
discovery_info.properties[zeroconf.ATTR_PROPERTIES_ID]
|
2021-11-21 13:55:54 +00:00
|
|
|
)
|
2022-09-17 17:44:24 +00:00
|
|
|
self._abort_if_unique_id_configured()
|
2020-03-14 05:46:17 +00:00
|
|
|
return await self.async_step_user()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
2022-06-13 11:30:41 +00:00
|
|
|
def async_get_options_flow(
|
|
|
|
config_entry: config_entries.ConfigEntry,
|
|
|
|
) -> OptionsFlowHandler:
|
2020-03-14 05:46:17 +00:00
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return OptionsFlowHandler(config_entry)
|
|
|
|
|
|
|
|
|
|
|
|
class OptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
"""Handle a option flow for Rachio."""
|
|
|
|
|
2021-05-20 13:58:17 +00:00
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
2020-03-14 05:46:17 +00:00
|
|
|
"""Initialize options flow."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
"""Handle options flow."""
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
|
|
|
|
data_schema = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_MANUAL_RUN_MINS,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_MANUAL_RUN_MINS, DEFAULT_MANUAL_RUN_MINS
|
|
|
|
),
|
|
|
|
): int
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return self.async_show_form(step_id="init", data_schema=data_schema)
|
|
|
|
|
|
|
|
|
|
|
|
class CannotConnect(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate we cannot connect."""
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidAuth(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate there is invalid auth."""
|