2021-12-05 14:11:02 +00:00
|
|
|
"""NINA sensor platform."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDeviceClass,
|
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
2022-10-02 15:57:32 +00:00
|
|
|
from . import NINADataUpdateCoordinator
|
2021-12-05 14:11:02 +00:00
|
|
|
from .const import (
|
2023-08-22 20:23:34 +00:00
|
|
|
ATTR_AFFECTED_AREAS,
|
2022-03-29 08:34:09 +00:00
|
|
|
ATTR_DESCRIPTION,
|
2021-12-05 14:11:02 +00:00
|
|
|
ATTR_EXPIRES,
|
|
|
|
ATTR_HEADLINE,
|
|
|
|
ATTR_ID,
|
2022-12-27 10:24:54 +00:00
|
|
|
ATTR_RECOMMENDED_ACTIONS,
|
2022-03-29 08:34:09 +00:00
|
|
|
ATTR_SENDER,
|
2021-12-05 14:11:02 +00:00
|
|
|
ATTR_SENT,
|
2022-03-29 08:34:09 +00:00
|
|
|
ATTR_SEVERITY,
|
2021-12-05 14:11:02 +00:00
|
|
|
ATTR_START,
|
|
|
|
CONF_MESSAGE_SLOTS,
|
|
|
|
CONF_REGIONS,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up entries."""
|
|
|
|
|
|
|
|
coordinator: NINADataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
|
|
|
|
regions: dict[str, str] = config_entry.data[CONF_REGIONS]
|
|
|
|
message_slots: int = config_entry.data[CONF_MESSAGE_SLOTS]
|
|
|
|
|
|
|
|
entities: list[NINAMessage] = []
|
|
|
|
|
|
|
|
for ent in coordinator.data:
|
|
|
|
for i in range(0, message_slots):
|
|
|
|
entities.append(NINAMessage(coordinator, ent, regions[ent], i + 1))
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2022-03-21 13:14:46 +00:00
|
|
|
class NINAMessage(CoordinatorEntity[NINADataUpdateCoordinator], BinarySensorEntity):
|
2021-12-05 14:11:02 +00:00
|
|
|
"""Representation of an NINA warning."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: NINADataUpdateCoordinator,
|
|
|
|
region: str,
|
2021-12-17 15:14:59 +00:00
|
|
|
region_name: str,
|
|
|
|
slot_id: int,
|
2021-12-05 14:11:02 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
|
2022-11-29 19:31:25 +00:00
|
|
|
self._region = region
|
|
|
|
self._warning_index = slot_id - 1
|
2021-12-05 14:11:02 +00:00
|
|
|
|
2022-11-29 19:31:25 +00:00
|
|
|
self._attr_name = f"Warning: {region_name} {slot_id}"
|
|
|
|
self._attr_unique_id = f"{region}-{slot_id}"
|
|
|
|
self._attr_device_class = BinarySensorDeviceClass.SAFETY
|
2021-12-05 14:11:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return the state of the sensor."""
|
2023-08-22 20:23:34 +00:00
|
|
|
if len(self.coordinator.data[self._region]) <= self._warning_index:
|
2022-10-02 04:22:18 +00:00
|
|
|
return False
|
|
|
|
|
2022-10-02 15:57:32 +00:00
|
|
|
data = self.coordinator.data[self._region][self._warning_index]
|
2022-10-02 04:22:18 +00:00
|
|
|
|
|
|
|
return data.is_valid
|
2021-12-05 14:11:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
|
|
|
"""Return extra attributes of the sensor."""
|
2022-10-02 04:22:18 +00:00
|
|
|
if not self.is_on:
|
2021-12-05 14:11:02 +00:00
|
|
|
return {}
|
|
|
|
|
2022-10-02 15:57:32 +00:00
|
|
|
data = self.coordinator.data[self._region][self._warning_index]
|
2021-12-05 14:11:02 +00:00
|
|
|
|
|
|
|
return {
|
2022-10-02 04:22:18 +00:00
|
|
|
ATTR_HEADLINE: data.headline,
|
|
|
|
ATTR_DESCRIPTION: data.description,
|
|
|
|
ATTR_SENDER: data.sender,
|
|
|
|
ATTR_SEVERITY: data.severity,
|
2022-12-27 10:24:54 +00:00
|
|
|
ATTR_RECOMMENDED_ACTIONS: data.recommended_actions,
|
2023-08-22 20:23:34 +00:00
|
|
|
ATTR_AFFECTED_AREAS: data.affected_areas,
|
2022-10-02 04:22:18 +00:00
|
|
|
ATTR_ID: data.id,
|
|
|
|
ATTR_SENT: data.sent,
|
|
|
|
ATTR_START: data.start,
|
|
|
|
ATTR_EXPIRES: data.expires,
|
2021-12-05 14:11:02 +00:00
|
|
|
}
|