Skip to content

Commit

Permalink
Retry on pair() exceptions (#30)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abmantis authored Jul 1, 2024
1 parent b3ad83a commit 745650f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
10 changes: 6 additions & 4 deletions idasen_ha/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
10 changes: 9 additions & 1 deletion tests/test_connection_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 745650f

Please sign in to comment.