Skip to content

Commit 46c9226

Browse files
committed
Disable negative Int deserialize for known positive values
1 parent 5adde96 commit 46c9226

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

plugwise/messages/responses.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def __init__(self):
229229
self.unknown1 = Int(0, length=2)
230230
self.network_is_online = Int(0, length=2)
231231
self.circle_plus_mac = String(None, length=16)
232-
self.network_id = Int(0, length=4)
232+
self.network_id = Int(0, 4, False)
233233
self.unknown2 = Int(0, length=2)
234234
self.params += [
235235
self.unknown1,
@@ -257,7 +257,7 @@ def __init__(self):
257257
super().__init__()
258258
self.in_RSSI = Int(0, length=2)
259259
self.out_RSSI = Int(0, length=2)
260-
self.ping_ms = Int(0, length=4)
260+
self.ping_ms = Int(0, 4, False)
261261
self.params += [
262262
self.in_RSSI,
263263
self.out_RSSI,
@@ -302,7 +302,7 @@ class CirclePlusScanResponse(NodeResponse):
302302
def __init__(self):
303303
super().__init__()
304304
self.node_mac = String(None, length=16)
305-
self.node_address = Int(0, length=2)
305+
self.node_address = Int(0, 2, False)
306306
self.params += [self.node_mac, self.node_address]
307307

308308

@@ -383,7 +383,7 @@ def __init__(self):
383383
super().__init__()
384384

385385
self.time = RealClockTime()
386-
self.day_of_week = Int(0, length=2)
386+
self.day_of_week = Int(0, 2, False)
387387
self.date = RealClockDate()
388388
self.params += [self.time, self.day_of_week, self.date]
389389

@@ -400,7 +400,7 @@ class CircleClockResponse(NodeResponse):
400400
def __init__(self):
401401
super().__init__()
402402
self.time = Time()
403-
self.day_of_week = Int(0, 2)
403+
self.day_of_week = Int(0, 2, False)
404404
self.unknown = Int(0, 2)
405405
self.unknown2 = Int(0, 4)
406406
self.params += [self.time, self.day_of_week, self.unknown, self.unknown2]
@@ -458,7 +458,7 @@ class NodeAwakeResponse(NodeResponse):
458458

459459
def __init__(self):
460460
super().__init__()
461-
self.awake_type = Int(0, length=2)
461+
self.awake_type = Int(0, 2, False)
462462
self.params += [self.awake_type]
463463

464464

@@ -475,7 +475,7 @@ class NodeSwitchGroupResponse(NodeResponse):
475475

476476
def __init__(self):
477477
super().__init__()
478-
self.group = Int(0, length=2)
478+
self.group = Int(0, 2, False)
479479
self.power_state = Int(0, length=2)
480480
self.params += [
481481
self.group,
@@ -526,7 +526,7 @@ class NodeAckResponse(NodeResponse):
526526

527527
def __init__(self):
528528
super().__init__()
529-
self.ack_id = Int(0, length=2)
529+
self.ack_id = Int(0, 2, False)
530530

531531

532532
class SenseReportResponse(NodeResponse):

plugwise/util.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,20 @@ class String(BaseType):
171171

172172

173173
class Int(BaseType):
174-
def __init__(self, value, length=2):
174+
def __init__(self, value, length=2, negative=True):
175175
self.value = value
176176
self.length = length
177+
self.negative = negative
177178

178179
def serialize(self):
179180
fmt = "%%0%dX" % self.length
180181
return bytes(fmt % self.value, UTF8_DECODE)
181182

182183
def deserialize(self, val):
183184
self.value = int(val, 16)
184-
mask = 1 << (self.length * 4 - 1)
185-
self.value = -(self.value & mask) + (self.value & ~mask)
185+
if self.negative:
186+
mask = 1 << (self.length * 4 - 1)
187+
self.value = -(self.value & mask) + (self.value & ~mask)
186188

187189

188190
class SInt(BaseType):
@@ -232,36 +234,33 @@ class DateTime(CompositeType):
232234
def __init__(self, year=0, month=0, minutes=0):
233235
CompositeType.__init__(self)
234236
self.year = Year2k(year - PLUGWISE_EPOCH, 2)
235-
self.month = Int(month, 2)
236-
self.minutes = Int(minutes, 4)
237+
self.month = Int(month, 2, False)
238+
self.minutes = Int(minutes, 4, False)
237239
self.contents += [self.year, self.month, self.minutes]
238240

239241
def deserialize(self, val):
240242
CompositeType.deserialize(self, val)
241243
minutes = self.minutes.value
242-
hours = minutes // 60
243-
days = hours // 24
244-
hours -= days * 24
245-
minutes -= (days * 24 * 60) + (hours * 60)
246-
try:
244+
if minutes == 65535:
245+
self.value = None
246+
else:
247+
hours = minutes // 60
248+
days = hours // 24
249+
hours -= days * 24
250+
minutes -= (days * 24 * 60) + (hours * 60)
247251
self.value = datetime.datetime(
248252
self.year.value, self.month.value, days + 1, hours, minutes
249253
)
250-
except ValueError:
251-
# debug(
252-
# "encountered value error while attempting to construct datetime object"
253-
# )
254-
self.value = None
255254

256255

257256
class Time(CompositeType):
258257
"""time value as used in the clock info response"""
259258

260259
def __init__(self, hour=0, minute=0, second=0):
261260
CompositeType.__init__(self)
262-
self.hour = Int(hour, 2)
263-
self.minute = Int(minute, 2)
264-
self.second = Int(second, 2)
261+
self.hour = Int(hour, 2, False)
262+
self.minute = Int(minute, 2, False)
263+
self.second = Int(second, 2, False)
265264
self.contents += [self.hour, self.minute, self.second]
266265

267266
def deserialize(self, val):

0 commit comments

Comments
 (0)