hass-core/homeassistant/components/switchbot_cloud/config_flow.py
2024-04-16 21:39:34 +10:00

62 lines
2 KiB
Python

"""Config flow for SwitchBot via API integration."""
from logging import getLogger
from typing import Any
from switchbot_api import CannotConnect, InvalidAuth, SwitchBotAPI
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_SCAN_INTERVAL
from homeassistant.helpers import config_validation as cv
from .const import DEFAULT_SCAN_INTERVAL_SECONDS, DOMAIN, ENTRY_TITLE
_LOGGER = getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_API_TOKEN): str,
vol.Required(CONF_API_KEY): str,
vol.Required(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL_SECONDS
): cv.positive_int,
}
)
class SwitchBotCloudConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for SwitchBot via API."""
VERSION = 1
MINOR_VERSION = 2
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
try:
await SwitchBotAPI(
token=user_input[CONF_API_TOKEN], secret=user_input[CONF_API_KEY]
).list_devices()
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"
else:
await self.async_set_unique_id(
user_input[CONF_API_TOKEN], raise_on_progress=False
)
self._abort_if_unique_id_configured()
return self.async_create_entry(title=ENTRY_TITLE, data=user_input)
return self.async_show_form(
step_id="user",
data_schema=STEP_USER_DATA_SCHEMA,
errors=errors,
)