core/homeassistant/components/stookalert/binary_sensor.py

57 lines
1.9 KiB
Python
Raw Normal View History

2021-10-08 09:34:22 +00:00
"""This integration provides support for Stookalert Binary Sensor."""
from __future__ import annotations
from datetime import timedelta
import stookalert
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
2021-10-08 09:34:22 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
2021-10-08 09:34:22 +00:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_PROVINCE, DOMAIN
2021-10-08 09:34:22 +00:00
SCAN_INTERVAL = timedelta(minutes=60)
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)
class StookalertBinarySensor(BinarySensorEntity):
2021-10-08 09:34:22 +00:00
"""Defines a Stookalert binary sensor."""
_attr_attribution = "Data provided by rivm.nl"
_attr_device_class = BinarySensorDeviceClass.SAFETY
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
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, f"{entry.entry_id}")},
name=entry.data[CONF_PROVINCE],
manufacturer="RIVM",
model="Stookalert",
entry_type=DeviceEntryType.SERVICE,
configuration_url="https://www.rivm.nl/stookalert",
)
2021-10-08 09:34:22 +00:00
def update(self) -> None:
"""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