Handle RGB color values, seperated by semicolon

This commit is contained in:
Ollo 2024-12-22 21:45:04 +01:00
parent 0fd58d07a2
commit 10bbb68dc0

View File

@ -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))