This commit is contained in:
Thomas55555 2024-10-04 17:34:06 +00:00
parent f0d6035c3b
commit e8ddaea3d7
2 changed files with 31 additions and 18 deletions

View file

@ -1,5 +1,6 @@
"""Test helpers for Husqvarna Automower.""" """Test helpers for Husqvarna Automower."""
import asyncio
from collections.abc import Generator from collections.abc import Generator
import time import time
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
@ -80,8 +81,14 @@ async def setup_credentials(hass: HomeAssistant) -> None:
) )
@pytest.fixture(name="listen_block")
def mock_listen_block_fixture():
"""Mock a listen block."""
return asyncio.Event()
@pytest.fixture @pytest.fixture
def mock_automower_client() -> Generator[AsyncMock]: def mock_automower_client(listen_block) -> Generator[AsyncMock]:
"""Mock a Husqvarna Automower client.""" """Mock a Husqvarna Automower client."""
mower_dict = mower_list_to_dictionary_dataclass( mower_dict = mower_list_to_dictionary_dataclass(
@ -96,5 +103,17 @@ def mock_automower_client() -> Generator[AsyncMock]:
with patch( with patch(
"homeassistant.components.husqvarna_automower.AutomowerSession", "homeassistant.components.husqvarna_automower.AutomowerSession",
return_value=mock, return_value=mock,
): ) as client_class:
yield mock client = client_class.return_value
async def connect():
await asyncio.sleep(0)
client.auth.websocket_connect = AsyncMock()
async def listen() -> None:
await listen_block.wait()
client.auth.websocket_connect = AsyncMock(side_effect=connect)
client.start_listening = AsyncMock(side_effect=listen)
yield client

View file

@ -8,7 +8,6 @@ from aioautomower.exceptions import (
ApiException, ApiException,
AuthException, AuthException,
HusqvarnaWSServerHandshakeError, HusqvarnaWSServerHandshakeError,
TimeoutException,
) )
from aioautomower.utils import mower_list_to_dictionary_dataclass from aioautomower.utils import mower_list_to_dictionary_dataclass
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
@ -23,11 +22,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import setup_integration from . import setup_integration
from .const import TEST_MOWER_ID from .const import TEST_MOWER_ID
from tests.common import ( from tests.common import MockConfigEntry, load_json_value_fixture
MockConfigEntry,
async_fire_time_changed,
load_json_value_fixture,
)
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
@ -130,21 +125,21 @@ async def test_websocket_not_available(
mock_automower_client: AsyncMock, mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
listen_block,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test trying to reload the websocket.""" """Test trying to reload the websocket."""
# Patch DEFAULT_RECONNECT_TIME to 0 for the test
with patch( with patch(
"homeassistant.components.husqvarna_automower.coordinator.DEFAULT_RECONNECT_TIME", "homeassistant.components.husqvarna_automower.coordinator.DEFAULT_RECONNECT_TIME",
new=0, new=0,
): ):
# Simulate a WebSocket handshake error
mock_automower_client.auth.websocket_connect.side_effect = ( mock_automower_client.auth.websocket_connect.side_effect = (
HusqvarnaWSServerHandshakeError("Boom") HusqvarnaWSServerHandshakeError("Boom")
) )
# Setup integration and verify initial log error message # Setup integration and verify initial log error message
await setup_integration(hass, mock_config_entry) await setup_integration(hass, mock_config_entry)
assert ( assert (
"Failed to connect to websocket. Trying to reconnect: Boom" in caplog.text "Failed to connect to websocket. Trying to reconnect: Boom" in caplog.text
) )
@ -158,17 +153,16 @@ async def test_websocket_not_available(
assert "Trying to reconnect: Boom" not in caplog.text assert "Trying to reconnect: Boom" not in caplog.text
# Simulate a start_listening TimeoutException # Simulate a start_listening TimeoutException
mock_automower_client.start_listening.side_effect = TimeoutException("Boom") listen_block.set()
await hass.async_block_till_done() await hass.async_block_till_done()
assert mock_automower_client.auth.websocket_connect.call_count == 2 assert mock_automower_client.auth.websocket_connect.call_count == 1
assert mock_automower_client.start_listening.call_count == 2 assert mock_automower_client.start_listening.call_count == 1
# Simulate a successful connection # Simulate a successful connection
caplog.clear() caplog.clear()
mock_automower_client.start_listening.side_effect = None
await hass.async_block_till_done() await hass.async_block_till_done()
assert mock_automower_client.auth.websocket_connect.call_count == 3 assert mock_automower_client.auth.websocket_connect.call_count == 2
assert mock_automower_client.start_listening.call_count == 3 assert mock_automower_client.start_listening.call_count == 2
assert "Trying to reconnect: Boom" not in caplog.text assert "Trying to reconnect: Boom" not in caplog.text
# Simulate hass shutting down # Simulate hass shutting down