def get_device_serialized(addr): response.content_type = 'application/octet-stream' try: device = devices.get_device(addr) return pickle.dumps(device) except KeyError: pass
def on_message(self, client, userdata, msg): topic = msg.topic.replace(self.cmnd_topic.strip('/') + '/', '').split('/') cmnd = topic[0] addr = int(topic[1], 10) if self.debug: print(" < MQTT: {} {} {}".format( addr, cmnd, msg.payload.decode('utf_8').strip())) sys.stdout.flush() try: payload = msg.payload.decode('utf_8').strip() device = devices.get_device(addr) if cmnd == CommandMode.abbr: try: payload = json.loads( os.getenv("MQTT_MODES_RECEIVE"))[payload] except KeyError: pass device.set_mode(payload) if device.group is not None: for dev in device.group.devices: self.publish_availability(dev) else: self.publish_availability(device) elif cmnd == CommandTemperature.abbr: device.set_temperature(payload) if device.group is not None: for dev in device.group.devices: self.publish_availability(dev) else: self.publish_availability(device) elif cmnd == CommandStatus.abbr: device.update_stats() self.publish_availability(device) elif cmnd == CommandReboot.abbr: device.reboot_device() self.publish_availability(device) elif cmnd == 'preset': try: payload = json.loads( os.getenv("MQTT_PRESETS_RECEIVE"))[payload] except KeyError: pass device.set_preset(payload) if device.group is not None: for dev in device.group.devices: self.publish_availability(dev) except KeyError: pass except ValueError: pass
def parse_device_line(self, line): try: self.device = devices.get_device(int(line[1:3], 16)) self.data = line[4:] print(' ({})'.format(self.device.name) if self.device is not None else '', end='') self.device.set_availability() if not self.device.is_available(): commands.discard_all(self.device) except KeyError: pass
def cancel_commands(addr): try: if devices.is_remote_device(addr): RemoteController.redirect_command(request, addr) else: device = devices.get_device(addr) device.cancel_commands() mqtt.publish_availability(device) ws.send_device_stats(device) except KeyError: pass print(' < HTTP: {} cancel_commands'.format(addr)) sys.stdout.flush()
def settings(addr): try: if devices.is_remote_device(addr): settings = devices.get_device_from_remote(addr).settings else: settings = devices.get_device(addr).settings if 'ff' in settings: layout = get_eeprom_layout(int('0x' + settings['ff'], 16)) return template('settings', title='Settings', layout=layout, device_settings=settings) except KeyError: pass print(' < HTTP: {} settings'.format(addr))
def set_settings(addr): try: if devices.is_remote_device(addr): RemoteController.redirect_command(request, addr) else: device = devices.get_device(addr) for idx, value in device.settings.items(): new = request.json.get(idx) if new != value: device.send_setting(idx, new) mqtt.publish_availability(device) ws.send_device_stats(device) except KeyError: pass print(' < HTTP: {} set_settings'.format(addr)) sys.stdout.flush()
def timers(addr): try: if devices.is_remote_device(addr): device = devices.get_device_from_remote(addr) else: device = devices.get_device(addr) mode = device.settings['22'] mode = 1 if mode is not None and int(mode, 16) > 0 else 0 preset0 = device.settings['01'] preset1 = device.settings['02'] preset2 = device.settings['03'] preset3 = device.settings['04'] presets = [ { 'id': 0, 'name': 'Frost', 'temp': preset0 if preset0 is not None else '00' }, { 'id': 1, 'name': 'Eco', 'temp': preset1 if preset1 is not None else '00' }, { 'id': 2, 'name': 'Comfort', 'temp': preset2 if preset2 is not None else '00' }, { 'id': 3, 'name': 'Super Comfort', 'temp': preset3 if preset3 is not None else '00' }, ] return template('timers', title='Timers', mode=mode, timers=device.timers, presets=presets) except KeyError: pass print(' < HTTP: {} timers'.format(addr))
def set_timers(addr): try: if devices.is_remote_device(addr): RemoteController.redirect_command(request, addr) else: device = devices.get_device(addr) timers = device.timers for day, value in request.json['timers'].items(): if timers[int(day[0])][int(day[1])] != value: device.send_timer(day, value) new_mode = int(request.json['mode']) if new_mode != (0 if int(device.settings['22'], 16) == 0 else 1): device.send_setting('22', '{:02x}'.format(new_mode)) mqtt.publish_availability(device) ws.send_device_stats(device) except KeyError: pass print(' < HTTP: {} set_timers'.format(addr)) sys.stdout.flush()
def set_mode(addr): mode = request.json.get('mode') try: if devices.is_remote_device(addr): RemoteController.redirect_command(request, addr) else: device = devices.get_device(addr) device.set_mode(mode) if device.group is not None: for dev in device.group.devices: mqtt.publish_availability(dev) ws.send_device_stats(dev) else: mqtt.publish_availability(device) ws.send_device_stats(device) except KeyError: pass except ValueError: pass print(' < HTTP: {} mode {}'.format(addr, mode)) sys.stdout.flush()
def set_temp(addr): temp = float(request.json.get('temp')) try: if devices.is_remote_device(addr): RemoteController.redirect_command(request, addr) else: device = devices.get_device(addr) device.set_temperature(temp) if device.group is not None: for dev in device.group.devices: mqtt.publish_availability(dev) ws.send_device_stats(dev) else: mqtt.publish_availability(device) ws.send_device_stats(device) except KeyError: pass except ValueError: pass print(' < HTTP: {} temp {}'.format(addr, temp)) sys.stdout.flush()