Files
hotspring/custom_components/hotspring/switch.py
T
Hank MuellerandClaude Opus 5 cde2bbc772 Add lights/jets/salt control, app-traffic mimicry, and optional local read path
- New entities: number (salt output level 0-10), switch (lights, jets); the
  read-only salt sensor is replaced by the settable number.
- Look identical to the official app on the wire: send the app's Dalvik UA on
  REST, okhttp/4.12.0 on the mTLS creds fetch, and "mqtt-<uuid>" client ids.
- Consolidate every constant value into constants.py (well commented); protocol.py
  keeps only the command builders; const.py removed.
- Optional local read path: a `local_status_url` config option polls the dongle's
  local /status endpoint for telemetry instead of the cloud MQTT subscription
  (control stays cloud MQTT). State getters handle both schemas.

No private/internal values in the repo (the spa IP is runtime config only).

Fixes bug-41mqxddz7zeh

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F6B8b3iYNv6QUftK2FDfYb
2026-09-01 15:12:24 +00:00

85 lines
2.9 KiB
Python

"""Switch entities: spa lights (all zones) and jets (all)."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import HotSpringConfigEntry
from .constants import DOMAIN, MANUFACTURER
from .coordinator import HotSpringCoordinator
@dataclass(frozen=True, kw_only=True)
class HotSpringSwitchDescription(SwitchEntityDescription):
is_on_fn: Callable[[HotSpringCoordinator], bool | None]
set_fn: Callable[[HotSpringCoordinator, bool], Awaitable[None]]
SWITCHES: tuple[HotSpringSwitchDescription, ...] = (
HotSpringSwitchDescription(
key="lights",
translation_key="lights",
icon="mdi:lightbulb",
is_on_fn=lambda c: c.spa.lights_on() if c.spa else None,
set_fn=lambda c, on: c.async_set_lights(on),
),
HotSpringSwitchDescription(
key="jets",
translation_key="jets",
icon="mdi:chart-bubble",
is_on_fn=lambda c: c.spa.jets_on() if c.spa else None,
set_fn=lambda c, on: c.async_set_jets(on),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: HotSpringConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
coordinator = entry.runtime_data
async_add_entities(HotSpringSwitch(coordinator, d) for d in SWITCHES)
class HotSpringSwitch(CoordinatorEntity[HotSpringCoordinator], SwitchEntity):
_attr_has_entity_name = True
entity_description: HotSpringSwitchDescription
def __init__(
self,
coordinator: HotSpringCoordinator,
description: HotSpringSwitchDescription,
) -> None:
super().__init__(coordinator)
self.entity_description = description
root = coordinator.spa_info.root_topic if coordinator.spa_info else "spa"
self._attr_unique_id = f"{root}_{description.key}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, root)},
manufacturer=MANUFACTURER,
name=coordinator.spa_info.name if coordinator.spa_info else "Hot Spring Spa",
model="Connected Spa",
)
@property
def is_on(self) -> bool | None:
return self.entity_description.is_on_fn(self.coordinator)
async def async_turn_on(self, **kwargs) -> None:
await self.entity_description.set_fn(self.coordinator, True)
async def async_turn_off(self, **kwargs) -> None:
await self.entity_description.set_fn(self.coordinator, False)
@callback
def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()