mirror of
https://github.com/home-assistant/core.git
synced 2026-08-28 10:16:02 -05:00
140 lines
4.3 KiB
Python
140 lines
4.3 KiB
Python
"""NINA binary sensor platform."""
|
|
|
|
from typing import Any, override
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
BinarySensorDeviceClass,
|
|
BinarySensorEntity,
|
|
)
|
|
from homeassistant.const import ATTR_ID
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
from .const import (
|
|
ATTR_AFFECTED_AREAS,
|
|
ATTR_DESCRIPTION,
|
|
ATTR_EXPIRES,
|
|
ATTR_HEADLINE,
|
|
ATTR_RECOMMENDED_ACTIONS,
|
|
ATTR_SENDER,
|
|
ATTR_SENT,
|
|
ATTR_SEVERITY,
|
|
ATTR_START,
|
|
ATTR_WEB,
|
|
CONF_MESSAGE_SLOTS,
|
|
CONF_REGIONS,
|
|
SERVICE_DATA_AFFECTED_AREAS,
|
|
SERVICE_DATA_DESCRIPTION,
|
|
SERVICE_DATA_EXPIRES,
|
|
SERVICE_DATA_HEADLINE,
|
|
SERVICE_DATA_ID,
|
|
SERVICE_DATA_RECOMMENDED_ACTIONS,
|
|
SERVICE_DATA_SENDER,
|
|
SERVICE_DATA_SENT,
|
|
SERVICE_DATA_SEVERITY,
|
|
SERVICE_DATA_START,
|
|
SERVICE_DATA_WEB,
|
|
)
|
|
from .coordinator import NinaConfigEntry, NINADataUpdateCoordinator
|
|
from .entity import NinaEntity
|
|
|
|
|
|
async def async_setup_entry(
|
|
_: HomeAssistant,
|
|
config_entry: NinaConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up entries."""
|
|
|
|
coordinator = config_entry.runtime_data
|
|
|
|
regions: dict[str, str] = config_entry.data[CONF_REGIONS]
|
|
message_slots: int = config_entry.data[CONF_MESSAGE_SLOTS]
|
|
|
|
async_add_entities(
|
|
NINAMessage(coordinator, ent, regions[ent], i + 1)
|
|
for ent in coordinator.data
|
|
for i in range(message_slots)
|
|
)
|
|
|
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
|
class NINAMessage(NinaEntity, BinarySensorEntity):
|
|
"""Representation of an NINA warning."""
|
|
|
|
_attr_device_class = BinarySensorDeviceClass.SAFETY
|
|
_attr_has_entity_name = True
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: NINADataUpdateCoordinator,
|
|
region: str,
|
|
region_name: str,
|
|
slot_id: int,
|
|
) -> None:
|
|
"""Initialize."""
|
|
super().__init__(coordinator, region, region_name, slot_id)
|
|
|
|
self._attr_translation_key = "warning"
|
|
self._attr_unique_id = f"{region}-{slot_id}"
|
|
|
|
@property
|
|
@override
|
|
def is_on(self) -> bool:
|
|
"""Return the state of the sensor."""
|
|
if self._get_active_warnings_count() <= self._warning_index:
|
|
return False
|
|
|
|
return self._get_warning_data().is_valid
|
|
|
|
@property
|
|
@override
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
|
"""Return extra attributes of the sensor."""
|
|
if not self.is_on:
|
|
return {}
|
|
|
|
data = self._get_warning_data()
|
|
|
|
return {
|
|
ATTR_HEADLINE: data.headline, # Deprecated, remove in 2026.11
|
|
ATTR_DESCRIPTION: data.description, # Deprecated, remove in 2026.11
|
|
ATTR_SENDER: data.sender, # Deprecated, remove in 2026.11
|
|
ATTR_SEVERITY: data.severity or "Unknown", # Deprecated, remove in 2026.11
|
|
# Deprecated, remove in 2026.11
|
|
ATTR_RECOMMENDED_ACTIONS: data.recommended_actions,
|
|
ATTR_AFFECTED_AREAS: data.affected_areas, # Deprecated, remove in 2026.11
|
|
ATTR_WEB: data.more_info_url, # Deprecated, remove in 2026.11
|
|
ATTR_ID: data.id,
|
|
ATTR_SENT: data.sent.isoformat(), # Deprecated, remove in 2026.11
|
|
ATTR_START: data.start.isoformat()
|
|
if data.start
|
|
else "", # Deprecated, remove in 2026.11
|
|
ATTR_EXPIRES: data.expires.isoformat()
|
|
if data.expires
|
|
else "", # Deprecated, remove in 2026.11
|
|
}
|
|
|
|
def get_details(self) -> dict[str, str] | None:
|
|
"""Return the details of the warning."""
|
|
if not self.is_on:
|
|
return None
|
|
|
|
data = self._get_warning_data()
|
|
|
|
return {
|
|
SERVICE_DATA_HEADLINE: data.headline,
|
|
SERVICE_DATA_DESCRIPTION: data.description,
|
|
SERVICE_DATA_SENDER: data.sender,
|
|
SERVICE_DATA_SEVERITY: data.severity or "Unknown",
|
|
SERVICE_DATA_RECOMMENDED_ACTIONS: data.recommended_actions,
|
|
SERVICE_DATA_AFFECTED_AREAS: data.affected_areas,
|
|
SERVICE_DATA_WEB: data.more_info_url,
|
|
SERVICE_DATA_ID: data.id,
|
|
SERVICE_DATA_SENT: data.sent.isoformat(),
|
|
SERVICE_DATA_START: data.start.isoformat() if data.start else "",
|
|
SERVICE_DATA_EXPIRES: data.expires.isoformat() if data.expires else "",
|
|
}
|