|
@@ -1,4 +1,5 @@
|
1
|
1
|
import colored
|
|
2
|
+import copy
|
2
|
3
|
import json
|
3
|
4
|
import task
|
4
|
5
|
import time
|
|
@@ -107,6 +108,15 @@ class base(object):
|
107
|
108
|
if self.AUTOSEND and len(keys_changed) > 0:
|
108
|
109
|
self.__tx__(keys_changed)
|
109
|
110
|
|
|
111
|
+ def get_data(self, key, default=None):
|
|
112
|
+ rv = self.data.get(key, default)
|
|
113
|
+ try:
|
|
114
|
+ rv = True if rv.lower() == 'on' else rv
|
|
115
|
+ rv = False if rv.lower() == 'off' else rv
|
|
116
|
+ except AttributeError:
|
|
117
|
+ pass
|
|
118
|
+ return rv
|
|
119
|
+
|
110
|
120
|
def __tx__(self, keys_changed):
|
111
|
121
|
self.mqtt_client.send(self.topic, json.dumps(self.data))
|
112
|
122
|
|
|
@@ -138,7 +148,7 @@ class shelly(base):
|
138
|
148
|
def __init__(self, mqtt_client, topic, input_0_func=None, input_1_func=None, output_0_auto_off=None):
|
139
|
149
|
super().__init__(mqtt_client, topic)
|
140
|
150
|
#
|
141
|
|
- self.store_data(**{self.KEY_OUTPUT_0: "off", self.KEY_OUTPUT_1: "off", self.KEY_INPUT_0: "off", self.KEY_INPUT_1: "off"})
|
|
151
|
+ self.store_data(**{self.KEY_OUTPUT_0: False, self.KEY_OUTPUT_1: False, self.KEY_INPUT_0: False, self.KEY_INPUT_1: False})
|
142
|
152
|
self.__input_0_func = input_0_func
|
143
|
153
|
self.__input_1_func = input_1_func
|
144
|
154
|
self.__output_0_auto_off__ = output_0_auto_off
|
|
@@ -146,8 +156,8 @@ class shelly(base):
|
146
|
156
|
self.__delayed_off__ = task.delayed(float(self.__output_0_auto_off__), self.__auto_off__, self.KEY_OUTPUT_0)
|
147
|
157
|
#
|
148
|
158
|
self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
149
|
|
- self.add_callback(self.KEY_OUTPUT_0, self.__start_auto_off__, "on")
|
150
|
|
- self.add_callback(self.KEY_OUTPUT_0, self.__stop_auto_off__, "off")
|
|
159
|
+ self.add_callback(self.KEY_OUTPUT_0, self.__start_auto_off__, True)
|
|
160
|
+ self.add_callback(self.KEY_OUTPUT_0, self.__stop_auto_off__, True)
|
151
|
161
|
self.add_callback(self.KEY_OUTPUT_1, self.print_formatted, None)
|
152
|
162
|
#
|
153
|
163
|
self.add_callback(self.KEY_INPUT_0, self.__input_function__, None)
|
|
@@ -164,7 +174,7 @@ class shelly(base):
|
164
|
174
|
|
165
|
175
|
def __tx__(self, keys_changed):
|
166
|
176
|
for key in keys_changed:
|
167
|
|
- self.mqtt_client.send(self.topic + '/' + key, self.data.get(key))
|
|
177
|
+ self.mqtt_client.send(self.topic + '/' + key, "on" if self.data.get(key) else "off")
|
168
|
178
|
|
169
|
179
|
def __input_function__(self, device, key, data):
|
170
|
180
|
if key == self.KEY_INPUT_0:
|
|
@@ -197,14 +207,11 @@ class shelly(base):
|
197
|
207
|
self.__set_data__(key, 'off')
|
198
|
208
|
|
199
|
209
|
def __set_data__(self, key, value):
|
200
|
|
- if value in ["on", "off"]:
|
201
|
|
- self.store_data(**{key: value})
|
202
|
|
- else:
|
203
|
|
- print("Wrong value (%s)!" % repr(value))
|
|
210
|
+ self.store_data(**{key: value == "on"})
|
204
|
211
|
|
205
|
212
|
def __toggle_data__(self, key):
|
206
|
213
|
if key in self.data:
|
207
|
|
- self.__set_data__(key, 'on' if self.data.get(key) == 'off' else 'off')
|
|
214
|
+ self.__set_data__(key, "on" if not self.data.get(key) else "off")
|
208
|
215
|
|
209
|
216
|
def command(self, command):
|
210
|
217
|
if command in self.COMMANDS:
|
|
@@ -227,15 +234,15 @@ class shelly(base):
|
227
|
234
|
elif command == self.COMMANDS[8]:
|
228
|
235
|
self.__toggle_data__(self.KEY_INPUT_0)
|
229
|
236
|
time.sleep(0.4)
|
230
|
|
- self.__set_data__(self.KEY_LONGPUSH_0, 'on')
|
|
237
|
+ self.__set_data__(self.KEY_LONGPUSH_0, True)
|
231
|
238
|
time.sleep(0.1)
|
232
|
|
- self.__set_data__(self.KEY_LONGPUSH_0, 'off')
|
|
239
|
+ self.__set_data__(self.KEY_LONGPUSH_0, False)
|
233
|
240
|
elif command == self.COMMANDS[9]:
|
234
|
241
|
self.__toggle_data__(self.KEY_INPUT_1)
|
235
|
242
|
time.sleep(0.4)
|
236
|
|
- self.__set_data__(self.KEY_LONGPUSH_1, 'on')
|
|
243
|
+ self.__set_data__(self.KEY_LONGPUSH_1, True)
|
237
|
244
|
time.sleep(0.1)
|
238
|
|
- self.__set_data__(self.KEY_LONGPUSH_1, 'off')
|
|
245
|
+ self.__set_data__(self.KEY_LONGPUSH_1, False)
|
239
|
246
|
else:
|
240
|
247
|
print("%s: not yet implemented!" % command)
|
241
|
248
|
else:
|
|
@@ -243,45 +250,33 @@ class shelly(base):
|
243
|
250
|
|
244
|
251
|
def print_formatted(self, device, key, value):
|
245
|
252
|
if value is not None:
|
246
|
|
- info = (" - %ds" % self.__output_0_auto_off__) if self.__output_0_auto_off__ is not None and value == "on" else ""
|
|
253
|
+ info = (" - %ds" % self.__output_0_auto_off__) if self.__output_0_auto_off__ is not None and value else ""
|
247
|
254
|
channel = "(%s%s)" % (self.names.get(key, key), info)
|
248
|
|
- print_light(COLOR_SHELLY, value == "on", self.topic, channel)
|
|
255
|
+ print_light(COLOR_SHELLY, value, self.topic, channel)
|
249
|
256
|
|
250
|
257
|
|
251
|
258
|
class my_powerplug(base):
|
252
|
|
- KEY_OUTPUT_0 = "0"
|
253
|
|
- KEY_OUTPUT_1 = "1"
|
254
|
|
- KEY_OUTPUT_2 = "2"
|
255
|
|
- KEY_OUTPUT_3 = "3"
|
|
259
|
+ KEY_OUTPUT_0 = "state"
|
256
|
260
|
COMMANDS = [
|
257
|
|
- "get_output_0", "toggle_output_0",
|
258
|
|
- "get_output_1", "toggle_output_1",
|
259
|
|
- "get_output_2", "toggle_output_2",
|
260
|
|
- "get_output_3", "toggle_output_3",
|
|
261
|
+ "get_output", "toggle_output",
|
261
|
262
|
]
|
262
|
263
|
|
263
|
|
- def __init__(self, mqtt_client, topic):
|
264
|
|
- super().__init__(mqtt_client, topic)
|
|
264
|
+ def __init__(self, mqtt_client, topic, channel):
|
|
265
|
+ super().__init__(mqtt_client, topic + '/' + "output/%d" % (channel + 1))
|
265
|
266
|
#
|
266
|
|
- for i in range(0, 4):
|
267
|
|
- self.data[str(i)] = False
|
268
|
|
- self.add_callback(str(i), self.print_formatted, None)
|
|
267
|
+ self.data[self.KEY_OUTPUT_0] = False
|
|
268
|
+ self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
269
|
269
|
|
270
|
270
|
def __rx__(self, client, userdata, message):
|
271
|
|
- if message.topic.startswith(self.topic + '/output/') and message.topic.endswith('/set'):
|
272
|
|
- try:
|
273
|
|
- channels = [int(message.topic.split('/')[-2]) - 1]
|
274
|
|
- except ValueError:
|
275
|
|
- channels = range(0, 4)
|
276
|
|
- for channel in channels:
|
277
|
|
- payload = payload_filter(message.payload)
|
278
|
|
- if payload == "toggle":
|
279
|
|
- payload = not self.data.get(str(channel))
|
280
|
|
- self.store_data(**{str(channel): payload})
|
|
271
|
+ if message.topic == self.topic + '/set':
|
|
272
|
+ payload = payload_filter(message.payload)
|
|
273
|
+ if payload == "toggle":
|
|
274
|
+ payload = not self.data.get(self.KEY_OUTPUT_0)
|
|
275
|
+ self.store_data(**{self.KEY_OUTPUT_0: payload})
|
281
|
276
|
|
282
|
277
|
def __tx__(self, keys_changed):
|
283
|
278
|
for key in keys_changed:
|
284
|
|
- self.mqtt_client.send(self.topic + "/output/" + str(int(key) + 1), json.dumps(self.data.get(key)))
|
|
279
|
+ self.mqtt_client.send(self.topic, json.dumps(self.data.get(key)))
|
285
|
280
|
|
286
|
281
|
def command(self, command):
|
287
|
282
|
if command in self.COMMANDS:
|
|
@@ -289,18 +284,6 @@ class my_powerplug(base):
|
289
|
284
|
self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
290
|
285
|
elif command == self.COMMANDS[1]:
|
291
|
286
|
self.store_data(**{self.KEY_OUTPUT_0: not self.data.get(self.KEY_OUTPUT_0)})
|
292
|
|
- elif command == self.COMMANDS[2]:
|
293
|
|
- self.print_formatted(self, self.KEY_OUTPUT_1, self.data.get(self.KEY_OUTPUT_1))
|
294
|
|
- elif command == self.COMMANDS[3]:
|
295
|
|
- self.store_data(**{self.KEY_OUTPUT_1: not self.data.get(self.KEY_OUTPUT_1)})
|
296
|
|
- elif command == self.COMMANDS[4]:
|
297
|
|
- self.print_formatted(self, self.KEY_OUTPUT_2, self.data.get(self.KEY_OUTPUT_2))
|
298
|
|
- elif command == self.COMMANDS[5]:
|
299
|
|
- self.store_data(**{self.KEY_OUTPUT_2: not self.data.get(self.KEY_OUTPUT_2)})
|
300
|
|
- elif command == self.COMMANDS[6]:
|
301
|
|
- self.print_formatted(self, self.KEY_OUTPUT_3, self.data.get(self.KEY_OUTPUT_3))
|
302
|
|
- elif command == self.COMMANDS[7]:
|
303
|
|
- self.store_data(**{self.KEY_OUTPUT_3: not self.data.get(self.KEY_OUTPUT_3)})
|
304
|
287
|
else:
|
305
|
288
|
print("%s: not yet implemented!" % command)
|
306
|
289
|
else:
|
|
@@ -308,7 +291,7 @@ class my_powerplug(base):
|
308
|
291
|
|
309
|
292
|
def print_formatted(self, device, key, value):
|
310
|
293
|
if value is not None:
|
311
|
|
- print_light(COLOR_POWERPLUG, value, self.topic, "(%s)" % self.names.get(key, "Channel %d" % (int(key) + 1)))
|
|
294
|
+ print_light(COLOR_POWERPLUG, value, self.topic, "(%s)" % self.names.get(key, "State"))
|
312
|
295
|
|
313
|
296
|
|
314
|
297
|
class silvercrest_powerplug(base):
|
|
@@ -322,7 +305,7 @@ class silvercrest_powerplug(base):
|
322
|
305
|
super().__init__(mqtt_client, topic)
|
323
|
306
|
self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
324
|
307
|
#
|
325
|
|
- self.store_data(**{self.KEY_OUTPUT_0: "off"})
|
|
308
|
+ self.store_data(**{self.KEY_OUTPUT_0: False})
|
326
|
309
|
|
327
|
310
|
def __rx__(self, client, userdata, message):
|
328
|
311
|
if message.topic == self.topic + '/set':
|
|
@@ -331,20 +314,24 @@ class silvercrest_powerplug(base):
|
331
|
314
|
state = json.loads(message.payload).get('state').lower()
|
332
|
315
|
if state in STATES:
|
333
|
316
|
if state == STATES[0]:
|
334
|
|
- self.store_data(**{self.KEY_OUTPUT_0: 'on'})
|
|
317
|
+ self.store_data(**{self.KEY_OUTPUT_0: True})
|
335
|
318
|
elif state == STATES[1]:
|
336
|
|
- self.store_data(**{self.KEY_OUTPUT_0: 'off'})
|
|
319
|
+ self.store_data(**{self.KEY_OUTPUT_0: False})
|
337
|
320
|
else:
|
338
|
|
- self.store_data(**{self.KEY_OUTPUT_0: "off" if self.data.get(self.KEY_OUTPUT_0) == "on" else "on"})
|
|
321
|
+ self.store_data(**{not self.data.get(self.KEY_OUTPUT_0)})
|
|
322
|
+
|
|
323
|
+ def __tx__(self, keys_changed):
|
|
324
|
+ for key in keys_changed:
|
|
325
|
+ self.mqtt_client.send(self.topic + '/' + key, "on" if self.data.get(key) else "off")
|
339
|
326
|
|
340
|
327
|
def command(self, command):
|
341
|
328
|
if command in self.COMMANDS:
|
342
|
329
|
if command == self.COMMANDS[0]:
|
343
|
330
|
self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
344
|
331
|
elif command == self.COMMANDS[1]:
|
345
|
|
- self.store_data(**{self.KEY_OUTPUT_0: 'on'})
|
|
332
|
+ self.store_data(**{self.KEY_OUTPUT_0: True})
|
346
|
333
|
elif command == self.COMMANDS[2]:
|
347
|
|
- self.store_data(**{self.KEY_OUTPUT_0: 'off'})
|
|
334
|
+ self.store_data(**{self.KEY_OUTPUT_0: False})
|
348
|
335
|
else:
|
349
|
336
|
print("%s: not yet implemented!" % command)
|
350
|
337
|
else:
|
|
@@ -352,40 +339,11 @@ class silvercrest_powerplug(base):
|
352
|
339
|
|
353
|
340
|
def print_formatted(self, device, key, value):
|
354
|
341
|
if value is not None:
|
355
|
|
- print_light(COLOR_POWERPLUG, value == "on", self.topic, "(%s)" % self.names.get(key, key))
|
356
|
|
-
|
357
|
|
-
|
358
|
|
-class silvercrest_motion_sensor(base):
|
359
|
|
- KEY_OCCUPANCY = "occupancy"
|
360
|
|
- COMMANDS = ['motion']
|
361
|
|
-
|
362
|
|
- def __init__(self, mqtt_client, topic):
|
363
|
|
- super().__init__(mqtt_client, topic)
|
364
|
|
- self.data[self.KEY_OCCUPANCY] = False
|
365
|
|
- self.add_callback(self.KEY_OCCUPANCY, self.print_formatted, None)
|
366
|
|
-
|
367
|
|
- def __rx__(self, client, userdata, message):
|
368
|
|
- pass
|
369
|
|
-
|
370
|
|
- def command(self, command):
|
371
|
|
- try:
|
372
|
|
- command, value = command.split(' ')
|
373
|
|
- except ValueError:
|
374
|
|
- value = None
|
375
|
|
- else:
|
376
|
|
- value = json.loads(value)
|
377
|
|
- if command == self.COMMANDS[0]:
|
378
|
|
- self.store_data(**{self.KEY_OCCUPANCY: True})
|
379
|
|
- time.sleep(value or 10)
|
380
|
|
- self.store_data(**{self.KEY_OCCUPANCY: False})
|
381
|
|
-
|
382
|
|
- def print_formatted(self, device, key, value):
|
383
|
|
- if value is not None:
|
384
|
|
- print_light(COLOR_MOTION_SENSOR, value, self.topic, "")
|
|
342
|
+ print_light(COLOR_POWERPLUG, value, self.topic, "(%s)" % self.names.get(key, key))
|
385
|
343
|
|
386
|
344
|
|
387
|
345
|
class tradfri_light(base):
|
388
|
|
- KEY_STATE = "state"
|
|
346
|
+ KEY_OUTPUT_0 = "state"
|
389
|
347
|
KEY_BRIGHTNESS = "brightness"
|
390
|
348
|
KEY_COLOR_TEMP = "color_temp"
|
391
|
349
|
KEY_BRIGHTNESS_MOVE = "brightness_move"
|
|
@@ -397,7 +355,7 @@ class tradfri_light(base):
|
397
|
355
|
def __init__(self, mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False, send_on_power_on=True):
|
398
|
356
|
super().__init__(mqtt_client, topic)
|
399
|
357
|
self.send_on_power_on = send_on_power_on
|
400
|
|
- self.add_callback(self.KEY_STATE, self.print_formatted, None)
|
|
358
|
+ self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
401
|
359
|
self.add_callback(self.KEY_BRIGHTNESS, self.print_formatted, None)
|
402
|
360
|
self.add_callback(self.KEY_COLOR_TEMP, self.print_formatted, None)
|
403
|
361
|
#
|
|
@@ -413,34 +371,41 @@ class tradfri_light(base):
|
413
|
371
|
def __init_data__(self, enable_state, enable_brightness, enable_color_temp):
|
414
|
372
|
data = {}
|
415
|
373
|
if enable_state:
|
416
|
|
- data[self.KEY_STATE] = 'off'
|
|
374
|
+ data[self.KEY_OUTPUT_0] = False
|
417
|
375
|
self.commands.extend(self.STATE_COMMANDS)
|
418
|
376
|
if enable_brightness:
|
419
|
|
- data[self.KEY_BRIGHTNESS] = 128
|
|
377
|
+ data[self.KEY_BRIGHTNESS] = 50
|
420
|
378
|
self.brightnes_move = (0, time.time())
|
421
|
379
|
self.commands.extend(self.BRIGHTNESS_COMMANDS)
|
422
|
380
|
if enable_color_temp:
|
423
|
|
- data[self.KEY_COLOR_TEMP] = 352
|
|
381
|
+ data[self.KEY_COLOR_TEMP] = 5
|
424
|
382
|
self.commands.extend(self.COLOR_TEMP_COMMANDS)
|
425
|
383
|
self.store_data(**data)
|
426
|
384
|
|
427
|
385
|
def __rx__(self, client, userdata, message):
|
428
|
386
|
data = json.loads(message.payload)
|
429
|
|
- if self.data.get(self.KEY_STATE) == 'on' or data.get(self.KEY_STATE) in ['on', 'toggle']:
|
|
387
|
+ if self.data.get(self.KEY_OUTPUT_0) or data.get(self.KEY_OUTPUT_0) in ['on', 'toggle']:
|
430
|
388
|
if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
431
|
389
|
for targetkey in data:
|
432
|
390
|
value = data[targetkey]
|
433
|
391
|
if targetkey in self.data.keys():
|
434
|
|
- if targetkey == self.KEY_STATE and value == "toggle":
|
435
|
|
- value = "on" if self.data.get(self.KEY_STATE) == "off" else "off"
|
|
392
|
+ if targetkey == self.KEY_OUTPUT_0:
|
|
393
|
+ if value == "toggle":
|
|
394
|
+ value = not self.data.get(self.KEY_OUTPUT_0)
|
|
395
|
+ else:
|
|
396
|
+ value = value == "on"
|
|
397
|
+ elif targetkey == self.KEY_BRIGHTNESS:
|
|
398
|
+ value = round((value - 1) / 2.53, 0)
|
|
399
|
+ elif targetkey == self.KEY_COLOR_TEMP:
|
|
400
|
+ value = round((value - 250) / 20.4, 0)
|
436
|
401
|
self.store_data(**{targetkey: value})
|
437
|
402
|
else:
|
438
|
403
|
if targetkey == self.KEY_BRIGHTNESS_MOVE:
|
439
|
404
|
new_value = self.data.get(self.KEY_BRIGHTNESS) + (time.time() - self.brightnes_move[1]) * self.brightnes_move[0]
|
440
|
405
|
if new_value < 0:
|
441
|
406
|
new_value = 0
|
442
|
|
- if new_value > 255:
|
443
|
|
- new_value = 255
|
|
407
|
+ if new_value > 256:
|
|
408
|
+ new_value = 256
|
444
|
409
|
self.store_data(**{self.KEY_BRIGHTNESS: int(new_value)})
|
445
|
410
|
self.brightnes_move = (value, time.time())
|
446
|
411
|
else:
|
|
@@ -448,6 +413,16 @@ class tradfri_light(base):
|
448
|
413
|
elif message.topic == self.topic + '/get':
|
449
|
414
|
self.__tx__(None)
|
450
|
415
|
|
|
416
|
+ def __tx__(self, keys_changed):
|
|
417
|
+ tx_data = copy.copy(self.data)
|
|
418
|
+ if self.KEY_OUTPUT_0 in tx_data:
|
|
419
|
+ tx_data[self.KEY_OUTPUT_0] = "on" if tx_data[self.KEY_OUTPUT_0] else "off"
|
|
420
|
+ if self.KEY_BRIGHTNESS in tx_data:
|
|
421
|
+ tx_data[self.KEY_BRIGHTNESS] = 1 + round(2.53 * tx_data[self.KEY_BRIGHTNESS], 0)
|
|
422
|
+ if self.KEY_COLOR_TEMP in tx_data:
|
|
423
|
+ tx_data[self.KEY_COLOR_TEMP] = 250 + round(20.4 * tx_data[self.KEY_COLOR_TEMP], 0)
|
|
424
|
+ self.mqtt_client.send(self.topic, json.dumps(tx_data))
|
|
425
|
+
|
451
|
426
|
def command(self, command):
|
452
|
427
|
try:
|
453
|
428
|
command, value = command.split(' ')
|
|
@@ -455,9 +430,9 @@ class tradfri_light(base):
|
455
|
430
|
value = None
|
456
|
431
|
if command in self.capabilities():
|
457
|
432
|
if command == self.STATE_COMMANDS[0]:
|
458
|
|
- self.print_formatted(self, self.KEY_STATE, self.data.get(self.KEY_STATE))
|
|
433
|
+ self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
459
|
434
|
elif command == self.STATE_COMMANDS[1]:
|
460
|
|
- self.store_data(**{self.KEY_STATE: 'off' if self.data.get(self.KEY_STATE) == 'on' else 'on'})
|
|
435
|
+ self.store_data(**{self.KEY_OUTPUT_0: not self.data.get(self.KEY_OUTPUT_0)})
|
461
|
436
|
elif command == self.BRIGHTNESS_COMMANDS[0]:
|
462
|
437
|
self.print_formatted(self, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
463
|
438
|
elif command == self.BRIGHTNESS_COMMANDS[1]:
|
|
@@ -472,28 +447,28 @@ class tradfri_light(base):
|
472
|
447
|
print("Unknown command!")
|
473
|
448
|
|
474
|
449
|
def power_off(self, device, key, value):
|
475
|
|
- self.data[self.KEY_STATE] = 'off'
|
476
|
|
- self.print_formatted(self, self.KEY_STATE, 'off')
|
|
450
|
+ self.data[self.KEY_OUTPUT_0] = False
|
|
451
|
+ self.print_formatted(self, self.KEY_OUTPUT_0, False)
|
477
|
452
|
|
478
|
453
|
def power_on(self, device, key, value):
|
479
|
454
|
if self.send_on_power_on:
|
480
|
|
- self.store_data(**{self.KEY_STATE: 'on'})
|
|
455
|
+ self.store_data(**{self.KEY_OUTPUT_0: True})
|
481
|
456
|
else:
|
482
|
|
- self.data[self.KEY_STATE] = 'on'
|
483
|
|
- self.print_formatted(self, self.KEY_STATE, 'on')
|
|
457
|
+ self.data[self.KEY_OUTPUT_0] = True
|
|
458
|
+ self.print_formatted(self, self.KEY_OUTPUT_0, True)
|
484
|
459
|
|
485
|
460
|
def print_formatted(self, device, key, value):
|
486
|
461
|
if value is not None:
|
487
|
462
|
color = COLOR_LIGHT_ACTIVE
|
488
|
|
- if key == self.KEY_STATE:
|
489
|
|
- print_light(COLOR_LIGHT_ACTIVE, value == "on", self.topic, "")
|
|
463
|
+ if key == self.KEY_OUTPUT_0:
|
|
464
|
+ print_light(COLOR_LIGHT_ACTIVE, value, self.topic, "")
|
490
|
465
|
self.print_formatted(device, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
491
|
466
|
self.print_formatted(device, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
492
|
467
|
elif key in [self.KEY_BRIGHTNESS, self.KEY_COLOR_TEMP]:
|
493
|
|
- perc_value = round(value * 100 / 256, 0) if key == self.KEY_BRIGHTNESS else round((value - 250) * 100 / 204, 0)
|
|
468
|
+ perc_value = round(value, 0) if key == self.KEY_BRIGHTNESS else round(10 * value, 0)
|
494
|
469
|
print_percent(
|
495
|
|
- COLOR_LIGHT_PASSIVE if self.data.get(self.KEY_STATE) != "on" else COLOR_LIGHT_ACTIVE,
|
496
|
|
- 'B' if key == gui_light.KEY_BRIGHTNESS else 'C',
|
|
470
|
+ COLOR_LIGHT_PASSIVE if not self.data.get(self.KEY_OUTPUT_0) else COLOR_LIGHT_ACTIVE,
|
|
471
|
+ 'B' if key == self.KEY_BRIGHTNESS else 'C',
|
497
|
472
|
perc_value,
|
498
|
473
|
"%3d%%" % perc_value,
|
499
|
474
|
self.topic,
|
|
@@ -501,44 +476,109 @@ class tradfri_light(base):
|
501
|
476
|
)
|
502
|
477
|
|
503
|
478
|
|
504
|
|
-class gui_light(tradfri_light):
|
|
479
|
+class brennenstuhl_heating_valve(base):
|
|
480
|
+ TEMP_RANGE = [10, 30]
|
|
481
|
+ #
|
|
482
|
+ KEY_TEMPERATURE_SETPOINT = "current_heating_setpoint"
|
|
483
|
+ KEY_TEMPERATURE = "local_temperature"
|
|
484
|
+ #
|
|
485
|
+ COMMANDS = [
|
|
486
|
+ "get_temperature_setpoint", "set_temperature_setpoint", "set_local_temperature",
|
|
487
|
+ ]
|
|
488
|
+
|
|
489
|
+ def __init__(self, mqtt_client, topic):
|
|
490
|
+ super().__init__(mqtt_client, topic)
|
|
491
|
+ self.store_data(**{
|
|
492
|
+ self.KEY_TEMPERATURE_SETPOINT: 20,
|
|
493
|
+ self.KEY_TEMPERATURE: 20.7,
|
|
494
|
+ })
|
|
495
|
+ self.add_callback(self.KEY_TEMPERATURE_SETPOINT, self.print_formatted, None)
|
|
496
|
+
|
|
497
|
+ def __rx__(self, client, userdata, message):
|
|
498
|
+ if message.topic.startswith(self.topic) and message.topic.endswith("/set"):
|
|
499
|
+ payload = payload_filter(message.payload)
|
|
500
|
+ self.store_data(**payload)
|
|
501
|
+
|
|
502
|
+ def command(self, command):
|
|
503
|
+ try:
|
|
504
|
+ command, value = command.split(' ')
|
|
505
|
+ except ValueError:
|
|
506
|
+ value = None
|
|
507
|
+ if command in self.COMMANDS:
|
|
508
|
+ if command == self.COMMANDS[0]:
|
|
509
|
+ self.print_formatted(self, self.KEY_TEMPERATURE_SETPOINT, self.data.get(self.KEY_TEMPERATURE_SETPOINT))
|
|
510
|
+ elif command == self.COMMANDS[1]:
|
|
511
|
+ self.store_data(**{self.KEY_TEMPERATURE_SETPOINT: command_float_value(value)})
|
|
512
|
+ elif command == self.COMMANDS[2]:
|
|
513
|
+ self.store_data(**{self.KEY_TEMPERATURE: command_float_value(value)})
|
|
514
|
+
|
|
515
|
+ def print_formatted(self, device, key, value):
|
|
516
|
+ devicename = ' - '.join(self.topic.split('/')[1:])
|
|
517
|
+ if key == self.KEY_TEMPERATURE_SETPOINT:
|
|
518
|
+ perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
|
519
|
+ perc = 100 if perc > 100 else perc
|
|
520
|
+ perc = 0 if perc < 0 else perc
|
|
521
|
+ print_percent(COLOR_HEATING_VALVE, '\u03d1', perc, "%4.1f°C" % value, self.topic, "")
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+class videv_light(base):
|
505
|
525
|
AUTOSEND = False
|
506
|
526
|
#
|
507
|
|
- KEY_ENABLE = "enable"
|
|
527
|
+ KEY_STATE = "state"
|
|
528
|
+ KEY_BRIGHTNESS = "brightness"
|
|
529
|
+ KEY_COLOR_TEMP = "color_temp"
|
508
|
530
|
KEY_TIMER = "timer"
|
509
|
|
- KEY_LED_X = "led%d"
|
510
|
|
-
|
511
|
|
- def __init__(self, mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False):
|
512
|
|
- super().__init__(mqtt_client, topic, enable_state, enable_brightness, enable_color_temp)
|
513
|
|
- self.add_callback(self.KEY_ENABLE, self.print_formatted, None)
|
514
|
|
- self.add_callback(self.KEY_TIMER, self.print_formatted, None)
|
515
|
|
- for i in range(0, 10):
|
516
|
|
- self.add_callback(self.KEY_LED_X % i, self.print_formatted, None)
|
517
|
|
- self.led_names = {}
|
|
531
|
+ #
|
|
532
|
+ STATE_COMMANDS = ("get_state", "toggle_state", )
|
|
533
|
+ BRIGHTNESS_COMMANDS = ("get_brightness", "set_brightness", )
|
|
534
|
+ COLOR_TEMP_COMMANDS = ("get_color_temp", "set_color_temp", )
|
|
535
|
+ TIMER_COMMANDS = ("get_timer", )
|
|
536
|
+
|
|
537
|
+ def __init__(self, mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False, enable_timer=False):
|
|
538
|
+ super().__init__(mqtt_client, topic)
|
|
539
|
+ self.enable_state = enable_state
|
|
540
|
+ self.enable_brightness = enable_brightness
|
|
541
|
+ self.enable_color_temp = enable_color_temp
|
|
542
|
+ self.enable_timer = enable_timer
|
518
|
543
|
#
|
519
|
544
|
self.maxvalue = None
|
520
|
|
-
|
521
|
|
- def __init_data__(self, enable_state, enable_brightness, enable_color_temp):
|
522
|
|
- data = {}
|
523
|
|
- data[self.KEY_ENABLE] = False
|
|
545
|
+ # add commands to be available
|
524
|
546
|
if enable_state:
|
525
|
|
- data[self.KEY_STATE] = False
|
|
547
|
+ # init default value
|
|
548
|
+ self.data[self.KEY_STATE] = False
|
|
549
|
+ # add print callback
|
|
550
|
+ self.add_callback(self.KEY_STATE, self.print_formatted, None)
|
|
551
|
+ # add commands to be available
|
|
552
|
+ self.commands.extend(self.STATE_COMMANDS)
|
526
|
553
|
if enable_brightness:
|
527
|
|
- data[self.KEY_BRIGHTNESS] = 50
|
|
554
|
+ # init default value
|
|
555
|
+ self.data[self.KEY_BRIGHTNESS] = 50
|
|
556
|
+ # add print callback
|
|
557
|
+ self.add_callback(self.KEY_BRIGHTNESS, self.print_formatted, None)
|
|
558
|
+ # add commands to be available
|
|
559
|
+ self.commands.extend(self.BRIGHTNESS_COMMANDS)
|
528
|
560
|
if enable_color_temp:
|
529
|
|
- data[self.KEY_COLOR_TEMP] = 5
|
530
|
|
- data[self.KEY_TIMER] = '-'
|
531
|
|
- for i in range(0, 10):
|
532
|
|
- data[self.KEY_LED_X % i] = False
|
533
|
|
- self.store_data(**data)
|
|
561
|
+ # init default value
|
|
562
|
+ self.data[self.KEY_COLOR_TEMP] = 5
|
|
563
|
+ # add print callback
|
|
564
|
+ self.add_callback(self.KEY_COLOR_TEMP, self.print_formatted, None)
|
|
565
|
+ # add commands to be available
|
|
566
|
+ self.commands.extend(self.COLOR_TEMP_COMMANDS)
|
|
567
|
+ if enable_timer:
|
|
568
|
+ # init default value
|
|
569
|
+ self.data[self.KEY_TIMER] = 0
|
|
570
|
+ # add print callback
|
|
571
|
+ self.add_callback(self.KEY_TIMER, self.print_formatted, None)
|
|
572
|
+ # add commands to be available
|
|
573
|
+ self.commands.extend(self.TIMER_COMMANDS)
|
534
|
574
|
|
535
|
575
|
def __rx__(self, client, userdata, message):
|
536
|
576
|
value = payload_filter(message.payload)
|
537
|
|
- if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
538
|
|
- targetkey = message.topic.split('/')[-2]
|
|
577
|
+ if message.topic.startswith(self.topic):
|
|
578
|
+ targetkey = message.topic.split('/')[-1]
|
539
|
579
|
if targetkey in self.data.keys():
|
540
|
580
|
self.store_data(**{targetkey: value})
|
541
|
|
- else:
|
|
581
|
+ elif targetkey != "__info__":
|
542
|
582
|
print("Unknown key %s in %s::%s" % (targetkey, message.topic, self.__class__.__name__))
|
543
|
583
|
elif message.topic == self.topic + '/get':
|
544
|
584
|
self.__tx__(None)
|
|
@@ -566,26 +606,22 @@ class gui_light(tradfri_light):
|
566
|
606
|
self.print_formatted(self, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
567
|
607
|
elif command == self.COLOR_TEMP_COMMANDS[1]:
|
568
|
608
|
self.send(self.KEY_COLOR_TEMP, command_int_value(value))
|
|
609
|
+ elif command == self.TIMER_COMMANDS[0]:
|
|
610
|
+ self.print_formatted(self, self.KEY_TIMER, self.data.get(self.KEY_TIMER))
|
569
|
611
|
else:
|
570
|
612
|
print("%s: not yet implemented!" % command)
|
571
|
613
|
else:
|
572
|
614
|
print("Unknown command!")
|
573
|
615
|
|
574
|
|
- def add_led_name(self, key, name):
|
575
|
|
- self.led_names[key] = name
|
576
|
|
-
|
577
|
616
|
def print_formatted(self, device, key, value):
|
578
|
617
|
if value is not None:
|
579
|
618
|
device = " - ".join(self.topic.split('/')[1:])
|
580
|
619
|
if key == self.KEY_STATE:
|
581
|
620
|
print_switch(COLOR_GUI_ACTIVE, value, self.topic, "")
|
582
|
|
- elif key == self.KEY_ENABLE:
|
583
|
|
- self.print_formatted(device, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
584
|
|
- self.print_formatted(device, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
585
|
621
|
elif key in [self.KEY_BRIGHTNESS, self.KEY_COLOR_TEMP]:
|
586
|
622
|
perc_value = round(value * 10 if key == self.KEY_COLOR_TEMP else value, 0)
|
587
|
623
|
print_percent(
|
588
|
|
- COLOR_GUI_PASSIVE if not self.data.get(self.KEY_ENABLE, False) else COLOR_GUI_ACTIVE,
|
|
624
|
+ COLOR_GUI_ACTIVE,
|
589
|
625
|
'B' if key == self.KEY_BRIGHTNESS else 'C',
|
590
|
626
|
perc_value,
|
591
|
627
|
"%3d%%" % perc_value,
|
|
@@ -606,241 +642,191 @@ class gui_light(tradfri_light):
|
606
|
642
|
perc = 0
|
607
|
643
|
self.maxvalue = None
|
608
|
644
|
print_percent(COLOR_GUI_ACTIVE, 't', perc, '%3d%%' % perc, self.topic, '(%.1f)' % disp_value)
|
609
|
|
- elif key.startswith(self.KEY_LED_X[:-2]):
|
610
|
|
- print_light(COLOR_GUI_ACTIVE, value, self.topic, '(%s)' % self.led_names.get(key, key), True)
|
611
|
|
-
|
612
|
645
|
|
613
|
|
-class tradfri_button(base):
|
614
|
|
- KEY_ACTION = "action"
|
615
|
|
- #
|
616
|
|
- ACTION_TOGGLE = "toggle"
|
617
|
|
- ACTION_BRIGHTNESS_UP = "brightness_up_click"
|
618
|
|
- ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
|
619
|
|
- ACTION_RIGHT = "arrow_right_click"
|
620
|
|
- ACTION_LEFT = "arrow_left_click"
|
621
|
|
- ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
|
622
|
|
- ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
|
623
|
|
- ACTION_RIGHT_LONG = "arrow_right_hold"
|
624
|
|
- ACTION_LEFT_LONG = "arrow_left_hold"
|
625
|
|
- #
|
626
|
|
- COMMANDS = [ACTION_TOGGLE, ACTION_LEFT, ACTION_RIGHT, ACTION_BRIGHTNESS_UP, ACTION_BRIGHTNESS_DOWN,
|
627
|
|
- ACTION_LEFT_LONG, ACTION_RIGHT_LONG, ACTION_BRIGHTNESS_UP_LONG, ACTION_BRIGHTNESS_DOWN_LONG]
|
628
|
646
|
|
629
|
|
- def __init__(self, mqtt_client, topic):
|
630
|
|
- super().__init__(mqtt_client, topic)
|
631
|
|
-
|
632
|
|
- def __rx__(self, client, userdata, message):
|
633
|
|
- pass
|
634
|
|
-
|
635
|
|
- def command(self, command):
|
636
|
|
- try:
|
637
|
|
- command, value = command.split(' ')
|
638
|
|
- except ValueError:
|
639
|
|
- value = None
|
640
|
|
- else:
|
641
|
|
- value = json.loads(value)
|
642
|
|
- if command in self.capabilities():
|
643
|
|
- action = self.COMMANDS[self.COMMANDS.index(command)]
|
644
|
|
- if self.COMMANDS.index(command) <= 4:
|
645
|
|
- self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
646
|
|
- elif self.COMMANDS.index(command) <= 8:
|
647
|
|
- self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
648
|
|
- time.sleep(value or 0.5)
|
649
|
|
- action = '_'.join(action.split('_')[:-1] + ['release'])
|
650
|
|
- self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
651
|
|
-
|
652
|
|
-
|
653
|
|
-class gui_led_array(base):
|
654
|
|
- AUTOSEND = False
|
655
|
|
- #
|
656
|
|
- KEY_LED_0 = "led0"
|
657
|
|
- KEY_LED_1 = "led1"
|
658
|
|
- KEY_LED_2 = "led2"
|
659
|
|
- KEY_LED_3 = "led3"
|
660
|
|
- KEY_LED_4 = "led4"
|
661
|
|
- KEY_LED_5 = "led5"
|
662
|
|
- KEY_LED_6 = "led6"
|
663
|
|
- KEY_LED_7 = "led7"
|
664
|
|
- KEY_LED_8 = "led8"
|
665
|
|
- KEY_LED_9 = "led9"
|
666
|
|
-
|
667
|
|
- def __init__(self, mqtt_client, topic, ):
|
668
|
|
- super().__init__(mqtt_client, topic)
|
669
|
|
- for i in range(0, 10):
|
670
|
|
- key = getattr(self, "KEY_LED_%d" % i)
|
671
|
|
- self.data[key] = False
|
672
|
|
- self.add_callback(key, self.print_formatted, None)
|
673
|
|
-
|
674
|
|
- def __rx__(self, client, userdata, message):
|
675
|
|
- value = payload_filter(message.payload)
|
676
|
|
- if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
677
|
|
- targetkey = message.topic.split('/')[-2]
|
678
|
|
- if targetkey in self.data.keys():
|
679
|
|
- self.store_data(**{targetkey: value})
|
680
|
|
- else:
|
681
|
|
- print("Unknown key %s in %s" % (targetkey, self.__class__.__name__))
|
682
|
|
-
|
683
|
|
- def print_formatted(self, device, key, value):
|
684
|
|
- print_light(COLOR_GUI_ACTIVE, value, self.topic, '(%s)' % self.names.get(key, key), True)
|
685
|
|
-
|
686
|
|
-
|
687
|
|
-class remote(base):
|
688
|
|
- def __rx__(self, client, userdata, message):
|
689
|
|
- if message.topic == self.topic + "/VOLUP":
|
690
|
|
- if payload_filter(message.payload):
|
691
|
|
- icon = u'\u1403'
|
692
|
|
- else:
|
693
|
|
- icon = u'\u25a1'
|
694
|
|
- elif message.topic == self.topic + "/VOLDOWN":
|
695
|
|
- if payload_filter(message.payload):
|
696
|
|
- icon = u'\u1401'
|
697
|
|
- else:
|
698
|
|
- icon = u'\u25a1'
|
699
|
|
- else:
|
700
|
|
- return
|
701
|
|
- devicename = ' - '.join(self.topic.split('/')[1:-1])
|
702
|
|
- print(COLOR_REMOTE + 10 * ' ' + icon + 6 * ' ' + devicename + colored.attr("reset"))
|
703
|
|
-
|
704
|
|
-
|
705
|
|
-class brennenstuhl_heating_valve(base):
|
706
|
|
- TEMP_RANGE = [10, 30]
|
707
|
|
- #
|
708
|
|
- KEY_TEMPERATURE_SETPOINT = "current_heating_setpoint"
|
709
|
|
- KEY_TEMPERATURE = "local_temperature"
|
710
|
|
- #
|
711
|
|
- COMMANDS = [
|
712
|
|
- "get_temperature_setpoint", "set_temperature_setpoint",
|
713
|
|
- ]
|
714
|
|
-
|
715
|
|
- def __init__(self, mqtt_client, topic):
|
716
|
|
- super().__init__(mqtt_client, topic)
|
717
|
|
- self.store_data(**{
|
718
|
|
- self.KEY_TEMPERATURE_SETPOINT: 20,
|
719
|
|
- self.KEY_TEMPERATURE: 20.7,
|
720
|
|
- })
|
721
|
|
- self.add_callback(self.KEY_TEMPERATURE_SETPOINT, self.print_formatted, None)
|
722
|
|
-
|
723
|
|
- def __rx__(self, client, userdata, message):
|
724
|
|
- if message.topic.startswith(self.topic) and message.topic.endswith("/set"):
|
725
|
|
- payload = payload_filter(message.payload)
|
726
|
|
- self.store_data(**payload)
|
727
|
|
-
|
728
|
|
- def command(self, command):
|
729
|
|
- try:
|
730
|
|
- command, value = command.split(' ')
|
731
|
|
- except ValueError:
|
732
|
|
- value = None
|
733
|
|
- if command in self.COMMANDS:
|
734
|
|
- if command == self.COMMANDS[0]:
|
735
|
|
- self.print_formatted(self, self.KEY_TEMPERATURE_SETPOINT, self.data.get(self.KEY_TEMPERATURE_SETPOINT))
|
736
|
|
- elif command == self.COMMANDS[1]:
|
737
|
|
- self.store_data(**{self.KEY_TEMPERATURE_SETPOINT: command_float_value(value)})
|
738
|
|
-
|
739
|
|
- def print_formatted(self, device, key, value):
|
740
|
|
- devicename = ' - '.join(self.topic.split('/')[1:])
|
741
|
|
- if key == self.KEY_TEMPERATURE_SETPOINT:
|
742
|
|
- perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
743
|
|
- perc = 100 if perc > 100 else perc
|
744
|
|
- perc = 0 if perc < 0 else perc
|
745
|
|
- print_percent(COLOR_HEATING_VALVE, '\u03d1', perc, "%4.1f°C" % value, self.topic, "")
|
746
|
|
-
|
747
|
|
-
|
748
|
|
-class gui_heating_valve(base):
|
749
|
|
- AUTOSEND = False
|
750
|
|
- #
|
751
|
|
- TEMP_RANGE = [10, 30]
|
752
|
|
- #
|
753
|
|
- KEY_TIMER = "timer"
|
754
|
|
- KEY_TEMPERATURE = "temperature"
|
755
|
|
- KEY_SETPOINT_TEMP = "setpoint_temp"
|
756
|
|
- KEY_SETPOINT_TO_DEFAULT = "setpoint_to_default"
|
757
|
|
- KEY_BOOST = 'boost'
|
758
|
|
- KEY_AWAY = "away"
|
759
|
|
- KEY_SUMMER = "summer"
|
760
|
|
- KEY_ENABLE = "enable"
|
761
|
|
- #
|
762
|
|
- COMMANDS = [
|
763
|
|
- "get_temperature",
|
764
|
|
- "get_temperature_setpoint", "set_temperature_setpoint",
|
765
|
|
- "trigger_boost", "trigger_setpoint_to_default",
|
766
|
|
- "toggle_away", "toggle_summer",
|
767
|
|
- ]
|
768
|
|
-
|
769
|
|
- def __init__(self, mqtt_client, topic):
|
770
|
|
- super().__init__(mqtt_client, topic)
|
771
|
|
- self.add_callback(self.KEY_SETPOINT_TEMP, self.print_formatted, None)
|
772
|
|
- self.add_callback(self.KEY_TIMER, self.print_formatted, None)
|
773
|
|
- self.add_callback(self.KEY_AWAY, self.print_formatted, None)
|
774
|
|
- self.add_callback(self.KEY_SUMMER, self.print_formatted, None)
|
775
|
|
- #
|
776
|
|
- self.store_data(**{
|
777
|
|
- self.KEY_TEMPERATURE: 20.7,
|
778
|
|
- self.KEY_SETPOINT_TEMP: 20,
|
779
|
|
- self.KEY_TIMER: 0,
|
780
|
|
- self.KEY_AWAY: False,
|
781
|
|
- self.KEY_SUMMER: False,
|
782
|
|
- self.KEY_ENABLE: True
|
783
|
|
- })
|
784
|
|
-
|
785
|
|
- def __rx__(self, client, userdata, message):
|
786
|
|
- value = payload_filter(message.payload)
|
787
|
|
- if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
788
|
|
- targetkey = message.topic.split('/')[-2]
|
789
|
|
- if targetkey in self.data.keys():
|
790
|
|
- self.store_data(**{targetkey: value})
|
791
|
|
- else:
|
792
|
|
- print("Unknown key %s in %s::%s" % (targetkey, message.topic, self.__class__.__name__))
|
793
|
|
- elif message.topic == self.topic + '/get':
|
794
|
|
- self.__tx__(None)
|
795
|
|
-
|
796
|
|
- def send(self, key, data):
|
797
|
|
- if data is not None:
|
798
|
|
- topic = self.topic + '/' + key
|
799
|
|
- self.mqtt_client.send(topic, json.dumps(data))
|
800
|
|
-
|
801
|
|
- def command(self, command):
|
802
|
|
- try:
|
803
|
|
- command, value = command.split(' ')
|
804
|
|
- except ValueError:
|
805
|
|
- value = None
|
806
|
|
- if command in self.COMMANDS:
|
807
|
|
- if command == self.COMMANDS[0]:
|
808
|
|
- self.print_formatted(self, self.KEY_TEMPERATURE, self.data.get(self.KEY_TEMPERATURE))
|
809
|
|
- elif command == self.COMMANDS[1]:
|
810
|
|
- self.print_formatted(self, self.KEY_SETPOINT_TEMP, self.data.get(self.KEY_SETPOINT_TEMP))
|
811
|
|
- elif command == self.COMMANDS[2]:
|
812
|
|
- self.send(self.KEY_SETPOINT_TEMP, command_float_value(value))
|
813
|
|
- elif command == self.COMMANDS[3]:
|
814
|
|
- self.send(self.KEY_BOOST, True)
|
815
|
|
- elif command == self.COMMANDS[4]:
|
816
|
|
- self.send(self.KEY_SETPOINT_TO_DEFAULT, True)
|
817
|
|
- elif command == self.COMMANDS[5]:
|
818
|
|
- self.send(self.KEY_AWAY, not self.data.get(self.KEY_AWAY))
|
819
|
|
- elif command == self.COMMANDS[6]:
|
820
|
|
- self.send(self.KEY_SUMMER, not self.data.get(self.KEY_SUMMER))
|
821
|
|
-
|
822
|
|
- def print_formatted(self, device, key, value):
|
823
|
|
- devicename = ' - '.join(self.topic.split('/')[1:])
|
824
|
|
- if key == self.KEY_TIMER:
|
825
|
|
- value /= 60
|
826
|
|
- try:
|
827
|
|
- perc = 100 * value / 60
|
828
|
|
- except TypeError:
|
829
|
|
- value = 0
|
830
|
|
- perc = 0
|
831
|
|
- print_percent(COLOR_GUI_ACTIVE, 't', perc, "%4.1fmin" % value, self.topic, "(Timer)")
|
832
|
|
- elif key == self.KEY_TEMPERATURE:
|
833
|
|
- perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
834
|
|
- perc = 100 if perc > 100 else perc
|
835
|
|
- perc = 0 if perc < 0 else perc
|
836
|
|
- print_percent(COLOR_GUI_ACTIVE, '\u03d1', perc, "%4.1f°C" % value, self.topic, "(Temperature)")
|
837
|
|
- elif key == self.KEY_SETPOINT_TEMP:
|
838
|
|
- perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
839
|
|
- perc = 100 if perc > 100 else perc
|
840
|
|
- perc = 0 if perc < 0 else perc
|
841
|
|
- print_percent(COLOR_GUI_ACTIVE if self.data.get(self.KEY_ENABLE) else COLOR_GUI_PASSIVE,
|
842
|
|
- '\u03d1', perc, "%4.1f°C" % value, self.topic, "(Setpoint)")
|
843
|
|
- elif key == self.KEY_AWAY:
|
844
|
|
- print_switch(COLOR_GUI_ACTIVE, value, self.topic, "(Away Mode)")
|
845
|
|
- elif key == self.KEY_SUMMER:
|
846
|
|
- print_switch(COLOR_GUI_ACTIVE, value, self.topic, "(Summer Mode)")
|
|
647
|
+# class silvercrest_motion_sensor(base):
|
|
648
|
+# KEY_OCCUPANCY = "occupancy"
|
|
649
|
+# COMMANDS = ['motion']
|
|
650
|
+
|
|
651
|
+# def __init__(self, mqtt_client, topic):
|
|
652
|
+# super().__init__(mqtt_client, topic)
|
|
653
|
+# self.data[self.KEY_OCCUPANCY] = False
|
|
654
|
+# self.add_callback(self.KEY_OCCUPANCY, self.print_formatted, None)
|
|
655
|
+
|
|
656
|
+# def __rx__(self, client, userdata, message):
|
|
657
|
+# pass
|
|
658
|
+
|
|
659
|
+# def command(self, command):
|
|
660
|
+# try:
|
|
661
|
+# command, value = command.split(' ')
|
|
662
|
+# except ValueError:
|
|
663
|
+# value = None
|
|
664
|
+# else:
|
|
665
|
+# value = json.loads(value)
|
|
666
|
+# if command == self.COMMANDS[0]:
|
|
667
|
+# self.store_data(**{self.KEY_OCCUPANCY: True})
|
|
668
|
+# time.sleep(value or 10)
|
|
669
|
+# self.store_data(**{self.KEY_OCCUPANCY: False})
|
|
670
|
+
|
|
671
|
+# def print_formatted(self, device, key, value):
|
|
672
|
+# if value is not None:
|
|
673
|
+# print_light(COLOR_MOTION_SENSOR, value, self.topic, "")
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+# class tradfri_button(base):
|
|
677
|
+# KEY_ACTION = "action"
|
|
678
|
+# #
|
|
679
|
+# ACTION_TOGGLE = "toggle"
|
|
680
|
+# ACTION_BRIGHTNESS_UP = "brightness_up_click"
|
|
681
|
+# ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
|
|
682
|
+# ACTION_RIGHT = "arrow_right_click"
|
|
683
|
+# ACTION_LEFT = "arrow_left_click"
|
|
684
|
+# ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
|
|
685
|
+# ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
|
|
686
|
+# ACTION_RIGHT_LONG = "arrow_right_hold"
|
|
687
|
+# ACTION_LEFT_LONG = "arrow_left_hold"
|
|
688
|
+# #
|
|
689
|
+# COMMANDS = [ACTION_TOGGLE, ACTION_LEFT, ACTION_RIGHT, ACTION_BRIGHTNESS_UP, ACTION_BRIGHTNESS_DOWN,
|
|
690
|
+# ACTION_LEFT_LONG, ACTION_RIGHT_LONG, ACTION_BRIGHTNESS_UP_LONG, ACTION_BRIGHTNESS_DOWN_LONG]
|
|
691
|
+
|
|
692
|
+# def __init__(self, mqtt_client, topic):
|
|
693
|
+# super().__init__(mqtt_client, topic)
|
|
694
|
+
|
|
695
|
+# def __rx__(self, client, userdata, message):
|
|
696
|
+# pass
|
|
697
|
+
|
|
698
|
+# def command(self, command):
|
|
699
|
+# try:
|
|
700
|
+# command, value = command.split(' ')
|
|
701
|
+# except ValueError:
|
|
702
|
+# value = None
|
|
703
|
+# else:
|
|
704
|
+# value = json.loads(value)
|
|
705
|
+# if command in self.capabilities():
|
|
706
|
+# action = self.COMMANDS[self.COMMANDS.index(command)]
|
|
707
|
+# if self.COMMANDS.index(command) <= 4:
|
|
708
|
+# self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
|
709
|
+# elif self.COMMANDS.index(command) <= 8:
|
|
710
|
+# self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
|
711
|
+# time.sleep(value or 0.5)
|
|
712
|
+# action = '_'.join(action.split('_')[:-1] + ['release'])
|
|
713
|
+# self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+# class remote(base):
|
|
717
|
+# def __rx__(self, client, userdata, message):
|
|
718
|
+# if message.topic == self.topic + "/VOLUP":
|
|
719
|
+# if payload_filter(message.payload):
|
|
720
|
+# icon = u'\u1403'
|
|
721
|
+# else:
|
|
722
|
+# icon = u'\u25a1'
|
|
723
|
+# elif message.topic == self.topic + "/VOLDOWN":
|
|
724
|
+# if payload_filter(message.payload):
|
|
725
|
+# icon = u'\u1401'
|
|
726
|
+# else:
|
|
727
|
+# icon = u'\u25a1'
|
|
728
|
+# else:
|
|
729
|
+# return
|
|
730
|
+# devicename = ' - '.join(self.topic.split('/')[1:-1])
|
|
731
|
+# print(COLOR_REMOTE + 10 * ' ' + icon + 6 * ' ' + devicename + colored.attr("reset"))
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+# class gui_heating_valve(base):
|
|
735
|
+# AUTOSEND = False
|
|
736
|
+# #
|
|
737
|
+# TEMP_RANGE = [10, 30]
|
|
738
|
+# #
|
|
739
|
+# KEY_TIMER = "timer"
|
|
740
|
+# KEY_TEMPERATURE = "temperature"
|
|
741
|
+# KEY_SETPOINT_TEMP = "setpoint_temp"
|
|
742
|
+# KEY_SETPOINT_TO_DEFAULT = "setpoint_to_default"
|
|
743
|
+# KEY_BOOST = 'boost'
|
|
744
|
+# KEY_AWAY = "away"
|
|
745
|
+# KEY_SUMMER = "summer"
|
|
746
|
+# KEY_ENABLE = "enable"
|
|
747
|
+# #
|
|
748
|
+# COMMANDS = [
|
|
749
|
+# "get_temperature",
|
|
750
|
+# "get_temperature_setpoint", "set_temperature_setpoint",
|
|
751
|
+# "trigger_boost", "trigger_setpoint_to_default",
|
|
752
|
+# "toggle_away", "toggle_summer",
|
|
753
|
+# ]
|
|
754
|
+
|
|
755
|
+# def __init__(self, mqtt_client, topic):
|
|
756
|
+# super().__init__(mqtt_client, topic)
|
|
757
|
+# self.add_callback(self.KEY_SETPOINT_TEMP, self.print_formatted, None)
|
|
758
|
+# self.add_callback(self.KEY_TIMER, self.print_formatted, None)
|
|
759
|
+# self.add_callback(self.KEY_AWAY, self.print_formatted, None)
|
|
760
|
+# self.add_callback(self.KEY_SUMMER, self.print_formatted, None)
|
|
761
|
+# #
|
|
762
|
+# self.store_data(**{
|
|
763
|
+# self.KEY_TEMPERATURE: 20.7,
|
|
764
|
+# self.KEY_SETPOINT_TEMP: 20,
|
|
765
|
+# self.KEY_TIMER: 0,
|
|
766
|
+# self.KEY_AWAY: False,
|
|
767
|
+# self.KEY_SUMMER: False,
|
|
768
|
+# self.KEY_ENABLE: True
|
|
769
|
+# })
|
|
770
|
+
|
|
771
|
+# def __rx__(self, client, userdata, message):
|
|
772
|
+# value = payload_filter(message.payload)
|
|
773
|
+# if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
|
774
|
+# targetkey = message.topic.split('/')[-2]
|
|
775
|
+# if targetkey in self.data.keys():
|
|
776
|
+# self.store_data(**{targetkey: value})
|
|
777
|
+# else:
|
|
778
|
+# print("Unknown key %s in %s::%s" % (targetkey, message.topic, self.__class__.__name__))
|
|
779
|
+# elif message.topic == self.topic + '/get':
|
|
780
|
+# self.__tx__(None)
|
|
781
|
+
|
|
782
|
+# def send(self, key, data):
|
|
783
|
+# if data is not None:
|
|
784
|
+# topic = self.topic + '/' + key
|
|
785
|
+# self.mqtt_client.send(topic, json.dumps(data))
|
|
786
|
+
|
|
787
|
+# def command(self, command):
|
|
788
|
+# try:
|
|
789
|
+# command, value = command.split(' ')
|
|
790
|
+# except ValueError:
|
|
791
|
+# value = None
|
|
792
|
+# if command in self.COMMANDS:
|
|
793
|
+# if command == self.COMMANDS[0]:
|
|
794
|
+# self.print_formatted(self, self.KEY_TEMPERATURE, self.data.get(self.KEY_TEMPERATURE))
|
|
795
|
+# elif command == self.COMMANDS[1]:
|
|
796
|
+# self.print_formatted(self, self.KEY_SETPOINT_TEMP, self.data.get(self.KEY_SETPOINT_TEMP))
|
|
797
|
+# elif command == self.COMMANDS[2]:
|
|
798
|
+# self.send(self.KEY_SETPOINT_TEMP, command_float_value(value))
|
|
799
|
+# elif command == self.COMMANDS[3]:
|
|
800
|
+# self.send(self.KEY_BOOST, True)
|
|
801
|
+# elif command == self.COMMANDS[4]:
|
|
802
|
+# self.send(self.KEY_SETPOINT_TO_DEFAULT, True)
|
|
803
|
+# elif command == self.COMMANDS[5]:
|
|
804
|
+# self.send(self.KEY_AWAY, not self.data.get(self.KEY_AWAY))
|
|
805
|
+# elif command == self.COMMANDS[6]:
|
|
806
|
+# self.send(self.KEY_SUMMER, not self.data.get(self.KEY_SUMMER))
|
|
807
|
+
|
|
808
|
+# def print_formatted(self, device, key, value):
|
|
809
|
+# devicename = ' - '.join(self.topic.split('/')[1:])
|
|
810
|
+# if key == self.KEY_TIMER:
|
|
811
|
+# value /= 60
|
|
812
|
+# try:
|
|
813
|
+# perc = 100 * value / 60
|
|
814
|
+# except TypeError:
|
|
815
|
+# value = 0
|
|
816
|
+# perc = 0
|
|
817
|
+# print_percent(COLOR_GUI_ACTIVE, 't', perc, "%4.1fmin" % value, self.topic, "(Timer)")
|
|
818
|
+# elif key == self.KEY_TEMPERATURE:
|
|
819
|
+# perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
|
820
|
+# perc = 100 if perc > 100 else perc
|
|
821
|
+# perc = 0 if perc < 0 else perc
|
|
822
|
+# print_percent(COLOR_GUI_ACTIVE, '\u03d1', perc, "%4.1f°C" % value, self.topic, "(Temperature)")
|
|
823
|
+# elif key == self.KEY_SETPOINT_TEMP:
|
|
824
|
+# perc = 100 * (value - self.TEMP_RANGE[0]) / (self.TEMP_RANGE[1] - self.TEMP_RANGE[0])
|
|
825
|
+# perc = 100 if perc > 100 else perc
|
|
826
|
+# perc = 0 if perc < 0 else perc
|
|
827
|
+# print_percent(COLOR_GUI_ACTIVE if self.data.get(self.KEY_ENABLE) else COLOR_GUI_PASSIVE,
|
|
828
|
+# '\u03d1', perc, "%4.1f°C" % value, self.topic, "(Setpoint)")
|
|
829
|
+# elif key == self.KEY_AWAY:
|
|
830
|
+# print_switch(COLOR_GUI_ACTIVE, value, self.topic, "(Away Mode)")
|
|
831
|
+# elif key == self.KEY_SUMMER:
|
|
832
|
+# print_switch(COLOR_GUI_ACTIVE, value, self.topic, "(Summer Mode)")
|