From 309fcb5c30223935fff73a6bf5d26cd354356cb0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 16 Mar 2024 17:15:46 -1000 Subject: [PATCH] Eagerly shutdown homekit_controller at the stop event (#113650) --- homeassistant/components/homekit_controller/__init__.py | 7 +++++-- homeassistant/components/homekit_controller/utils.py | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/homekit_controller/__init__.py b/homeassistant/components/homekit_controller/__init__.py index 9244a9f95d8..218094ddaf5 100644 --- a/homeassistant/components/homekit_controller/__init__.py +++ b/homeassistant/components/homekit_controller/__init__.py @@ -24,6 +24,7 @@ from homeassistant.core import Event, HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers.typing import ConfigType +from homeassistant.util.async_ import create_eager_task from .config_flow import normalize_hkid from .connection import HKDevice @@ -80,12 +81,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def _async_stop_homekit_controller(event: Event) -> None: await asyncio.gather( *( - connection.async_unload() + create_eager_task(connection.async_unload()) for connection in hass.data[KNOWN_DEVICES].values() ) ) - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller) + hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller, run_immediately=True + ) return True diff --git a/homeassistant/components/homekit_controller/utils.py b/homeassistant/components/homekit_controller/utils.py index 2f94f5bac92..40879a533f4 100644 --- a/homeassistant/components/homekit_controller/utils.py +++ b/homeassistant/components/homekit_controller/utils.py @@ -77,7 +77,9 @@ async def async_get_controller(hass: HomeAssistant) -> Controller: # Right now _async_stop_homekit_controller is only called on HA exiting # So we don't have to worry about leaking a callback here. - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller) + hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller, run_immediately=True + ) await controller.async_start()