Fix lingering tasks in snooz tests (#127523)

This commit is contained in:
epenet 2024-10-04 08:24:01 +02:00 committed by GitHub
parent 6eb49991a4
commit c191a7cfdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 11 deletions

View File

@ -6,7 +6,8 @@ from dataclasses import dataclass
from unittest.mock import patch
from pysnooz.commands import SnoozCommandData
from pysnooz.testing import MockSnoozDevice
from pysnooz.device import DisconnectionReason, SnoozConnectionStatus
from pysnooz.testing import MockSnoozDevice as ParentMockSnoozDevice
from homeassistant.components.snooz.const import DOMAIN
from homeassistant.const import CONF_ADDRESS, CONF_TOKEN
@ -66,6 +67,36 @@ class SnoozFixture:
device: MockSnoozDevice
class MockSnoozDevice(ParentMockSnoozDevice):
"""Used for testing integration with Bleak.
Adjusted for https://github.com/AustinBrunkhorst/pysnooz/pull/19
"""
async def async_disconnect(self) -> None:
"""Disconnect from the device."""
self._is_manually_disconnecting = True
try:
self._cancel_current_command()
if (
self._reconnection_task is not None
and not self._reconnection_task.done()
):
self._reconnection_task.cancel()
if self._connection_task is not None and not self._connection_task.done():
self._connection_task.cancel()
if self._api is not None:
await self._api.async_disconnect()
if self.connection_status != SnoozConnectionStatus.DISCONNECTED:
self._machine.device_disconnected(reason=DisconnectionReason.USER)
finally:
self._is_manually_disconnecting = False
async def create_mock_snooz(
connected: bool = True,
initial_state: SnoozCommandData = SnoozCommandData(on=False, volume=0),

View File

@ -149,8 +149,6 @@ async def test_transition_off(hass: HomeAssistant, snooz_fan_entity_id: str) ->
assert ATTR_ASSUMED_STATE not in state.attributes
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
async def test_push_events(
hass: HomeAssistant, mock_connected_snooz: SnoozFixture, snooz_fan_entity_id: str
) -> None:
@ -174,9 +172,10 @@ async def test_push_events(
state = hass.states.get(snooz_fan_entity_id)
assert state.attributes[ATTR_ASSUMED_STATE] is True
# Don't attempt to reconnect
await mock_connected_snooz.device.async_disconnect()
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
async def test_restore_state(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:

View File

@ -2,15 +2,11 @@
from __future__ import annotations
import pytest
from homeassistant.core import HomeAssistant
from . import SnoozFixture
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
async def test_removing_entry_cleans_up_connections(
hass: HomeAssistant, mock_connected_snooz: SnoozFixture
) -> None:
@ -21,8 +17,6 @@ async def test_removing_entry_cleans_up_connections(
assert not mock_connected_snooz.device.is_connected
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
async def test_reloading_entry_cleans_up_connections(
hass: HomeAssistant, mock_connected_snooz: SnoozFixture
) -> None: