2022-02-16 14:51:29 +00:00
|
|
|
"""Config flow to configure SleepIQ component."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2022-02-18 18:50:44 +00:00
|
|
|
from asyncsleepiq import AsyncSleepIQ, SleepIQLoginException, SleepIQTimeoutException
|
2022-02-16 14:51:29 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2022-02-18 18:50:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-02-16 14:51:29 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2022-02-18 18:50:44 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2022-02-16 14:51:29 +00:00
|
|
|
|
2022-02-18 18:50:44 +00:00
|
|
|
from .const import DOMAIN
|
2022-02-16 14:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SleepIQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a SleepIQ config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
|
|
|
|
"""Import a SleepIQ account as a config entry.
|
|
|
|
|
|
|
|
This flow is triggered by 'async_setup' for configured accounts.
|
|
|
|
"""
|
|
|
|
await self.async_set_unique_id(import_config[CONF_USERNAME].lower())
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=import_config[CONF_USERNAME], data=import_config
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
# Don't allow multiple instances with the same username
|
|
|
|
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
2022-02-18 18:50:44 +00:00
|
|
|
try:
|
|
|
|
await try_connection(self.hass, user_input)
|
|
|
|
except SleepIQLoginException:
|
|
|
|
errors["base"] = "invalid_auth"
|
|
|
|
except SleepIQTimeoutException:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
else:
|
2022-02-16 14:51:29 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input[CONF_USERNAME], data=user_input
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(
|
|
|
|
CONF_USERNAME,
|
|
|
|
default=user_input.get(CONF_USERNAME)
|
|
|
|
if user_input is not None
|
|
|
|
else "",
|
|
|
|
): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors,
|
|
|
|
last_step=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-18 18:50:44 +00:00
|
|
|
async def try_connection(hass: HomeAssistant, user_input: dict[str, Any]) -> None:
|
2022-02-16 14:51:29 +00:00
|
|
|
"""Test if the given credentials can successfully login to SleepIQ."""
|
|
|
|
|
2022-02-18 18:50:44 +00:00
|
|
|
client_session = async_get_clientsession(hass)
|
2022-02-16 14:51:29 +00:00
|
|
|
|
2022-02-18 18:50:44 +00:00
|
|
|
gateway = AsyncSleepIQ(client_session=client_session)
|
|
|
|
await gateway.login(user_input[CONF_USERNAME], user_input[CONF_PASSWORD])
|