From f30821e0a8df513155101d58d94db64900919782 Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Fri, 12 Jan 2024 22:30:38 +0100 Subject: [PATCH] Prepare support for weather warning --- custom_components/irm_kmi/binary_sensor.py | 48 ++++++++++++++++++++++ custom_components/irm_kmi/const.py | 2 +- custom_components/irm_kmi/coordinator.py | 2 +- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 custom_components/irm_kmi/binary_sensor.py diff --git a/custom_components/irm_kmi/binary_sensor.py b/custom_components/irm_kmi/binary_sensor.py new file mode 100644 index 0000000..531c35c --- /dev/null +++ b/custom_components/irm_kmi/binary_sensor.py @@ -0,0 +1,48 @@ +"""Sensor to signal weather warning from the IRM KMI""" + +import logging + +from homeassistant.components import binary_sensor +from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import DeviceInfo, DeviceEntryType +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from custom_components.irm_kmi import IrmKmiCoordinator, DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback): + """Set up the binary platform""" + coordinator = hass.data[DOMAIN][entry.entry_id] + async_add_entities([IrmKmiWarning(coordinator, entry)]) + + +class IrmKmiWarning(CoordinatorEntity, BinarySensorEntity): + """Representation of a weather warning binary sensor""" + + def __init__(self, + coordinator: IrmKmiCoordinator, + entry: ConfigEntry + ) -> None: + _LOGGER.info(f"{entry.entry_id}, {entry.title}") + super().__init__(coordinator) + BinarySensorEntity.__init__(self) + self._attr_device_class = BinarySensorDeviceClass.SAFETY + self._attr_unique_id = entry.entry_id + self.entity_id = binary_sensor.ENTITY_ID_FORMAT.format(f"weather_warning_{str(entry.title).lower()}") + self._attr_name = f"Warning {entry.title}" + self._attr_device_info = DeviceInfo( + entry_type=DeviceEntryType.SERVICE, + identifiers={(DOMAIN, entry.entry_id)}, + manufacturer="IRM KMI", + name=f"Warning {entry.title}" + ) + + @property + def is_on(self) -> bool | None: + # TODO return a real value but first, change implementation of coordinator to expose the data + return True diff --git a/custom_components/irm_kmi/const.py b/custom_components/irm_kmi/const.py index 862438c..c710614 100644 --- a/custom_components/irm_kmi/const.py +++ b/custom_components/irm_kmi/const.py @@ -15,7 +15,7 @@ from homeassistant.components.weather import (ATTR_CONDITION_CLEAR_NIGHT, from homeassistant.const import Platform DOMAIN: Final = 'irm_kmi' -PLATFORMS: Final = [Platform.WEATHER, Platform.CAMERA] +PLATFORMS: Final = [Platform.WEATHER, Platform.CAMERA, Platform.BINARY_SENSOR] CONFIG_FLOW_VERSION = 3 OUT_OF_BENELUX: Final = ["außerhalb der Benelux (Brussels)", diff --git a/custom_components/irm_kmi/coordinator.py b/custom_components/irm_kmi/coordinator.py index a70d2b5..f326fe7 100644 --- a/custom_components/irm_kmi/coordinator.py +++ b/custom_components/irm_kmi/coordinator.py @@ -39,7 +39,7 @@ class IrmKmiCoordinator(DataUpdateCoordinator): # Name of the data. For logging purposes. name="IRM KMI weather", # Polling interval. Will only be polled if there are subscribers. - update_interval=timedelta(seconds=15), + update_interval=timedelta(minutes=7), ) self._api_client = IrmKmiApiClient(session=async_get_clientsession(hass)) self._zone = get_config_value(entry, CONF_ZONE)