|
@@ -0,0 +1,560 @@
|
|
1
|
+import colored
|
|
2
|
+import json
|
|
3
|
+import sys
|
|
4
|
+import time
|
|
5
|
+
|
|
6
|
+# TODO: implement my powerplug
|
|
7
|
+
|
|
8
|
+COLOR_GUI_ACTIVE = colored.fg("light_blue")
|
|
9
|
+COLOR_GUI_PASSIVE = COLOR_GUI_ACTIVE + colored.attr("dim")
|
|
10
|
+COLOR_SHELLY_1 = colored.fg("light_magenta")
|
|
11
|
+COLOR_SHELLY_2 = colored.fg("light_cyan")
|
|
12
|
+COLOR_LIGHT_ACTIVE = colored.fg("yellow")
|
|
13
|
+COLOR_LIGHT_PASSIVE = COLOR_LIGHT_ACTIVE + colored.attr("dim")
|
|
14
|
+COLOR_MOTION_SENSOR = colored.fg("red")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+def payload_filter(payload):
|
|
18
|
+ try:
|
|
19
|
+ return json.loads(payload)
|
|
20
|
+ except json.decoder.JSONDecodeError:
|
|
21
|
+ return payload.decode("utf-8")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+def percent_print(value):
|
|
25
|
+ for i in range(0, 10):
|
|
26
|
+ if (value - 5) > 10*i:
|
|
27
|
+ sys.stdout.write(u"\u25ac")
|
|
28
|
+ else:
|
|
29
|
+ sys.stdout.write(u"\u25ad")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+def command_int_value(value):
|
|
33
|
+ try:
|
|
34
|
+ return int(value)
|
|
35
|
+ except TypeError:
|
|
36
|
+ print("You need to give a integer parameter not '%s'" % str(value))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+class base(object):
|
|
40
|
+ AUTOSEND = True
|
|
41
|
+ COMMANDS = []
|
|
42
|
+
|
|
43
|
+ def __init__(self, mqtt_client, topic):
|
|
44
|
+ self.mqtt_client = mqtt_client
|
|
45
|
+ self.topic = topic
|
|
46
|
+ #
|
|
47
|
+ self.data = {}
|
|
48
|
+ self.callbacks = {}
|
|
49
|
+ self.commands = self.COMMANDS[:]
|
|
50
|
+ #
|
|
51
|
+ self.mqtt_client.add_callback(self.topic, self.__rx__)
|
|
52
|
+ self.mqtt_client.add_callback(self.topic + '/#', self.__rx__)
|
|
53
|
+
|
|
54
|
+ def add_callback(self, key, callback, value):
|
|
55
|
+ if self.callbacks.get(key) is None:
|
|
56
|
+ self.callbacks[key] = []
|
|
57
|
+ self.callbacks[key].append((callback, value))
|
|
58
|
+
|
|
59
|
+ def capabilities(self):
|
|
60
|
+ return self.commands
|
|
61
|
+
|
|
62
|
+ def store_data(self, *args, **kwargs):
|
|
63
|
+ keys_changed = []
|
|
64
|
+ for key in kwargs:
|
|
65
|
+ if kwargs[key] is not None and kwargs[key] != self.data.get(key):
|
|
66
|
+ keys_changed.append(key)
|
|
67
|
+ self.data[key] = kwargs[key]
|
|
68
|
+ for callback, value in self.callbacks.get(key, [(None, None)]):
|
|
69
|
+ if callback is not None and (value is None or value == kwargs[key]):
|
|
70
|
+ callback(self, key, kwargs[key])
|
|
71
|
+ if self.AUTOSEND and len(keys_changed) > 0:
|
|
72
|
+ self.__tx__(keys_changed)
|
|
73
|
+
|
|
74
|
+ def __tx__(self, keys_changed):
|
|
75
|
+ self.mqtt_client.send(self.topic, json.dumps(self.data))
|
|
76
|
+
|
|
77
|
+ def __rx__(self, client, userdata, message):
|
|
78
|
+ print("%s: __rx__ not handled!" % self.__class__.__name__)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+class shelly(base):
|
|
82
|
+ KEY_OUTPUT_0 = "relay/0"
|
|
83
|
+ KEY_OUTPUT_1 = "relay/1"
|
|
84
|
+ #
|
|
85
|
+ COMMANDS = [
|
|
86
|
+ "get_relay_0", "set_relay_0", "unset_relay_0",
|
|
87
|
+ "get_relay_1", "set_relay_1", "unset_relay_1",
|
|
88
|
+ ]
|
|
89
|
+
|
|
90
|
+ def __init__(self, mqtt_client, topic):
|
|
91
|
+ super().__init__(mqtt_client, topic)
|
|
92
|
+ #
|
|
93
|
+ self.store_data(**{self.KEY_OUTPUT_0: "off", self.KEY_OUTPUT_1: "off"})
|
|
94
|
+ self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
|
95
|
+
|
|
96
|
+ def __rx__(self, client, userdata, message):
|
|
97
|
+ value = payload_filter(message.payload)
|
|
98
|
+ if message.topic == self.topic + "/relay/0/command":
|
|
99
|
+ if value in ['on', 'off']:
|
|
100
|
+ self.store_data(**{self.KEY_OUTPUT_0: value})
|
|
101
|
+ elif value == 'toggle':
|
|
102
|
+ self.store_data(**{self.KEY_OUTPUT_0: 'on' if self.data.get(self.KEY_OUTPUT_0) == 'off' else 'off'})
|
|
103
|
+ elif message.topic == self.topic + "/relay/1/command":
|
|
104
|
+ if value in ['on', 'off']:
|
|
105
|
+ self.store_data(**{self.KEY_OUTPUT_1: value})
|
|
106
|
+ elif value == 'toggle':
|
|
107
|
+ self.store_data(**{self.KEY_OUTPUT_1: 'on' if self.data.get(self.KEY_OUTPUT_1) == 'off' else 'off'})
|
|
108
|
+
|
|
109
|
+ def __tx__(self, keys_changed):
|
|
110
|
+ for key in keys_changed:
|
|
111
|
+ self.mqtt_client.send(self.topic + '/' + key, self.data.get(key))
|
|
112
|
+
|
|
113
|
+ def command(self, command):
|
|
114
|
+ if command in self.COMMANDS:
|
|
115
|
+ if command == self.COMMANDS[0]:
|
|
116
|
+ self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
|
117
|
+ elif command == self.COMMANDS[1]:
|
|
118
|
+ self.store_data(**{self.KEY_OUTPUT_0: "on"})
|
|
119
|
+ elif command == self.COMMANDS[2]:
|
|
120
|
+ self.store_data(**{self.KEY_OUTPUT_0: "off"})
|
|
121
|
+ elif command == self.COMMANDS[3]:
|
|
122
|
+ self.print_formatted(self, self.KEY_OUTPUT_1, self.data.get(self.KEY_OUTPUT_1))
|
|
123
|
+ elif command == self.COMMANDS[4]:
|
|
124
|
+ self.store_data(**{self.KEY_OUTPUT_1: "on"})
|
|
125
|
+ elif command == self.COMMANDS[5]:
|
|
126
|
+ self.store_data(**{self.KEY_OUTPUT_1: "off"})
|
|
127
|
+ else:
|
|
128
|
+ print("%s: not yet implemented!" % command)
|
|
129
|
+ else:
|
|
130
|
+ print("Unknown command!")
|
|
131
|
+
|
|
132
|
+ def print_formatted(self, device, key, value):
|
|
133
|
+ if value is not None:
|
|
134
|
+ if key == shelly.KEY_OUTPUT_0:
|
|
135
|
+ color = COLOR_SHELLY_1
|
|
136
|
+ else:
|
|
137
|
+ color = COLOR_SHELLY_2
|
|
138
|
+ if value == "on":
|
|
139
|
+ print(color + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
140
|
+ else:
|
|
141
|
+ print(color + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+class my_powerplug(base):
|
|
145
|
+ COMMANDS = [
|
|
146
|
+ "get_state", "set_state", "unset_state",
|
|
147
|
+ ]
|
|
148
|
+
|
|
149
|
+ def __init__(self, mqtt_client, topic, names=[]):
|
|
150
|
+ super().__init__(mqtt_client, topic)
|
|
151
|
+ #
|
|
152
|
+ self.names = []
|
|
153
|
+ #
|
|
154
|
+ for i in range(0, 4):
|
|
155
|
+ self.data[str(i)] = False
|
|
156
|
+ try:
|
|
157
|
+ self.names.append(names[i])
|
|
158
|
+ except IndexError:
|
|
159
|
+ self.names.append("Channel %d" % (i + 1))
|
|
160
|
+ self.add_callback(str(i), self.print_formatted, None)
|
|
161
|
+
|
|
162
|
+ def __rx__(self, client, userdata, message):
|
|
163
|
+ if message.topic.startswith(self.topic + '/output/') and message.topic.endswith('/set'):
|
|
164
|
+ channel = int(message.topic.split('/')[-2]) - 1
|
|
165
|
+ payload = payload_filter(message.payload)
|
|
166
|
+ if payload == "toggle":
|
|
167
|
+ payload = not self.data.get(str(channel))
|
|
168
|
+ self.store_data(**{str(channel): payload})
|
|
169
|
+
|
|
170
|
+ def __tx__(self, keys_changed):
|
|
171
|
+ for key in keys_changed:
|
|
172
|
+ self.mqtt_client.send(self.topic + "/output/" + str(int(key) + 1), json.dumps(self.data.get(key)))
|
|
173
|
+
|
|
174
|
+ def command(self, command):
|
|
175
|
+ if command in self.COMMANDS:
|
|
176
|
+ if command == self.COMMANDS[0]:
|
|
177
|
+ self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
|
178
|
+ elif command == self.COMMANDS[1]:
|
|
179
|
+ self.store_data(**{self.KEY_OUTPUT_0: 'on'})
|
|
180
|
+ elif command == self.COMMANDS[2]:
|
|
181
|
+ self.store_data(**{self.KEY_OUTPUT_0: 'off'})
|
|
182
|
+ else:
|
|
183
|
+ print("%s: not yet implemented!" % command)
|
|
184
|
+ else:
|
|
185
|
+ print("Unknown command!")
|
|
186
|
+
|
|
187
|
+ def print_formatted(self, device, key, value):
|
|
188
|
+ if value is not None:
|
|
189
|
+ if value:
|
|
190
|
+ print(COLOR_SHELLY_1 + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')
|
|
191
|
+ [1:]) + " (%s)" % self.names[int(key)] + colored.attr("reset"))
|
|
192
|
+ else:
|
|
193
|
+ print(COLOR_SHELLY_1 + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')
|
|
194
|
+ [1:]) + " (%s)" % self.names[int(key)] + colored.attr("reset"))
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+class silvercrest_powerplug(base):
|
|
198
|
+ KEY_OUTPUT_0 = "state"
|
|
199
|
+ #
|
|
200
|
+ COMMANDS = [
|
|
201
|
+ "get_state", "set_state", "unset_state",
|
|
202
|
+ ]
|
|
203
|
+
|
|
204
|
+ def __init__(self, mqtt_client, topic):
|
|
205
|
+ super().__init__(mqtt_client, topic)
|
|
206
|
+ self.add_callback(self.KEY_OUTPUT_0, self.print_formatted, None)
|
|
207
|
+ #
|
|
208
|
+ self.store_data(**{self.KEY_OUTPUT_0: "off"})
|
|
209
|
+
|
|
210
|
+ def __rx__(self, client, userdata, message):
|
|
211
|
+ if message.topic == self.topic + '/set':
|
|
212
|
+ STATES = ["on", "off", "toggle"]
|
|
213
|
+ #
|
|
214
|
+ state = json.loads(message.payload).get('state').lower()
|
|
215
|
+ if state in STATES:
|
|
216
|
+ if state == STATES[0]:
|
|
217
|
+ self.store_data(**{self.KEY_OUTPUT_0: 'on'})
|
|
218
|
+ elif state == STATES[1]:
|
|
219
|
+ self.store_data(**{self.KEY_OUTPUT_0: 'off'})
|
|
220
|
+ else:
|
|
221
|
+ self.store_data(**{self.KEY_OUTPUT_0: "off" if self.data.get(self.KEY_OUTPUT_0) == "on" else "on"})
|
|
222
|
+
|
|
223
|
+ def command(self, command):
|
|
224
|
+ if command in self.COMMANDS:
|
|
225
|
+ if command == self.COMMANDS[0]:
|
|
226
|
+ self.print_formatted(self, self.KEY_OUTPUT_0, self.data.get(self.KEY_OUTPUT_0))
|
|
227
|
+ elif command == self.COMMANDS[1]:
|
|
228
|
+ self.store_data(**{self.KEY_OUTPUT_0: 'on'})
|
|
229
|
+ elif command == self.COMMANDS[2]:
|
|
230
|
+ self.store_data(**{self.KEY_OUTPUT_0: 'off'})
|
|
231
|
+ else:
|
|
232
|
+ print("%s: not yet implemented!" % command)
|
|
233
|
+ else:
|
|
234
|
+ print("Unknown command!")
|
|
235
|
+
|
|
236
|
+ def print_formatted(self, device, key, value):
|
|
237
|
+ if value is not None:
|
|
238
|
+ if value == "on":
|
|
239
|
+ print(COLOR_SHELLY_1 + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
240
|
+ else:
|
|
241
|
+ print(COLOR_SHELLY_1 + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+class silvercrest_motion_sensor(base):
|
|
245
|
+ KEY_OCCUPANCY = "occupancy"
|
|
246
|
+ COMMANDS = ['motion']
|
|
247
|
+
|
|
248
|
+ def __init__(self, mqtt_client, topic):
|
|
249
|
+ super().__init__(mqtt_client, topic)
|
|
250
|
+ self.data[self.KEY_OCCUPANCY] = False
|
|
251
|
+ self.add_callback(self.KEY_OCCUPANCY, self.print_formatted, None)
|
|
252
|
+
|
|
253
|
+ def __rx__(self, client, userdata, message):
|
|
254
|
+ pass
|
|
255
|
+
|
|
256
|
+ def command(self, command):
|
|
257
|
+ try:
|
|
258
|
+ command, value = command.split(' ')
|
|
259
|
+ except ValueError:
|
|
260
|
+ value = None
|
|
261
|
+ else:
|
|
262
|
+ value = json.loads(value)
|
|
263
|
+ if command == self.COMMANDS[0]:
|
|
264
|
+ self.store_data(**{self.KEY_OCCUPANCY: True})
|
|
265
|
+ time.sleep(value or 10)
|
|
266
|
+ self.store_data(**{self.KEY_OCCUPANCY: False})
|
|
267
|
+
|
|
268
|
+ def print_formatted(self, device, key, value):
|
|
269
|
+ if value is not None:
|
|
270
|
+ if value:
|
|
271
|
+ print(COLOR_MOTION_SENSOR + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
272
|
+ else:
|
|
273
|
+ print(COLOR_MOTION_SENSOR + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+class tradfri_light(base):
|
|
277
|
+ KEY_STATE = "state"
|
|
278
|
+ KEY_BRIGHTNESS = "brightness"
|
|
279
|
+ KEY_COLOR_TEMP = "color_temp"
|
|
280
|
+ KEY_BRIGHTNESS_MOVE = "brightness_move"
|
|
281
|
+ #
|
|
282
|
+ STATE_COMMANDS = ("get_state", "change_state", )
|
|
283
|
+ BRIGHTNESS_COMMANDS = ("get_brightness", "set_brightness",)
|
|
284
|
+ COLOR_TEMP_COMMANDS = ("get_color_temp", "set_color_temp",)
|
|
285
|
+
|
|
286
|
+ def __init__(self, mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False, send_on_power_on=True):
|
|
287
|
+ super().__init__(mqtt_client, topic)
|
|
288
|
+ self.send_on_power_on = send_on_power_on
|
|
289
|
+ self.add_callback(self.KEY_STATE, self.print_formatted, None)
|
|
290
|
+ self.add_callback(self.KEY_BRIGHTNESS, self.print_formatted, None)
|
|
291
|
+ self.add_callback(self.KEY_COLOR_TEMP, self.print_formatted, None)
|
|
292
|
+ #
|
|
293
|
+ self.commands = []
|
|
294
|
+ if enable_state:
|
|
295
|
+ self.commands.extend(self.STATE_COMMANDS)
|
|
296
|
+ if enable_brightness:
|
|
297
|
+ self.commands.extend(self.BRIGHTNESS_COMMANDS)
|
|
298
|
+ if enable_color_temp:
|
|
299
|
+ self.commands.extend(self.COLOR_TEMP_COMMANDS)
|
|
300
|
+ self.__init_data__(enable_state, enable_brightness, enable_color_temp)
|
|
301
|
+
|
|
302
|
+ def __init_data__(self, enable_state, enable_brightness, enable_color_temp):
|
|
303
|
+ data = {}
|
|
304
|
+ if enable_state:
|
|
305
|
+ data[self.KEY_STATE] = 'off'
|
|
306
|
+ self.commands.extend(self.STATE_COMMANDS)
|
|
307
|
+ if enable_brightness:
|
|
308
|
+ data[self.KEY_BRIGHTNESS] = 128
|
|
309
|
+ self.brightnes_move = (0, time.time())
|
|
310
|
+ self.commands.extend(self.BRIGHTNESS_COMMANDS)
|
|
311
|
+ if enable_color_temp:
|
|
312
|
+ data[self.KEY_COLOR_TEMP] = 352
|
|
313
|
+ self.commands.extend(self.COLOR_TEMP_COMMANDS)
|
|
314
|
+ self.store_data(**data)
|
|
315
|
+
|
|
316
|
+ def __rx__(self, client, userdata, message):
|
|
317
|
+ data = json.loads(message.payload)
|
|
318
|
+ if self.data.get(self.KEY_STATE) == 'on' or data.get(self.KEY_STATE) in ['on', 'toggle']:
|
|
319
|
+ if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
|
320
|
+ for targetkey in data:
|
|
321
|
+ value = data[targetkey]
|
|
322
|
+ if targetkey in self.data.keys():
|
|
323
|
+ if targetkey == self.KEY_STATE and value == "toggle":
|
|
324
|
+ value = "on" if self.data.get(self.KEY_STATE) == "off" else "off"
|
|
325
|
+ self.store_data(**{targetkey: value})
|
|
326
|
+ else:
|
|
327
|
+ if targetkey == self.KEY_BRIGHTNESS_MOVE:
|
|
328
|
+ new_value = self.data.get(self.KEY_BRIGHTNESS) + (time.time() - self.brightnes_move[1]) * self.brightnes_move[0]
|
|
329
|
+ if new_value < 0:
|
|
330
|
+ new_value = 0
|
|
331
|
+ if new_value > 255:
|
|
332
|
+ new_value = 255
|
|
333
|
+ self.store_data(**{self.KEY_BRIGHTNESS: int(new_value)})
|
|
334
|
+ self.brightnes_move = (value, time.time())
|
|
335
|
+ else:
|
|
336
|
+ print("%s: UNKNOWN KEY %s" % (message.topic, targetkey))
|
|
337
|
+ elif message.topic == self.topic + '/get':
|
|
338
|
+ self.__tx__(None)
|
|
339
|
+
|
|
340
|
+ def command(self, command):
|
|
341
|
+ try:
|
|
342
|
+ command, value = command.split(' ')
|
|
343
|
+ except ValueError:
|
|
344
|
+ value = None
|
|
345
|
+ if command in self.capabilities():
|
|
346
|
+ if command == self.STATE_COMMANDS[0]:
|
|
347
|
+ self.print_formatted(self, self.KEY_STATE, self.data.get(self.KEY_STATE))
|
|
348
|
+ elif command == self.STATE_COMMANDS[1]:
|
|
349
|
+ self.store_data(**{self.KEY_STATE: 'off' if self.data.get(self.KEY_STATE) == 'on' else 'on'})
|
|
350
|
+ elif command == self.BRIGHTNESS_COMMANDS[0]:
|
|
351
|
+ self.print_formatted(self, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
|
352
|
+ elif command == self.BRIGHTNESS_COMMANDS[1]:
|
|
353
|
+ self.store_data(**{self.KEY_BRIGHTNESS: command_int_value(value)})
|
|
354
|
+ elif command == self.COLOR_TEMP_COMMANDS[0]:
|
|
355
|
+ self.print_formatted(self, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
|
356
|
+ elif command == self.COLOR_TEMP_COMMANDS[1]:
|
|
357
|
+ self.store_data(**{self.KEY_COLOR_TEMP: command_int_value(value)})
|
|
358
|
+ else:
|
|
359
|
+ print("%s: not yet implemented!" % command)
|
|
360
|
+ else:
|
|
361
|
+ print("Unknown command!")
|
|
362
|
+
|
|
363
|
+ def power_off(self, device, key, value):
|
|
364
|
+ self.data[self.KEY_STATE] = 'off'
|
|
365
|
+ self.print_formatted(self, self.KEY_STATE, 'off')
|
|
366
|
+
|
|
367
|
+ def power_on(self, device, key, value):
|
|
368
|
+ if self.send_on_power_on:
|
|
369
|
+ self.store_data(**{self.KEY_STATE: 'on'})
|
|
370
|
+ else:
|
|
371
|
+ self.data[self.KEY_STATE] = 'on'
|
|
372
|
+ self.print_formatted(self, self.KEY_STATE, 'on')
|
|
373
|
+
|
|
374
|
+ def print_formatted(self, device, key, value):
|
|
375
|
+ if value is not None:
|
|
376
|
+ color = COLOR_LIGHT_ACTIVE
|
|
377
|
+ if key == self.KEY_STATE:
|
|
378
|
+ if value == "on":
|
|
379
|
+ print(color + 10 * ' ' + u'\u2b24' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
380
|
+ else:
|
|
381
|
+ print(color + 10 * ' ' + u'\u25ef' + 6 * ' ' + " - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
382
|
+ self.print_formatted(device, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
|
383
|
+ self.print_formatted(device, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
|
384
|
+ elif key in [self.KEY_BRIGHTNESS, self.KEY_COLOR_TEMP]:
|
|
385
|
+ if self.data.get(self.KEY_STATE) != "on":
|
|
386
|
+ color = COLOR_LIGHT_PASSIVE
|
|
387
|
+ if key == self.KEY_BRIGHTNESS:
|
|
388
|
+ value = round(value * 100 / 256, 0)
|
|
389
|
+ else:
|
|
390
|
+ value = round((value - 250) * 100 / 204, 0)
|
|
391
|
+ sys.stdout.write(color)
|
|
392
|
+ sys.stdout.write('B' if key == gui_light.KEY_BRIGHTNESS else 'C')
|
|
393
|
+ percent_print(value)
|
|
394
|
+ sys.stdout.write("%3d%% " % value)
|
|
395
|
+ print(" - ".join(self.topic.split('/')[1:]) + colored.attr("reset"))
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+class gui_light(tradfri_light):
|
|
399
|
+ AUTOSEND = False
|
|
400
|
+ #
|
|
401
|
+ KEY_ENABLE = "enable"
|
|
402
|
+
|
|
403
|
+ def __init__(self, mqtt_client, topic, enable_state=True, enable_brightness=False, enable_color_temp=False):
|
|
404
|
+ super().__init__(mqtt_client, topic, enable_state, enable_brightness, enable_color_temp)
|
|
405
|
+ self.add_callback(self.KEY_ENABLE, self.print_formatted, None)
|
|
406
|
+
|
|
407
|
+ def __init_data__(self, enable_state, enable_brightness, enable_color_temp):
|
|
408
|
+ data = {}
|
|
409
|
+ data[self.KEY_ENABLE] = False
|
|
410
|
+ if enable_state:
|
|
411
|
+ data[self.KEY_STATE] = False
|
|
412
|
+ if enable_brightness:
|
|
413
|
+ data[self.KEY_BRIGHTNESS] = 50
|
|
414
|
+ if enable_color_temp:
|
|
415
|
+ data[self.KEY_COLOR_TEMP] = 5
|
|
416
|
+ self.store_data(**data)
|
|
417
|
+
|
|
418
|
+ def __rx__(self, client, userdata, message):
|
|
419
|
+ value = payload_filter(message.payload)
|
|
420
|
+ if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
|
421
|
+ targetkey = message.topic.split('/')[-2]
|
|
422
|
+ if targetkey in self.data.keys():
|
|
423
|
+ self.store_data(**{targetkey: value})
|
|
424
|
+ else:
|
|
425
|
+ print("Unknown key %s in %s" % (targetkey, self.__class__.__name__))
|
|
426
|
+ elif message.topic == self.topic + '/get':
|
|
427
|
+ self.__tx__(None)
|
|
428
|
+
|
|
429
|
+ def send(self, key, data):
|
|
430
|
+ if data is not None:
|
|
431
|
+ topic = self.topic + '/' + key
|
|
432
|
+ self.mqtt_client.send(topic, json.dumps(data))
|
|
433
|
+
|
|
434
|
+ def command(self, command):
|
|
435
|
+ try:
|
|
436
|
+ command, value = command.split(' ')
|
|
437
|
+ except ValueError:
|
|
438
|
+ value = None
|
|
439
|
+ if command in self.capabilities():
|
|
440
|
+ if command == self.STATE_COMMANDS[0]:
|
|
441
|
+ self.print_formatted(self, self.KEY_STATE, self.data.get(self.KEY_STATE))
|
|
442
|
+ elif command == self.STATE_COMMANDS[1]:
|
|
443
|
+ self.send(self.KEY_STATE, not self.data.get(self.KEY_STATE))
|
|
444
|
+ elif command == self.BRIGHTNESS_COMMANDS[0]:
|
|
445
|
+ self.print_formatted(self, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
|
446
|
+ elif command == self.BRIGHTNESS_COMMANDS[1]:
|
|
447
|
+ self.send(self.KEY_BRIGHTNESS, command_int_value(value))
|
|
448
|
+ elif command == self.COLOR_TEMP_COMMANDS[0]:
|
|
449
|
+ self.print_formatted(self, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
|
450
|
+ elif command == self.COLOR_TEMP_COMMANDS[1]:
|
|
451
|
+ self.send(self.KEY_COLOR_TEMP, command_int_value(value))
|
|
452
|
+ else:
|
|
453
|
+ print("%s: not yet implemented!" % command)
|
|
454
|
+ else:
|
|
455
|
+ print("Unknown command!")
|
|
456
|
+
|
|
457
|
+ def print_formatted(self, device, key, value):
|
|
458
|
+ if value is not None:
|
|
459
|
+ color = COLOR_GUI_ACTIVE
|
|
460
|
+ if key == self.KEY_STATE:
|
|
461
|
+ if value == True:
|
|
462
|
+ print(color + 10 * ' ' + u'\u25a0' + 6 * ' ' + " - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
|
|
463
|
+ else:
|
|
464
|
+ print(color + 10 * ' ' + u'\u25a1' + 6*' ' + " - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
|
|
465
|
+ elif key == self.KEY_ENABLE:
|
|
466
|
+ self.print_formatted(device, self.KEY_BRIGHTNESS, self.data.get(self.KEY_BRIGHTNESS))
|
|
467
|
+ self.print_formatted(device, self.KEY_COLOR_TEMP, self.data.get(self.KEY_COLOR_TEMP))
|
|
468
|
+ elif key in [self.KEY_BRIGHTNESS, self.KEY_COLOR_TEMP]:
|
|
469
|
+ if not self.data.get(self.KEY_ENABLE, False):
|
|
470
|
+ color = COLOR_GUI_PASSIVE
|
|
471
|
+ value = round(value * 10 if key == self.KEY_COLOR_TEMP else value, 0)
|
|
472
|
+ sys.stdout.write(color)
|
|
473
|
+ sys.stdout.write('B' if key == self.KEY_BRIGHTNESS else 'C')
|
|
474
|
+ percent_print(value)
|
|
475
|
+ sys.stdout.write("%3d%% " % value)
|
|
476
|
+ print(" - ".join(self.topic.split('/')[1:-1]) + colored.attr("reset"))
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+class tradfri_button(base):
|
|
480
|
+ KEY_ACTION = "action"
|
|
481
|
+ #
|
|
482
|
+ ACTION_TOGGLE = "toggle"
|
|
483
|
+ ACTION_BRIGHTNESS_UP = "brightness_up_click"
|
|
484
|
+ ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
|
|
485
|
+ ACTION_RIGHT = "arrow_right_click"
|
|
486
|
+ ACTION_LEFT = "arrow_left_click"
|
|
487
|
+ ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
|
|
488
|
+ ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
|
|
489
|
+ ACTION_RIGHT_LONG = "arrow_right_hold"
|
|
490
|
+ ACTION_LEFT_LONG = "arrow_left_hold"
|
|
491
|
+ #
|
|
492
|
+ COMMANDS = [ACTION_TOGGLE, ACTION_LEFT, ACTION_RIGHT, ACTION_BRIGHTNESS_UP, ACTION_BRIGHTNESS_DOWN,
|
|
493
|
+ ACTION_LEFT_LONG, ACTION_RIGHT_LONG, ACTION_BRIGHTNESS_UP_LONG, ACTION_BRIGHTNESS_DOWN_LONG]
|
|
494
|
+
|
|
495
|
+ def __init__(self, mqtt_client, topic):
|
|
496
|
+ super().__init__(mqtt_client, topic)
|
|
497
|
+
|
|
498
|
+ def __rx__(self, client, userdata, message):
|
|
499
|
+ pass
|
|
500
|
+
|
|
501
|
+ def command(self, command):
|
|
502
|
+ try:
|
|
503
|
+ command, value = command.split(' ')
|
|
504
|
+ except ValueError:
|
|
505
|
+ value = None
|
|
506
|
+ else:
|
|
507
|
+ value = json.loads(value)
|
|
508
|
+ if command in self.capabilities():
|
|
509
|
+ action = self.COMMANDS[self.COMMANDS.index(command)]
|
|
510
|
+ if self.COMMANDS.index(command) <= 4:
|
|
511
|
+ self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
|
512
|
+ elif self.COMMANDS.index(command) <= 8:
|
|
513
|
+ self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
|
514
|
+ time.sleep(value or 0.5)
|
|
515
|
+ action = '_'.join(action.split('_')[:-1] + ['release'])
|
|
516
|
+ self.mqtt_client.send(self.topic, json.dumps({self.KEY_ACTION: action}))
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+class gui_led_array(base):
|
|
520
|
+ AUTOSEND = False
|
|
521
|
+ #
|
|
522
|
+ KEY_LED_0 = "led0"
|
|
523
|
+ KEY_LED_1 = "led1"
|
|
524
|
+ KEY_LED_2 = "led2"
|
|
525
|
+ KEY_LED_3 = "led3"
|
|
526
|
+ KEY_LED_4 = "led4"
|
|
527
|
+ KEY_LED_5 = "led5"
|
|
528
|
+ KEY_LED_6 = "led6"
|
|
529
|
+ KEY_LED_7 = "led7"
|
|
530
|
+ KEY_LED_8 = "led8"
|
|
531
|
+ KEY_LED_9 = "led9"
|
|
532
|
+
|
|
533
|
+ def __init__(self, mqtt_client, topic, names=[]):
|
|
534
|
+ super().__init__(mqtt_client, topic)
|
|
535
|
+ self.names = {}
|
|
536
|
+ for i in range(0, 10):
|
|
537
|
+ key = getattr(self, "KEY_LED_%d" % i)
|
|
538
|
+ self.data[key] = False
|
|
539
|
+ try:
|
|
540
|
+ self.names[key] = names[i]
|
|
541
|
+ except IndexError:
|
|
542
|
+ self.names[key] = key
|
|
543
|
+ self.add_callback(key, self.print_formatted, None)
|
|
544
|
+
|
|
545
|
+ def __rx__(self, client, userdata, message):
|
|
546
|
+ value = payload_filter(message.payload)
|
|
547
|
+ if message.topic.startswith(self.topic) and message.topic.endswith('/set'):
|
|
548
|
+ targetkey = message.topic.split('/')[-2]
|
|
549
|
+ if targetkey in self.data.keys():
|
|
550
|
+ self.store_data(**{targetkey: value})
|
|
551
|
+ else:
|
|
552
|
+ print("Unknown key %s in %s" % (targetkey, self.__class__.__name__))
|
|
553
|
+
|
|
554
|
+ def print_formatted(self, device, key, value):
|
|
555
|
+ if value:
|
|
556
|
+ color = colored.fg('green')
|
|
557
|
+ else:
|
|
558
|
+ color = ""
|
|
559
|
+ print(color + 10 * ' ' + u"\u2b24" + 6 * ' ' + COLOR_GUI_ACTIVE +
|
|
560
|
+ ' - '.join(self.topic.split('/')[:-1]) + ' (%s)' % self.names[key] + colored.attr("reset"))
|