core/homeassistant/components/mutesync/config_flow.py

82 lines
2.4 KiB
Python

"""Config flow for mütesync integration."""
from __future__ import annotations
import asyncio
from typing import Any
import aiohttp
import mutesync
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required("host"): str})
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
"""Validate the user input allows us to connect.
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
"""
session = async_get_clientsession(hass)
try:
async with asyncio.timeout(5):
token = await mutesync.authenticate(session, data["host"])
except aiohttp.ClientResponseError as error:
if error.status == 403:
raise InvalidAuth from error
raise CannotConnect from error
except (aiohttp.ClientError, TimeoutError) as error:
raise CannotConnect from error
return token
class MuteSyncConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for mütesync."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA
)
errors = {}
try:
token = await validate_input(self.hass, user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except Exception: # noqa: BLE001
errors["base"] = "unknown"
else:
return self.async_create_entry(
title=user_input["host"],
data={"token": token, "host": user_input["host"]},
)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)
class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""
class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""