Skip to content

Commit feda4db

Browse files
committed
tion: decode temperature from device with additional code
Tion use additional code for coding returned temperature and we should process it correctly. Fixes #1
1 parent c6f599d commit feda4db

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

s3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def _decode_response(self, response: bytearray) -> dict:
8888
"mode": self._process_mode(int(list("{:02x}".format(response[2]))[0])),
8989
"fan_speed": int(list("{:02x}".format(response[2]))[1]),
9090
"heater_temp": response[3],
91-
"in_temp": response[8],
92-
"out_temp": response[7],
91+
"in_temp": self.decode_temperature(response[8]),
92+
"out_temp": self.decode_temperature(response[7]),
9393
"filter_remain": response[10]*256 + response[9],
9494
"time": "{}:{}".format(response[11],response[12]),
9595
"request_error_code": response[13],

tests/decode_temperature.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python
2+
import sys
3+
import os
4+
import unittest
5+
6+
PACKAGE_PARENT = '..'
7+
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
8+
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
9+
10+
import tion
11+
12+
class TestDecodeTemperature(unittest.TestCase, tion.tion):
13+
def test_positive(self):
14+
self.assertEqual(self.decode_temperature(0x09), 9, "Should be 9")
15+
def test_negative(self):
16+
self.assertEqual(self.decode_temperature(0xFF), -1, "Should be -1")
17+
18+
if __name__ == '__main__':
19+
unittest.main()

tion.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,18 @@ def get() -> dict:
4141
dictionay with device paramters
4242
"""
4343
pass
44+
45+
def decode_temperature(self, raw: bytes) -> int:
46+
""" Converts temperature from bytes with addition code to int
47+
Args:
48+
raw: raw temperature value from Tion
49+
Returns:
50+
Integer value for temperature
51+
"""
52+
barrier = 0b10000000
53+
if (raw < barrier):
54+
result = raw
55+
else:
56+
result = -(~(result - barrier) + barrier + 1)
57+
58+
return result

0 commit comments

Comments
 (0)