From d451a74c2d3bf8eadf0c2000337cc49a0a3ce6e1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 27 Nov 2022 16:17:35 -1000 Subject: [PATCH] Add a destruction check to the ESPHome bluetooth client (#82760) --- .../components/esphome/bluetooth/client.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/esphome/bluetooth/client.py b/homeassistant/components/esphome/bluetooth/client.py index 66b687b6de9..ef3a6197a37 100644 --- a/homeassistant/components/esphome/bluetooth/client.py +++ b/homeassistant/components/esphome/bluetooth/client.py @@ -23,7 +23,7 @@ from bleak.backends.service import BleakGATTServiceCollection from bleak.exc import BleakError from homeassistant.components.bluetooth import async_scanner_by_source -from homeassistant.core import CALLBACK_TYPE +from homeassistant.core import CALLBACK_TYPE, HomeAssistant from ..domain_data import DomainData from .characteristic import BleakGATTCharacteristicESPHome @@ -123,7 +123,7 @@ class ESPHomeClient(BaseBleakClient): """Initialize the ESPHomeClient.""" assert isinstance(address_or_ble_device, BLEDevice) super().__init__(address_or_ble_device, *args, **kwargs) - self._hass = kwargs["hass"] + self._hass: HomeAssistant = kwargs["hass"] self._ble_device = address_or_ble_device self._address_as_int = mac_to_int(self._ble_device.address) assert self._ble_device.details is not None @@ -540,3 +540,15 @@ class ESPHomeClient(BaseBleakClient): # to be consistent with the behavior of the BlueZ backend if coro := self._notify_cancels.pop(characteristic.handle, None): await coro() + + def __del__(self) -> None: + """Destructor to make sure the connection state is unsubscribed.""" + if self._cancel_connection_state: + _LOGGER.warning( + "%s: %s - %s: ESPHomeClient bleak client was not properly disconnected before destruction", + self._source, + self._ble_device.name, + self._ble_device.address, + ) + if not self._hass.loop.is_closed(): + self._hass.loop.call_soon_threadsafe(self._unsubscribe_connection_state)