The dongle has no DNS name and takes its address from DHCP, so a configured local /status URL goes stale when the lease changes. Setup treated that as fatal: every entity went unavailable and control (which never used the local path) went down with it. - A local URL that does not answer at setup no longer fails the entry; the coordinator subscribes to the cloud broker for telemetry instead and logs a warning. - After four consecutive failed local polls the coordinator switches to the cloud the same way, instead of holding the last local values forever. - When the local URL answers again it takes over: the cloud subscription is dropped along with what it delivered, so a stale cloud value cannot shadow a fresh local one. - New reconfigure step changes the local URL without signing in again. - Version 0.1.1. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
142 lines
4.5 KiB
Python
142 lines
4.5 KiB
Python
"""Config flow: prompt for the Hot Spring account e-mail + password.
|
|
|
|
Credentials are validated by logging in, then stored in the config entry (Home
|
|
Assistant encrypts entry storage at rest). They are never written to this repo.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.helpers.selector import (
|
|
TextSelector,
|
|
TextSelectorConfig,
|
|
TextSelectorType,
|
|
)
|
|
|
|
from .api import HotSpringApiError, HotSpringAuthError, HotSpringCloud
|
|
from .constants import (
|
|
CA_FILE,
|
|
CERT_FILE,
|
|
CONF_CERT_DIR,
|
|
CONF_EMAIL,
|
|
CONF_LOCAL_STATUS_URL,
|
|
CONF_PASSWORD,
|
|
DEFAULT_CERT_DIR,
|
|
DOMAIN,
|
|
KEY_FILE,
|
|
)
|
|
|
|
|
|
def _schema(defaults: dict[str, Any] | None = None) -> vol.Schema:
|
|
defaults = defaults or {}
|
|
return vol.Schema(
|
|
{
|
|
vol.Required(CONF_EMAIL, default=defaults.get(CONF_EMAIL, "")): str,
|
|
vol.Required(CONF_PASSWORD): TextSelector(
|
|
TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
|
),
|
|
vol.Optional(
|
|
CONF_CERT_DIR, default=defaults.get(CONF_CERT_DIR, DEFAULT_CERT_DIR)
|
|
): str,
|
|
vol.Optional(
|
|
CONF_LOCAL_STATUS_URL,
|
|
default=defaults.get(CONF_LOCAL_STATUS_URL, ""),
|
|
): str,
|
|
}
|
|
)
|
|
|
|
|
|
class HotSpringConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle the UI setup."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
errors: dict[str, str] = {}
|
|
if user_input is not None:
|
|
cert_dir = user_input[CONF_CERT_DIR]
|
|
missing = [
|
|
f
|
|
for f in (CERT_FILE, KEY_FILE, CA_FILE)
|
|
if not os.path.isfile(os.path.join(cert_dir, f))
|
|
]
|
|
if missing:
|
|
errors["base"] = "missing_certs"
|
|
else:
|
|
result = await self._validate(user_input)
|
|
if result is None:
|
|
spa_name = self._spa_name or "Hot Spring Spa"
|
|
await self.async_set_unique_id(self._unique_id)
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(title=spa_name, data=user_input)
|
|
errors["base"] = result
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=_schema(user_input), errors=errors
|
|
)
|
|
|
|
async def async_step_reconfigure(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Change the local ``/status`` URL without signing in again.
|
|
|
|
The dongle takes its address from DHCP, so this is the one setting that
|
|
goes stale on its own.
|
|
"""
|
|
entry = self._get_reconfigure_entry()
|
|
if user_input is not None:
|
|
url = (user_input.get(CONF_LOCAL_STATUS_URL) or "").strip()
|
|
return self.async_update_reload_and_abort(
|
|
entry, data_updates={CONF_LOCAL_STATUS_URL: url}
|
|
)
|
|
return self.async_show_form(
|
|
step_id="reconfigure",
|
|
data_schema=vol.Schema(
|
|
{
|
|
vol.Optional(
|
|
CONF_LOCAL_STATUS_URL,
|
|
description={
|
|
"suggested_value": entry.data.get(CONF_LOCAL_STATUS_URL, "")
|
|
},
|
|
): str,
|
|
}
|
|
),
|
|
)
|
|
|
|
async def _validate(self, data: dict[str, Any]) -> str | None:
|
|
"""Return None on success, or an error key."""
|
|
cert_dir = data[CONF_CERT_DIR]
|
|
cloud = HotSpringCloud(
|
|
data[CONF_EMAIL],
|
|
data[CONF_PASSWORD],
|
|
os.path.join(cert_dir, CERT_FILE),
|
|
os.path.join(cert_dir, KEY_FILE),
|
|
os.path.join(cert_dir, CA_FILE),
|
|
)
|
|
|
|
def _check() -> tuple[str | None, str | None]:
|
|
cloud.login()
|
|
spas = cloud.get_spas()
|
|
if not spas:
|
|
return None, None
|
|
return spas[0].name, spas[0].root_topic
|
|
|
|
try:
|
|
self._spa_name, self._unique_id = await self.hass.async_add_executor_job(
|
|
_check
|
|
)
|
|
except HotSpringAuthError:
|
|
return "invalid_auth"
|
|
except HotSpringApiError:
|
|
return "cannot_connect"
|
|
except Exception: # noqa: BLE001 - surface as generic to the user
|
|
return "unknown"
|
|
return None
|