Skip to content

Commit

Permalink
Merge pull request #8 from pawlizio/merge_try
Browse files Browse the repository at this point in the history
Merge_try
  • Loading branch information
pawlizio authored Dec 5, 2023
2 parents 575757a + a7d5da4 commit eb2aa67
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ jobs:
- name: Create coverage report
run: |
coverage combine coverage*/.coverage*
coverage report --fail-under=79
coverage report --fail-under=80
coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
52 changes: 23 additions & 29 deletions pyvlx/opening_device.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Module for window openers."""
"""Module for Opening devices."""
import datetime
from typing import TYPE_CHECKING, Any, Optional

from .api.command_send import CommandSend
from .api.get_limitation import GetLimitation
from .const import Velocity
from .exception import PyVLXException
from .log import PYVLXLOG
from .node import Node
from .parameter import (
CurrentPosition, DualRollerShutterPosition, IgnorePosition, Parameter,
Expand Down Expand Up @@ -41,14 +40,14 @@ def __init__(
super().__init__(
pyvlx=pyvlx, node_id=node_id, name=name, serial_number=serial_number
)
self.position = Position(parameter=position_parameter)
self.target = Position(parameter=position_parameter)
self.is_opening = False
self.is_closing = False
self.position: Position = Position(parameter=position_parameter)
self.target: Position = Position(parameter=position_parameter)
self.is_opening: bool = False
self.is_closing: bool = False
self.state_received_at: Optional[datetime.datetime] = None
self.estimated_completion: Optional[datetime.datetime] = None
self.use_default_velocity = False
self.default_velocity = Velocity.DEFAULT
self.use_default_velocity: bool = False
self.default_velocity: Velocity = Velocity.DEFAULT
self.open_position_target: int = 0
self.close_position_target: int = 100

Expand All @@ -58,7 +57,7 @@ async def set_position(
velocity: Velocity | int | None = Velocity.DEFAULT,
wait_for_completion: bool = True,
) -> None:
"""Set window to desired position.
"""Set opening device to desired position.
Parameters:
* position: Position object containing the target position.
Expand Down Expand Up @@ -98,7 +97,7 @@ async def open(
velocity: Velocity | int | None = Velocity.DEFAULT,
wait_for_completion: bool = True,
) -> None:
"""Open window.
"""Open opening device.
Parameters:
* velocity: Velocity to be used during transition.
Expand All @@ -117,7 +116,7 @@ async def close(
velocity: Velocity | int | None = Velocity.DEFAULT,
wait_for_completion: bool = True,
) -> None:
"""Close window.
"""Close opening device.
Parameters:
* velocity: Velocity to be used during transition.
Expand All @@ -132,7 +131,7 @@ async def close(
)

async def stop(self, wait_for_completion: bool = True) -> None:
"""Stop window.
"""Stop opening device.
Parameters:
* wait_for_completion: If set, function will return
Expand All @@ -156,32 +155,27 @@ def movement_percent(self) -> int:
):
return 100

movement_duration_s = (
movement_duration_s: float = (
self.estimated_completion - self.state_received_at
).total_seconds()
time_passed_s = (
time_passed_s: float = (
datetime.datetime.now() - self.state_received_at
).total_seconds()

percent = int(time_passed_s / movement_duration_s * 100)
percent: int = int(time_passed_s / movement_duration_s * 100)
percent = max(percent, 0)
percent = min(percent, 100)
return percent

def get_position(self) -> Position:
"""Return position of the cover."""
PYVLXLOG.debug("get_position")
if self.is_moving():
PYVLXLOG.debug("get_position: is moving")
percent = self.movement_percent()
PYVLXLOG.debug("get_position: %d percent", percent)
movement_origin = self.position.position_percent
movement_target = self.target.position_percent
PYVLXLOG.debug("get_position: %s => %s", movement_origin, movement_target)
current_position = (
movement_origin + (movement_target - movement_origin) / 100 * percent
)
PYVLXLOG.debug("get_position: current_position=%d", int(current_position))
return Position(position_percent=int(current_position))
return self.position

Expand Down Expand Up @@ -277,9 +271,9 @@ def __init__(
serial_number=serial_number,
position_parameter=position_parameter,
)
self.orientation = Position(position_percent=0)
self.target_orientation = Position()
self.target_position = Position()
self.orientation: Position = Position(position_percent=0)
self.target_orientation: Position = Position()
self.target_position: Position = Position()
self.open_orientation_target: int = 50
self.close_orientation_target: int = 100

Expand Down Expand Up @@ -498,10 +492,10 @@ def __init__(
serial_number=serial_number,
position_parameter=position_parameter,
)
self.position_upper_curtain = Position(position_percent=0)
self.position_lower_curtain = Position(position_percent=0)
self.position_upper_curtain: Position = Position(position_percent=0)
self.position_lower_curtain: Position = Position(position_percent=0)
self.target_position: Any = Position()
self.active_parameter = 0
self.active_parameter: int = 0

async def set_position(
self,
Expand All @@ -510,7 +504,7 @@ async def set_position(
wait_for_completion: bool = True,
curtain: str = "dual",
) -> None:
"""Set window to desired position.
"""Set DualRollerShutter to desired position.
Parameters:
* position: Position object containing the current position.
Expand Down Expand Up @@ -573,7 +567,7 @@ async def open(
wait_for_completion: bool = True,
curtain: str = "dual",
) -> None:
"""Open window.
"""Open DualRollerShutter.
Parameters:
* wait_for_completion: If set, function will return
Expand All @@ -593,7 +587,7 @@ async def close(
wait_for_completion: bool = True,
curtain: str = "dual",
) -> None:
"""Close window.
"""Close DualRollerShutter.
Parameters:
* wait_for_completion: If set, function will return
Expand Down
2 changes: 1 addition & 1 deletion pyvlx/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def from_parameter(self, parameter: "Parameter") -> None:

@staticmethod
def from_int(value: int) -> bytes:
"""Create raw out of position vlaue."""
"""Create raw out of position value."""
if not isinstance(value, int):
raise PyVLXException("value_has_to_be_int")
if not Parameter.is_valid_int(value):
Expand Down
7 changes: 7 additions & 0 deletions test/command_send_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ async def test_handle_frame(self) -> None:
frame.status = CommandSendConfirmationStatus.ACCEPTED
self.assertTrue(await self.command_send.handle_frame(frame=frame))
self.assertTrue(self.command_send.success)

self.command_send.success = False
self.command_send.wait_for_completion = True
frame.status = CommandSendConfirmationStatus.ACCEPTED
self.assertFalse(await self.command_send.handle_frame(frame=frame))
self.assertTrue(self.command_send.success)

self.command_send.success = False
self.command_send.wait_for_completion = False
frame.status = CommandSendConfirmationStatus.REJECTED
self.assertTrue(await self.command_send.handle_frame(frame=frame))
self.assertFalse(self.command_send.success)

self.command_send.success = False
self.command_send.wait_for_completion = True
frame.status = CommandSendConfirmationStatus.REJECTED
Expand All @@ -56,6 +59,10 @@ async def test_handle_frame(self) -> None:
frame.session_id = session_id
self.assertTrue(await self.command_send.handle_frame(frame=frame))

frame = MagicMock(spec=FrameSessionFinishedNotification)
frame.session_id = session_id + 1
self.assertFalse(await self.command_send.handle_frame(frame=frame))

@patch("pyvlx.api.ApiEvent.do_api_call", new_callable=AsyncMock)
async def test_send(self, do_api_call: AsyncMock) -> None:
"""Test send function of CommandSend object."""
Expand Down
18 changes: 9 additions & 9 deletions test/frame_status_request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class TestFrameStatusRequestRequest(unittest.TestCase):
EXAMPLE_FRAME = b"\x00\x1d\x03\x05\x00\xab\x02\x01\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
b"\x00\x00\x00\x00\x01\xfe\x00N"

def test_bytes(self):
def test_bytes(self) -> None:
"""Test FrameStatusRequestRequest with nodes 1,2 and session_id 0xAB."""
frame = FrameStatusRequestRequest(node_ids=[1, 2], session_id=0xAB)
self.assertEqual(bytes(frame), self.EXAMPLE_FRAME)

def test_frame_from_raw(self):
def test_frame_from_raw(self) -> None:
"""Test parse FrameStatusRequestRequest from raw."""
frame = frame_from_raw(self.EXAMPLE_FRAME)
self.assertTrue(isinstance(frame, FrameStatusRequestRequest))

def test_str(self):
def test_str(self) -> None:
"""Test string representation of FrameStatusRequestRequest."""
frame = FrameStatusRequestRequest(node_ids=[1, 2], session_id=0xAB)
self.assertEqual(str(frame), "<FrameStatusRequestRequest session_id=\"171\" node_ids=\"[1, 2]\" "
Expand All @@ -40,17 +40,17 @@ class TestFrameStatusRequestConfirmation(unittest.TestCase):

EXAMPLE_FRAME = b"\x00\x06\x03\x06\x00\xab\x01\xa9"

def test_bytes(self):
def test_bytes(self) -> None:
"""Test FrameStatusRequestConfirmation with session_id 0xAB and status ACCEPTED."""
frame = FrameStatusRequestConfirmation(session_id=0xAB, status=StatusRequestStatus.ACCEPTED)
self.assertEqual(bytes(frame), self.EXAMPLE_FRAME)

def test_frame_from_raw(self):
def test_frame_from_raw(self) -> None:
"""Test parse FrameStatusRequestConfirmation from raw."""
frame = frame_from_raw(self.EXAMPLE_FRAME)
self.assertTrue(isinstance(frame, FrameStatusRequestConfirmation))

def test_str(self):
def test_str(self) -> None:
"""Test string representation of FrameStatusRequestConfirmation."""
frame = FrameStatusRequestConfirmation(session_id=0xAB, status=StatusRequestStatus.ACCEPTED)
self.assertEqual(str(frame),
Expand All @@ -69,17 +69,17 @@ class TestFrameStatusRequestNotification(unittest.TestCase):
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00: "

def test_bytes(self):
def test_bytes(self) -> None:
"""Test FrameStatusRequestNotification."""
frame = FrameStatusRequestNotification()
self.assertEqual(bytes(frame), self.EXAMPLE_FRAME_EMPTY)

def test_frame_from_raw(self):
def test_frame_from_raw(self) -> None:
"""Test parse FrameStatusRequestNotification from raw."""
frame = frame_from_raw(self.EXAMPLE_FRAME_EMPTY)
self.assertTrue(isinstance(frame, FrameStatusRequestNotification))

def test_str(self):
def test_str(self) -> None:
"""Test string representation of FrameStatusRequestNotification."""
frame = FrameStatusRequestNotification()
self.assertEqual(str(frame), "<FrameStatusRequestNotification session_id=\"0\" "
Expand Down

0 comments on commit eb2aa67

Please sign in to comment.