From f20f19219a2fa9675358cd64a9519e1c0f862eb3 Mon Sep 17 00:00:00 2001 From: brefra Date: Sun, 29 Nov 2020 20:58:42 +0100 Subject: [PATCH 1/4] Disable negative Int deserialize for known positive values --- plugwise/messages/responses.py | 16 ++++++++-------- plugwise/util.py | 35 +++++++++++++++++----------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/plugwise/messages/responses.py b/plugwise/messages/responses.py index e23e8508a..c35f7a68c 100644 --- a/plugwise/messages/responses.py +++ b/plugwise/messages/responses.py @@ -217,7 +217,7 @@ def __init__(self): self.unknown1 = Int(0, length=2) self.network_is_online = Int(0, length=2) self.circle_plus_mac = String(None, length=16) - self.network_id = Int(0, length=4) + self.network_id = Int(0, 4, False) self.unknown2 = Int(0, length=2) self.params += [ self.unknown1, @@ -245,7 +245,7 @@ def __init__(self): super().__init__() self.in_RSSI = Int(0, length=2) self.out_RSSI = Int(0, length=2) - self.ping_ms = Int(0, length=4) + self.ping_ms = Int(0, 4, False) self.params += [ self.in_RSSI, self.out_RSSI, @@ -290,7 +290,7 @@ class CirclePlusScanResponse(NodeResponse): def __init__(self): super().__init__() self.node_mac = String(None, length=16) - self.node_address = Int(0, length=2) + self.node_address = Int(0, 2, False) self.params += [self.node_mac, self.node_address] @@ -371,7 +371,7 @@ def __init__(self): super().__init__() self.time = RealClockTime() - self.day_of_week = Int(0, length=2) + self.day_of_week = Int(0, 2, False) self.date = RealClockDate() self.params += [self.time, self.day_of_week, self.date] @@ -388,7 +388,7 @@ class CircleClockResponse(NodeResponse): def __init__(self): super().__init__() self.time = Time() - self.day_of_week = Int(0, 2) + self.day_of_week = Int(0, 2, False) self.unknown = Int(0, 2) self.unknown2 = Int(0, 4) self.params += [self.time, self.day_of_week, self.unknown, self.unknown2] @@ -446,7 +446,7 @@ class NodeAwakeResponse(NodeResponse): def __init__(self): super().__init__() - self.awake_type = Int(0, length=2) + self.awake_type = Int(0, 2, False) self.params += [self.awake_type] @@ -463,7 +463,7 @@ class NodeSwitchGroupResponse(NodeResponse): def __init__(self): super().__init__() - self.group = Int(0, length=2) + self.group = Int(0, 2, False) self.power_state = Int(0, length=2) self.params += [ self.group, @@ -514,7 +514,7 @@ class NodeAckResponse(NodeResponse): def __init__(self): super().__init__() - self.ack_id = Int(0, length=2) + self.ack_id = Int(0, 2, False) class SenseReportResponse(NodeResponse): diff --git a/plugwise/util.py b/plugwise/util.py index 5c648ac3d..91a598c41 100644 --- a/plugwise/util.py +++ b/plugwise/util.py @@ -162,9 +162,10 @@ class String(BaseType): class Int(BaseType): - def __init__(self, value, length=2): + def __init__(self, value, length=2, negative=True): self.value = value self.length = length + self.negative = negative def serialize(self): fmt = "%%0%dX" % self.length @@ -172,8 +173,9 @@ def serialize(self): def deserialize(self, val): self.value = int(val, 16) - mask = 1 << (self.length * 4 - 1) - self.value = -(self.value & mask) + (self.value & ~mask) + if self.negative: + mask = 1 << (self.length * 4 - 1) + self.value = -(self.value & mask) + (self.value & ~mask) class SInt(BaseType): @@ -223,26 +225,23 @@ class DateTime(CompositeType): def __init__(self, year=0, month=0, minutes=0): CompositeType.__init__(self) self.year = Year2k(year - PLUGWISE_EPOCH, 2) - self.month = Int(month, 2) - self.minutes = Int(minutes, 4) + self.month = Int(month, 2, False) + self.minutes = Int(minutes, 4, False) self.contents += [self.year, self.month, self.minutes] def deserialize(self, val): CompositeType.deserialize(self, val) minutes = self.minutes.value - hours = minutes // 60 - days = hours // 24 - hours -= days * 24 - minutes -= (days * 24 * 60) + (hours * 60) - try: + if minutes == 65535: + self.value = None + else: + hours = minutes // 60 + days = hours // 24 + hours -= days * 24 + minutes -= (days * 24 * 60) + (hours * 60) self.value = datetime.datetime( self.year.value, self.month.value, days + 1, hours, minutes ) - except ValueError: - # debug( - # "encountered value error while attempting to construct datetime object" - # ) - self.value = None class Time(CompositeType): @@ -250,9 +249,9 @@ class Time(CompositeType): def __init__(self, hour=0, minute=0, second=0): CompositeType.__init__(self) - self.hour = Int(hour, 2) - self.minute = Int(minute, 2) - self.second = Int(second, 2) + self.hour = Int(hour, 2, False) + self.minute = Int(minute, 2, False) + self.second = Int(second, 2, False) self.contents += [self.hour, self.minute, self.second] def deserialize(self, val): From 5f547ec5f12eb996c5ccbdf429a3978659f732d4 Mon Sep 17 00:00:00 2001 From: brefra Date: Tue, 1 Dec 2020 22:24:10 +0100 Subject: [PATCH 2/4] Mark UnixTimestamp as positive Int --- plugwise/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugwise/util.py b/plugwise/util.py index 91a598c41..d826192ff 100644 --- a/plugwise/util.py +++ b/plugwise/util.py @@ -200,7 +200,7 @@ def deserialize(self, val): class UnixTimestamp(Int): def __init__(self, value, length=8): - Int.__init__(self, value, length=length) + Int.__init__(self, value, length, False) def deserialize(self, val): Int.deserialize(self, val) From 7e05718295942a431f9007047bc2e23e5c2f0c62 Mon Sep 17 00:00:00 2001 From: brefra Date: Tue, 1 Dec 2020 22:25:42 +0100 Subject: [PATCH 3/4] Improve DateTime conversion --- plugwise/util.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/plugwise/util.py b/plugwise/util.py index d826192ff..58446babe 100644 --- a/plugwise/util.py +++ b/plugwise/util.py @@ -222,7 +222,7 @@ class DateTime(CompositeType): and last four bytes are offset from the beginning of the month in minutes """ - def __init__(self, year=0, month=0, minutes=0): + def __init__(self, year=0, month=1, minutes=0): CompositeType.__init__(self) self.year = Year2k(year - PLUGWISE_EPOCH, 2) self.month = Int(month, 2, False) @@ -232,16 +232,21 @@ def __init__(self, year=0, month=0, minutes=0): def deserialize(self, val): CompositeType.deserialize(self, val) minutes = self.minutes.value - if minutes == 65535: + if minutes == 0: + self.value = datetime.datetime(PLUGWISE_EPOCH, 1, 1, 0, 0) + elif minutes == 65535: self.value = None else: hours = minutes // 60 days = hours // 24 hours -= days * 24 minutes -= (days * 24 * 60) + (hours * 60) - self.value = datetime.datetime( - self.year.value, self.month.value, days + 1, hours, minutes - ) + try: + self.value = datetime.datetime( + self.year.value, self.month.value, days + 1, hours, minutes + ) + except: + self.value = None class Time(CompositeType): From 194617c72128aa0194e6e3888259487a75dbc4fb Mon Sep 17 00:00:00 2001 From: brefra Date: Tue, 8 Dec 2020 22:08:13 +0100 Subject: [PATCH 4/4] Specify exception --- plugwise/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugwise/util.py b/plugwise/util.py index 58446babe..84ed9d68f 100644 --- a/plugwise/util.py +++ b/plugwise/util.py @@ -245,7 +245,7 @@ def deserialize(self, val): self.value = datetime.datetime( self.year.value, self.month.value, days + 1, hours, minutes ) - except: + except datetime.datetime.ValueError: self.value = None