From 709fafef20681cfa5d7665faacaa33ff043b0ad6 Mon Sep 17 00:00:00 2001 From: cdnninja Date: Mon, 9 Nov 2026 04:36:59 -0600 Subject: [PATCH] Update vesync swtich.py tests (#191334) --- tests/components/vesync/test_switch.py | 126 ++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 12 deletions(-) diff --git a/tests/components/vesync/test_switch.py b/tests/components/vesync/test_switch.py index 9bb1a65b3442..c4bcbada940f 100644 --- a/tests/components/vesync/test_switch.py +++ b/tests/components/vesync/test_switch.py @@ -1,11 +1,13 @@ """Tests for the switch module.""" from contextlib import nullcontext -from unittest.mock import patch +from unittest.mock import PropertyMock, patch import pytest from pytest_homeassistant.common import MockConfigEntry from pytest_homeassistant.test_util.aiohttp import AiohttpClientMocker +from pyvesync.devices.vesynchumidifier import VeSyncHumid200S +from pyvesync.utils.errors import ResponseInfo from syrupy.assertion import SnapshotAssertion from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN @@ -17,6 +19,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from .common import ALL_DEVICE_NAMES, ENTITY_SWITCH_DISPLAY, mock_devices_response NoException = nullcontext() +DEVICE_ERROR = ResponseInfo(name="error", error_type="error", message="Device error") @pytest.mark.parametrize("device_name", ALL_DEVICE_NAMES) @@ -58,15 +61,25 @@ async def test_switch_state( @pytest.mark.parametrize( - ("action", "command"), + ("action", "last_response", "message"), [ - ( - SERVICE_TURN_ON, - "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_display", + pytest.param( + SERVICE_TURN_ON, DEVICE_ERROR, "Device error", id="on_last_response" ), - ( + pytest.param( + SERVICE_TURN_ON, + None, + "Unknown error turning on device, no response.", + id="on_no_last_response", + ), + pytest.param( + SERVICE_TURN_OFF, DEVICE_ERROR, "Device error", id="off_last_response" + ), + pytest.param( SERVICE_TURN_OFF, - "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_display", + None, + "Unknown error turning off device, no response.", + id="off_no_last_response", ), ], ) @@ -75,18 +88,25 @@ async def test_turn_on_off_display_raises_error( humidifier_config_entry: MockConfigEntry, aioclient_mock: AiohttpClientMocker, action: str, - command: str, + last_response: ResponseInfo | None, + message: str, ) -> None: """Test switch turn on and off command raises HomeAssistantError.""" mock_devices_response(aioclient_mock, "Humidifier 200s") + async def _fail_toggle(device: VeSyncHumid200S, toggle: bool) -> bool: + device.last_response = last_response + return False + with ( - patch( - command, - return_value=False, + patch.object( + VeSyncHumid200S, + "toggle_display", + autospec=True, + side_effect=_fail_toggle, ) as method_mock, - pytest.raises(HomeAssistantError), + pytest.raises(HomeAssistantError, match=message), ): await hass.services.async_call( SWITCH_DOMAIN, @@ -99,6 +119,88 @@ async def test_turn_on_off_display_raises_error( method_mock.assert_called_once() +@pytest.mark.parametrize( + ("device_name", "entity_id", "action", "command", "message"), + [ + pytest.param( + "Outlet", + "switch.outlet", + SERVICE_TURN_ON, + "pyvesync.devices.vesyncoutlet.VeSyncOutlet.turn_on", + "Device does not support toggling power.", + id="power_on", + ), + pytest.param( + "Outlet", + "switch.outlet", + SERVICE_TURN_OFF, + "pyvesync.devices.vesyncoutlet.VeSyncOutlet.turn_off", + "Device does not support toggling power.", + id="power_off", + ), + pytest.param( + "Humidifier 200s", + "switch.humidifier_200s_display", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_display", + "Device does not support toggling display.", + id="display", + ), + pytest.param( + "Humidifier 6000s", + "switch.humidifier_6000s_child_lock", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncSuperior6000S.toggle_child_lock", + "Device does not support toggling child lock.", + id="child_lock", + ), + pytest.param( + "Humidifier 200s", + "switch.humidifier_200s_auto_off", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_automatic_stop", + "Device does not support toggling automatic stop.", + id="auto_stop", + ), + pytest.param( + "Humidifier 6000s", + "switch.humidifier_6000s_enable_drying_mode_while_power_is_off", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncSuperior6000S.toggle_drying_mode", + "Device does not support toggling drying mode.", + id="drying_mode", + ), + ], +) +async def test_switch_unsupported_command( + hass: HomeAssistant, + config_entry: MockConfigEntry, + aioclient_mock: AiohttpClientMocker, + device_name: str, + entity_id: str, + action: str, + command: str, + message: str, +) -> None: + """Test an error is raised when the device lacks the switch command.""" + mock_devices_response(aioclient_mock, device_name) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + # hasattr() is False when attribute lookup raises AttributeError + with ( + patch(command, new_callable=PropertyMock, side_effect=AttributeError), + pytest.raises(HomeAssistantError, match=message), + ): + await hass.services.async_call( + SWITCH_DOMAIN, + action, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + @pytest.mark.parametrize( ("device_name", "entity_id", "action", "command"), [