52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""Config flow for Steamist integration."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from aiosteamist import Steamist
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import CONF_HOST
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import CONNECTION_EXCEPTIONS, DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Steamist."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> FlowResult:
|
|
"""Handle the initial step."""
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
try:
|
|
await Steamist(
|
|
user_input[CONF_HOST],
|
|
async_get_clientsession(self.hass),
|
|
).async_get_status()
|
|
except CONNECTION_EXCEPTIONS:
|
|
errors["base"] = "cannot_connect"
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
else:
|
|
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
|
return self.async_create_entry(
|
|
title=user_input[CONF_HOST], data=user_input
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
|
|
errors=errors,
|
|
)
|