Kaynağa Gözat

First house simulation implemented

tags/v1.0.0
Dirk Alders 1 yıl önce
ebeveyn
işleme
d2db60ac5b

+ 0
- 0
__simulation__/__init__.py Dosyayı Görüntüle


+ 560
- 0
__simulation__/devices.py Dosyayı Görüntüle

@@ -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"))

+ 189
- 0
__simulation__/rooms.py Dosyayı Görüntüle

@@ -0,0 +1,189 @@
1
+import config
2
+from __simulation__.devices import shelly, silvercrest_powerplug, gui_light, tradfri_light, tradfri_button, gui_led_array, silvercrest_motion_sensor, my_powerplug
3
+import inspect
4
+
5
+# TODO: ffe_floor:      Implement longpress shelly
6
+# TODO: gfw_dirk:       Implement 2nd input for floor light
7
+# TODO: ffe_sleep:      Implement heating valve
8
+# TODO: gfw_dirk:       Add at least brightness amplifier remote feedback to console
9
+# TODO: ffe_diningroom: Implement garland gui_switch
10
+# TODO: ffe_kitchen:    Implement circulation pump
11
+
12
+
13
+class base(object):
14
+    def getmembers(self, prefix=''):
15
+        rv = []
16
+        for name, obj in inspect.getmembers(self):
17
+            if prefix:
18
+                full_name = prefix + '.' + name
19
+            else:
20
+                full_name = name
21
+            if not name.startswith('_'):
22
+                try:
23
+                    if obj.__module__.endswith('devices'):
24
+                        rv.append(full_name)
25
+                    else:
26
+                        rv.extend(obj.getmembers(full_name))
27
+                except AttributeError:
28
+                    pass
29
+        return rv
30
+
31
+    def getobjbyname(self, name):
32
+        obj = self
33
+        for subname in name.split('.'):
34
+            obj = getattr(obj, subname)
35
+        return obj
36
+
37
+    def command(self, full_command):
38
+        try:
39
+            parameter = " " + full_command.split(' ')[1]
40
+        except IndexError:
41
+            parameter = ""
42
+        command = full_command.split(' ')[0].split('.')[-1] + parameter
43
+        device_name = '.'.join(full_command.split(' ')[0].split('.')[:-1])
44
+        self.getobjbyname(device_name).command(command)
45
+
46
+
47
+class gfw_floor(base):
48
+    def __init__(self, mqtt_client):
49
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI_SWITCH, True, False, False)
50
+        self.main_light = shelly(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_SHELLY)
51
+        self.main_light_zigbee_1 = tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_1_ZIGBEE, True, True, True, False)
52
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_1.power_on, "on")
53
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_1.power_off, "off")
54
+        self.main_light_zigbee_2 = tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_2_ZIGBEE, True, True, True, False)
55
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_2.power_on, "on")
56
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee_1.power_off, "off")
57
+        self.gui_br_ct_main_light = gui_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_GUI_BR_CT, False, True, True)
58
+
59
+
60
+class gfw_marion(base):
61
+    def __init__(self, mqtt_client):
62
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_GUI_SWITCH, True, False, False)
63
+        self.main_light = shelly(mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_SHELLY)
64
+
65
+
66
+class gfw_dirk(base):
67
+    def __init__(self, mqtt_client):
68
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_GUI_SWITCH, True, False, False)
69
+        self.main_light = shelly(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_SHELLY)
70
+        self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_ZIGBEE, True, True, True)
71
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
72
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
73
+        self.gui_br_ct_main_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_GUI_BR_CT, False, True, True)
74
+        #
75
+        self.powerplug = my_powerplug(mqtt_client, config.TOPIC_GFW_DIRK_POWERPLUG, ["Amplifier", "Desk Light", "CD-Player", "PC-Dock"])
76
+        self.gui_switch_amplifier = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_GUI_SWITCH, True, False, False)
77
+        self.gui_switch_desk_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_GUI_SWITCH, True, False, False)
78
+        self.gui_br_ct_desk_light = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_GUI_BR_CT, False, True, True)
79
+        self.gui_switch_cd_player = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_GUI_SWITCH, True, False, False)
80
+        self.gui_switch_pc_dock = gui_light(mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_GUI_SWITCH, True, False, False)
81
+        #
82
+        self.input_device = tradfri_button(mqtt_client, config.TOPIC_GFW_DIRK_INPUT_DEVICE)
83
+        self.led_array = gui_led_array(mqtt_client, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_LED, ["Main Light", "Desk Light", "Amplifier"])
84
+
85
+
86
+class gfw(base):
87
+    def __init__(self, mqtt_client):
88
+        self.floor = gfw_floor(mqtt_client)
89
+        self.marion = gfw_marion(mqtt_client)
90
+        self.dirk = gfw_dirk(mqtt_client)
91
+
92
+
93
+class ffw_julian(base):
94
+    def __init__(self, mqtt_client):
95
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_GUI_SWITCH, True, False, False)
96
+        self.main_light = shelly(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_SHELLY)
97
+        self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_ZIGBEE, True, True, True)
98
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
99
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
100
+        self.gui_br_ct_main_light = gui_light(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_GUI_BR_CT, False, True, True)
101
+
102
+
103
+class ffw_livingroom(base):
104
+    def __init__(self, mqtt_client):
105
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_GUI_SWITCH, True, False, False)
106
+        self.main_light = shelly(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_SHELLY)
107
+
108
+
109
+class ffw(base):
110
+    def __init__(self, mqtt_client):
111
+        self.julian = ffw_julian(mqtt_client)
112
+        self.livingroom = ffw_livingroom(mqtt_client)
113
+
114
+
115
+class ffe_floor(base):
116
+    def __init__(self, mqtt_client):
117
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_GUI_SWITCH, True, False, False)
118
+        self.main_light = shelly(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_SHELLY)
119
+
120
+
121
+class ffe_kitchen(base):
122
+    def __init__(self, mqtt_client):
123
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_GUI_SWITCH, True, False, False)
124
+        self.main_light = shelly(mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_SHELLY)
125
+
126
+
127
+class ffe_diningroom(base):
128
+    def __init__(self, mqtt_client):
129
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_GUI_SWITCH, True, False, False)
130
+        self.main_light = shelly(mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_SHELLY)
131
+        self.gui_switch_floor_lamp = gui_light(mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_GUI_SWITCH, True, False, False)
132
+        self.floor_lamp = silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_POWERPLUG)
133
+        self.garland = silvercrest_powerplug(mqtt_client, config.TOPIC_FFE_DININGROOM_GARLAND_POWERPLUG)
134
+
135
+
136
+class ffe_sleep(base):
137
+    def __init__(self, mqtt_client):
138
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_GUI_SWITCH, True, False, False)
139
+        self.main_light = shelly(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_SHELLY)
140
+        self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_ZIGBEE, True, True, True)
141
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
142
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
143
+        self.gui_br_ct_main_light = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_GUI_BR_CT, False, True, True)
144
+        #
145
+        self.gui_switch_bed_light_di = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_GUI_SWITCH, True, False, False)
146
+        self.bed_light_di_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_ZIGBEE, True, True, False)
147
+        self.gui_br_ct_bed_light_di = gui_light(mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_GUI_BR_CT, False, True, False)
148
+        #
149
+        self.input_device = tradfri_button(mqtt_client, config.TOPIC_FFE_SLEEP_INPUT_DEVICE)
150
+        self.led_array = gui_led_array(mqtt_client, config.TOPIC_FFE_SLEEP_DEVICE_CHOOSER_LED, ["Main Light", "Bed Light Dirk"])
151
+
152
+
153
+class ffe_livingroom(base):
154
+    def __init__(self, mqtt_client):
155
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_GUI_SWITCH, True, False, False)
156
+        self.main_light = shelly(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_SHELLY)
157
+        self.main_light_zigbee = tradfri_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_ZIGBEE, True, True, True)
158
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_on, "on")
159
+        self.main_light.add_callback(shelly.KEY_OUTPUT_0, self.main_light_zigbee.power_off, "off")
160
+        self.gui_br_ct_main_light = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_GUI_BR_CT, False, True, True)
161
+        for i in range(1, 7):
162
+            setattr(self, "floor_lamp_%d" % i, tradfri_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_ZIGBEE % i, True, True, True))
163
+        self.gui_switch_floor_lamp = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_GUI_SWITCH, True, False, False)
164
+        self.gui_br_ct_floor_lamp = gui_light(mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_GUI_BR_CT, False, True, True)
165
+
166
+
167
+class ffe(base):
168
+    def __init__(self, mqtt_client):
169
+        self.floor = ffe_floor(mqtt_client)
170
+        self.kitchen = ffe_kitchen(mqtt_client)
171
+        self.diningroom = ffe_diningroom(mqtt_client)
172
+        self.sleep = ffe_sleep(mqtt_client)
173
+        self.livingroom = ffe_livingroom(mqtt_client)
174
+
175
+
176
+class stairway(base):
177
+    def __init__(self, mqtt_client):
178
+        self.gui_switch_main_light = gui_light(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_GUI_SWITCH, True, False, False)
179
+        self.main_light = shelly(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY)
180
+        self.motion_sensor_gf = silvercrest_motion_sensor(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_GF)
181
+        self.motion_sensor_ff = silvercrest_motion_sensor(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_FF)
182
+
183
+
184
+class house(base):
185
+    def __init__(self, mqtt_client):
186
+        self.gfw = gfw(mqtt_client)
187
+        self.ffw = ffw(mqtt_client)
188
+        self.ffe = ffe(mqtt_client)
189
+        self.stairway = stairway(mqtt_client)

