forked from eschava/broadlink-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.py
executable file
·145 lines (114 loc) · 4.23 KB
/
mqtt.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
#!/usr/bin/env python
import paho.mqtt.client as paho # pip install paho-mqtt
import broadlink # pip install broadlink
import os
import sys
import time
import logging
import logging.config
import socket
# read initial config files
logging.config.fileConfig('logging.conf')
CONFIG = os.getenv('BROADLINKMQTTCONFIG', 'mqtt.conf')
class Config(object):
def __init__(self, filename=CONFIG):
self.config = {}
execfile(filename, self.config)
def get(self, key, default=None):
return self.config.get(key, default)
try:
cf = Config()
except Exception, e:
print "Cannot load configuration from file %s: %s" % (CONFIG, str(e))
sys.exit(2)
qos = cf.get('mqtt_qos', 0)
retain = cf.get('mqtt_retain', False)
topic_prefix = cf.get('mqtt_topic_prefix', 'broadlink/')
# noinspection PyUnusedLocal
def on_message(mosq, device, msg):
logging.debug("Received MQTT message " + msg.topic + " " + str(msg.payload))
command = msg.topic[len(topic_prefix):]
file = "commands/" + command
action = str(msg.payload)
try:
if action == '' or action == 'auto':
record_or_replay(device, file)
elif action == 'record':
record(device, file)
elif action == 'replay':
replay(device, file)
else:
logging.debug("Unrecognized MQTT message " + action)
except Exception:
logging.exception("I/O error")
# noinspection PyUnusedLocal
def on_connect(mosq, device, result_code):
topic = topic_prefix + '#'
logging.debug("Connected to MQTT broker, subscribing to topic " + topic)
mqttc.subscribe(topic, qos)
# noinspection PyUnusedLocal
def on_disconnect(mosq, device, rc):
logging.debug("OOOOPS! Broadlink disconnects")
time.sleep(10)
def record_or_replay(device, file):
if os.path.isfile(file):
replay(device, file)
else:
record(device, file)
def record(device, file):
logging.debug("Recording command to file " + file)
# receive packet
device.enter_learning()
ir_packet = None
attempt = 0
while ir_packet is None and attempt < 6:
time.sleep(5)
ir_packet = device.check_data()
attempt = attempt + 1
if ir_packet is not None:
# write to file
directory = os.path.dirname(file)
if not os.path.exists(directory):
os.makedirs(directory)
with open(file, 'wb') as f:
f.write(str(ir_packet).encode('hex'))
logging.debug("Done")
else:
logging.warn("No command received")
def replay(device, file):
logging.debug("Replaying command from file " + file)
with open(file, 'rb') as f:
ir_packet = f.read()
device.send_data(ir_packet.decode('hex'))
if __name__ == '__main__':
local_address = cf.get('local_address', None)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink device found')
sys.exit(2)
if len(devices) > 1:
logging.error('More than one Broadlink device found (' + ', '.join([d.host for d in devices]) + ')')
sys.exit(2)
device = devices[0]
device.auth()
logging.debug('Connected to %s Broadlink device at %s' % (device.type, device.host))
clientid = cf.get('mqtt_clientid', 'broadlink-%s' % os.getpid())
# initialise MQTT broker connection
mqttc = paho.Client(clientid, clean_session=cf.get('mqtt_clean_session', False), userdata=device)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.will_set('clients/broadlink', payload="Adios!", qos=0, retain=False)
# Delays will be: 3, 6, 12, 24, 30, 30, ...
# mqttc.reconnect_delay_set(delay=3, delay_max=30, exponential_backoff=True)
mqttc.username_pw_set(cf.get('mqtt_username'), cf.get('mqtt_password'))
mqttc.connect(cf.get('mqtt_broker', 'localhost'), int(cf.get('mqtt_port', '1883')), 60)
while True:
try:
mqttc.loop_forever()
except socket.error:
time.sleep(5)
except KeyboardInterrupt:
sys.exit(0)