Update vesync swtich.py tests (#191334)

This commit is contained in:
cdnninja
2026-11-09 11:36:59 +01:00
committed by GitHub
parent 104e48d754
commit 709fafef20
+114 -12
View File
@@ -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"),
[