Skip to content

Commit e893f7c

Browse files
committed
added setting temperature
1 parent 3146910 commit e893f7c

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

maxcube/connection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ def read(self):
3535
break
3636
self.response = buffer.decode('utf-8')
3737

38+
def send(self, command):
39+
self.socket.send(command.encode('utf-8'))
40+
self.read()
41+
3842
def disconnect(self):
3943
if self.socket:
40-
# self.send_message(QuitMessage())??
4144
self.socket.close()
4245
self.socket = None

maxcube/cube.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import struct
3+
34
from maxcube.device import \
45
MaxDevice, \
56
MAX_CUBE, \
@@ -26,10 +27,7 @@ def __init__(self, connection):
2627
self.init()
2728

2829
def init(self):
29-
self.connection.connect()
30-
response = self.connection.response
31-
self.parse_response(response)
32-
self.connection.disconnect()
30+
self.update()
3331
logger.info('Cube (rf=%s, firmware=%s)' % (self.rf_address, self.firmware_version))
3432
for device in self.devices:
3533
if self.is_thermostat(device):
@@ -40,6 +38,12 @@ def init(self):
4038
else:
4139
logger.info('Device (rf=%s, name=%s' % (device.rf_address, device.name))
4240

41+
def update(self):
42+
self.connection.connect()
43+
response = self.connection.response
44+
self.parse_response(response)
45+
self.connection.disconnect()
46+
4347
def device_by_rf(self, rf):
4448
for device in self.devices:
4549
if device.rf_address == rf:
@@ -95,6 +99,7 @@ def parse_m_message(self, message):
9599
device_rf_address = ''.join("%X" % x for x in data[pos + 1: pos + 1 + 3])
96100
device_name_length = data[pos + 14]
97101
device_name = data[pos + 15:pos + 15 + device_name_length].decode('utf-8')
102+
room_id = data[pos + 15 + device_name_length]
98103

99104
device = self.device_by_rf(device_rf_address)
100105

@@ -108,6 +113,7 @@ def parse_m_message(self, message):
108113
if device:
109114
device.type = device_type
110115
device.rf_address = device_rf_address
116+
device.room_id = room_id
111117
device.name = device_name
112118

113119
pos += 1 + 3 + 10 + device_name_length + 2
@@ -135,6 +141,26 @@ def parse_l_message(self, message):
135141
device.target_temperature = (data[pos + 7] & 0x7F) / 2
136142
pos += length
137143

144+
def set_target_temperature(self, thermostat, temperature):
145+
rf_address = thermostat.rf_address
146+
if len(rf_address) < 6:
147+
rf_address = '0' + rf_address
148+
room = str(thermostat.room_id)
149+
if thermostat.room_id < 10:
150+
room = '0' + room
151+
target_temperature = int(temperature * 2)
152+
target_temperature |= (1 << 6)
153+
target_temperature &= ~ (1 << 7)
154+
155+
byte_cmd = '000440000000' + rf_address + room + hex(target_temperature)[2:]
156+
command = 's:' + base64.b64encode(bytearray.fromhex(byte_cmd)).decode('utf-8') + '\r\n'
157+
158+
self.connection.connect()
159+
self.connection.send(command)
160+
self.connection.disconnect()
161+
thermostat.target_temperature = int(temperature * 2)/2
162+
163+
138164
@classmethod
139165
def resolve_device_mode(cls, bits):
140166
if not bool(bits & 0x02) and not bool(bits & 0x01):

maxcube/device.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ class MaxDevice:
1515
def __init__(self):
1616
self.type = None
1717
self.rf_address = None
18+
self.room_id = None
1819
self.name = None

tests/test_cube.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
MAX_THERMOSTAT_PLUS, \
99
MAX_DEVICE_MODE_AUTOMATIC, \
1010
MAX_DEVICE_MODE_MANUAL
11+
from maxcube.thermostat import MaxThermostat
1112

1213
INIT_RESPONSE = \
1314
'H:KEQ0566338,0b6475,0113,00000000,74b7b6f7,00,32,0f0c19,1527,03,0000\n\r' \
@@ -34,11 +35,15 @@
3435
class MaxCubeConnectionMock(MaxCubeConnection):
3536
def __init__(self):
3637
super().__init__(None, None)
38+
self.command = None
3739

3840
def connect(self):
3941
self.response = INIT_RESPONSE
4042
return
4143

44+
def send(self, command):
45+
self.command = command
46+
4247
def disconnect(self):
4348
return
4449

@@ -115,6 +120,12 @@ def test_is_thermostat(self):
115120
device.type = MAX_THERMOSTAT_PLUS
116121
self.assertEqual(True, self.cube.is_thermostat(device))
117122

123+
def test_set_target_temperature(self):
124+
self.cube.set_target_temperature(self.cube.devices[0], 24.5)
125+
self.assertEqual('s:AARAAAAABrxTAXE=\r\n', self.cube.connection.command)
126+
self.assertEqual(24.5, self.cube.devices[0].target_temperature)
127+
self.cube.set_target_temperature(self.cube.devices[0], 24.6)
128+
self.assertEqual(24.5, self.cube.devices[0].target_temperature)
118129

119130

120131

0 commit comments

Comments
 (0)