83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
|
"""Config flow for mütesync integration."""
|
||
|
from __future__ import annotations
|
||
|
|
||
|
import asyncio
|
||
|
from typing import Any
|
||
|
|
||
|
import aiohttp
|
||
|
import async_timeout
|
||
|
import mutesync
|
||
|
import voluptuous as vol
|
||
|
|
||
|
from homeassistant import config_entries
|
||
|
from homeassistant.core import HomeAssistant
|
||
|
from homeassistant.data_entry_flow import FlowResultDict
|
||
|
from homeassistant.exceptions import HomeAssistantError
|
||
|
|
||
|
from .const import DOMAIN
|
||
|
|
||
|
STEP_USER_DATA_SCHEMA = vol.Schema({"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 = hass.helpers.aiohttp_client.async_get_clientsession()
|
||
|
try:
|
||
|
async with async_timeout.timeout(10):
|
||
|
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, asyncio.TimeoutError) as error:
|
||
|
raise CannotConnect from error
|
||
|
|
||
|
return token
|
||
|
|
||
|
|
||
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||
|
"""Handle a config flow for mütesync."""
|
||
|
|
||
|
VERSION = 1
|
||
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
||
|
|
||
|
async def async_step_user(
|
||
|
self, user_input: dict[str, Any] | None = None
|
||
|
) -> FlowResultDict:
|
||
|
"""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: # pylint: disable=broad-except
|
||
|
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."""
|