Stuff
This commit is contained in:
parent
eb198d7b0e
commit
1cb2339270
3 changed files with 81 additions and 0 deletions
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
config.py
|
6
config.py.example
Normal file
6
config.py.example
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
mqttBroker = "127.0.0.1"
|
||||||
|
mqttPort = 1883
|
||||||
|
mqttUser = ""
|
||||||
|
mqttPass = ""
|
||||||
|
deviceId = "lock_leitstelle"
|
||||||
|
deviceName = "Tür Leitstelle"
|
69
mqtt.py
Executable file
69
mqtt.py
Executable file
|
@ -0,0 +1,69 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
import socket
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
from paho.mqtt import client as mqtt_client
|
||||||
|
import config
|
||||||
|
|
||||||
|
debug = True
|
||||||
|
|
||||||
|
lsock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
|
||||||
|
lsock.bind(("127.0.0.1", 3667))
|
||||||
|
|
||||||
|
client_id = f'{config.deviceId}-{random.randint(0, 1000)}'
|
||||||
|
print(client_id)
|
||||||
|
|
||||||
|
|
||||||
|
def publishActionConfig(action):
|
||||||
|
publishTopic = "homeassistant/device_automation/%s/action_%s/config" % (config.deviceId, action)
|
||||||
|
publishPayload = {
|
||||||
|
"automation_type": "trigger",
|
||||||
|
"type": "action",
|
||||||
|
"subtype": action,
|
||||||
|
"payload": action,
|
||||||
|
"topic": "homeassistant/device_automation/%s/action" % (config.deviceId),
|
||||||
|
"device": {
|
||||||
|
"identifiers": config.deviceId,
|
||||||
|
"name": config.deviceName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if debug:
|
||||||
|
print(publishTopic)
|
||||||
|
print(publishPayload)
|
||||||
|
client.publish(publishTopic, json.dumps(publishPayload))
|
||||||
|
|
||||||
|
def publishActionPayload(payload):
|
||||||
|
publishTopic = "homeassistant/device_automation/%s/action" % (config.deviceId)
|
||||||
|
if debug:
|
||||||
|
print(publishTopic)
|
||||||
|
print(payload)
|
||||||
|
client.publish(publishTopic, payload)
|
||||||
|
|
||||||
|
def publishActions():
|
||||||
|
publishActionConfig("locked")
|
||||||
|
publishActionConfig("unlocked")
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
if rc == 0:
|
||||||
|
print("Connected to MQTT Broker!")
|
||||||
|
publishActions()
|
||||||
|
|
||||||
|
|
||||||
|
client = mqtt_client.Client(client_id)
|
||||||
|
client.username_pw_set(config.mqttUser, config.mqttPass)
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.connect(config.mqttBroker, config.mqttPort)
|
||||||
|
client.loop_start()
|
||||||
|
while True:
|
||||||
|
data, remote = lsock.recvfrom(1024)
|
||||||
|
try:
|
||||||
|
if data[0] == 2 and data[1] == 1: #Length 2, Type 1 (Completed Action)
|
||||||
|
if data[2] == 0: #Lock
|
||||||
|
publishActionPayload("locked")
|
||||||
|
if data[2] == 1 or data[2] == 2: #Unlock / Open
|
||||||
|
publishActionPayload("unlocked")
|
||||||
|
except:
|
||||||
|
print("Packet parse failed")
|
||||||
|
print(remote)
|
||||||
|
print(data)
|
Loading…
Reference in a new issue