Update roborock tests to patch client before test setup (#136587)

This commit is contained in:
Allen Porter 2025-01-28 01:54:36 -08:00 committed by GitHub
parent 1ad2598c6f
commit b1fec51e2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 44 deletions

View File

@ -2,7 +2,8 @@
from collections.abc import Generator from collections.abc import Generator
from copy import deepcopy from copy import deepcopy
from unittest.mock import patch from typing import Any
from unittest.mock import Mock, patch
import pytest import pytest
from roborock import RoborockCategory, RoomMapping from roborock import RoborockCategory, RoomMapping
@ -139,6 +140,22 @@ def bypass_api_fixture() -> None:
yield yield
@pytest.fixture(name="send_message_side_effect")
def send_message_side_effect_fixture() -> Any:
"""Fixture to return a side effect for the send_message method."""
return None
@pytest.fixture(name="mock_send_message")
def mock_send_message_fixture(send_message_side_effect: Any) -> Mock:
"""Fixture to mock the send_message method."""
with patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command",
side_effect=send_message_side_effect,
) as mock_send_message:
yield mock_send_message
@pytest.fixture @pytest.fixture
def bypass_api_fixture_v1_only(bypass_api_fixture) -> None: def bypass_api_fixture_v1_only(bypass_api_fixture) -> None:
"""Bypass api for tests that require only having v1 devices.""" """Bypass api for tests that require only having v1 devices."""

View File

@ -1,6 +1,6 @@
"""Test Roborock Switch platform.""" """Test Roborock Switch platform."""
from unittest.mock import patch from unittest.mock import Mock
import pytest import pytest
import roborock import roborock
@ -29,6 +29,7 @@ def platforms() -> list[Platform]:
) )
async def test_update_success( async def test_update_success(
hass: HomeAssistant, hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture, bypass_api_fixture,
setup_entry: MockConfigEntry, setup_entry: MockConfigEntry,
entity_id: str, entity_id: str,
@ -36,27 +37,22 @@ async def test_update_success(
"""Test turning switch entities on and off.""" """Test turning switch entities on and off."""
# Ensure that the entity exist, as these test can pass even if there is no entity. # Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None assert hass.states.get(entity_id) is not None
with patch( await hass.services.async_call(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command" "switch",
) as mock_send_message: SERVICE_TURN_ON,
await hass.services.async_call( service_data=None,
"switch", blocking=True,
SERVICE_TURN_ON, target={"entity_id": entity_id},
service_data=None, )
blocking=True,
target={"entity_id": entity_id},
)
assert mock_send_message.assert_called_once assert mock_send_message.assert_called_once
with patch( mock_send_message.reset_mock()
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1.send_message" await hass.services.async_call(
) as mock_send_message: "switch",
await hass.services.async_call( SERVICE_TURN_OFF,
"switch", service_data=None,
SERVICE_TURN_OFF, blocking=True,
service_data=None, target={"entity_id": entity_id},
blocking=True, )
target={"entity_id": entity_id},
)
assert mock_send_message.assert_called_once assert mock_send_message.assert_called_once
@ -67,8 +63,12 @@ async def test_update_success(
("switch.roborock_s7_maxv_status_indicator_light", SERVICE_TURN_OFF), ("switch.roborock_s7_maxv_status_indicator_light", SERVICE_TURN_OFF),
], ],
) )
@pytest.mark.parametrize(
"send_message_side_effect", [roborock.exceptions.RoborockTimeout]
)
async def test_update_failed( async def test_update_failed(
hass: HomeAssistant, hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture, bypass_api_fixture,
setup_entry: MockConfigEntry, setup_entry: MockConfigEntry,
entity_id: str, entity_id: str,
@ -78,10 +78,6 @@ async def test_update_failed(
# Ensure that the entity exist, as these test can pass even if there is no entity. # Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None assert hass.states.get(entity_id) is not None
with ( with (
patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command",
side_effect=roborock.exceptions.RoborockTimeout,
) as mock_send_message,
pytest.raises(HomeAssistantError, match="Failed to update Roborock options"), pytest.raises(HomeAssistantError, match="Failed to update Roborock options"),
): ):
await hass.services.async_call( await hass.services.async_call(

View File

@ -1,7 +1,7 @@
"""Test Roborock Time platform.""" """Test Roborock Time platform."""
from datetime import time from datetime import time
from unittest.mock import patch from unittest.mock import Mock
import pytest import pytest
import roborock import roborock
@ -29,6 +29,7 @@ def platforms() -> list[Platform]:
) )
async def test_update_success( async def test_update_success(
hass: HomeAssistant, hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture, bypass_api_fixture,
setup_entry: MockConfigEntry, setup_entry: MockConfigEntry,
entity_id: str, entity_id: str,
@ -36,16 +37,13 @@ async def test_update_success(
"""Test turning switch entities on and off.""" """Test turning switch entities on and off."""
# Ensure that the entity exist, as these test can pass even if there is no entity. # Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None assert hass.states.get(entity_id) is not None
with patch( await hass.services.async_call(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command" "time",
) as mock_send_message: SERVICE_SET_VALUE,
await hass.services.async_call( service_data={"time": time(hour=1, minute=1)},
"time", blocking=True,
SERVICE_SET_VALUE, target={"entity_id": entity_id},
service_data={"time": time(hour=1, minute=1)}, )
blocking=True,
target={"entity_id": entity_id},
)
assert mock_send_message.assert_called_once assert mock_send_message.assert_called_once
@ -55,8 +53,12 @@ async def test_update_success(
("time.roborock_s7_maxv_do_not_disturb_begin"), ("time.roborock_s7_maxv_do_not_disturb_begin"),
], ],
) )
@pytest.mark.parametrize(
"send_message_side_effect", [roborock.exceptions.RoborockTimeout]
)
async def test_update_failure( async def test_update_failure(
hass: HomeAssistant, hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture, bypass_api_fixture,
setup_entry: MockConfigEntry, setup_entry: MockConfigEntry,
entity_id: str, entity_id: str,
@ -64,13 +66,7 @@ async def test_update_failure(
"""Test turning switch entities on and off.""" """Test turning switch entities on and off."""
# Ensure that the entity exist, as these test can pass even if there is no entity. # Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None assert hass.states.get(entity_id) is not None
with ( with pytest.raises(HomeAssistantError, match="Failed to update Roborock options"):
patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command",
side_effect=roborock.exceptions.RoborockTimeout,
) as mock_send_message,
pytest.raises(HomeAssistantError, match="Failed to update Roborock options"),
):
await hass.services.async_call( await hass.services.async_call(
"time", "time",
SERVICE_SET_VALUE, SERVICE_SET_VALUE,