11import base64
22import struct
3+
34from 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 ):
0 commit comments