From 98b1005b63993090828662750dbb8ead1806e176 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 22 Apr 2023 21:26:07 +0200 Subject: [PATCH] Ensure config entries are unloaded in tests (#90850) --- tests/components/snooz/__init__.py | 15 ++++++++++++++- tests/conftest.py | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/components/snooz/__init__.py b/tests/components/snooz/__init__.py index 1e38978f447..1e414fb337c 100644 --- a/tests/components/snooz/__init__.py +++ b/tests/components/snooz/__init__.py @@ -5,7 +5,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 +from pysnooz.testing import MockSnoozDevice as ParentMockSnoozDevice from homeassistant.components.snooz.const import DOMAIN from homeassistant.const import CONF_ADDRESS, CONF_TOKEN @@ -65,6 +66,18 @@ class SnoozFixture: device: MockSnoozDevice +class MockSnoozDevice(ParentMockSnoozDevice): + """Used for testing integration with Bleak. + + Adjusted for https://github.com/AustinBrunkhorst/pysnooz/issues/6 + """ + + def _on_device_disconnected(self, e) -> None: + if self._is_manually_disconnecting: + e.kwargs.set("reason", DisconnectionReason.USER) + return super()._on_device_disconnected(e) + + async def create_mock_snooz( connected: bool = True, initial_state: SnoozCommandData = SnoozCommandData(on=False, volume=0), diff --git a/tests/conftest.py b/tests/conftest.py index b076a394a22..7184fac8189 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -538,6 +538,15 @@ async def _hass( yield hass + # Config entries are not normally unloaded on HA shutdown. They are unloaded here + # to ensure that they could, and to help track lingering tasks and timers. + await asyncio.gather( + *( + config_entry.async_unload(hass) + for config_entry in hass.config_entries.async_entries() + ) + ) + await hass.async_stop(force=True) # Restore timezone, it is set when creating the hass object