2021-10-08 09:34:22 +00:00
|
|
|
"""This integration provides support for Stookalert Binary Sensor."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-08 20:03:50 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
import stookalert
|
|
|
|
|
2020-08-30 16:01:04 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-02 03:16:09 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-08-30 16:01:04 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2021-12-21 11:53:19 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-10-08 09:34:22 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-11-22 17:14:15 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
2021-10-14 20:31:12 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-10-08 09:34:22 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2021-12-21 11:53:19 +00:00
|
|
|
from .const import CONF_PROVINCE, DOMAIN
|
2020-01-08 20:03:50 +00:00
|
|
|
|
2021-10-08 09:34:22 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=60)
|
2020-01-08 20:03:50 +00:00
|
|
|
|
2021-10-08 09:34:22 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up Stookalert binary sensor from a config entry."""
|
|
|
|
client = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities([StookalertBinarySensor(client, entry)], update_before_add=True)
|
2020-01-08 20:03:50 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class StookalertBinarySensor(BinarySensorEntity):
|
2021-10-08 09:34:22 +00:00
|
|
|
"""Defines a Stookalert binary sensor."""
|
2020-01-08 20:03:50 +00:00
|
|
|
|
2021-12-21 11:53:19 +00:00
|
|
|
_attr_attribution = "Data provided by rivm.nl"
|
2021-12-02 03:16:09 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.SAFETY
|
2020-01-08 20:03:50 +00:00
|
|
|
|
2021-10-08 09:34:22 +00:00
|
|
|
def __init__(self, client: stookalert.stookalert, entry: ConfigEntry) -> None:
|
|
|
|
"""Initialize a Stookalert device."""
|
|
|
|
self._client = client
|
|
|
|
self._attr_name = f"Stookalert {entry.data[CONF_PROVINCE]}"
|
|
|
|
self._attr_unique_id = entry.unique_id
|
2021-10-14 20:31:12 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, f"{entry.entry_id}")},
|
|
|
|
name=entry.data[CONF_PROVINCE],
|
|
|
|
manufacturer="RIVM",
|
|
|
|
model="Stookalert",
|
2021-11-22 17:14:15 +00:00
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
2021-10-14 20:31:12 +00:00
|
|
|
configuration_url="https://www.rivm.nl/stookalert",
|
|
|
|
)
|
2021-10-08 09:34:22 +00:00
|
|
|
|
|
|
|
def update(self) -> None:
|
2020-01-08 20:03:50 +00:00
|
|
|
"""Update the data from the Stookalert handler."""
|
2021-10-08 09:34:22 +00:00
|
|
|
self._client.get_alerts()
|
|
|
|
self._attr_is_on = self._client.state == 1
|