"""Test Tuya select platform.""" from __future__ import annotations from typing import Any from unittest.mock import patch from freezegun.api import FrozenDateTimeFactory import pytest from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice, Manager from homeassistant.components.select import ( ATTR_OPTION, DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, ) from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import entity_registry as er from . import MockDeviceListener, check_selective_state_update, initialize_entry from tests.common import MockConfigEntry, snapshot_platform @patch("homeassistant.components.tuya.PLATFORMS", [Platform.SELECT]) async def test_platform_setup_and_discovery( hass: HomeAssistant, mock_manager: Manager, mock_config_entry: MockConfigEntry, mock_devices: list[CustomerDevice], entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Test platform setup and discovery.""" await initialize_entry(hass, mock_manager, mock_config_entry, mock_devices) await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) @pytest.mark.parametrize( "mock_device_code", ["cl_zah67ekd"], ) @pytest.mark.parametrize( ("updates", "expected_state", "last_reported"), [ # Update without dpcode - state should not change, last_reported stays at initial ({"control": "stop"}, "forward", "2024-01-01T00:00:00+00:00"), # Update with dpcode - state should change, last_reported advances ({"control_back_mode": "back"}, "back", "2024-01-01T00:01:00+00:00"), # Update with multiple properties including dpcode - state should change ( {"control": "stop", "control_back_mode": "back"}, "back", "2024-01-01T00:01:00+00:00", ), ], ) @patch("homeassistant.components.tuya.PLATFORMS", [Platform.SELECT]) @pytest.mark.freeze_time("2024-01-01") async def test_selective_state_update( hass: HomeAssistant, mock_manager: Manager, mock_config_entry: MockConfigEntry, mock_device: CustomerDevice, mock_listener: MockDeviceListener, freezer: FrozenDateTimeFactory, updates: dict[str, Any], expected_state: str, last_reported: str, ) -> None: """Test skip_update/last_reported.""" await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) await check_selective_state_update( hass, mock_device, mock_listener, freezer, entity_id="select.kitchen_blinds_motor_mode", dpcode="control_back_mode", initial_state="forward", updates=updates, expected_state=expected_state, last_reported=last_reported, ) @pytest.mark.parametrize( "mock_device_code", ["cl_zah67ekd"], ) async def test_select_option( hass: HomeAssistant, mock_manager: Manager, mock_config_entry: MockConfigEntry, mock_device: CustomerDevice, ) -> None: """Test fan mode with windspeed.""" entity_id = "select.kitchen_blinds_motor_mode" await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) state = hass.states.get(entity_id) assert state is not None, f"{entity_id} does not exist" await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, { ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "forward", }, blocking=True, ) mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "control_back_mode", "value": "forward"}] ) @pytest.mark.parametrize( "mock_device_code", ["cl_zah67ekd"], ) async def test_select_invalid_option( hass: HomeAssistant, mock_manager: Manager, mock_config_entry: MockConfigEntry, mock_device: CustomerDevice, ) -> None: """Test fan mode with windspeed.""" entity_id = "select.kitchen_blinds_motor_mode" await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) state = hass.states.get(entity_id) assert state is not None, f"{entity_id} does not exist" with pytest.raises(ServiceValidationError) as exc: await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, { ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "hello", }, blocking=True, ) assert exc.value.translation_key == "not_valid_option"