"""The Homee select platform."""

from typing import override

from pyHomee.const import AttributeType
from pyHomee.model import HomeeAttribute, HomeeNode

from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import HomeeConfigEntry
from .entity import HomeeEntity
from .helpers import setup_homee_platform

PARALLEL_UPDATES = 0

SELECT_DESCRIPTIONS: dict[AttributeType, SelectEntityDescription] = {
    AttributeType.DISPLAY_TEMPERATURE_SELECTION: SelectEntityDescription(
        key="display_temperature_selection",
        options=["target", "current"],
        entity_category=EntityCategory.CONFIG,
    ),
    AttributeType.REPEATER_MODE: SelectEntityDescription(
        key="repeater_mode",
        options=["off", "level1", "level2"],
        entity_category=EntityCategory.CONFIG,
    ),
}


async def add_select_entities(
    hass: HomeAssistant,
    config_entry: HomeeConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
    nodes: list[HomeeNode],
) -> None:
    """Add homee select entities."""
    async_add_entities(
        HomeeSelect(hass, attribute, config_entry, SELECT_DESCRIPTIONS[attribute.type])
        for node in nodes
        for attribute in node.attributes
        if attribute.type in SELECT_DESCRIPTIONS and attribute.editable
    )


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: HomeeConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Add the homee platform for the select component."""

    await setup_homee_platform(
        hass, add_select_entities, async_add_entities, config_entry
    )


class HomeeSelect(HomeeEntity, SelectEntity):
    """Representation of a Homee select entity."""

    def __init__(
        self,
        hass: HomeAssistant,
        attribute: HomeeAttribute,
        entry: HomeeConfigEntry,
        description: SelectEntityDescription,
    ) -> None:
        """Initialize a Homee select entity."""
        super().__init__(hass, attribute, entry)
        self.entity_description = description
        assert description.options is not None
        self._attr_options = description.options
        self._attr_translation_key = description.key

    @property
    @override
    def current_option(self) -> str:
        """Return the current selected option."""
        return self.options[int(self._attribute.current_value)]

    @override
    async def async_select_option(self, option: str) -> None:
        """Change the selected option."""
        await self.async_set_homee_value(self.options.index(option))
