"""Support for HomematicIP Cloud valve devices."""

from typing import override

from homematicip.base.functionalChannels import FunctionalChannelType
from homematicip.device import Device

from homeassistant.components.valve import (
    ValveDeviceClass,
    ValveEntity,
    ValveEntityFeature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .entity import HomematicipGenericEntity
from .hap import HomematicIPConfigEntry, HomematicipHAP


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: HomematicIPConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the HomematicIP valves from a config entry."""
    hap = config_entry.runtime_data
    entities = [
        HomematicipWateringValve(hap, device, ch.index)
        for device in hap.home.devices
        for ch in device.functionalChannels
        if ch.functionalChannelType == FunctionalChannelType.WATERING_ACTUATOR_CHANNEL
    ]

    async_add_entities(entities)


class HomematicipWateringValve(HomematicipGenericEntity, ValveEntity):
    """Representation of a HomematicIP valve."""

    _attr_reports_position = False
    _attr_supported_features = ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE
    _attr_device_class = ValveDeviceClass.WATER

    def __init__(self, hap: HomematicipHAP, device: Device, channel: int) -> None:
        """Initialize the valve."""
        super().__init__(
            hap,
            device=device,
            channel=channel,
            post="watering",
            is_multi_channel=True,
            feature_id="watering",
        )

    @override
    async def async_open_valve(self) -> None:
        """Open the valve."""
        channel = self.get_channel_or_raise()
        await channel.set_watering_switch_state_async(True)

    @override
    async def async_close_valve(self) -> None:
        """Close valve."""
        channel = self.get_channel_or_raise()
        await channel.set_watering_switch_state_async(False)

    @property
    @override
    def is_closed(self) -> bool:
        """Return if the valve is closed."""
        channel = self.get_channel_or_raise()
        return channel.wateringActive is False
