diff --git a/mqttclient/fanATserial2mqtt.py b/mqttclient/fanATserial2mqtt.py index 44da183..a4b3f58 100644 --- a/mqttclient/fanATserial2mqtt.py +++ b/mqttclient/fanATserial2mqtt.py @@ -52,8 +52,24 @@ def on_message(client, userdata, msg): else: client.publish(mqtt_topic + "cmd/exception", "Serial Connection closed") # clear exception elif ((mqtt_topic + "cmd/led") == msg.topic): - command = COMMAND_PREFIX+"l" + payload - ser.write(command.encode('utf-8')) + # OpenHAB r,g,b payload: 20,0,250 + try: + if (',' in payload): + red, green, blue = str(payload).split(',') + logging.debug("Red=" + str(red) + " Green=" + str(green) + " Blue=" + str(blue)) + redB= int((int(red)*255)/250) + logging.debug("Byte Red=" + str(redB)) + greenB= int((int(green)*255)/250) + logging.debug("Byte Green =" + str(greenB)) + blueB= int((int(blue)*255)/250) + logging.debug("Byte Blue =" + str(blueB)) + command = COMMAND_PREFIX+"l{:02x}{:02x}{:02x}".format(redB, greenB, blueB) + else: + command = COMMAND_PREFIX+"l" + payload + ser.write(command.encode('utf-8')) + except (ValueError): + logging.error("Wrong MQTT Command: '" + str(payload) + "'") + else: logging.warning(msg.topic+" "+str(msg.payload))