From ce98f21c979dbeb6eb3f5f49d609683c2816e0f6 Mon Sep 17 00:00:00 2001 From: tilman Date: Sun, 5 Jul 2026 15:58:27 +0000 Subject: [PATCH] klingel --- doorbell/klingel_sender_nano_serial/doorbell.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/doorbell/klingel_sender_nano_serial/doorbell.py b/doorbell/klingel_sender_nano_serial/doorbell.py index b6e1513..20b2bad 100644 --- a/doorbell/klingel_sender_nano_serial/doorbell.py +++ b/doorbell/klingel_sender_nano_serial/doorbell.py @@ -25,6 +25,11 @@ arduino = serial.Serial('/dev/Doorbell', 9600, timeout=.1) #arduino.setRTS(False) last_ring = time.time() +headers = { + "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjYjY5NjA5NmY0N2E0NmZlOGEzYjczOGYzYmUzOTk5ZSIsImlhdCI6MTY2MTM0NDgxOSwiZXhwIjoxOTc2NzA0ODE5fQ.z9fzbVOA1Pbur3Zb07481n-SoYesj8cetw-CX6WPmTs", + "Content-Type": "application/json" +} + while True: data = arduino.readline()[:-2] #the last bit gets rid of the new-line chars @@ -43,13 +48,15 @@ while True: # send MQTT if last ring was more than 5 seconds ago if ((time.time() - last_ring) > 5): last_ring = time.time() - headers = { - "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjYjY5NjA5NmY0N2E0NmZlOGEzYjczOGYzYmUzOTk5ZSIsImlhdCI6MTY2MTM0NDgxOSwiZXhwIjoxOTc2NzA0ODE5fQ.z9fzbVOA1Pbur3Zb07481n-SoYesj8cetw-CX6WPmTs", - "Content-Type": "application/json" - } data = json.dumps({'payload': 'RING', 'topic': 'lewe/doorbell', 'retain': 'False'}).encode('utf8') request = urllib.request.Request("https://ha.nodebucket.de/api/services/mqtt/publish", data, headers) response = urllib.request.urlopen(request) except Exception: logger.error("Could not ring doorbell") - time.sleep(300) \ No newline at end of file + time.sleep(300) + elif (data == 'CLICK'): + if ((time.time() - last_ring) > 5): + last_ring = time.time() + data = json.dumps({'payload': 'CLICK', 'topic': 'lewe/doorbell', 'retain': 'False'}).encode('utf8') + request = urllib.request.Request("https://ha.nodebucket.de/api/services/mqtt/publish", data, headers) + response = urllib.request.urlopen(request)