Skip to content

Commit 23776d6

Browse files
committed
Apply priority to response messages too
1 parent e103baf commit 23776d6

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

plugwise_usb/messages/__init__.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
from __future__ import annotations
44

5+
from enum import Enum
56
from typing import Any
67

78
from ..constants import MESSAGE_FOOTER, MESSAGE_HEADER, UTF8
89
from ..helpers.util import crc_fun
910

11+
class Priority(int, Enum):
12+
"""Message priority levels for USB-stick message requests."""
13+
14+
CANCEL = 0
15+
HIGH = 1
16+
MEDIUM = 2
17+
LOW = 3
1018

1119
class PlugwiseMessage:
1220
"""Plugwise message base class."""
1321

22+
priority: Priority = Priority.MEDIUM
23+
1424
def __init__(self, identifier: bytes) -> None:
1525
"""Initialize a plugwise message."""
1626
self._identifier = identifier
@@ -59,3 +69,35 @@ def serialize(self) -> bytes:
5969
def calculate_checksum(data: bytes) -> bytes:
6070
"""Calculate crc checksum."""
6171
return bytes("%04X" % crc_fun(data), UTF8)
72+
73+
def __gt__(self, other: PlugwiseMessage) -> bool:
74+
"""Greater than."""
75+
if self.priority.value == other.priority.value:
76+
return self.timestamp > other.timestamp
77+
if self.priority.value < other.priority.value:
78+
return True
79+
return False
80+
81+
def __lt__(self, other: PlugwiseMessage) -> bool:
82+
"""Less than."""
83+
if self.priority.value == other.priority.value:
84+
return self.timestamp < other.timestamp
85+
if self.priority.value > other.priority.value:
86+
return True
87+
return False
88+
89+
def __ge__(self, other: PlugwiseMessage) -> bool:
90+
"""Greater than or equal."""
91+
if self.priority.value == other.priority.value:
92+
return self.timestamp >= other.timestamp
93+
if self.priority.value < other.priority.value:
94+
return True
95+
return False
96+
97+
def __le__(self, other: PlugwiseMessage) -> bool:
98+
"""Less than or equal."""
99+
if self.priority.value == other.priority.value:
100+
return self.timestamp <= other.timestamp
101+
if self.priority.value > other.priority.value:
102+
return True
103+
return False

plugwise_usb/messages/requests.py

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from collections.abc import Callable
66
from copy import copy
77
from datetime import UTC, datetime
8-
from enum import Enum
98
import logging
109

1110
from ..constants import (
@@ -19,7 +18,7 @@
1918
)
2019
from ..exceptions import MessageError, NodeError, NodeTimeout, StickError, StickTimeout
2120
from ..messages.responses import PlugwiseResponse, StickResponse, StickResponseType
22-
from . import PlugwiseMessage
21+
from . import PlugwiseMessage, Priority
2322
from .properties import (
2423
DateTime,
2524
Int,
@@ -34,20 +33,10 @@
3433
_LOGGER = logging.getLogger(__name__)
3534

3635

37-
class Priority(int, Enum):
38-
"""Message priority levels for USB-stick message requests."""
39-
40-
CANCEL = 0
41-
HIGH = 1
42-
MEDIUM = 2
43-
LOW = 3
44-
45-
4636
class PlugwiseRequest(PlugwiseMessage):
4737
"""Base class for request messages to be send from by USB-Stick."""
4838

4939
arguments: list = []
50-
priority: Priority = Priority.MEDIUM
5140

5241
def __init__(
5342
self,
@@ -260,38 +249,6 @@ def add_send_attempt(self):
260249
"""Increase the number of retries."""
261250
self._send_counter += 1
262251

263-
def __gt__(self, other: PlugwiseRequest) -> bool:
264-
"""Greater than."""
265-
if self.priority.value == other.priority.value:
266-
return self.timestamp > other.timestamp
267-
if self.priority.value < other.priority.value:
268-
return True
269-
return False
270-
271-
def __lt__(self, other: PlugwiseRequest) -> bool:
272-
"""Less than."""
273-
if self.priority.value == other.priority.value:
274-
return self.timestamp < other.timestamp
275-
if self.priority.value > other.priority.value:
276-
return True
277-
return False
278-
279-
def __ge__(self, other: PlugwiseRequest) -> bool:
280-
"""Greater than or equal."""
281-
if self.priority.value == other.priority.value:
282-
return self.timestamp >= other.timestamp
283-
if self.priority.value < other.priority.value:
284-
return True
285-
return False
286-
287-
def __le__(self, other: PlugwiseRequest) -> bool:
288-
"""Less than or equal."""
289-
if self.priority.value == other.priority.value:
290-
return self.timestamp <= other.timestamp
291-
if self.priority.value > other.priority.value:
292-
return True
293-
return False
294-
295252

296253
class StickNetworkInfoRequest(PlugwiseRequest):
297254
"""Request network information.

plugwise_usb/messages/responses.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ..api import NodeType
99
from ..constants import MESSAGE_FOOTER, MESSAGE_HEADER, UTF8
1010
from ..exceptions import MessageError
11-
from . import PlugwiseMessage
11+
from . import PlugwiseMessage, Priority
1212
from .properties import (
1313
BaseType,
1414
DateTime,
@@ -227,6 +227,7 @@ class StickResponse(PlugwiseResponse):
227227
def __init__(self) -> None:
228228
"""Initialize StickResponse message object."""
229229
super().__init__(b"0000", decode_ack=True, decode_mac=False)
230+
self.priority = Priority.HIGH
230231

231232
def __repr__(self) -> str:
232233
"""Convert request into writable str."""
@@ -787,6 +788,7 @@ def __init__(self) -> None:
787788
super().__init__(NODE_AWAKE_RESPONSE_ID)
788789
self._awake_type = Int(0, 2, False)
789790
self._params += [self._awake_type]
791+
self.priority = Priority.HIGH
790792

791793
@property
792794
def awake_type(self) -> NodeAwakeResponseType:
@@ -863,6 +865,7 @@ def __init__(self) -> None:
863865
super().__init__(b"0100")
864866
self._node_ack_type = BaseType(0, length=4)
865867
self._params += [self._node_ack_type]
868+
self.priority = Priority.HIGH
866869

867870
@property
868871
def node_ack_type(self) -> NodeAckResponseType:

0 commit comments

Comments
 (0)