Files
hotspring/custom_components/hotspring/sensor.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

88 lines
2.9 KiB
Python

"""Sensors: salt output level, cartridge state, current water temperature."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import UnitOfTemperature
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 HotSpringSensorDescription(SensorEntityDescription):
value_fn: Callable[[HotSpringCoordinator], float | str | None]
SENSORS: tuple[HotSpringSensorDescription, ...] = (
HotSpringSensorDescription(
key="water_temperature",
translation_key="water_temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda c: c.spa.current_temperature() if c.spa else None,
),
HotSpringSensorDescription(
key="cartridge",
translation_key="cartridge",
icon="mdi:filter-outline",
value_fn=lambda c: (
None
if not c.spa or c.spa.cartridge_installed() is None
else ("installed" if c.spa.cartridge_installed() else "missing")
),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: HotSpringConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
coordinator = entry.runtime_data
async_add_entities(HotSpringSensor(coordinator, d) for d in SENSORS)
class HotSpringSensor(CoordinatorEntity[HotSpringCoordinator], SensorEntity):
_attr_has_entity_name = True
entity_description: HotSpringSensorDescription
def __init__(
self,
coordinator: HotSpringCoordinator,
description: HotSpringSensorDescription,
) -> 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 native_value(self) -> float | str | None:
return self.entity_description.value_fn(self.coordinator)
@callback
def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()