"""Support for Verisure Smartplugs."""

from time import monotonic
from typing import Any, override

from homeassistant.components.switch import SwitchEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import CONF_GIID, DOMAIN
from .coordinator import VerisureConfigEntry, VerisureDataUpdateCoordinator


async def async_setup_entry(
    hass: HomeAssistant,
    entry: VerisureConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up Verisure alarm control panel from a config entry."""
    coordinator = entry.runtime_data
    async_add_entities(
        VerisureSmartplug(coordinator, serial_number)
        for serial_number in coordinator.data["smart_plugs"]
    )


class VerisureSmartplug(CoordinatorEntity[VerisureDataUpdateCoordinator], SwitchEntity):
    """Representation of a Verisure smartplug."""

    _attr_has_entity_name = True
    _attr_name = None

    def __init__(
        self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
    ) -> None:
        """Initialize the Verisure device."""
        super().__init__(coordinator)
        self._attr_unique_id = serial_number

        self.serial_number = serial_number
        self._change_timestamp: float = 0
        self._state = False
        area = coordinator.data["smart_plugs"][serial_number]["device"]["area"]
        self._attr_device_info = DeviceInfo(
            name=area,
            manufacturer="Verisure",
            model="SmartPlug",
            identifiers={(DOMAIN, serial_number)},
            via_device_id=dr.async_get_device_id_by_identifier(
                coordinator.hass,
                (DOMAIN, coordinator.config_entry.data[CONF_GIID]),
                config_entry_id=coordinator.config_entry.entry_id,
            ),
            configuration_url="https://mypages.verisure.com",
        )

    @property
    @override
    def is_on(self) -> bool:
        """Return true if on."""
        if monotonic() - self._change_timestamp < 10:
            return self._state
        self._state = (
            self.coordinator.data["smart_plugs"][self.serial_number]["currentState"]
            == "ON"
        )
        return self._state

    @property
    @override
    def available(self) -> bool:
        """Return True if entity is available."""
        return (
            super().available
            and self.serial_number in self.coordinator.data["smart_plugs"]
        )

    @override
    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn the smartplug on."""
        await self.async_set_plug_state(True)

    @override
    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn the smartplug off."""
        await self.async_set_plug_state(False)

    async def async_set_plug_state(self, state: bool) -> None:
        """Set smartplug state."""
        command: dict[str, str | dict[str, str]] = (
            self.coordinator.verisure.set_smartplug(self.serial_number, state)
        )
        await self.hass.async_add_executor_job(
            self.coordinator.verisure.request,
            command,
        )
        self._state = state
        self._change_timestamp = monotonic()
        self.async_write_ha_state()
