"""Setup Mullvad VPN Binary Sensors."""

from typing import override

from homeassistant.components.binary_sensor import (
    BinarySensorDeviceClass,
    BinarySensorEntity,
    BinarySensorEntityDescription,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import MullvadConfigEntry, MullvadCoordinator

BINARY_SENSORS = (
    BinarySensorEntityDescription(
        key="mullvad_exit_ip",
        translation_key="exit_ip",
        device_class=BinarySensorDeviceClass.CONNECTIVITY,
    ),
)


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: MullvadConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Defer sensor setup to the shared sensor module."""
    coordinator = config_entry.runtime_data

    async_add_entities(
        MullvadBinarySensor(coordinator, entity_description, config_entry)
        for entity_description in BINARY_SENSORS
    )


class MullvadBinarySensor(CoordinatorEntity[MullvadCoordinator], BinarySensorEntity):
    """Represents a Mullvad binary sensor."""

    _attr_has_entity_name = True

    def __init__(
        self,
        coordinator: MullvadCoordinator,
        entity_description: BinarySensorEntityDescription,
        config_entry: MullvadConfigEntry,
    ) -> None:
        """Initialize the Mullvad binary sensor."""
        super().__init__(coordinator)
        self.entity_description = entity_description
        self._attr_unique_id = f"{config_entry.entry_id}_{entity_description.key}"
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, config_entry.entry_id)},
            name="Mullvad VPN",
            manufacturer="Mullvad",
        )

    @property
    @override
    def is_on(self) -> bool:
        """Return the state for this binary sensor."""
        return self.coordinator.data[self.entity_description.key]
