Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interrupt movement routine when desk stops moving #352

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions idasen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import asyncio
import logging
import sys
import time


_UUID_HEIGHT: str = "99fa0021-338a-1024-8a49-009c0215f78a"
Expand Down Expand Up @@ -299,6 +300,7 @@ async def move_to_target(self, target: float):
async def do_move():
previous_height = await self.get_height()
will_move_up = target > previous_height
last_move_time: float | None = None
abmantis marked this conversation as resolved.
Show resolved Hide resolved
while True:
height = await self.get_height()
difference = target - height
Expand All @@ -310,6 +312,20 @@ async def do_move():
"stopped moving because desk safety feature kicked in"
)
return

if height == previous_height:
if (
last_move_time is not None
and time.time() - last_move_time > 0.5
):
self._logger.warning(
"desk is not moving anymore. physical button probably "
"pressed"
)
return
else:
last_move_time = time.time()

if not self._moving:
return

Expand Down
20 changes: 20 additions & 0 deletions tests/test_idasen.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ async def test_move_to_target(desk: IdasenDesk, target: float):
assert abs(await desk.get_height() - target) < 0.005


async def test_move_abort_when_no_movement():
desk = IdasenDesk(mac=desk_mac)
client = MockBleakClient()
desk._client = client
client.write_gatt_char = mock.AsyncMock()

async def write_gatt_char_mock(
uuid: str, command: bytearray, response: bool = False
):
if client.write_gatt_char.call_count == 1:
assert desk.is_moving
client._height -= 0.001

client.write_gatt_char.side_effect = write_gatt_char_mock

async with desk:
await desk.move_to_target(0.7)
assert not desk.is_moving


async def test_move_stop():
desk = IdasenDesk(mac=desk_mac)
client = MockBleakClient()
Expand Down