robertmuth / PyZwaver

Z-Wave library written in Python3
GNU General Public License v3.0
38 stars 9 forks source link

Two's complement conversion fails for signed values #11

Closed tkintscher closed 5 years ago

tkintscher commented 5 years ago

Hi, I discovered this nice project of yours and was playing around with it to see if I could control my thermostats. Doing so I noticed that the two's complement conversion for sensor values is wrong for negative numbers. Probably not a huge issue as most sensors will report positive numbers, but the whole function can be fixed by using Python's built-in conversion (int.from_bytes).

Example with 1585:

>>> pyzwaver.command._GetSignedValue([0x06, 0x31])
1585
>>> int.from_bytes([0x06, 0x31], 'big', signed=True)
1585

Example with -1585:

>>> pyzwaver.command._GetSignedValue([0xF9, 0xCF])
64207
>>> int.from_bytes([0xF9, 0xCF], 'big', signed=True)
-1585

Using one bit for the sign, 64207 is too large to be represented in two bytes.