"""Support for Freebox devices (Freebox v6 and Freebox mini 4K).""" from __future__ import annotations import logging from typing import Any from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( EntityCategory, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN from .router import FreeboxRouter _LOGGER = logging.getLogger(__name__) RAID_SENSORS: tuple[BinarySensorEntityDescription, ...] = ( BinarySensorEntityDescription( key="raid_degraded", name="degraded", device_class=BinarySensorDeviceClass.PROBLEM, entity_category=EntityCategory.DIAGNOSTIC, ), ) async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up the binary sensors.""" router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] _LOGGER.debug("%s - %s - %s raid(s)", router.name, router.mac, len(router.raids)) binary_entities = [ FreeboxRaidDegradedSensor(router, raid, description) for raid in router.raids.values() for description in RAID_SENSORS ] if binary_entities: async_add_entities(binary_entities, True) class FreeboxRaidDegradedSensor(BinarySensorEntity): """Representation of a Freebox raid sensor.""" _attr_should_poll = False _attr_has_entity_name = True def __init__( self, router: FreeboxRouter, raid: dict[str, Any], description: BinarySensorEntityDescription, ) -> None: """Initialize a Freebox raid degraded sensor.""" self.entity_description = description self._router = router self._attr_device_info = router.device_info self._raid = raid self._attr_name = f"Raid array {raid['id']} {description.name}" self._attr_unique_id = ( f"{router.mac} {description.key} {raid['name']} {raid['id']}" ) @callback def async_update_state(self) -> None: """Update the Freebox Raid sensor.""" self._raid = self._router.raids[self._raid["id"]] @property def is_on(self) -> bool: """Return true if degraded.""" return self._raid["degraded"] @callback def async_on_demand_update(self): """Update state.""" self.async_update_state() self.async_write_ha_state() async def async_added_to_hass(self) -> None: """Register state update callback.""" self.async_update_state() self.async_on_remove( async_dispatcher_connect( self.hass, self._router.signal_sensor_update, self.async_on_demand_update, ) )