-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelldus_ctrl.py
82 lines (64 loc) · 2.33 KB
/
telldus_ctrl.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
#!/usr/local/bin/python3
import requests
import json
import paho.mqtt.client as paho
import config
def on_message(client, userdata, message):
# print("message received " ,str(message.payload.decode("utf-8")))
# print("message topic=",message.topic)
# print("message qos=",message.qos)
# print("message retain flag=",message.retain)
received = str(message.payload.decode("utf-8"))
print("Received: " + str(received))
try:
dict_received = json.loads(received)
command = 'device/' + str(dict_received['Action'])
payload = 'id=' + str(dict_received['id'])
print ("API reply: " + str(telldus_request(command, config.HEADERS, payload)))
except Exception:
print("Unknown request: " + str(received))
def telldus_request(command, headers, payload):
command_request = config.API + command
if DEBUG:
print(command_request)
print(payload)
try:
json_data = requests.get(command_request, headers=headers, params=payload, timeout=config.REQUEST_TIMEOUT)
dict_data = json_data.json()
# dict_data['success'] = True
# dict_data['message'] = 'OK'
except requests.exceptions.ConnectionError:
dict_data = {
'success': False,
'message': 'Connection Error'
}
except requests.exceptions.Timeout:
dict_data = {
'success': False,
'message': 'Request timed out'
}
except requests.exceptions:
dict_data = {
'error': True,
'message': 'Unknown error'
}
return dict_data
# command = 'device/turnOn'
# payload = 'id=19'
# print (telldus_request(command, headers, payload))
"""
{
"id": 19,
"Action": "turnOn"
}
"""
def main():
client1 = paho.Client("Telldus_MQTT_control") # create client object
# ret= client1.publish("house/bulb1","on") # publish
client1.on_message = on_message #assign function to callback
client1.connect(config.BROKER, config.PORT) #establish connection
client1.subscribe(config.TOPIC)
print(str("Telldus MQTT Control listening to {}:{}:{} API: {}").format(config.BROKER, config.PORT, config.TOPIC, config.API))
client1.loop_forever()
if __name__ == "__main__":
main()