feat: erste Fassung der ninstrip-Integration fuer Home Assistant
Auf dem Geruest von ha-ninlight aufgebaut und bewusst parallel gehalten: gleiche Fehlerklassen in api.py, gleicher Coordinator mit Sperre und Fehlertoleranz, gleicher Einrichtungsablauf samt Zeroconf und Reauth. Eigenes Repo statt Erweiterung von ha-ninlight: die Geraete melden sich unter verschiedenen mDNS-Diensten, das Datenmodell unterscheidet sich deutlich (Segmente und Szenen gegen ein einzelnes Licht), und ha-ninlight laeuft produktiv mit vier Lampen - Verzweigungen dort haetten beide Produkte gefaehrdet. Entitaeten: light (Hauptschalter und Helligkeit), select (Szene), button (Neustart), vier Diagnosesensoren. In ha-dev eingebunden und am echten Geraet geprueft: Aus- und Einschalten, Helligkeit 40 Prozent, Szenenwechsel auf Regenbogen - das Geraet meldet jeweils genau das zurueck.
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
"""Diagnosewerte eines ninstrip."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||
EntityCategory,
|
||||
UnitOfInformation,
|
||||
UnitOfTime,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import NinstripConfigEntry, NinstripCoordinator, NinstripData
|
||||
from .entity import NinstripEntity
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class NinstripSensorDescription(SensorEntityDescription):
|
||||
"""Beschreibung samt Zugriff auf den Wert."""
|
||||
|
||||
value: Callable[[NinstripData], Any]
|
||||
|
||||
|
||||
def _wifi(data: NinstripData, key: str) -> Any:
|
||||
wifi = data.info.get("wifi")
|
||||
return wifi.get(key) if isinstance(wifi, dict) else None
|
||||
|
||||
|
||||
def _strip(data: NinstripData, key: str) -> Any:
|
||||
strip = data.info.get("strip")
|
||||
return strip.get(key) if isinstance(strip, dict) else None
|
||||
|
||||
|
||||
SENSORS: tuple[NinstripSensorDescription, ...] = (
|
||||
NinstripSensorDescription(
|
||||
key="rssi",
|
||||
translation_key="rssi",
|
||||
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
||||
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
value=lambda d: _wifi(d, "rssi"),
|
||||
),
|
||||
NinstripSensorDescription(
|
||||
key="uptime",
|
||||
translation_key="uptime",
|
||||
native_unit_of_measurement=UnitOfTime.SECONDS,
|
||||
device_class=SensorDeviceClass.DURATION,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
value=lambda d: d.info.get("uptime"),
|
||||
),
|
||||
NinstripSensorDescription(
|
||||
key="free_heap",
|
||||
translation_key="free_heap",
|
||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
value=lambda d: d.info.get("free_heap"),
|
||||
),
|
||||
# Sagt, ob die Strombegrenzung gerade herunterregelt. 100 heisst: nein.
|
||||
NinstripSensorDescription(
|
||||
key="power_scale",
|
||||
translation_key="power_scale",
|
||||
native_unit_of_measurement="%",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
value=lambda d: _strip(d, "power_scale"),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: NinstripConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
coordinator = entry.runtime_data
|
||||
async_add_entities(NinstripSensor(coordinator, d) for d in SENSORS)
|
||||
|
||||
|
||||
class NinstripSensor(NinstripEntity, SensorEntity):
|
||||
entity_description: NinstripSensorDescription
|
||||
|
||||
def __init__(
|
||||
self, coordinator: NinstripCoordinator, description: NinstripSensorDescription
|
||||
) -> None:
|
||||
super().__init__(coordinator, description.key)
|
||||
self.entity_description = description
|
||||
|
||||
@property
|
||||
def native_value(self) -> Any:
|
||||
return self.entity_description.value(self.coordinator.data)
|
||||
Reference in New Issue
Block a user