From 10bbb68dc0dceb7fdb569419e4afe994622ac393 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 22 Dec 2024 21:45:04 +0100 Subject: [PATCH] Handle RGB color values, seperated by semicolon --- mqttclient/fanATserial2mqtt.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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))