+ 70
- 0
house_n_gui_sim.py Dosyayı Görüntüle

@@ -0,0 +1,70 @@
1
+import config
2
+import logging
3
+import mqtt
4
+import readline
5
+import report
6
+from __simulation__.rooms import house
7
+import time
8
+
9
+if __name__ == "__main__":
10
+    report.stdoutLoggingConfigure(((config.APP_NAME, logging.WARNING), ), report.SHORT_FMT)
11
+    #
12
+    mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
13
+                          password=config.MQTT_PASSWORD, name=config.APP_NAME + '_simulation')
14
+    #
15
+    COMMANDS = ['quit', 'help']
16
+    #
17
+    h = house(mc)
18
+    for name in h.getmembers():
19
+        d = h.getobjbyname(name)
20
+        for c in d.capabilities():
21
+            COMMANDS.append(name + '.' + c)
22
+    #
23
+
24
+    def reduced_list(text):
25
+        """
26
+        Create reduced completation list
27
+        """
28
+        reduced_list = {}
29
+        for cmd in COMMANDS:
30
+            next_dot = cmd[len(text):].find('.')
31
+            if next_dot >= 0:
32
+                reduced_list[cmd[:len(text) + next_dot + 1]] = None
33
+            else:
34
+                reduced_list[cmd] = None
35
+        return reduced_list.keys()
36
+
37
+    def completer(text, state):
38
+        """
39
+        Our custom completer function
40
+        """
41
+        options = [x for x in reduced_list(text) if x.startswith(text)]
42
+        return options[state]
43
+
44
+    def complete(text, state):
45
+        for cmd in COMMANDS:
46
+            if cmd.startswith(text):
47
+                if not state:
48
+                    hit = ""
49
+                    index = 0
50
+                    sub_list = cmd.split('.')
51
+                    while len(text) >= len(hit):
52
+                        hit += sub_list[index] + '.'
53
+                    return hit  # cmd
54
+                else:
55
+                    state -= 1
56
+
57
+    readline.parse_and_bind("tab: complete")
58
+    readline.set_completer(completer)
59
+    print("\nEnter command: ")
60
+    while True:
61
+        userfeedback = input('')
62
+        command = userfeedback.split(' ')[0]
63
+        if userfeedback == 'quit':
64
+            break
65
+        elif userfeedback == 'help':
66
+            print("Help is not yet implemented!")
67
+        elif command in COMMANDS[2:]:
68
+            h.command(userfeedback)
69
+        else:
70
+            print("Unknown command!")

Loading…
İptal
Kaydet