For testing new stuff on Home Assistant I created a MQTT switch ‘simulator’. This way I can use switches in the frontend without have a physical switch connected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/usr/bin/python3 import paho.mqtt.client as mqtt import paho.mqtt.publish as publish HOST = '127.0.0.1' PORT = 1883 TOPIC = 'home-assistant/device1/0' PAYLOAD_ON = '1' PAYLOAD_OFF = '0' def on_message(mqttc, obj, msg): if msg.payload.decode('utf-8') == PAYLOAD_ON: publish.single(TOPIC, PAYLOAD_ON, hostname=HOST) else: publish.single(TOPIC, PAYLOAD_OFF, hostname=HOST) mqttc = mqtt.Client() mqttc.on_message = on_message mqttc.connect(HOST, PORT, 60) mqttc.subscribe('{}/set'.format(TOPIC), 0) mqttc.loop_forever() |
And the part for the configuration file.
1 2 3 4 5 6 7 |
switch: - platform: mqtt name: "Dummy switch" state_topic: "home-assistant/device1/0" command_topic: "home-assistant/device1/0/set" payload_on: "1" payload_off: "0" |