hass_button_emulator.py: restructure + remove on_message handler + require hostname as arg
This commit is contained in:
parent
c84ff53b16
commit
b0d9c1c382
1 changed files with 29 additions and 23 deletions
|
@ -2,33 +2,39 @@
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import mqtt_client
|
import mqtt_client
|
||||||
|
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, reason_code, properties):
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description="Allen & Heath Qu MQTT event emulator")
|
||||||
|
parser.add_argument("mqtt_hostname", help="IP of the MQTT broker")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
print(f"MQTT broker hostname: {args.mqtt_hostname}")
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, reason_code, properties):
|
||||||
print(f"Connected: {reason_code}")
|
print(f"Connected: {reason_code}")
|
||||||
client.subscribe("#")
|
client.subscribe("#")
|
||||||
|
|
||||||
|
client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
|
||||||
|
client.on_connect = on_connect
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
client.connect(args.mqtt_hostname, mqtt_client.MQTT_SERVER_PORT, 60)
|
||||||
print(f"{msg.topic}: {msg.payload.decode()}")
|
client.loop_start()
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
client.loop_stop()
|
||||||
|
client.disconnect()
|
||||||
|
|
||||||
|
|
||||||
client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
|
if __name__ == "__main__":
|
||||||
client.on_connect = on_connect
|
main()
|
||||||
client.on_message = on_message
|
|
||||||
|
|
||||||
client.connect(mqtt_client.MQTT_SERVER_ADDRESS, mqtt_client.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_TOPIC, payload=j, qos=0, retain=False)
|
|
||||||
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
client.loop_stop()
|
|
||||||
client.disconnect()
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue