Stop the nest subscriber on Home Assistant stop (#117830)

This commit is contained in:
Allen Porter 2024-05-20 22:59:11 -07:00 committed by GitHub
parent 9cbcf5f2a5
commit fc931ac449
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 4 deletions

View File

@ -34,9 +34,10 @@ from homeassistant.const import (
CONF_MONITORED_CONDITIONS,
CONF_SENSORS,
CONF_STRUCTURE,
EVENT_HOMEASSISTANT_STOP,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
ConfigEntryNotReady,
@ -196,8 +197,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_config_reload() -> None:
await hass.config_entries.async_reload(entry.entry_id)
callback = SignalUpdateCallback(hass, async_config_reload)
subscriber.set_update_callback(callback.async_handle_event)
update_callback = SignalUpdateCallback(hass, async_config_reload)
subscriber.set_update_callback(update_callback.async_handle_event)
try:
await subscriber.start_async()
except AuthException as err:
@ -218,6 +219,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
subscriber.stop_async()
raise ConfigEntryNotReady(f"Device manager error: {err!s}") from err
@callback
def on_hass_stop(_: Event) -> None:
"""Close connection when hass stops."""
subscriber.stop_async()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
hass.data[DOMAIN][entry.entry_id] = {
DATA_SUBSCRIBER: subscriber,
DATA_DEVICE_MANAGER: device_manager,

View File

@ -90,6 +90,8 @@ TEST_CONFIG_ENTRY_LEGACY = NestTestConfig(
class FakeSubscriber(GoogleNestSubscriber):
"""Fake subscriber that supplies a FakeDeviceManager."""
stop_calls = 0
def __init__(self):
"""Initialize Fake Subscriber."""
self._device_manager = DeviceManager()
@ -121,7 +123,7 @@ class FakeSubscriber(GoogleNestSubscriber):
def stop_async(self):
"""No-op to stop the subscriber."""
return None
self.stop_calls += 1
async def async_receive_event(self, event_message: EventMessage):
"""Simulate a received pubsub message, invoked by tests."""

View File

@ -32,6 +32,7 @@ from .common import (
TEST_CONFIG_LEGACY,
TEST_CONFIGFLOW_APP_CREDS,
FakeSubscriber,
PlatformSetup,
YieldFixture,
)
@ -241,6 +242,23 @@ async def test_remove_entry(
assert not entries
async def test_home_assistant_stop(
hass: HomeAssistant,
setup_platform: PlatformSetup,
subscriber: FakeSubscriber,
) -> None:
"""Test successful subscriber shutdown when HomeAssistant stops."""
await setup_platform()
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
entry = entries[0]
assert entry.state is ConfigEntryState.LOADED
await hass.async_stop()
assert subscriber.stop_calls == 1
async def test_remove_entry_delete_subscriber_failure(
hass: HomeAssistant, setup_base_platform
) -> None: