"""Test Tuya select platform.""" from __future__ import annotations from unittest.mock import patch import pytest from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice from homeassistant.components.select import ( DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, ) from homeassistant.components.tuya import ManagerCompat from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import entity_registry as er from . import DEVICE_MOCKS, initialize_entry from tests.common import MockConfigEntry, snapshot_platform @pytest.mark.parametrize( "mock_device_code", [k for k, v in DEVICE_MOCKS.items() if Platform.SELECT in v] ) @patch("homeassistant.components.tuya.PLATFORMS", [Platform.SELECT]) async def test_platform_setup_and_discovery( hass: HomeAssistant, mock_manager: ManagerCompat, mock_config_entry: MockConfigEntry, mock_device: CustomerDevice, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Test platform setup and discovery.""" await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) @pytest.mark.parametrize( "mock_device_code", [k for k, v in DEVICE_MOCKS.items() if Platform.SELECT not in v] ) @patch("homeassistant.components.tuya.PLATFORMS", [Platform.SELECT]) async def test_platform_setup_no_discovery( hass: HomeAssistant, mock_manager: ManagerCompat, mock_config_entry: MockConfigEntry, mock_device: CustomerDevice, entity_registry: er.EntityRegistry, ) -> None: """Test platform setup without discovery.""" await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) assert not er.async_entries_for_config_entry( entity_registry, mock_config_entry.entry_id ) @pytest.mark.parametrize( "mock_device_code", ["cl_am43_corded_motor_zigbee_cover"], ) async def test_select_option( hass: HomeAssistant, mock_manager: ManagerCompat, 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, { "entity_id": entity_id, "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_am43_corded_motor_zigbee_cover"], ) async def test_select_invalid_option( hass: HomeAssistant, mock_manager: ManagerCompat, 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, { "entity_id": entity_id, "option": "hello", }, blocking=True, ) assert exc.value.translation_key == "not_valid_option"