Harden bridge state and reauthentication handling
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Tests for MB-Secure snapshot and event coordination."""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -8,7 +8,10 @@ from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers.update_coordinator import UpdateFailed
|
||||
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
||||
|
||||
from custom_components.mb_secure.api import BridgeAuthenticationError
|
||||
from custom_components.mb_secure.api import (
|
||||
BridgeAuthenticationError,
|
||||
BridgeConnectionError,
|
||||
)
|
||||
from custom_components.mb_secure.const import CONF_TOKEN, DOMAIN
|
||||
from custom_components.mb_secure.coordinator import MBSecureCoordinator
|
||||
from custom_components.mb_secure.models import BridgeInfo, BridgeSnapshot, DomainEvent
|
||||
@@ -91,6 +94,65 @@ async def test_snapshot_rejects_changed_bridge_identity(
|
||||
client.async_get_snapshot.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_bridge_restart_accepts_a_new_snapshot_revision(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""A full snapshot reconciles revision reset after a bridge restart."""
|
||||
client = MagicMock()
|
||||
client.async_get_info = AsyncMock(
|
||||
return_value=BridgeInfo(
|
||||
bridge_id="bridge-installation-1",
|
||||
bridge_version="0.1.0",
|
||||
api_version=1,
|
||||
connected=True,
|
||||
capabilities=frozenset(),
|
||||
)
|
||||
)
|
||||
client.async_get_snapshot = AsyncMock(return_value=snapshot(revision=0))
|
||||
instance, _ = coordinator(hass, client)
|
||||
instance.async_set_updated_data(snapshot(revision=20))
|
||||
|
||||
result = await instance._async_update_data()
|
||||
|
||||
assert result.revision == 0
|
||||
|
||||
|
||||
async def test_event_stream_reconnects_after_connection_loss(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""The event listener reconnects and resumes contiguous updates."""
|
||||
client = MagicMock()
|
||||
attempts = 0
|
||||
|
||||
async def events():
|
||||
nonlocal attempts
|
||||
attempts += 1
|
||||
if attempts == 1:
|
||||
if False:
|
||||
yield
|
||||
raise BridgeConnectionError
|
||||
yield DomainEvent(
|
||||
revision=2,
|
||||
event_type="state_changed",
|
||||
object_type="area",
|
||||
object_id="area-1",
|
||||
state="armed_away",
|
||||
)
|
||||
raise BridgeAuthenticationError
|
||||
|
||||
client.async_events = events
|
||||
instance, entry = coordinator(hass, client)
|
||||
instance.async_set_updated_data(snapshot())
|
||||
entry.async_start_reauth = MagicMock()
|
||||
|
||||
with patch("custom_components.mb_secure.coordinator.asyncio.sleep") as sleep:
|
||||
await instance._async_event_loop()
|
||||
|
||||
sleep.assert_awaited_once_with(1)
|
||||
assert attempts == 2
|
||||
assert instance.data.areas["area-1"].state == "armed_away"
|
||||
|
||||
|
||||
async def test_contiguous_event_updates_coordinator_data(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user