From 745650f1b3dd8249d8eb97a17e5a7eaa2c57e459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=ADlio=20Costa?= Date: Mon, 1 Jul 2024 23:11:55 +0100 Subject: [PATCH] Retry on pair() exceptions (#30) Once in a while the desk replies with an auth error, but triggering a connect again will connect just fine, so the retry logic should also apply to pair() failures. --- idasen_ha/connection_manager.py | 10 ++++++---- pyproject.toml | 2 +- tests/test_connection_management.py | 10 +++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/idasen_ha/connection_manager.py b/idasen_ha/connection_manager.py index 6fb1418..b5dc080 100644 --- a/idasen_ha/connection_manager.py +++ b/idasen_ha/connection_manager.py @@ -71,14 +71,17 @@ async def _connect(self, retry: bool) -> None: if retry: self._schedule_reconnect() return - else: - raise ex + raise ex try: _LOGGER.info("Pairing...") await self._idasen_desk.pair() except BleakDBusError as ex: + _LOGGER.exception("Pair failed") await self._idasen_desk.disconnect() + if retry: + self._schedule_reconnect() + return if ex.dbus_error == "org.bluez.Error.AuthenticationFailed": raise AuthFailedError() from ex raise ex @@ -88,8 +91,7 @@ async def _connect(self, retry: bool) -> None: if retry: self._schedule_reconnect() return - else: - raise ex + raise ex await self._handle_connect() finally: diff --git a/pyproject.toml b/pyproject.toml index f80b1b2..7c5876f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "idasen-ha" -version = "2.6.1" +version = "2.6.2" authors = [{name = "AbĂ­lio Costa", email = "amfcalt@gmail.com"}] description = "Home Assistant helper lib for the IKEA Idasen Desk integration" classifiers = [ diff --git a/tests/test_connection_management.py b/tests/test_connection_management.py index 2f6aeed..8b29f91 100644 --- a/tests/test_connection_management.py +++ b/tests/test_connection_management.py @@ -174,7 +174,15 @@ async def sleep_handler(delay): @mock.patch("idasen_ha.connection_manager.asyncio.sleep") -@pytest.mark.parametrize("exception", [TimeoutError(), BleakError()]) +@pytest.mark.parametrize( + "exception", + [ + TimeoutError(), + BleakError(), + BleakDBusError("", []), + BleakDBusError("org.bluez.Error.AuthenticationFailed", []), + ], +) @pytest.mark.parametrize("fail_call_name", ["connect", "pair"]) async def test_connect_exception_retry_success( sleep_mock,