reach maximum recursion depth exceeded in tests

This commit is contained in:
Thomas Protzner
2024-08-02 19:53:00 +00:00
parent 34b561b211
commit 9645412148
2 changed files with 39 additions and 20 deletions

View File

@@ -69,14 +69,17 @@ class AutomowerDataUpdateCoordinator(DataUpdateCoordinator[dict[str, MowerAttrib
"""Listen with the client.""" """Listen with the client."""
try: try:
await automower_client.auth.websocket_connect() await automower_client.auth.websocket_connect()
reconnect_time = 2
await automower_client.start_listening()
except HusqvarnaWSServerHandshakeError as err: except HusqvarnaWSServerHandshakeError as err:
_LOGGER.debug( _LOGGER.debug(
"Failed to connect to websocket. Trying to reconnect: %s", err "Failed to connect to websocket. Trying to reconnect: %s", err
) )
if not hass.is_stopping: if not hass.is_stopping:
# self.hass.async_create_background_task(
# hass,
# self.client_listen(hass, entry, automower_client),
# "reconnect_task",
# )
reconnect_time = 2
await asyncio.sleep(reconnect_time) await asyncio.sleep(reconnect_time)
reconnect_time = min(reconnect_time * 2, MAX_WS_RECONNECT_TIME) reconnect_time = min(reconnect_time * 2, MAX_WS_RECONNECT_TIME)
await self.client_listen( await self.client_listen(
@@ -85,3 +88,14 @@ class AutomowerDataUpdateCoordinator(DataUpdateCoordinator[dict[str, MowerAttrib
automower_client=automower_client, automower_client=automower_client,
reconnect_time=reconnect_time, reconnect_time=reconnect_time,
) )
else:
await automower_client.start_listening()
async def try_reconnect(
self,
hass: HomeAssistant,
entry: ConfigEntry,
automower_client: AutomowerSession,
reconnect_time: int = 2,
) -> None:
"""Listen with the client."""

View File

@@ -15,6 +15,9 @@ import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.components.husqvarna_automower.const import DOMAIN, OAUTH2_TOKEN from homeassistant.components.husqvarna_automower.const import DOMAIN, OAUTH2_TOKEN
from homeassistant.components.husqvarna_automower.coordinator import (
MAX_WS_RECONNECT_TIME,
)
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
@@ -128,19 +131,21 @@ async def test_websocket_not_available(
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test trying reload the websocket.""" """Test trying reload the websocket."""
mock_automower_client.start_listening.side_effect = HusqvarnaWSServerHandshakeError( mock_automower_client.auth.websocket_connect.side_effect = (
"Boom" HusqvarnaWSServerHandshakeError("Boom")
) )
await setup_integration(hass, mock_config_entry) await setup_integration(hass, mock_config_entry)
assert "Failed to connect to websocket. Trying to reconnect: Boom" in caplog.text assert "Failed to connect to websocket. Trying to reconnect: Boom" in caplog.text
assert mock_automower_client.auth.websocket_connect.call_count == 1 assert mock_automower_client.auth.websocket_connect.call_count == 1
assert mock_automower_client.start_listening.call_count == 1
assert mock_config_entry.state is ConfigEntryState.LOADED assert mock_config_entry.state is ConfigEntryState.LOADED
freezer.tick(timedelta(seconds=2)) reconnect_time = 2
for count in range(1, 945):
reconnect_time = min(reconnect_time * 2, MAX_WS_RECONNECT_TIME)
print("reconnect_t", reconnect_time)
freezer.tick(timedelta(seconds=reconnect_time))
async_fire_time_changed(hass) async_fire_time_changed(hass)
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 == count + 1
assert mock_automower_client.start_listening.call_count == 2
assert mock_config_entry.state is ConfigEntryState.LOADED assert mock_config_entry.state is ConfigEntryState.LOADED