22
33from __future__ import annotations
44
5+ from typing import Any
56from unittest .mock import patch
67
8+ import pytest
79from syrupy .assertion import SnapshotAssertion
810from tuya_sharing import CustomerDevice , Manager
911
10- from homeassistant .const import Platform
12+ from homeassistant .components .fan import (
13+ DOMAIN as FAN_DOMAIN ,
14+ SERVICE_OSCILLATE ,
15+ SERVICE_SET_DIRECTION ,
16+ SERVICE_SET_PRESET_MODE ,
17+ SERVICE_TURN_OFF ,
18+ SERVICE_TURN_ON ,
19+ )
20+ from homeassistant .const import ATTR_ENTITY_ID , Platform
1121from homeassistant .core import HomeAssistant
1222from homeassistant .helpers import entity_registry as er
1323
@@ -29,3 +39,80 @@ async def test_platform_setup_and_discovery(
2939 await initialize_entry (hass , mock_manager , mock_config_entry , mock_devices )
3040
3141 await snapshot_platform (hass , entity_registry , snapshot , mock_config_entry .entry_id )
42+
43+
44+ @patch ("homeassistant.components.tuya.PLATFORMS" , [Platform .FAN ])
45+ @pytest .mark .parametrize (
46+ ("mock_device_code" , "entity_id" , "service" , "service_data" , "expected_commands" ),
47+ [
48+ (
49+ "ks_j9fa8ahzac8uvlfl" ,
50+ "fan.tower_fan_ca_407g_smart" ,
51+ SERVICE_OSCILLATE ,
52+ {"oscillating" : False },
53+ [{"code" : "switch_horizontal" , "value" : False }],
54+ ),
55+ (
56+ "ks_j9fa8ahzac8uvlfl" ,
57+ "fan.tower_fan_ca_407g_smart" ,
58+ SERVICE_OSCILLATE ,
59+ {"oscillating" : True },
60+ [{"code" : "switch_horizontal" , "value" : True }],
61+ ),
62+ (
63+ "fs_g0ewlb1vmwqljzji" ,
64+ "fan.ceiling_fan_with_light" ,
65+ SERVICE_SET_DIRECTION ,
66+ {"direction" : "forward" },
67+ [{"code" : "fan_direction" , "value" : "forward" }],
68+ ),
69+ (
70+ "ks_j9fa8ahzac8uvlfl" ,
71+ "fan.tower_fan_ca_407g_smart" ,
72+ SERVICE_SET_PRESET_MODE ,
73+ {"preset_mode" : "sleep" },
74+ [{"code" : "mode" , "value" : "sleep" }],
75+ ),
76+ (
77+ "fs_g0ewlb1vmwqljzji" ,
78+ "fan.ceiling_fan_with_light" ,
79+ SERVICE_TURN_OFF ,
80+ {},
81+ [{"code" : "switch" , "value" : False }],
82+ ),
83+ (
84+ "fs_g0ewlb1vmwqljzji" ,
85+ "fan.ceiling_fan_with_light" ,
86+ SERVICE_TURN_ON ,
87+ {"preset_mode" : "sleep" },
88+ [{"code" : "switch" , "value" : True }, {"code" : "mode" , "value" : "sleep" }],
89+ ),
90+ ],
91+ )
92+ async def test_action (
93+ hass : HomeAssistant ,
94+ mock_manager : Manager ,
95+ mock_config_entry : MockConfigEntry ,
96+ mock_device : CustomerDevice ,
97+ entity_id : str ,
98+ service : str ,
99+ service_data : dict [str , Any ],
100+ expected_commands : list [dict [str , Any ]],
101+ ) -> None :
102+ """Test fan action."""
103+ await initialize_entry (hass , mock_manager , mock_config_entry , mock_device )
104+
105+ state = hass .states .get (entity_id )
106+ assert state is not None , f"{ entity_id } does not exist"
107+ await hass .services .async_call (
108+ FAN_DOMAIN ,
109+ service ,
110+ {
111+ ATTR_ENTITY_ID : entity_id ,
112+ ** service_data ,
113+ },
114+ blocking = True ,
115+ )
116+ mock_manager .send_commands .assert_called_once_with (
117+ mock_device .id , expected_commands
118+ )
0 commit comments