-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_august_bridge.py
193 lines (170 loc) · 7.88 KB
/
mqtt_august_bridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import paho.mqtt.client as mqtt
import bluepy.btle as btle
import augustpy.lock
import json
import time
import argparse
import threading
global mqtt_event
global mqtt_message
global lock
def onStatusUpdate(state):
client.publish("august/lock/state", state)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("august/lock/set")
def on_mqtt(client, userdata, message):
# global lock
global mqtt_event
global mqtt_message
# print("received message: " ,str(message.payload.decode("utf-8")))
# print("received topic: " ,str(message.topic.decode("utf-8")))
print(" Received message " + str(message.payload)
+ " on topic '" + message.topic
+ "' with QoS " + str(message.qos))
mqtt_message = message
mqtt_event.set()
# if(str(message.payload.decode("utf-8")) == 'LOCK'):
# print("got lock!")
# if(lock.is_connected()):
# try:
# lock.force_lock()
# except btle.BTLEDisconnectError:
# #lock.conn_state = "disconnected"
# lock.is_secure = False
# lock.session = None
# lock.peripheral = None
# print("Device disconnected unexpectedly!")
# lock.connect()
# lock.force_lock()
# elif(str(message.payload.decode("utf-8")) == 'UNLOCK'):
# print("got unlock!")
# if(lock.is_connected()):
# try:
# lock.force_unlock()
# except btle.BTLEDisconnectError:
# print("Failed... Reconnecting.")
# lock.connect()
# elif(str(message.payload.decode("utf-8")) == 'STATUS'):
# if(lock.is_connected()):
# lock.getStatus()
mqtt_event = threading.Event()
broker_address="192.168.0.192" # <== this is where your MQTT server IP goes. No need for the port.
client = mqtt.Client("august_rpi") # <== this is just the name of the MQTT client. I called mine "august_rpi"
client.username_pw_set("august", "lock") # <== use this if your MQTT server requires authentication. If not, you can comment out this whole line.
client.connect(broker_address)
client.publish("august/bridge/availability", "online", retain=True)
#client.subscribe("august/lock/set")
client.on_message = on_mqtt
client.on_connect = on_connect
client.loop_start()
config = None
i = 0
with open("config.json", "r") as config_file:
config = json.load(config_file)
if type(config) is dict:
config = [config]
locks = []
for lock_config in config:
lock = augustpy.lock.Lock(lock_config["bluetoothAddress"], lock_config["handshakeKey"], lock_config["handshakeKeyIndex"])
if "name" in lock_config:
lock.set_name(lock_config["name"])
locks.append(lock)
lock = locks[0] ##sketch AF....
lock._onStatusUpdate = onStatusUpdate
if(lock.connect()):
client.publish("homeassistant/lock/L30DGX6/config", '{"name": "Front Door Lock", "state_topic": "august/lock/state", "availability_topic": "august/lock/availability", "unique_id": "L30DGX6", "command_topic": "august/lock/set"}', retain=True)
client.publish("august/lock/availability", "online", retain=True)
lock.getStatus()
# client.publish("august/lock/state", lock.getStatus())
else:
client.publish("august/lock/availability", "offline")
while(1):
if(mqtt_event.wait(75)): #we got an mqtt event
mqtt_event.clear()
if(str(mqtt_message.payload.decode("utf-8")) == 'LOCK'):
print("got lock mqtt message!")
if(lock.is_connected()):
if(lock.force_lock()):
pass
else:
#client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
lock.force_lock()
else:
print("Couldn't reconnect & lock")
#client.publish("august/lock/availability", "offline", retain=True)
else:
client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
lock.force_lock()
elif(str(mqtt_message.payload.decode("utf-8")) == 'UNLOCK'):
print("got unlock mqtt message!")
if(lock.is_connected()):
if(lock.force_unlock()):
pass
else:
#client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
lock.force_unlock()
else:
print("Couldn't reconnect & unlock")
#client.publish("august/lock/availability", "offline", retain=True)
else:
client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
lock.force_lock()
else: #75 seconds expired
if(i==0):
i=1
if(lock.is_connected()):
if(lock.getStatus()):
client.publish("august/lock/availability", "online", retain=True)
else:
if(lock.is_connected()):
lock.getStatus() # try again
if(not lock.is_connected()):
#client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
else:
print("Couldn't reconnect")
# client.publish("august/lock/availability", "offline", retain=True)
else:
#client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
else:
client.publish("august/lock/availability", "offline", retain=True)
else:
i=0
if(lock.is_connected()):
resp = lock.getVoltage()
if(resp):
client.publish("august/lock/voltage", resp)
client.publish("august/lock/availability", "online", retain=True)
else:
if(lock.is_connected()):
resp = lock.getVoltage() # try again
if(resp):
client.publish("august/lock/voltage", resp)
client.publish("august/lock/availability", "online", retain=True)
if(not lock.is_connected()):
#client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
else:
print("Couldn't reconnect")
# client.publish("august/lock/availability", "offline", retain=True)
else:
#client.publish("august/lock/availability", "offline", retain=True)
if(lock.connect()):
client.publish("august/lock/availability", "online", retain=True)
else:
client.publish("august/lock/availability", "offline", retain=True)