|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from typing import Any |
5 | 6 | from unittest.mock import patch |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 | from syrupy.assertion import SnapshotAssertion |
9 | 10 | from tuya_sharing import CustomerDevice, Manager |
10 | 11 |
|
11 | | -from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN |
| 12 | +from homeassistant.components.switch import ( |
| 13 | + DOMAIN as SWITCH_DOMAIN, |
| 14 | + SERVICE_TURN_OFF, |
| 15 | + SERVICE_TURN_ON, |
| 16 | +) |
12 | 17 | from homeassistant.components.tuya import DOMAIN |
13 | | -from homeassistant.const import Platform |
| 18 | +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform |
14 | 19 | from homeassistant.core import HomeAssistant |
15 | 20 | from homeassistant.helpers import entity_registry as er, issue_registry as ir |
16 | 21 |
|
@@ -83,3 +88,95 @@ async def test_sfkzq_deprecated_switch( |
83 | 88 | ) |
84 | 89 | is not None |
85 | 90 | ) is expected_issue |
| 91 | + |
| 92 | + |
| 93 | +@patch("homeassistant.components.tuya.PLATFORMS", [Platform.SWITCH]) |
| 94 | +@pytest.mark.parametrize( |
| 95 | + "mock_device_code", |
| 96 | + ["cz_PGEkBctAbtzKOZng"], |
| 97 | +) |
| 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]) |
| 124 | +@pytest.mark.parametrize( |
| 125 | + "mock_device_code", |
| 126 | + ["cz_PGEkBctAbtzKOZng"], |
| 127 | +) |
| 128 | +async def test_turn_off( |
| 129 | + hass: HomeAssistant, |
| 130 | + mock_manager: Manager, |
| 131 | + mock_config_entry: MockConfigEntry, |
| 132 | + mock_device: CustomerDevice, |
| 133 | +) -> None: |
| 134 | + """Test turning off a switch.""" |
| 135 | + entity_id = "switch.din_socket" |
| 136 | + await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) |
| 137 | + |
| 138 | + state = hass.states.get(entity_id) |
| 139 | + assert state is not None, f"{entity_id} does not exist" |
| 140 | + await hass.services.async_call( |
| 141 | + SWITCH_DOMAIN, |
| 142 | + SERVICE_TURN_OFF, |
| 143 | + { |
| 144 | + ATTR_ENTITY_ID: entity_id, |
| 145 | + }, |
| 146 | + blocking=True, |
| 147 | + ) |
| 148 | + mock_manager.send_commands.assert_called_once_with( |
| 149 | + mock_device.id, [{"code": "switch", "value": False}] |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +@patch("homeassistant.components.tuya.PLATFORMS", [Platform.SWITCH]) |
| 154 | +@pytest.mark.parametrize( |
| 155 | + "mock_device_code", |
| 156 | + ["cz_PGEkBctAbtzKOZng"], |
| 157 | +) |
| 158 | +@pytest.mark.parametrize( |
| 159 | + ("initial_status", "expected_state"), |
| 160 | + [ |
| 161 | + (True, "on"), |
| 162 | + (False, "off"), |
| 163 | + (None, STATE_UNKNOWN), |
| 164 | + ("some string", STATE_UNKNOWN), |
| 165 | + ], |
| 166 | +) |
| 167 | +async def test_state( |
| 168 | + hass: HomeAssistant, |
| 169 | + mock_manager: Manager, |
| 170 | + mock_config_entry: MockConfigEntry, |
| 171 | + mock_device: CustomerDevice, |
| 172 | + initial_status: Any, |
| 173 | + expected_state: str, |
| 174 | +) -> None: |
| 175 | + """Test switch state.""" |
| 176 | + entity_id = "switch.din_socket" |
| 177 | + mock_device.status["switch"] = initial_status |
| 178 | + await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) |
| 179 | + |
| 180 | + state = hass.states.get(entity_id) |
| 181 | + assert state is not None, f"{entity_id} does not exist" |
| 182 | + assert state.state == expected_state |
0 commit comments