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..84ed9d68f 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): @@ -198,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) @@ -220,29 +222,31 @@ 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) - 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: - 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" - # ) + 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) + try: + self.value = datetime.datetime( + self.year.value, self.month.value, days + 1, hours, minutes + ) + except datetime.datetime.ValueError: + self.value = None class Time(CompositeType): @@ -250,9 +254,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):