Skip to content

Instantly share code, notes, and snippets.

@ghomasHudson
Last active January 22, 2023 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ghomasHudson/7cc24aa187e8141003073e36e068a5a2 to your computer and use it in GitHub Desktop.
Save ghomasHudson/7cc24aa187e8141003073e36e068a5a2 to your computer and use it in GitHub Desktop.
OpenRGB to MQTT light

Install dependancies: pip install openrgb-python paho-mqtt.

  1. Run openrgb server if not already running: openrgb --server
  2. Run openrgb_to_mqtt.py with th adress of your mqtt server: python openrgb_to_mqtt.py 192.168.1.1

Your light should now appear in homeassistant.

You might want to tweak:

  • ENTITY_NAME - this will effect what the light is called in home assistant
  • Which light we're controlling. Currently I just pick the first device of type MOTHERBOARD. This could be changed to any type or you could just loop through all client.devices.
from openrgb import OpenRGBClient
from openrgb.utils import RGBColor, DeviceType
import paho.mqtt.client as mqtt
import json
import time
import argparse
ENTITY_NAME = "pc"
parser = argparse.ArgumentParser()
parser.add_argument("--openrgb_port", help="The port the openrgb port is using", default=6742, type=int)
parser.add_argument("mqtt_address", help="URL for mqtt", type=str)
args = parser.parse_args()
base_topic = "homeassistant/light/" + ENTITY_NAME
state = {"state":"ON","brightness":255,"color":{"r":255,"g":255,"b":255}}
def on_connect(client, userdata, flags, rc):
print("Connection returned result: "+mqtt.connack_string(rc))
def get_real_color(state):
"""Apply brightness scaling to RGB"""
brightness_multiplier = state["brightness"]/255
return [int(state["color"]["r"]*brightness_multiplier),int(state["color"]["g"]*brightness_multiplier),int(state["color"]["b"]*brightness_multiplier)]
def on_message(client, userdata, message):
global state
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
msg = json.loads(message.payload)
starting_col = get_real_color(state)
state = {**state, **msg}
client.publish(base_topic+"/state", payload=json.dumps(state), qos=0, retain=False)
if msg["state"] == "OFF":
final_col = [0, 0, 0]
else:
final_col = get_real_color(state)
light.set_color(RGBColor(final_col[0],final_col[1],final_col[2]))
def Initialise_clients(cname):
#callback assignment
client= mqtt.Client(cname,False) #don't use clean session
client.on_connect= on_connect #attach function to callback
client.on_message=on_message #attach function to callback
client.topic_ack=[]
client.run_flag=False
client.running_loop=False
client.subscribe_flag=False
client.bad_connection_flag=False
client.connected_flag=False
client.disconnect_flag=False
return client
client = Initialise_clients(ENTITY_NAME+"_rgb")
# client.username_pw_set("user", password="password")
rgb_client = OpenRGBClient('127.0.0.1', args.openrgb_port, 'mqtt')
light = rgb_client.get_devices_by_type(DeviceType.MOTHERBOARD)[0]
client.connect(args.mqtt_address)
client.subscribe(base_topic+"/set")
config = {"~": base_topic, "name":ENTITY_NAME, "unique_id":ENTITY_NAME+"_light", "cmd_t": "~/set","stat_t":"~/state", "schema": "json", "brightness":"true","rgb":"true"}
client.publish(base_topic+"/config", payload=json.dumps(config), qos=0, retain=False)
client.publish(base_topic+"/state", payload=json.dumps(state), qos=0, retain=False)
# Set initial light
c = get_real_color(state)
light.set_color(RGBColor(c[0],c[1],c[2]))
client.loop_forever()
@Dav1d-Fn
Copy link

Hey, i have a problem with the connection. It seems to be running good, but there is nothing in Homa Assistant.. I use mqtt with username and password, could this be the problem? I tried it with line 60 in your code, but it doesn't change anything

@ghomasHudson
Copy link
Author

Hey, i have a problem with the connection. It seems to be running good, but there is nothing in Homa Assistant.. I use mqtt with username and password, could this be the problem? I tried it with line 60 in your code, but it doesn't change anything

Hmmm. I'd try using mqtt-explorer or similar and check whether the mqtt entries are made or not. Otherwise, I'm directly using the paho-mqtt library so check the documentation there.

If you figure it out - let me know and I'll update the code.

@Dav1d-Fn
Copy link

The tool was very helpful. I have it now in Home Assistant and I can see it in MQTT Explorer. My issue was simple... in client.username_pw_set("user", password="password") I put the password= in the brackets.

client.username_pw_set("user", "password") would be a little bit better.

But now I have the problem, that there is no possibility to control the light. I have options for brightness and on/off, but nothing for RGB and also the given options won't work.

@Dav1d-Fn
Copy link

I have the right entity name "pc", but nothing like "pc_rgb". In line 59, why is there ENTITY_NAME+"_rgb"?

@ghomasHudson
Copy link
Author

I have the right entity name "pc", but nothing like "pc_rgb". In line 59, why is there ENTITY_NAME+"_rgb"?

That's just the name you connect to the mqtt broker with - it shouldn't appear in home assistant. I added _rgb in case you have some over mqtt stuff on the same PC.

@ghomasHudson
Copy link
Author

What output do you get when you run the script?
Do you get the "Connection returned result" bit? I'm assuming so because the entry is added in MQTT Explorer.

Do you get any "message received" stuff when you change the light in home assistant? It should appear as a normal RGB light.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment