Add nina integration (#56647)
* Added nina integration * Improvements implemented * Fixed lint errors * Added tests * Improvements implemented * Use client session from HA * Added custom coordinator * Fixed tests * Fix pylint errors * Library updated to 0.1.4 * Optimization of static attributes * Removed unused code * Switched to BinarySensorDeviceClass * Switched to Platform Enum * Improve repetition * Improve repetition * Fix corona filter * Removed intermediate variable Co-authored-by: Marvin Wichmann <marvin@fam-wichmann.de> * Fix black formatting Co-authored-by: Marvin Wichmann <marvin@fam-wichmann.de>
This commit is contained in:
parent
cf371ea8dd
commit
9f7b8d3009
17 changed files with 924 additions and 0 deletions
94
homeassistant/components/nina/binary_sensor.py
Normal file
94
homeassistant/components/nina/binary_sensor.py
Normal file
|
@ -0,0 +1,94 @@
|
|||
"""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
|
||||
|
||||
from . import NINADataUpdateCoordinator
|
||||
from .const import (
|
||||
ATTR_EXPIRES,
|
||||
ATTR_HEADLINE,
|
||||
ATTR_ID,
|
||||
ATTR_SENT,
|
||||
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)
|
||||
|
||||
|
||||
class NINAMessage(CoordinatorEntity, BinarySensorEntity):
|
||||
"""Representation of an NINA warning."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NINADataUpdateCoordinator,
|
||||
region: str,
|
||||
regionName: str,
|
||||
slotID: int,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self._region: str = region
|
||||
self._region_name: str = regionName
|
||||
self._slot_id: int = slotID
|
||||
self._warning_index: int = slotID - 1
|
||||
|
||||
self._coordinator: NINADataUpdateCoordinator = coordinator
|
||||
|
||||
self._attr_name: str = f"Warning: {self._region_name} {self._slot_id}"
|
||||
self._attr_unique_id: str = f"{self._region}-{self._slot_id}"
|
||||
self._attr_device_class: str = BinarySensorDeviceClass.SAFETY
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return the state of the sensor."""
|
||||
return len(self._coordinator.data[self._region]) > self._warning_index
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
"""Return extra attributes of the sensor."""
|
||||
if (
|
||||
not len(self._coordinator.data[self._region]) > self._warning_index
|
||||
) or not self.is_on:
|
||||
return {}
|
||||
|
||||
data: dict[str, Any] = self._coordinator.data[self._region][self._warning_index]
|
||||
|
||||
return {
|
||||
ATTR_HEADLINE: data[ATTR_HEADLINE],
|
||||
ATTR_ID: data[ATTR_ID],
|
||||
ATTR_SENT: data[ATTR_SENT],
|
||||
ATTR_START: data[ATTR_START],
|
||||
ATTR_EXPIRES: data[ATTR_EXPIRES],
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue