"""Remote sensor platform for Unfolded Circle"""
from typing import Any
import logging

from homeassistant.components.remote import RemoteEntity, RemoteEntityFeature, RemoteEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity import ToggleEntityDescription
from homeassistant.helpers.device_registry import DeviceInfo
from collections.abc import Iterable
from .const import DOMAIN

from homeassistant.const import (
    ATTR_BATTERY_CHARGING,
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
    async_add_entities: AddEntitiesCallback,
) -> None:
    remote = hass.data[DOMAIN][config_entry.entry_id]

    # Verify that passed in configuration works
    if not await remote.can_connect():
        _LOGGER.error("Could not connect to Remote")
        return

    # Get Basic Device Information
    await remote.update()
    await remote.get_remotes()
    await remote.get_remote_codesets()
    await remote.get_docks()

    new_devices = []
    new_devices.append(RemoteSensor(remote))
    if new_devices:
        async_add_entities(new_devices)


class RemoteSensor(RemoteEntity):
    # The class of this device. Note the value should come from the homeassistant.const
    # module. More information on the available devices classes can be seen here:
    # https://developers.home-assistant.io/docs/core/entity/sensor
    _attr_icon = "mdi:remote"
    entity_description: ToggleEntityDescription
    _attr_supported_features: RemoteEntityFeature = RemoteEntityFeature.ACTIVITY

    @property
    def device_info(self) -> DeviceInfo:
        """Return the device info."""
        return DeviceInfo(
            identifiers={
                # Serial numbers are unique identifiers within a specific domain
                (DOMAIN, self._remote.serial_number)
            },
            name=self._remote.name,
            manufacturer=self._remote.manufacturer,
            model=self._remote.model_name,
            sw_version=self._remote.sw_version,
            hw_version=self._remote.hw_revision,
            configuration_url=self._remote.configuration_url,
        )

    def __init__(self, remote):
        """Initialize the sensor."""
        self._remote = remote

        # As per the sensor, this must be a unique value within this domain. This is done
        # by using the device ID, and appending "_battery"
        self._attr_unique_id = f"{self._remote.serial_number}_remote"

        # The name of the entity
        self._attr_name = f"{self._remote.name} Remote"
        self._attr_activity_list = []
        self._attr_is_on = False
        for activity in self._remote.activities:
            self._attr_activity_list.append(activity.name)

    async def async_turn_on(self, **kwargs: Any) -> None:
        """Turn the entity on."""
        self._attr_is_on = True

    async def async_turn_off(self, **kwargs: Any) -> None:
        """Turn the entity off."""
        self._attr_is_on = False

    async def async_send_command(self, command: Iterable[str], **kwargs):
        for indv_command in command:
            await self._remote.send_remote_command(
                device=kwargs.get("device"),
                command=indv_command,
                repeat=kwargs.get("num_repeats"),
            )
