From b4c677650ac7bdd6cc29fcbff898ac1456a12287 Mon Sep 17 00:00:00 2001 From: Alex Martens Date: Mon, 25 Dec 2023 12:43:15 -0800 Subject: [PATCH] add wakeup for the Linak DPG1C controller Co-authored-by: datbilling --- CHANGELOG.md | 4 ++++ idasen/__init__.py | 24 ++++++++++++++++++++++++ tests/test_idasen.py | 4 ++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 851eaa2..9c5bcfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Added +- Added a `wakeup` method for compatibility with the Linak DPG1C controller. + ## [0.10.3] - 2023-10-08 ### Added - Added support for python 3.12. diff --git a/idasen/__init__.py b/idasen/__init__.py index 3804f9e..b94c7a1 100644 --- a/idasen/__init__.py +++ b/idasen/__init__.py @@ -18,11 +18,13 @@ _UUID_COMMAND: str = "99fa0002-338a-1024-8a49-009c0215f78a" _UUID_REFERENCE_INPUT: str = "99fa0031-338a-1024-8a49-009c0215f78a" _UUID_ADV_SVC: str = "99fa0001-338a-1024-8a49-009c0215f78a" +_UUID_DPG: str = "99fa0011-338a-1024-8a49-009c0215f78a" _COMMAND_REFERENCE_INPUT_STOP: bytearray = bytearray([0x01, 0x80]) _COMMAND_UP: bytearray = bytearray([0x47, 0x00]) _COMMAND_DOWN: bytearray = bytearray([0x46, 0x00]) _COMMAND_STOP: bytearray = bytearray([0xFF, 0x00]) +_COMMAND_WAKEUP: bytearray = bytearray([0xFE, 0x00]) # height calculation offset in meters, assumed to be the same for all desks @@ -144,6 +146,7 @@ async def connect(self): while True: try: await self._client.connect() + await self.wakeup() return except Exception: if i >= self.RETRY_COUNT: @@ -238,6 +241,27 @@ def mac(self) -> str: """ return self._mac + async def wakeup(self): + """ + Wakeup the controller from sleep. + + This exists for compatibility with the Linak DPG1C controller, + it is not necessary with the original idasen controller. + + >>> async def example() -> str: + ... async with IdasenDesk(mac="AA:AA:AA:AA:AA:AA") as desk: + ... await desk.wakeup() + >>> asyncio.run(example()) + """ + # https://github.com/rhyst/linak-controller/issues/32#issuecomment-1784055470 + await self._client.write_gatt_char(_UUID_DPG, b"\x7F\x86\x00") + await self._client.write_gatt_char( + _UUID_DPG, + b"\x7F\x86\x80\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D" + b"\x0E\x0F\x10\x11", + ) + await self._client.write_gatt_char(_UUID_COMMAND, _COMMAND_WAKEUP) + async def move_up(self): """ Move the desk upwards. diff --git a/tests/test_idasen.py b/tests/test_idasen.py index 45718bb..6a1ae4e 100644 --- a/tests/test_idasen.py +++ b/tests/test_idasen.py @@ -151,7 +151,7 @@ async def test_move_abort_when_no_movement(): async def write_gatt_char_mock( uuid: str, command: bytearray, response: bool = False ): - if client.write_gatt_char.call_count == 1: + if client.write_gatt_char.call_count == 4: assert desk.is_moving client._height -= 0.001 @@ -171,7 +171,7 @@ async def test_move_stop(): async def write_gatt_char_mock( uuid: str, command: bytearray, response: bool = False ): - if client.write_gatt_char.call_count == 1: + if client.write_gatt_char.call_count == 4: assert desk.is_moving # Force this method to behave asynchronously, otherwise it will block the # eventloop