From f9525ebda7e44524a113d3effc91e93951be8949 Mon Sep 17 00:00:00 2001 From: cdnninja Date: Mon, 23 Mar 2026 11:46:35 -0600 Subject: [PATCH] Add switch tests to vesync (#163325) --- tests/components/vesync/test_switch.py | 154 +++++++++++++++++-------- 1 file changed, 109 insertions(+), 45 deletions(-) diff --git a/tests/components/vesync/test_switch.py b/tests/components/vesync/test_switch.py index a66b5ca845c..04c72356981 100644 --- a/tests/components/vesync/test_switch.py +++ b/tests/components/vesync/test_switch.py @@ -58,51 +58,6 @@ async def test_switch_state( assert hass.states.get(entity.entity_id) == snapshot(name=entity.entity_id) -@pytest.mark.parametrize( - ("action", "command"), - [ - ( - SERVICE_TURN_ON, - "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_display", - ), - ( - SERVICE_TURN_OFF, - "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_display", - ), - ], -) -async def test_turn_on_off_display_success( - hass: HomeAssistant, - humidifier_config_entry: MockConfigEntry, - aioclient_mock: AiohttpClientMocker, - action: str, - command: str, -) -> None: - """Test switch turn on and off command with success response.""" - - mock_devices_response(aioclient_mock, "Humidifier 200s") - - with ( - patch( - command, - return_value=True, - ) as method_mock, - patch( - "homeassistant.components.vesync.switch.VeSyncSwitchEntity.async_write_ha_state" - ) as update_mock, - ): - await hass.services.async_call( - SWITCH_DOMAIN, - action, - {ATTR_ENTITY_ID: ENTITY_SWITCH_DISPLAY}, - blocking=True, - ) - - await hass.async_block_till_done() - method_mock.assert_called_once() - update_mock.assert_called_once() - - @pytest.mark.parametrize( ("action", "command"), [ @@ -143,3 +98,112 @@ async def test_turn_on_off_display_raises_error( await hass.async_block_till_done() method_mock.assert_called_once() + + +@pytest.mark.parametrize( + ("device_name", "entity_id", "action", "command"), + [ + # device_status switch for outlet + ( + "Outlet", + "switch.outlet", + SERVICE_TURN_ON, + "pyvesync.devices.vesyncoutlet.VeSyncOutlet.turn_on", + ), + ( + "Outlet", + "switch.outlet", + SERVICE_TURN_OFF, + "pyvesync.devices.vesyncoutlet.VeSyncOutlet.turn_off", + ), + # display switch for humidifier + ( + "Humidifier 200s", + "switch.humidifier_200s_display", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_display", + ), + ( + "Humidifier 200s", + "switch.humidifier_200s_display", + SERVICE_TURN_OFF, + "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_display", + ), + # auto_off_config switch for humidifier + ( + "Humidifier 200s", + "switch.humidifier_200s_auto_off", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_automatic_stop", + ), + ( + "Humidifier 200s", + "switch.humidifier_200s_auto_off", + SERVICE_TURN_OFF, + "pyvesync.devices.vesynchumidifier.VeSyncHumid200S.toggle_automatic_stop", + ), + # child_lock switch for humidifier + ( + "Humidifier 6000s", + "switch.humidifier_6000s_child_lock", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncSuperior6000S.toggle_child_lock", + ), + ( + "Humidifier 6000s", + "switch.humidifier_6000s_child_lock", + SERVICE_TURN_OFF, + "pyvesync.devices.vesynchumidifier.VeSyncSuperior6000S.toggle_child_lock", + ), + # drying_mode_power_off switch for humidifier with drying mode + ( + "Humidifier 6000s", + "switch.humidifier_6000s_enable_drying_mode_while_power_is_off", + SERVICE_TURN_ON, + "pyvesync.devices.vesynchumidifier.VeSyncSuperior6000S.toggle_drying_mode", + ), + ( + "Humidifier 6000s", + "switch.humidifier_6000s_enable_drying_mode_while_power_is_off", + SERVICE_TURN_OFF, + "pyvesync.devices.vesynchumidifier.VeSyncSuperior6000S.toggle_drying_mode", + ), + ], +) +async def test_switch_operations( + hass: HomeAssistant, + config_entry: MockConfigEntry, + aioclient_mock: AiohttpClientMocker, + device_name: str, + entity_id: str, + action: str, + command: str, +) -> None: + """Test all switch operations with appropriate devices and parameters.""" + + # Configure the API devices call for the specific device + mock_devices_response(aioclient_mock, device_name) + + # Setup platform + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + with ( + patch( + command, + return_value=True, + ) as method_mock, + patch( + "homeassistant.components.vesync.switch.VeSyncSwitchEntity.async_write_ha_state" + ) as update_mock, + ): + await hass.services.async_call( + SWITCH_DOMAIN, + action, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + await hass.async_block_till_done() + method_mock.assert_called_once() + update_mock.assert_called_once()