93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
"""Sensors for Sternwarte."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity, SensorStateClass
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import PERCENTAGE, UnitOfSpeed
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .const import DOMAIN, ENTITY_ROOF_POSITION, ENTITY_ROOF_STATUS, ENTITY_WIND_SPEED
|
|
from .entity import SternwarteEntity
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up Sternwarte sensors."""
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
async_add_entities(
|
|
[
|
|
SternwarteRoofStatusSensor(coordinator),
|
|
SternwarteRoofPositionSensor(coordinator),
|
|
SternwarteWindSpeedSensor(coordinator),
|
|
]
|
|
)
|
|
|
|
|
|
class SternwarteWindBaseSensor(SternwarteEntity, SensorEntity):
|
|
"""Base wind sensor."""
|
|
|
|
_attr_device_class = SensorDeviceClass.WIND_SPEED
|
|
_attr_native_unit_of_measurement = UnitOfSpeed.KILOMETERS_PER_HOUR
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
|
|
|
|
|
class SternwarteRoofPositionSensor(SternwarteEntity, SensorEntity):
|
|
"""Roof position in percent."""
|
|
|
|
_attr_name = "Dachposition"
|
|
_attr_native_unit_of_measurement = PERCENTAGE
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
|
_attr_icon = "mdi:garage-variant"
|
|
|
|
def __init__(self, coordinator):
|
|
super().__init__(coordinator, ENTITY_ROOF_POSITION)
|
|
|
|
@property
|
|
def native_value(self) -> int:
|
|
return self.coordinator.roof_position_percent
|
|
|
|
|
|
class SternwarteRoofStatusSensor(SternwarteEntity, SensorEntity):
|
|
"""Roof status as text."""
|
|
|
|
_attr_name = "Dachstatus"
|
|
_attr_icon = "mdi:garage"
|
|
|
|
def __init__(self, coordinator):
|
|
super().__init__(coordinator, ENTITY_ROOF_STATUS)
|
|
|
|
@property
|
|
def native_value(self) -> str:
|
|
state = self.coordinator.data
|
|
if state.roof_moving:
|
|
return "in_bewegung"
|
|
if state.roof_open and not state.roof_closed:
|
|
return "offen"
|
|
if state.roof_closed and not state.roof_open:
|
|
return "geschlossen"
|
|
if state.roof_open and state.roof_closed:
|
|
return "sensorfehler"
|
|
return "unbekannt"
|
|
|
|
@property
|
|
def extra_state_attributes(self) -> dict[str, object]:
|
|
return {
|
|
"inputs": list(self.coordinator.data.inputs),
|
|
}
|
|
|
|
|
|
class SternwarteWindSpeedSensor(SternwarteWindBaseSensor):
|
|
_attr_name = "Windgeschwindigkeit"
|
|
|
|
def __init__(self, coordinator):
|
|
super().__init__(coordinator, ENTITY_WIND_SPEED)
|
|
|
|
@property
|
|
def native_value(self) -> float:
|
|
return round(self.coordinator.data.wind_speed_kmh, 1)
|