doorbell.py KI-Verbesserungen
This commit is contained in:
@@ -18,46 +18,53 @@ logger.addHandler(logHandler)
|
||||
|
||||
logger.info('Initializing')
|
||||
|
||||
|
||||
# Stelle sicher, dass der Symlink /dev/Doorbell auch auf dem neuen Server existiert!
|
||||
arduino = serial.Serial('/dev/Doorbell', 9600, timeout=.1)
|
||||
# for ESP32
|
||||
#arduino.setDTR(False)
|
||||
#arduino.setRTS(False)
|
||||
|
||||
last_ring = time.time()
|
||||
|
||||
# Ersetze den String nach "Bearer " durch deinen neu erstellten Token aus Schritt 1
|
||||
headers = {
|
||||
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI1YWM2YzBkZTY4ZWY0NTE1YjQ2NzNmNGRlZWQ3YWUxZCIsImlhdCI6MTc4NDA1NTE4OCwiZXhwIjoyMDk5NDE1MTg4fQ.8s50h9BNVSIDlCxmOZIM_gPLHoRBrSpOKvOX-sjFN8U",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
|
||||
while True:
|
||||
data = arduino.readline()[:-2] #the last bit gets rid of the new-line chars
|
||||
data = arduino.readline()[:-2] # Entfernt Newline-Zeichen (\r\n)
|
||||
if data:
|
||||
try:
|
||||
data = data.decode('utf8')
|
||||
except (UnicodeDecodeError) as ude:
|
||||
logger.error(ude)
|
||||
logger.error(f"Decode Error: {ude}")
|
||||
else:
|
||||
logger.info(data)
|
||||
logger.info(f"Received from Arduino: {data}")
|
||||
|
||||
if (data == 'RING'):
|
||||
if data == 'RING':
|
||||
# 1. Lokalen HTTP-Request an den Gong senden
|
||||
try:
|
||||
response = urllib.request.urlopen('http://192.168.178.60/bell/on', timeout=5).read().decode('utf-8')
|
||||
except Exception as e:
|
||||
logger.error(f"Could not trigger physical bell: {e}")
|
||||
|
||||
# send MQTT if last ring was more than 5 seconds ago
|
||||
if ((time.time() - last_ring) > 5):
|
||||
last_ring = time.time()
|
||||
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)
|
||||
elif (data == 'CLICK'):
|
||||
if ((time.time() - last_ring) > 5):
|
||||
# 2. MQTT-Meldung an Home Assistant senden (max. alle 5 Sekunden)
|
||||
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)
|
||||
try:
|
||||
payload_data = json.dumps({'payload': 'RING', 'topic': 'lewe/doorbell', 'retain': 'False'}).encode('utf8')
|
||||
request = urllib.request.Request("https://ha.nodebucket.de/api/services/mqtt/publish", payload_data, headers)
|
||||
response = urllib.request.urlopen(request, timeout=5)
|
||||
except Exception as e:
|
||||
logger.error(f"Could not send MQTT Ring Event to Home Assistant: {e}")
|
||||
|
||||
elif data == 'CLICK':
|
||||
if (time.time() - last_ring) > 5:
|
||||
last_ring = time.time()
|
||||
try:
|
||||
payload_data = json.dumps({'payload': 'CLICK', 'topic': 'lewe/doorbell', 'retain': 'False'}).encode('utf8')
|
||||
request = urllib.request.Request("https://ha.nodebucket.de/api/services/mqtt/publish", payload_data, headers)
|
||||
response = urllib.request.urlopen(request, timeout=5)
|
||||
except Exception as e:
|
||||
logger.error(f"Could not send MQTT Click Event to Home Assistant: {e}")
|
||||
|
||||
# Kleiner Sleep, um die CPU im Idle-Zustand zu entlasten
|
||||
time.sleep(0.05)
|
||||
Reference in New Issue
Block a user