uno-q-home-assistant-integration.py

Arduino UNO Q SBC Controller Bundle · File : uno-q-home-assistant-integration.py · Language : python · Use case : Envoyer les états des boutons Modulino vers un serveur Home Assistant via MQTT pour la domotique.

Network integration allowing physical Modulino buttons to become communicating smart switches via MQTT.

Configure an MQTT server (e.g., Mosquitto) on your local network. Modify the broker IP address in the script. Connect a Modulino Button node. The paho-mqtt library must be installed ('pip install paho-mqtt'). The script reads keyboard events and converts them into MQTT messages.

uno-q-home-assistant-integration.py

#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import evdev
import json

MQTT_BROKER = "192.168.1.100" # IP de votre serveur Home Assistant
MQTT_PORT = 1883
DEVICE_PATH = '/dev/input/event3' # Bouton Modulino

def on_connect(client, userdata, flags, rc):
    print("Connecté au broker MQTT avec code " + str(rc))

def main():
    client = mqtt.Client()
    client.on_connect = on_connect
    client.connect(MQTT_BROKER, MQTT_PORT, 60)
    client.loop_start()
    
    device = evdev.InputDevice(DEVICE_PATH)
    print(f"Surveillance de {device.name}...")
    
    for event in device.read_loop():
        if event.type == evdev.ecodes.EV_KEY:
            state = "ON" if event.value else "OFF"
            payload = json.dumps({"state": state})
            # Publie sur un topic Home Assistant
            client.publish("home/uno_q/button1", payload=payload, retain=True)
            print(f"Publié: {payload}")

if __name__ == '__main__':
    main()

← Back to the board