Skip to content

Commit

Permalink
Merge pull request #367 from pawlizio/update_position_while_moving
Browse files Browse the repository at this point in the history
Update position while cover is moving
  • Loading branch information
pawlizio authored Sep 12, 2024
2 parents 5b16cf4 + 27a4067 commit 3c7fee2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
14 changes: 14 additions & 0 deletions pyvlx/opening_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Module for Opening devices."""
import asyncio
import datetime
from asyncio import Task
from typing import TYPE_CHECKING, Any, Optional

from .api.command_send import CommandSend
Expand Down Expand Up @@ -50,6 +52,16 @@ def __init__(
self.default_velocity: Velocity = Velocity.DEFAULT
self.open_position_target: int = 0
self.close_position_target: int = 100
self._update_task: Task | None = None

async def _update_calls(self) -> None:
"""While cover are moving, perform periodically update calls."""
while self.is_moving():
await asyncio.sleep(1)
await self.after_update()
if self._update_task:
self._update_task.cancel()
self._update_task = None

async def set_position(
self,
Expand Down Expand Up @@ -176,6 +188,8 @@ def get_position(self) -> Position:
current_position = (
movement_origin + (movement_target - movement_origin) / 100 * percent
)
if not self._update_task:
self._update_task = self.pyvlx.loop.create_task(self._update_calls())
return Position(position_percent=int(current_position))
return self.position

Expand Down
16 changes: 0 additions & 16 deletions test/opening_device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@ async def test_stop(self, set_position: AsyncMock) -> None:
position=CurrentPosition(),
wait_for_completion=wait_for_completion)

def test_is_moving(self) -> None:
"""Test is moving boolean of OpeningDevice object."""
opening_device = OpeningDevice(pyvlx=self.mocked_pyvlx, node_id=23, name="Test Device")
opening_device.is_opening = True
opening_device.is_closing = False
self.assertTrue(opening_device.is_moving())
opening_device.is_opening = False
opening_device.is_closing = True
self.assertTrue(opening_device.is_moving())
opening_device.is_opening = True
opening_device.is_closing = True
self.assertTrue(opening_device.is_moving())
opening_device.is_opening = False
opening_device.is_closing = False
self.assertFalse(opening_device.is_moving())

def test_window_str(self) -> None:
"""Test string representation of Window object."""
pyvlx = self.mocked_pyvlx
Expand Down

0 comments on commit 3c7fee2

Please sign in to comment.