Skip to content

Instantly share code, notes, and snippets.

@buttercheetah
Forked from ghomasHudson/README.md
Last active October 13, 2021 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buttercheetah/6cf0c81a5a45404f00736f4ce52aaf97 to your computer and use it in GitHub Desktop.
Save buttercheetah/6cf0c81a5a45404f00736f4ce52aaf97 to your computer and use it in GitHub Desktop.
OpenRGB to MQTT light with authentication

This is a fork from ghomasHudson that allows for mqtt authentication. I also added a 5 second delay when loading the script to allow for autostarting at boot.

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 the adress of your mqtt server:

Example: 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.
  • mqtt_user/mqqt_pass - This setting allows you to have a mqtt username an dpassword. Leave Null if you dont have a login on your mqtt server.
from openrgb import OpenRGBClient
from openrgb.utils import RGBColor, DeviceType
import paho.mqtt.client as mqtt
import json
import time
import argparse
print("sleeping 5 sec to wait for OpenRGB to start")
time.sleep(5)
ENTITY_NAME = "Desktop_RGB" # The name of a Home Assistant entity that will be added
mqtt_user = 'Null' # Your mqtt login. Leave Null if you dont have a login
mqtt_pass = 'Null'
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]
if mqtt_user != 'Null' or mqtt_pass != 'Null':
client.username_pw_set(mqtt_user, mqtt_pass)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment