Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions plugwise/messages/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]


Expand Down Expand Up @@ -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]

Expand All @@ -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]
Expand Down Expand Up @@ -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]


Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
48 changes: 26 additions & 22 deletions plugwise/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,20 @@ 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
return bytes(fmt % self.value, UTF8_DECODE)

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):
Expand All @@ -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)
Expand All @@ -220,39 +222,41 @@ 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):
"""time value as used in the clock info response"""

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):
Expand Down