-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
executable file
·288 lines (225 loc) · 10.4 KB
/
main.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import logging.config
import random
import threading
import time
import yaml
from paho.mqtt import client as mqtt_client
import src.telldus as telldus
from src.telldus import const, td
THREADING_RLOCK = threading.RLock()
TYPES = {const.TELLSTICK_TEMPERATURE: 'temperature',
const.TELLSTICK_HUMIDITY: 'humidity',
const.TELLSTICK_RAINRATE: 'rainrate',
const.TELLSTICK_RAINTOTAL: 'raintotal',
const.TELLSTICK_WINDDIRECTION: 'winddirection',
const.TELLSTICK_WINDAVERAGE: 'windaverage',
const.TELLSTICK_WINDGUST: 'windgust'}
METHODS = {const.TELLSTICK_TURNON: 'turn on',
const.TELLSTICK_TURNOFF: 'turn off',
const.TELLSTICK_BELL: 'bell',
const.TELLSTICK_TOGGLE: 'toggle',
const.TELLSTICK_DIM: 'dim',
const.TELLSTICK_LEARN: 'learn',
const.TELLSTICK_EXECUTE: 'execute',
const.TELLSTICK_UP: 'up',
const.TELLSTICK_DOWN: 'down',
const.TELLSTICK_STOP: 'stop'}
def connect_mqtt(config, client_id) -> mqtt_client:
def on_connect(client, userdata, flags, return_code):
# pylint: disable=unused-argument
if return_code == 0:
client.connected_flag = True
logging.info('Connected to MQTT Broker as %s.', client_id)
else:
logging.critical('Failed to connect, return code %d', return_code)
client = mqtt_client.Client(client_id)
client.username_pw_set(config['mqtt']['user'], config['mqtt']['pass'])
client.on_connect = on_connect
client.connect(config['mqtt']['broker'], int(config['mqtt']['port']))
return client
def publish_mqtt(client, topic, msg):
with THREADING_RLOCK:
result = client.publish(topic, msg, retain=True)
if result[0] == 0:
logging.info('Send "%s" to topic "%s"', msg, topic)
else:
logging.error('Failed to send message to topic "%s"', topic)
def subscribe_device(client: mqtt_client):
logging.debug('Subscribing to MQTT device events')
def on_message(client, userdata, msg):
# pylint: disable=unused-argument
logging.info('Received "%s" from "%s" topic',
msg.payload.decode(), msg.topic)
device_id = msg.topic.split('/')[1]
module = msg.topic.split('/')[2]
action = msg.topic.split('/')[3]
cmd_status = False
if module == 'light':
if action == 'dim':
logging.debug('[DEVICE] Sending command DIM "%s" to device '
'id %s', msg.payload.decode(), device_id)
cmd_status = d.dim(device_id, int(msg.payload.decode()))
else:
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNON):
logging.debug('[DEVICE] Sending command DIM 255 to device '
'id %s', device_id)
cmd_status = d.dim(device_id, 255)
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNOFF):
logging.debug('[DEVICE] Sending command DIM 0 to device '
'id %s', device_id)
cmd_status = d.dim(device_id, 0)
if action != 'dim' and module != 'light':
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNON):
topic = d.create_topic(device_id, 'switch')
topic_data = d.create_topic_data('switch', const.TELLSTICK_TURNON)
publish_mqtt(mqtt_device, topic, topic_data)
logging.debug('[DEVICE] Sending command ON to device '
'id %s', device_id)
cmd_status = d.turn_on(device_id)
if int(msg.payload.decode()) == int(const.TELLSTICK_TURNOFF):
topic = d.create_topic(device_id, 'switch')
topic_data = d.create_topic_data('switch', const.TELLSTICK_TURNOFF)
publish_mqtt(mqtt_device, topic, topic_data)
logging.debug('[DEVICE] Sending command OFF to device '
'id %s', device_id)
cmd_status = d.turn_off(device_id)
# if int(msg.payload.decode()) == int(const.TELLSTICK_BELL):
# logging.debug('[DEVICE] Sending command BELL to device '
# 'id %s', device_id)
# cmd_status = d.bell(device_id)
# if int(msg.payload.decode()) == int(const.TELLSTICK_EXECUTE):
# logging.debug('[DEVICE] Sending command EXECUTE to device '
# 'id %s', device_id)
# cmd_status = d.execute(device_id)
# if int(msg.payload.decode()) == int(const.TELLSTICK_UP):
# logging.debug('[DEVICE] Sending command UP to device id %s',
# device_id)
# cmd_status = d.up(device_id)
# if int(msg.payload.decode()) == int(const.TELLSTICK_DOWN):
# logging.debug('[DEVICE] Sending command DOWN to device id %s',
# device_id)
# cmd_status = d.down(device_id)
# if int(msg.payload.decode()) == int(const.TELLSTICK_STOP):
# logging.debug('[DEVICE] Sending command STOP to device id %s',
# device_id)
# cmd_status = d.stop(device_id)
if not cmd_status:
logging.debug('[DEVICE] Command "%s" not supported, please open'
' a github issue with this message.', msg)
client.subscribe('{}/+/+/set'.format(
config['home_assistant']['state_topic']))
client.on_message = on_message
def raw_event(data, controller_id, cid):
# pylint: disable=unused-argument
if 'command' not in data:
return
# Sensors can be added or discovered in telldus-core without
# a restart, ensure config topic for HASS
command_topics = raw.create_topics(raw.get(data))
initial_publish(mqtt_command, command_topics)
topic = raw.create_topic(raw.serialized['id'], 'binary_sensor')
topic_data = raw.create_topic_data('binary_sensor',
raw.serialized['method'])
publish_mqtt(mqtt_command, topic, topic_data)
def device_event(id_, method, data, cid):
# pylint: disable=unused-argument
method_string = METHODS.get(method, 'UNKNOWN METHOD {0}'.format(method))
string = '[DEVICE] {0} -> {1} ({2})'.format(id_, method_string, method)
if method == const.TELLSTICK_DIM:
string += ' [{0}]'.format(data)
if method == const.TELLSTICK_DIM:
logging.debug('[DEVICE EVENT LIGHT] %s', string)
topic = d.create_topic(id_, 'light')
topic_data = d.create_topic_data('light', data)
else:
logging.debug('[DEVICE EVENT SWITCH] %s', string)
topic = d.create_topic(id_, 'switch')
topic_data = d.create_topic_data('switch', method)
publish_mqtt(mqtt_device, topic, topic_data)
def sensor_event(protocol, model, id_, data_type, value, timestamp, cid):
# pylint: disable=unused-argument
type_string = TYPES.get(data_type, 'UNKNOWN METHOD {0}'.format(data_type))
string = '[SENSOR] {0} {1} ({2}) = {3}'.format(
id_, model, type_string, value)
logging.debug(string)
# Sensors can be added or discovered in telldus-core without
# a restart, ensure config topic for HASS
sensor_topics = s.create_topics(s.get(id_))
initial_publish(mqtt_sensor, sensor_topics)
topic = s.create_topic(id_, type_string)
data = s.create_topic_data(type_string, value)
publish_mqtt(mqtt_sensor, topic, data)
def initial_publish(client_mqtt, topics):
for topic in topics:
if 'config' in topic:
publish_mqtt(client_mqtt, topic['config']['topic'],
topic['config']['data'])
if 'state' in topic:
publish_mqtt(client_mqtt, topic['state']['topic'],
topic['state']['data'])
with open('./logging.yaml', 'r', encoding='utf-8') as stream:
logging_config = yaml.load(stream, Loader=yaml.SafeLoader)
logging.config.dictConfig(logging_config)
logger = logging.getLogger('telldus-core-mqtt-main')
# Wait 5s for telldus-core to start and start collecting data
logging.info('Waiting for telldus-core to start...')
time.sleep(5)
logging.info('telldus-core have started.')
# Setup connection MQTT server
c = telldus.Telldus()
config = c.get_config
# Setting up MQTT connections
mqtt_sensor_id = 'telldus-core-mqtt-sensor-{}'.format(
random.randint(0, 1000)) # nosec
mqtt_sensor = connect_mqtt(config, mqtt_sensor_id)
mqtt_device_id = 'telldus-core-mqtt-device-{}'.format(
random.randint(0, 1000)) # nosec
mqtt_device = connect_mqtt(config, mqtt_device_id)
mqtt_command_id = 'telldus-core-mqtt-command-{}'.format(
random.randint(0, 1000)) # nosec
mqtt_command = connect_mqtt(config, mqtt_command_id)
mqtt_subscription_id = 'telldus-core-mqtt-subscription-{}'.format(
random.randint(0, 1000)) # nosec
mqtt_subscription = connect_mqtt(config, mqtt_subscription_id)
# # On program start, collect sensors to publish to MQTT server
s = telldus.Sensor(c.td_core)
sensors_topics = s.create_topics(s.get())
initial_publish(mqtt_sensor, sensors_topics)
# # On program start, collect devices to publish to MQTT server
d = telldus.Device(c.td_core)
devices_topics = d.create_topics(d.get())
initial_publish(mqtt_device, devices_topics)
# Collect raw commands
raw = telldus.Command(c.td_core)
# Initialize event listener for telldus-core
telldus_core = asyncio.new_event_loop()
asyncio.set_event_loop(telldus_core)
dispatcher = td.AsyncioCallbackDispatcher(telldus_core)
core = td.TelldusCore(callback_dispatcher=dispatcher)
callbacks = []
# # Events to listen for from telldus-core
callbacks.append(core.register_raw_device_event(raw_event))
callbacks.append(core.register_device_event(device_event))
callbacks.append(core.register_sensor_event(sensor_event))
# Main loop
try:
subscribe_device(mqtt_subscription)
mqtt_sensor.loop_start()
mqtt_device.loop_start()
mqtt_command.loop_start()
mqtt_subscription.loop_start()
telldus_core.run_forever()
except KeyboardInterrupt:
mqtt_device.unsubscribe('{}/+/+/set'.format(
config['home_assistant']['state_topic']))
mqtt_sensor.disconnect()
mqtt_device.disconnect()
mqtt_command.disconnect()
mqtt_subscription.disconnect()
mqtt_sensor.loop_stop()
mqtt_device.loop_stop()
mqtt_command.loop_stop()
mqtt_subscription.loop_stop()