Skip to content

Commit 12c04f5

Browse files
authored
Use pytest.parametrize in Tuya siren/switch/valve tests (home-assistant#156920)
1 parent 3ad1c6a commit 12c04f5

File tree

9 files changed

+60
-104
lines changed

9 files changed

+60
-104
lines changed

tests/components/tuya/test_button.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ async def test_platform_setup_and_discovery(
3939
"mock_device_code",
4040
["sd_lr33znaodtyarrrz"],
4141
)
42-
async def test_button_press(
42+
async def test_action(
4343
hass: HomeAssistant,
4444
mock_manager: Manager,
4545
mock_config_entry: MockConfigEntry,
4646
mock_device: CustomerDevice,
4747
) -> None:
48-
"""Test pressing a button."""
48+
"""Test button action."""
4949
entity_id = "button.v20_reset_duster_cloth"
5050
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
5151

tests/components/tuya/test_camera.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ async def test_platform_setup_and_discovery(
7272
),
7373
],
7474
)
75-
async def test_motion_detection(
75+
async def test_action(
7676
hass: HomeAssistant,
7777
mock_manager: Manager,
7878
mock_config_entry: MockConfigEntry,
7979
mock_device: CustomerDevice,
8080
service: str,
8181
expected_command: dict[str, Any],
8282
) -> None:
83-
"""Test turning off a switch."""
83+
"""Test camera action."""
8484
entity_id = "camera.burocam"
8585
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
8686

tests/components/tuya/test_climate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def test_action(
7171
service_data: dict[str, Any],
7272
expected_command: dict[str, Any],
7373
) -> None:
74-
"""Test service action."""
74+
"""Test climate action."""
7575
entity_id = "climate.air_conditioner"
7676
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
7777

tests/components/tuya/test_humidifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async def test_action(
6666
service_data: dict[str, Any],
6767
expected_command: dict[str, Any],
6868
) -> None:
69-
"""Test service action."""
69+
"""Test humidifier action."""
7070
entity_id = "humidifier.dehumidifier"
7171
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
7272

tests/components/tuya/test_light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def test_action(
107107
service_data: dict[str, Any],
108108
expected_commands: list[dict[str, Any]],
109109
) -> None:
110-
"""Test service action."""
110+
"""Test light action."""
111111
entity_id = "light.garage_light"
112112
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
113113

tests/components/tuya/test_siren.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from typing import Any
56
from unittest.mock import patch
67

78
import pytest
@@ -42,56 +43,41 @@ async def test_platform_setup_and_discovery(
4243
"mock_device_code",
4344
["sp_sdd5f5f2dl5wydjf"],
4445
)
45-
async def test_turn_on(
46-
hass: HomeAssistant,
47-
mock_manager: Manager,
48-
mock_config_entry: MockConfigEntry,
49-
mock_device: CustomerDevice,
50-
) -> None:
51-
"""Test turning on."""
52-
entity_id = "siren.c9"
53-
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
54-
55-
state = hass.states.get(entity_id)
56-
assert state is not None, f"{entity_id} does not exist"
57-
await hass.services.async_call(
58-
SIREN_DOMAIN,
59-
SERVICE_TURN_ON,
60-
{
61-
ATTR_ENTITY_ID: entity_id,
62-
},
63-
blocking=True,
64-
)
65-
mock_manager.send_commands.assert_called_once_with(
66-
mock_device.id, [{"code": "siren_switch", "value": True}]
67-
)
68-
69-
70-
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.SIREN])
7146
@pytest.mark.parametrize(
72-
"mock_device_code",
73-
["sp_sdd5f5f2dl5wydjf"],
47+
("service", "expected_commands"),
48+
[
49+
(
50+
SERVICE_TURN_ON,
51+
[{"code": "siren_switch", "value": True}],
52+
),
53+
(
54+
SERVICE_TURN_OFF,
55+
[{"code": "siren_switch", "value": False}],
56+
),
57+
],
7458
)
75-
async def test_turn_off(
59+
async def test_action(
7660
hass: HomeAssistant,
7761
mock_manager: Manager,
7862
mock_config_entry: MockConfigEntry,
7963
mock_device: CustomerDevice,
64+
service: str,
65+
expected_commands: list[dict[str, Any]],
8066
) -> None:
81-
"""Test turning off."""
67+
"""Test siren action."""
8268
entity_id = "siren.c9"
8369
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
8470

8571
state = hass.states.get(entity_id)
8672
assert state is not None, f"{entity_id} does not exist"
8773
await hass.services.async_call(
8874
SIREN_DOMAIN,
89-
SERVICE_TURN_OFF,
75+
service,
9076
{
9177
ATTR_ENTITY_ID: entity_id,
9278
},
9379
blocking=True,
9480
)
9581
mock_manager.send_commands.assert_called_once_with(
96-
mock_device.id, [{"code": "siren_switch", "value": False}]
82+
mock_device.id, expected_commands
9783
)

tests/components/tuya/test_switch.py

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,58 +95,43 @@ async def test_sfkzq_deprecated_switch(
9595
"mock_device_code",
9696
["cz_PGEkBctAbtzKOZng"],
9797
)
98-
async def test_turn_on(
99-
hass: HomeAssistant,
100-
mock_manager: Manager,
101-
mock_config_entry: MockConfigEntry,
102-
mock_device: CustomerDevice,
103-
) -> None:
104-
"""Test turning on a switch."""
105-
entity_id = "switch.din_socket"
106-
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
107-
108-
state = hass.states.get(entity_id)
109-
assert state is not None, f"{entity_id} does not exist"
110-
await hass.services.async_call(
111-
SWITCH_DOMAIN,
112-
SERVICE_TURN_ON,
113-
{
114-
ATTR_ENTITY_ID: entity_id,
115-
},
116-
blocking=True,
117-
)
118-
mock_manager.send_commands.assert_called_once_with(
119-
mock_device.id, [{"code": "switch", "value": True}]
120-
)
121-
122-
123-
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.SWITCH])
12498
@pytest.mark.parametrize(
125-
"mock_device_code",
126-
["cz_PGEkBctAbtzKOZng"],
99+
("service", "expected_commands"),
100+
[
101+
(
102+
SERVICE_TURN_ON,
103+
[{"code": "switch", "value": True}],
104+
),
105+
(
106+
SERVICE_TURN_OFF,
107+
[{"code": "switch", "value": False}],
108+
),
109+
],
127110
)
128-
async def test_turn_off(
111+
async def test_action(
129112
hass: HomeAssistant,
130113
mock_manager: Manager,
131114
mock_config_entry: MockConfigEntry,
132115
mock_device: CustomerDevice,
116+
service: str,
117+
expected_commands: list[dict[str, Any]],
133118
) -> None:
134-
"""Test turning off a switch."""
119+
"""Test switch action."""
135120
entity_id = "switch.din_socket"
136121
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
137122

138123
state = hass.states.get(entity_id)
139124
assert state is not None, f"{entity_id} does not exist"
140125
await hass.services.async_call(
141126
SWITCH_DOMAIN,
142-
SERVICE_TURN_OFF,
127+
service,
143128
{
144129
ATTR_ENTITY_ID: entity_id,
145130
},
146131
blocking=True,
147132
)
148133
mock_manager.send_commands.assert_called_once_with(
149-
mock_device.id, [{"code": "switch", "value": False}]
134+
mock_device.id, expected_commands
150135
)
151136

152137

tests/components/tuya/test_vacuum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async def test_action(
100100
service_data: dict[str, Any],
101101
expected_command: dict[str, Any],
102102
) -> None:
103-
"""Test service action."""
103+
"""Test vacuum action."""
104104
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
105105

106106
state = hass.states.get(entity_id)

tests/components/tuya/test_valve.py

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,58 +43,43 @@ async def test_platform_setup_and_discovery(
4343
"mock_device_code",
4444
["sfkzq_ed7frwissyqrejic"],
4545
)
46-
async def test_open_valve(
47-
hass: HomeAssistant,
48-
mock_manager: Manager,
49-
mock_config_entry: MockConfigEntry,
50-
mock_device: CustomerDevice,
51-
) -> None:
52-
"""Test opening a valve."""
53-
entity_id = "valve.jie_hashui_fa_valve_1"
54-
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
55-
56-
state = hass.states.get(entity_id)
57-
assert state is not None, f"{entity_id} does not exist"
58-
await hass.services.async_call(
59-
VALVE_DOMAIN,
60-
SERVICE_OPEN_VALVE,
61-
{
62-
ATTR_ENTITY_ID: entity_id,
63-
},
64-
blocking=True,
65-
)
66-
mock_manager.send_commands.assert_called_once_with(
67-
mock_device.id, [{"code": "switch_1", "value": True}]
68-
)
69-
70-
71-
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.VALVE])
7246
@pytest.mark.parametrize(
73-
"mock_device_code",
74-
["sfkzq_ed7frwissyqrejic"],
47+
("service", "expected_commands"),
48+
[
49+
(
50+
SERVICE_OPEN_VALVE,
51+
[{"code": "switch_1", "value": True}],
52+
),
53+
(
54+
SERVICE_CLOSE_VALVE,
55+
[{"code": "switch_1", "value": False}],
56+
),
57+
],
7558
)
76-
async def test_close_valve(
59+
async def test_action(
7760
hass: HomeAssistant,
7861
mock_manager: Manager,
7962
mock_config_entry: MockConfigEntry,
8063
mock_device: CustomerDevice,
64+
service: str,
65+
expected_commands: list[dict[str, Any]],
8166
) -> None:
82-
"""Test closing a valve."""
67+
"""Test valve action."""
8368
entity_id = "valve.jie_hashui_fa_valve_1"
8469
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
8570

8671
state = hass.states.get(entity_id)
8772
assert state is not None, f"{entity_id} does not exist"
8873
await hass.services.async_call(
8974
VALVE_DOMAIN,
90-
SERVICE_CLOSE_VALVE,
75+
service,
9176
{
9277
ATTR_ENTITY_ID: entity_id,
9378
},
9479
blocking=True,
9580
)
9681
mock_manager.send_commands.assert_called_once_with(
97-
mock_device.id, [{"code": "switch_1", "value": False}]
82+
mock_device.id, expected_commands
9883
)
9984

10085

0 commit comments

Comments
 (0)