"""Support for Flo Water Monitor binary sensors."""

from typing import Any, override

from homeassistant.components.binary_sensor import (
    BinarySensorDeviceClass,
    BinarySensorEntity,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .coordinator import FloConfigEntry
from .entity import FloEntity


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: FloConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the Flo sensors from config entry."""
    devices = config_entry.runtime_data.devices
    entities: list[BinarySensorEntity] = []
    for device in devices:
        if device.device_type == "puck_oem":
            # Flo "pucks" (leak detectors) *do* support pending alerts.
            # However these pending alerts mix unrelated issues like
            # low-battery alerts, humidity alerts, & temperature alerts
            # in addition to the critical "water detected" alert.
            #
            # Since there are non-binary sensors for battery, humidity,
            # and temperature, the binary sensor should only cover
            # water detection. If this sensor trips, you really have
            # a problem - vs. battery/temp/humidity which are warnings.
            entities.append(FloWaterDetectedBinarySensor(device))
        else:
            entities.append(FloPendingAlertsBinarySensor(device))
    async_add_entities(entities)


class FloPendingAlertsBinarySensor(FloEntity, BinarySensorEntity):
    """Binary sensor that reports on if there are any pending system alerts."""

    _attr_device_class = BinarySensorDeviceClass.PROBLEM
    _attr_translation_key = "pending_system_alerts"

    def __init__(self, device):
        """Initialize the pending alerts binary sensor."""
        super().__init__("pending_system_alerts", device)

    @property
    @override
    def is_on(self) -> bool:
        """Return true if the Flo device has pending alerts."""
        return self._device.has_alerts

    @property
    @override
    def extra_state_attributes(self) -> dict[str, Any]:
        """Return the state attributes."""
        if not self._device.has_alerts:
            return {}
        return {
            "info": self._device.pending_info_alerts_count,
            "warning": self._device.pending_warning_alerts_count,
            "critical": self._device.pending_critical_alerts_count,
        }


class FloWaterDetectedBinarySensor(FloEntity, BinarySensorEntity):
    """Binary sensor that reports if water is detected (for leak detectors)."""

    _attr_device_class = BinarySensorDeviceClass.PROBLEM
    _attr_translation_key = "water_detected"

    def __init__(self, device):
        """Initialize the pending alerts binary sensor."""
        super().__init__("water_detected", device)

    @property
    @override
    def is_on(self) -> bool:
        """Return true if the Flo device is detecting water."""
        return self._device.water_detected
