From 5ae6d1f8327c9581d2ba13915de5184435730689 Mon Sep 17 00:00:00 2001 From: coon Date: Fri, 26 Sep 2025 23:09:13 +0200 Subject: [PATCH 1/5] mqtt_client.py: add auto discovery for home assistant --- hass_button_emulator.py | 2 +- mqtt_client.py | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/hass_button_emulator.py b/hass_button_emulator.py index ef85604..66bb97d 100755 --- a/hass_button_emulator.py +++ b/hass_button_emulator.py @@ -28,7 +28,7 @@ def main(): j = json.dumps({"command": "scene_recall", "args": {"scene_id": 0}}) # j = json.dumps({'command': 'shutdown'}) - client.publish(mqtt_client.MQTT_TOPIC, payload=j, qos=0, retain=False) + client.publish(mqtt_client.MQTT_COMMAND_TOPIC, payload=j, qos=0, retain=False) time.sleep(2) diff --git a/mqtt_client.py b/mqtt_client.py index 3f96c78..9734cad 100644 --- a/mqtt_client.py +++ b/mqtt_client.py @@ -7,15 +7,38 @@ import paho.mqtt.client as mqtt import mixer from mixer import Mixer + MQTT_SERVER_PORT = 1883 -MQTT_TOPIC = "homeassistant/device/mixer" +MQTT_DISCOVERY_TOPIC = "homeassistant/button/mixer/config" +MQTT_COMMAND_TOPIC = "livingroom/voc/allen_heath_qu16_mixer" m: Mixer = None +def mqtt_autodiscovery(client): + j = json.dumps( + { + "name": "Reset Settings", + "command_topic": MQTT_COMMAND_TOPIC, + "command_template": '{"command": "scene_recall", "args": {"scene_id": 0}}', + "device": { + "name": "Mischpult", + "model": "Qu-16", + "manufacturer": "Allen & Heath", + "suggested_area": "livingroom", + "identifiers": "Mischpult", + }, + "unique_id": "mischpult_reset_settings", + } + ) + + client.publish(MQTT_DISCOVERY_TOPIC, payload=j, qos=0, retain=False) + + def on_connect(client, userdata, flags, reason_code, properties): print(f"Connected: {reason_code}") - client.subscribe(MQTT_TOPIC) + client.subscribe(MQTT_COMMAND_TOPIC) + mqtt_autodiscovery(client) def on_message(client, userdata, msg): From 6acb9194865e924702a260d82b9dceff05dbb570 Mon Sep 17 00:00:00 2001 From: coon Date: Fri, 26 Sep 2025 23:35:19 +0200 Subject: [PATCH 2/5] mqtt_client.py: move implemntation into MqttClient class --- hass_button_emulator.py | 4 +-- mqtt_client.py | 78 ++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/hass_button_emulator.py b/hass_button_emulator.py index 66bb97d..e7c854b 100755 --- a/hass_button_emulator.py +++ b/hass_button_emulator.py @@ -22,13 +22,13 @@ def main(): client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2) client.on_connect = on_connect - client.connect(args.mqtt_hostname, mqtt_client.MQTT_SERVER_PORT, 60) + client.connect(args.mqtt_hostname, mqtt_client.MqttClient.MQTT_SERVER_PORT, 60) client.loop_start() j = json.dumps({"command": "scene_recall", "args": {"scene_id": 0}}) # j = json.dumps({'command': 'shutdown'}) - client.publish(mqtt_client.MQTT_COMMAND_TOPIC, payload=j, qos=0, retain=False) + client.publish(mqtt_client.MqttClient.MQTT_COMMAND_TOPIC, payload=j, qos=0, retain=False) time.sleep(2) diff --git a/mqtt_client.py b/mqtt_client.py index 9734cad..9003a0f 100644 --- a/mqtt_client.py +++ b/mqtt_client.py @@ -15,44 +15,54 @@ MQTT_COMMAND_TOPIC = "livingroom/voc/allen_heath_qu16_mixer" m: Mixer = None -def mqtt_autodiscovery(client): - j = json.dumps( - { - "name": "Reset Settings", - "command_topic": MQTT_COMMAND_TOPIC, - "command_template": '{"command": "scene_recall", "args": {"scene_id": 0}}', - "device": { - "name": "Mischpult", - "model": "Qu-16", - "manufacturer": "Allen & Heath", - "suggested_area": "livingroom", - "identifiers": "Mischpult", - }, - "unique_id": "mischpult_reset_settings", - } - ) +class MqttClient: + def __init__(self): + self.client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2) - client.publish(MQTT_DISCOVERY_TOPIC, payload=j, qos=0, retain=False) + def mqtt_autodiscovery(self, client): + j = json.dumps( + { + "name": "Reset Settings", + "command_topic": MQTT_COMMAND_TOPIC, + "command_template": '{"command": "scene_recall", "args": {"scene_id": 0}}', + "device": { + "name": "Mischpult", + "model": "Qu-16", + "manufacturer": "Allen & Heath", + "suggested_area": "livingroom", + "identifiers": "Mischpult", + }, + "unique_id": "mischpult_reset_settings", + } + ) + self, client.publish(MQTT_DISCOVERY_TOPIC, payload=j, qos=0, retain=False) -def on_connect(client, userdata, flags, reason_code, properties): - print(f"Connected: {reason_code}") - client.subscribe(MQTT_COMMAND_TOPIC) - mqtt_autodiscovery(client) + def on_connect(self, client, userdata, flags, reason_code, properties): + print(f"Connected: {reason_code}") + client.subscribe(MQTT_COMMAND_TOPIC) + self.mqtt_autodiscovery(client) -def on_message(client, userdata, msg): - payload = msg.payload.decode() + def on_message(self, client, userdata, msg): + payload = msg.payload.decode() - print(f"{msg.topic}: {payload}") + print(f"{msg.topic}: {payload}") - j = json.loads(payload) + j = json.loads(payload) - match j["command"]: - case "scene_recall": - m.scene_recall(j["args"]["scene_id"]) - case "shutdown": - m.shutdown() + match j["command"]: + case "scene_recall": + m.scene_recall(j["args"]["scene_id"]) + case "shutdown": + m.shutdown() + + def run(self, hostname): + self.client.on_connect = self.on_connect + self.client.on_message = self.on_message + + self.client.connect(hostname, MQTT_SERVER_PORT, 60) + self.client.loop_forever() def main(): @@ -78,12 +88,8 @@ def main(): global m m = mixer.Mixer(args.mixer_ip) - client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2) - client.on_connect = on_connect - client.on_message = on_message - - client.connect(args.mqtt_hostname, MQTT_SERVER_PORT, 60) - client.loop_forever() + client = MqttClient() + client.run(args.mqtt_hostname) if __name__ == "__main__": From 3a97a7a05973acc501c14042eb4a6868a2971b33 Mon Sep 17 00:00:00 2001 From: coon Date: Fri, 26 Sep 2025 23:45:24 +0200 Subject: [PATCH 3/5] mqtt_client.py: move global vars into MqttClient class --- mqtt_client.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/mqtt_client.py b/mqtt_client.py index 9003a0f..6dbe75f 100644 --- a/mqtt_client.py +++ b/mqtt_client.py @@ -8,22 +8,20 @@ import mixer from mixer import Mixer -MQTT_SERVER_PORT = 1883 -MQTT_DISCOVERY_TOPIC = "homeassistant/button/mixer/config" -MQTT_COMMAND_TOPIC = "livingroom/voc/allen_heath_qu16_mixer" - -m: Mixer = None - - class MqttClient: - def __init__(self): + MQTT_SERVER_PORT = 1883 + MQTT_DISCOVERY_TOPIC = "homeassistant/button/mixer/config" + MQTT_COMMAND_TOPIC = "livingroom/voc/allen_heath_qu16_mixer" + + def __init__(self, mixer_ip): + self.m = mixer.Mixer(mixer_ip) self.client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2) def mqtt_autodiscovery(self, client): j = json.dumps( { "name": "Reset Settings", - "command_topic": MQTT_COMMAND_TOPIC, + "command_topic": self.MQTT_COMMAND_TOPIC, "command_template": '{"command": "scene_recall", "args": {"scene_id": 0}}', "device": { "name": "Mischpult", @@ -36,12 +34,12 @@ class MqttClient: } ) - self, client.publish(MQTT_DISCOVERY_TOPIC, payload=j, qos=0, retain=False) + self, client.publish(self.MQTT_DISCOVERY_TOPIC, payload=j, qos=0, retain=False) def on_connect(self, client, userdata, flags, reason_code, properties): print(f"Connected: {reason_code}") - client.subscribe(MQTT_COMMAND_TOPIC) + client.subscribe(self.MQTT_COMMAND_TOPIC) self.mqtt_autodiscovery(client) def on_message(self, client, userdata, msg): @@ -53,15 +51,15 @@ class MqttClient: match j["command"]: case "scene_recall": - m.scene_recall(j["args"]["scene_id"]) + self.m.scene_recall(j["args"]["scene_id"]) case "shutdown": - m.shutdown() + self.m.shutdown() def run(self, hostname): self.client.on_connect = self.on_connect self.client.on_message = self.on_message - self.client.connect(hostname, MQTT_SERVER_PORT, 60) + self.client.connect(hostname, self.MQTT_SERVER_PORT, 60) self.client.loop_forever() @@ -85,10 +83,7 @@ def main(): print(f"Mixer IP: {args.mixer_ip}, MQTT broker hostname: {args.mqtt_hostname}") - global m - m = mixer.Mixer(args.mixer_ip) - - client = MqttClient() + client = MqttClient(args.mixer_ip) client.run(args.mqtt_hostname) From 14c4ec65eef3cc5d24f903d94d01a4ec0062811e Mon Sep 17 00:00:00 2001 From: coon Date: Fri, 26 Sep 2025 23:47:12 +0200 Subject: [PATCH 4/5] mqtt_client.py: remove mixer import --- mqtt_client.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mqtt_client.py b/mqtt_client.py index 6dbe75f..f461db4 100644 --- a/mqtt_client.py +++ b/mqtt_client.py @@ -4,7 +4,6 @@ import argparse import json import paho.mqtt.client as mqtt -import mixer from mixer import Mixer @@ -14,7 +13,7 @@ class MqttClient: MQTT_COMMAND_TOPIC = "livingroom/voc/allen_heath_qu16_mixer" def __init__(self, mixer_ip): - self.m = mixer.Mixer(mixer_ip) + self.mixer = Mixer(mixer_ip) self.client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2) def mqtt_autodiscovery(self, client): @@ -51,9 +50,9 @@ class MqttClient: match j["command"]: case "scene_recall": - self.m.scene_recall(j["args"]["scene_id"]) + self.mixer.scene_recall(j["args"]["scene_id"]) case "shutdown": - self.m.shutdown() + self.mixer.shutdown() def run(self, hostname): self.client.on_connect = self.on_connect From bde5e940c825478ae5b0c097765472f07e644b8a Mon Sep 17 00:00:00 2001 From: coon Date: Fri, 26 Sep 2025 23:51:20 +0200 Subject: [PATCH 5/5] hass_button_emulator.py: ruff format --- hass_button_emulator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hass_button_emulator.py b/hass_button_emulator.py index e7c854b..bdc3213 100755 --- a/hass_button_emulator.py +++ b/hass_button_emulator.py @@ -28,7 +28,9 @@ def main(): j = json.dumps({"command": "scene_recall", "args": {"scene_id": 0}}) # j = json.dumps({'command': 'shutdown'}) - client.publish(mqtt_client.MqttClient.MQTT_COMMAND_TOPIC, payload=j, qos=0, retain=False) + client.publish( + mqtt_client.MqttClient.MQTT_COMMAND_TOPIC, payload=j, qos=0, retain=False + ) time.sleep(2)