forked from eriknl1982/zemirollshade
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollshade.py
117 lines (104 loc) · 3.6 KB
/
rollshade.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
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import time
import re
import Zemismart
### Variables
mqtt_client = "192.168.1.1" #mqtt IP
mqtt_port = 1883
mqtt_user = "username"
mqtt_password = "password"
mqtt_path = "blinds"
dev_pin = 8888
#### Don't edit below here
# btle.Debugging=True
def shade_command(fble, fcmd):
print ("["+ fble + "] Connecting")
shade = Zemismart.Zemismart(fble, dev_pin)
with shade:
print ("["+ fble + "] Connected!")
if fcmd == "open":
shade.open()
elif fcmd == "close":
shade.close()
elif fcmd == "stop":
shade.stop()
else:
print("Unrecognized command.")
print ("["+ fble + "] Disconnected")
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected to MQTT 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(mqtt_path + "/#")
def checkMAC(x):
if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", x.lower()):
return 1
else:
return 0
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
# print ("message comming in")
mac = msg.topic.replace(mqtt_path + "/", "")
if checkMAC(mac) == 0 and (msg.payload == open or msg.payload == close):
print ("["+ mac + "] Is not a valid Mac Address")
return
if msg.payload.decode() == "open":
t = 1
while t <= 3:
try:
shade_command(mac, "open")
client.publish(msg.topic + "/status", "on", qos=0, retain=False)
print ("["+ mac + "] Status Published: " + msg.topic + "/status")
print ("["+ mac + "] Finished")
break
except:
time.sleep(0.5)
if t <= 3:
print ("["+ mac + "] Error! - Trying to Connect Again! (" + str(t) + "/3)")
else:
print ("["+ mac + "] Error! - Can't Connect")
t += 1
if msg.payload.decode() == "close":
t = 1
while t <= 3:
try:
shade_command(mac, "close")
client.publish(msg.topic + "/status", "off", qos=0, retain=False)
print ("["+ mac + "] Status Published: " + msg.topic + "/status")
print ("["+ mac + "] Finished")
break
except:
time.sleep(0.5)
if t <= 3:
print ("["+ mac + "] Error! - Trying to Connect Again! (" + str(t) + "/3)")
else:
print ("["+ mac + "] Error! - Can't Connect")
t += 1
if msg.payload.decode() == "stop":
t = 1
while t <= 3:
try:
shade_command(mac, "stop")
client.publish(msg.topic + "/status", "on", qos=0, retain=False)
print ("["+ mac + "] Status Published: " + msg.topic + "/status")
print ("["+ mac + "] Finished")
break
except:
time.sleep(0.5)
if t <= 3:
print ("["+ mac + "] Error! - Trying to Connect Again! (" + str(t) + "/3)")
else:
print ("["+ mac + "] Error! - Can't Connect")
t += 1
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(mqtt_user, password=mqtt_password)
client.connect(mqtt_client, mqtt_port, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()