From 24b9bde9e5792cb3635e21e23ffd3377a8a7d2bd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 22 Jul 2023 06:10:41 -0500 Subject: [PATCH] Fix duplicate and missing decorators in ESPHome Bluetooth client (#97027) --- .../components/esphome/bluetooth/client.py | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/esphome/bluetooth/client.py b/homeassistant/components/esphome/bluetooth/client.py index ee629eed6f9..748035bedac 100644 --- a/homeassistant/components/esphome/bluetooth/client.py +++ b/homeassistant/components/esphome/bluetooth/client.py @@ -364,16 +364,18 @@ class ESPHomeClient(BaseBleakClient): await connected_future try: - await self.get_services(dangerous_use_bleak_cache=dangerous_use_bleak_cache) + await self._get_services( + dangerous_use_bleak_cache=dangerous_use_bleak_cache + ) except asyncio.CancelledError: # On cancel we must still raise cancelled error # to avoid blocking the cancellation even if the # disconnect call fails. with contextlib.suppress(Exception): - await self.disconnect() + await self._disconnect() raise except Exception: - await self.disconnect() + await self._disconnect() raise return True @@ -381,6 +383,9 @@ class ESPHomeClient(BaseBleakClient): @api_error_as_bleak_error async def disconnect(self) -> bool: """Disconnect from the peripheral device.""" + return await self._disconnect() + + async def _disconnect(self) -> bool: self._async_disconnected_cleanup() await self._client.bluetooth_device_disconnect(self._address_as_int) await self._wait_for_free_connection_slot(DISCONNECT_TIMEOUT) @@ -454,6 +459,18 @@ class ESPHomeClient(BaseBleakClient): A :py:class:`bleak.backends.service.BleakGATTServiceCollection` with this device's services tree. """ + return await self._get_services( + dangerous_use_bleak_cache=dangerous_use_bleak_cache, **kwargs + ) + + @verify_connected + async def _get_services( + self, dangerous_use_bleak_cache: bool = False, **kwargs: Any + ) -> BleakGATTServiceCollection: + """Get all services registered for this GATT server. + + Must only be called from get_services or connected + """ address_as_int = self._address_as_int cache = self._cache # If the connection version >= 3, we must use the cache @@ -538,6 +555,7 @@ class ESPHomeClient(BaseBleakClient): raise BleakError(f"Characteristic {char_specifier} was not found!") return characteristic + @verify_connected @api_error_as_bleak_error async def clear_cache(self) -> bool: """Clear the GATT cache.""" @@ -726,6 +744,7 @@ class ESPHomeClient(BaseBleakClient): wait_for_response=False, ) + @verify_connected @api_error_as_bleak_error async def stop_notify( self,