Michaels Wunsch: die Adresse soll man sehen, ohne erst einen Sensor einzuschalten - man braucht sie fuer die Firmware-API, zum Anpingen oder um ein Geraet wiederzufinden. ha-ninlight macht es genauso. WLAN-Signal, Laufzeit und freier Speicher bleiben aus: die braucht man erst im Fehlerfall.
118 lines
3.8 KiB
Python
118 lines
3.8 KiB
Python
"""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, ...] = (
|
|
# Von Haus aus an: die Adresse ist das, was man im Alltag sucht - fuer die
|
|
# Firmware-API, zum Anpingen oder um ein Geraet wiederzufinden. Wie in
|
|
# ha-ninlight.
|
|
NinstripSensorDescription(
|
|
key="ip_address",
|
|
translation_key="ip_address",
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
value=lambda d: _wifi(d, "ip"),
|
|
),
|
|
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)
|