Browse Source

videv implementation for lights

tags/v1.0.0
Dirk Alders 1 year ago
parent
commit
9233468a49

+ 45
- 0
base.py View File

1
+import logging
2
+
3
+try:
4
+    from config import APP_NAME as ROOT_LOGGER_NAME
5
+except ImportError:
6
+    ROOT_LOGGER_NAME = 'root'
7
+
8
+
9
+class common_base(dict):
10
+    DEFAULT_VALUES = {}
11
+
12
+    def __init__(self):
13
+        super().__init__(self.DEFAULT_VALUES)
14
+        self['__type__'] = self.__class__.__name__
15
+        #
16
+        self.__callback_list__ = []
17
+        self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
18
+
19
+    def add_callback(self, key, data, callback, on_change_only=True):
20
+        """
21
+        key: key or None for all keys
22
+        data: data or None for all data
23
+        """
24
+        cb_tup = (key, data, callback, on_change_only)
25
+        if cb_tup not in self.__callback_list__:
26
+            self.__callback_list__.append(cb_tup)
27
+
28
+    def set(self, key, data):
29
+        value_changed = self[key] != data
30
+        self[key] = data
31
+        for cb_key, cb_data, callback, on_change_only in self.__callback_list__:
32
+            if cb_key is None or key == cb_key:                 # key fits to callback definition
33
+                if cb_data is None or cb_data == self[key]:     # data fits to callback definition
34
+                    if value_changed or not on_change_only:     # change status fits to callback definition
35
+                        callback(self, key, self[key])
36
+
37
+
38
+class mqtt_base(common_base):
39
+    def __init__(self, mqtt_client, topic):
40
+        super().__init__()
41
+        #
42
+        self.mqtt_client = mqtt_client
43
+        self.topic = topic
44
+        for entry in self.topic.split('/'):
45
+            self.logger = self.logger.getChild(entry)

+ 5
- 0
devices/__init__.py View File

26
 
26
 
27
 """
27
 """
28
 
28
 
29
+# TODO: Usage of mqtt_base for all devices
30
+
29
 __DEPENDENCIES__ = []
31
 __DEPENDENCIES__ = []
30
 
32
 
31
 import json
33
 import json
506
         else:
508
         else:
507
             return super().pack_filter(key, data)
509
             return super().pack_filter(key, data)
508
 
510
 
511
+    def request_data(self):
512
+        self.mqtt_client.send(self.topic + "/get", '{%s: ""}' % self.KEY_OUTPUT_0)
513
+
509
     #
514
     #
510
     # RX
515
     # RX
511
     #
516
     #

+ 58
- 0
function/first_floor_east.py View File

7
 from function.modules import brightness_choose_n_action, circulation_pump, radiator_function
7
 from function.modules import brightness_choose_n_action, circulation_pump, radiator_function
8
 import logging
8
 import logging
9
 from function.rooms import room_shelly, room_shelly_motion_sensor, room_shelly_tradfri_light
9
 from function.rooms import room_shelly, room_shelly_motion_sensor, room_shelly_tradfri_light
10
+from function.videv import videv_switching, videv_switch_brightness, videv_switch_brightness_color_temp
10
 try:
11
 try:
11
     from config import APP_NAME as ROOT_LOGGER_NAME
12
     from config import APP_NAME as ROOT_LOGGER_NAME
12
 except ImportError:
13
 except ImportError:
18
     def __init__(self, mqtt_client):
19
     def __init__(self, mqtt_client):
19
         # http://shelly1l-3C6105E4E629
20
         # http://shelly1l-3C6105E4E629
20
         super().__init__(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_SHELLY, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_GUI)
21
         super().__init__(mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_SHELLY, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_GUI)
22
+        #
23
+        self.main_light = videv_switching(
24
+            mqtt_client, config.TOPIC_FFE_FLOOR_MAIN_LIGHT_VIDEV,
25
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
26
+        )
21
 
27
 
22
 
28
 
23
 class first_floor_east_kitchen(room_shelly):
29
 class first_floor_east_kitchen(room_shelly):
27
         #
33
         #
28
         self.circulation_pump = circulation_pump(mqtt_client)
34
         self.circulation_pump = circulation_pump(mqtt_client)
29
         self.circulation_pump.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.flash_main_light)
35
         self.circulation_pump.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.flash_main_light)
36
+        #
37
+        self.main_light_videv = videv_switching(
38
+            mqtt_client, config.TOPIC_FFE_KITCHEN_MAIN_LIGHT_VIDEV,
39
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
40
+        )
41
+        self.circulation_pump_videv = videv_switching(
42
+            mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_VIDEV,
43
+            self.circulation_pump.main_light_shelly, devices.shelly.KEY_OUTPUT_0
44
+        )
30
 
45
 
31
     def all_off(self, device=None, key=None, data=None):
46
     def all_off(self, device=None, key=None, data=None):
32
         self.circulation_pump.all_off(device, key, data)
47
         self.circulation_pump.all_off(device, key, data)
48
         self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_powerplug.set_output_0_mcb, True)
63
         self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_powerplug.set_output_0_mcb, True)
49
         self.gui_floorlamp.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.floorlamp_powerplug.set_output_0_mcb)
64
         self.gui_floorlamp.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.floorlamp_powerplug.set_output_0_mcb)
50
         self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_floorlamp.set_state_mcb)
65
         self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_floorlamp.set_state_mcb)
66
+        #
67
+        self.main_light_videv = videv_switching(
68
+            mqtt_client, config.TOPIC_FFE_DININGROOM_MAIN_LIGHT_VIDEV,
69
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
70
+        )
71
+        self.floorlamp_videv = videv_switching(
72
+            mqtt_client, config.TOPIC_FFE_DININGROOM_FLOOR_LAMP_VIDEV,
73
+            self.floorlamp_powerplug, devices.silvercrest_powerplug.KEY_OUTPUT_0
74
+        )
51
 
75
 
52
     def all_off(self, device=None, key=None, data=None):
76
     def all_off(self, device=None, key=None, data=None):
53
         super().all_off(device, key, data)
77
         super().all_off(device, key, data)
96
         self.gui_bed_light_di.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, self.bed_light_di_tradfri.set_brightness_mcb)
120
         self.gui_bed_light_di.add_callback(devices.nodered_gui_light.KEY_BRIGHTNESS, None, self.bed_light_di_tradfri.set_brightness_mcb)
97
         self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_bed_light_di.set_enable_mcb)
121
         self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_bed_light_di.set_enable_mcb)
98
         self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_bed_light_di.set_brightness_mcb)
122
         self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_bed_light_di.set_brightness_mcb)
123
+        #
124
+        self.main_light_videv = videv_switch_brightness_color_temp(
125
+            mqtt_client, config.TOPIC_FFE_SLEEP_MAIN_LIGHT_VIDEV,
126
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
127
+            self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
128
+            self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
129
+        )
130
+        self.bed_light_di_videv = videv_switch_brightness(
131
+            mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_DI_VIDEV,
132
+            self.bed_light_di_tradfri, devices.tradfri_light.KEY_OUTPUT_0,
133
+            self.bed_light_di_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
134
+        )
135
+        self.bed_light_ma_videv = videv_switching(
136
+            mqtt_client, config.TOPIC_FFE_SLEEP_BED_LIGHT_MA_VIDEV,
137
+            self.bed_light_ma_powerplug, devices.silvercrest_powerplug.KEY_OUTPUT_0
138
+        )
99
 
139
 
100
     def all_off(self, device=None, key=None, data=None):
140
     def all_off(self, device=None, key=None, data=None):
101
         super().all_off(device, key, data)
141
         super().all_off(device, key, data)
139
             self.gui_xmas_tree.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_xmas_tree.set_output_0_mcb)
179
             self.gui_xmas_tree.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_xmas_tree.set_output_0_mcb)
140
             #
180
             #
141
             self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.powerplug_xmas_star.set_output_0_mcb)
181
             self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.powerplug_xmas_star.set_output_0_mcb)
182
+        #
183
+        self.main_light_videv = videv_switch_brightness_color_temp(
184
+            mqtt_client, config.TOPIC_FFE_LIVINGROOM_MAIN_LIGHT_VIDEV,
185
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
186
+            self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
187
+            self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
188
+        )
189
+        self.floorlamp_videv = videv_switch_brightness_color_temp(
190
+            mqtt_client, config.TOPIC_FFE_LIVINGROOM_FLOOR_LAMP_VIDEV,
191
+            self.floorlamp_tradfri_1, devices.tradfri_light.KEY_OUTPUT_0,
192
+            self.floorlamp_tradfri_1, devices.tradfri_light.KEY_BRIGHTNESS,
193
+            self.floorlamp_tradfri_1, devices.tradfri_light.KEY_COLOR_TEMP
194
+        )
195
+        if config.CHRISTMAS:
196
+            self.xmas_tree_videv = videv_switching(
197
+                mqtt_client, config.TOPIC_FFE_LIVINGROOM_XMAS_TREE_VIDEV,
198
+                self.powerplug_xmas_tree, devices.silvercrest_powerplug.KEY_OUTPUT_0
199
+            )
142
 
200
 
143
     def all_off(self, device=None, key=None, data=None):
201
     def all_off(self, device=None, key=None, data=None):
144
         super().all_off(device, key, data)
202
         super().all_off(device, key, data)

+ 23
- 0
function/first_floor_west.py View File

3
 #
3
 #
4
 
4
 
5
 import config
5
 import config
6
+import devices
6
 import logging
7
 import logging
7
 from function.modules import radiator_function
8
 from function.modules import radiator_function
8
 from function.rooms import room_shelly, room_shelly_tradfri_light
9
 from function.rooms import room_shelly, room_shelly_tradfri_light
10
+from function.videv import videv_switch_brightness, videv_switch_brightness_color_temp
9
 
11
 
10
 
12
 
11
 try:
13
 try:
19
     # http://shelly1l-3C6105E43452
21
     # http://shelly1l-3C6105E43452
20
     def __init__(self, mqtt_client):
22
     def __init__(self, mqtt_client):
21
         super().__init__(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_SHELLY, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_GUI, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_ZIGBEE)
23
         super().__init__(mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_SHELLY, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_GUI, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_ZIGBEE)
24
+        #
25
+        self.main_light_videv = videv_switch_brightness_color_temp(
26
+            mqtt_client, config.TOPIC_FFW_JULIAN_MAIN_LIGHT_VIDEV,
27
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
28
+            self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
29
+            self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
30
+        )
22
 
31
 
23
 
32
 
24
 class first_floor_west_bath(object):
33
 class first_floor_west_bath(object):
26
         # radiator valve
35
         # radiator valve
27
         self.radiator_function = radiator_function(mqtt_client, config.TOPIC_FFW_BATH_RADIATOR_VALVE_ZIGBEE,
36
         self.radiator_function = radiator_function(mqtt_client, config.TOPIC_FFW_BATH_RADIATOR_VALVE_ZIGBEE,
28
                                                    config.TOPIC_FFW_BATH_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_FFW_BATH)
37
                                                    config.TOPIC_FFW_BATH_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_FFW_BATH)
38
+
29
     def all_off(self):
39
     def all_off(self):
30
         pass
40
         pass
31
 
41
 
35
     def __init__(self, mqtt_client):
45
     def __init__(self, mqtt_client):
36
         super().__init__(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_SHELLY,
46
         super().__init__(mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_SHELLY,
37
                          config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_GUI, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_ZIGBEE)
47
                          config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_GUI, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_ZIGBEE)
48
+        #
49
+        self.main_light_videv = videv_switch_brightness_color_temp(
50
+            mqtt_client, config.TOPIC_FFW_LIVINGROOM_MAIN_LIGHT_VIDEV,
51
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
52
+            self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
53
+            self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
54
+        )
38
 
55
 
39
 
56
 
40
 class first_floor_west_sleep(room_shelly_tradfri_light):
57
 class first_floor_west_sleep(room_shelly_tradfri_light):
41
     # http://shelly1-3494546A51F2
58
     # http://shelly1-3494546A51F2
42
     def __init__(self, mqtt_client):
59
     def __init__(self, mqtt_client):
43
         super().__init__(mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_SHELLY, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_GUI, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_ZIGBEE)
60
         super().__init__(mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_SHELLY, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_GUI, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_ZIGBEE)
61
+        #
62
+        self.main_light_videv = videv_switch_brightness(
63
+            mqtt_client, config.TOPIC_FFW_SLEEP_MAIN_LIGHT_VIDEV,
64
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
65
+            self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS
66
+        )

+ 39
- 1
function/ground_floor_west.py View File

7
 from function.modules import brightness_choose_n_action, radiator_function
7
 from function.modules import brightness_choose_n_action, radiator_function
8
 import logging
8
 import logging
9
 from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light
9
 from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light
10
+from function.videv import videv_switching, videv_switch_brightness_color_temp
10
 import task
11
 import task
11
 
12
 
12
 try:
13
 try:
26
         self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_2_ZIGBEE)
27
         self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_2_ZIGBEE)
27
         self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.main_light_tradfri_2.set_brightness_mcb)
28
         self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.main_light_tradfri_2.set_brightness_mcb)
28
         self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.main_light_tradfri_2.set_color_temp_mcb)
29
         self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.main_light_tradfri_2.set_color_temp_mcb)
30
+        #
31
+        self.main_light_videv = videv_switch_brightness_color_temp(
32
+            mqtt_client, config.TOPIC_GFW_FLOOR_MAIN_LIGHT_VIDEV,
33
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
34
+            self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
35
+            self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
36
+        )
29
 
37
 
30
     def send_init_message_main_light(self):
38
     def send_init_message_main_light(self):
31
         super().send_init_message_main_light()
39
         super().send_init_message_main_light()
32
-        self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
40
+        self.main_light_tradfri_2.request_data()
33
 
41
 
34
 
42
 
35
 class ground_floor_west_marion(room_shelly):
43
 class ground_floor_west_marion(room_shelly):
39
         # radiator valve
47
         # radiator valve
40
         self.radiator_function = radiator_function(mqtt_client, config.TOPIC_GFW_MARION_RADIATOR_VALVE_ZIGBEE,
48
         self.radiator_function = radiator_function(mqtt_client, config.TOPIC_GFW_MARION_RADIATOR_VALVE_ZIGBEE,
41
                                                    config.TOPIC_GFW_MARION_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_GFW_MARION)
49
                                                    config.TOPIC_GFW_MARION_RADIATOR_VALVE_GUI, config.DEFAULT_TEMPERATURE_GFW_MARION)
50
+        #
51
+        self.main_light_videv = videv_switching(
52
+            mqtt_client, config.TOPIC_GFW_MARION_MAIN_LIGHT_VIDEV,
53
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
54
+        )
42
 
55
 
43
 
56
 
44
 class ground_floor_west_dirk(room_shelly_tradfri_light):
57
 class ground_floor_west_dirk(room_shelly_tradfri_light):
136
                                          self.powerplug_common.toggle_output_3_mcb)
149
                                          self.powerplug_common.toggle_output_3_mcb)
137
         self.gui_pc_dock.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_3_mcb)
150
         self.gui_pc_dock.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_3_mcb)
138
         self.powerplug_common.add_callback(self.KEY_POWERPLUG_PC_DOCK, None, self.gui_pc_dock.set_state_mcb)
151
         self.powerplug_common.add_callback(self.KEY_POWERPLUG_PC_DOCK, None, self.gui_pc_dock.set_state_mcb)
152
+        #
153
+        self.main_light_videv = videv_switch_brightness_color_temp(
154
+            mqtt_client, config.TOPIC_GFW_DIRK_MAIN_LIGHT_VIDEV,
155
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0,
156
+            self.main_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
157
+            self.main_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
158
+        )
159
+        self.desk_light_videv = videv_switch_brightness_color_temp(
160
+            mqtt_client, config.TOPIC_GFW_DIRK_DESK_LIGHT_VIDEV,
161
+            self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT,
162
+            self.desk_light_tradfri, devices.tradfri_light.KEY_BRIGHTNESS,
163
+            self.desk_light_tradfri, devices.tradfri_light.KEY_COLOR_TEMP
164
+        )
165
+        self.amplifier_videv = videv_switching(
166
+            mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_VIDEV,
167
+            self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER
168
+        )
169
+        self.cd_player_videv = videv_switching(
170
+            mqtt_client, config.TOPIC_GFW_DIRK_CD_PLAYER_VIDEV,
171
+            self.powerplug_common, self.KEY_POWERPLUG_CD_PLAYER
172
+        )
173
+        self.pc_dock_videv = videv_switching(
174
+            mqtt_client, config.TOPIC_GFW_DIRK_PC_DOCK_VIDEV,
175
+            self.powerplug_common, self.KEY_POWERPLUG_PC_DOCK
176
+        )
139
 
177
 
140
     def all_off(self, device=None, key=None, data=None):
178
     def all_off(self, device=None, key=None, data=None):
141
         super().all_off(device, key, data)
179
         super().all_off(device, key, data)

+ 17
- 0
function/modules.py View File

1
 #!/usr/bin/env python
1
 #!/usr/bin/env python
2
 # -*- coding: utf-8 -*-
2
 # -*- coding: utf-8 -*-
3
 #
3
 #
4
+"""
5
+Functional Modules
6
+
7
+Targets:
8
+  * Device like structure to be compatible with videv
9
+    - KEY_* as part of the class for all parameters which needs to be accessed from videv
10
+    - Method *.set(key, data) to pass data from videv to Module
11
+    - Method .add_calback(key, data, callback, on_change_only=False) to register videv actualisation on changes
12
+"""
4
 
13
 
5
 import config
14
 import config
6
 import devices
15
 import devices
16
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
25
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
17
 
26
 
18
 
27
 
28
+class base(object):
29
+    def set(key, data):
30
+        pass
31
+
32
+    def add_calback(self, key, data, callback, on_change_only=False):
33
+        pass
34
+
35
+
19
 class brightness_choose_n_action(object):
36
 class brightness_choose_n_action(object):
20
     def __init__(self, mqtt_client, button_tradfri, topic_led):
37
     def __init__(self, mqtt_client, button_tradfri, topic_led):
21
         self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, topic_led)
38
         self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, topic_led)

+ 1
- 1
function/rooms.py View File

145
         self.main_light_shelly_last = data
145
         self.main_light_shelly_last = data
146
 
146
 
147
     def send_init_message_main_light(self):
147
     def send_init_message_main_light(self):
148
-        self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')
148
+        self.main_light_tradfri.request_data()

+ 8
- 0
function/stairway.py View File

3
 #
3
 #
4
 
4
 
5
 import config
5
 import config
6
+import devices
6
 from function.modules import brightness_choose_n_action
7
 from function.modules import brightness_choose_n_action
7
 import logging
8
 import logging
8
 from function.rooms import room_shelly_motion_sensor
9
 from function.rooms import room_shelly_motion_sensor
10
+from function.videv import videv_switching
11
+
9
 try:
12
 try:
10
     from config import APP_NAME as ROOT_LOGGER_NAME
13
     from config import APP_NAME as ROOT_LOGGER_NAME
11
 except ImportError:
14
 except ImportError:
19
         super().__init__(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_GUI,
22
         super().__init__(mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_SHELLY, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_GUI,
20
                          config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_GF, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_FF,
23
                          config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_GF, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_MOTION_SENSOR_FF,
21
                          timer_value=config.USER_ON_TIME_STAIRWAYS)
24
                          timer_value=config.USER_ON_TIME_STAIRWAYS)
25
+        #
26
+        self.main_light_videv = videv_switching(
27
+            mqtt_client, config.TOPIC_STW_STAIRWAY_MAIN_LIGHT_VIDEV,
28
+            self.main_light_shelly, devices.shelly.KEY_OUTPUT_0
29
+        )

+ 146
- 0
function/videv.py View File

1
+#!/usr/bin/env python
2
+# -*- coding: utf-8 -*-
3
+#
4
+"""
5
+Virtual Device(s)
6
+
7
+Targets:
8
+  * MQTT-Interface to control joined devices as one virtual device.
9
+  * Primary signal routing 
10
+  * No functionality should be implemented here
11
+"""
12
+
13
+from base import mqtt_base
14
+import devices
15
+import json
16
+import logging
17
+
18
+BASETOPIC = "videv"
19
+
20
+
21
+try:
22
+    from config import APP_NAME as ROOT_LOGGER_NAME
23
+except ImportError:
24
+    ROOT_LOGGER_NAME = 'root'
25
+
26
+
27
+class base(mqtt_base):
28
+    DEFAULT_VALUES = {}
29
+
30
+    def __init__(self, mqtt_client, topic, *args):
31
+        super().__init__(mqtt_client, topic)
32
+        self.__device_list__ = {}
33
+        for videv_key, device in [reduced[:2] for reduced in args]:
34
+            self.__device_list__[videv_key] = device
35
+        # send initial state
36
+        for key in self.keys():
37
+            self.__tx__(key, self[key])
38
+        # add receive topics
39
+        mqtt_client.add_callback(self.topic + "/#", self.__rx__)
40
+
41
+    def __tx__(self, key, data):
42
+        if type(data) not in (str, ):
43
+            data = json.dumps(data)
44
+        if key in self.keys():
45
+            self.mqtt_client.send(self.topic + '/' + key, data)
46
+        else:
47
+            self.logger.warning("Ignoring send request for key %s (not available for this class)", key)
48
+
49
+    def __rx__(self, client, userdata, message):
50
+        key = message.topic.split('/')[-1]
51
+        if key in self.keys():
52
+            try:
53
+                data = json.loads(message.payload)
54
+            except json.decoder.JSONDecodeError:
55
+                data = message.payload
56
+            if data != self[key]:
57
+                self.__rx_functionality__(key, data)
58
+            self.set(key, data)
59
+        else:
60
+            self.logger.info("Ignoring rx message with topic %s", message.topic)
61
+
62
+    def __rx_functionality__(self, key, data):
63
+        raise NotImplemented("Method __rx_functionality__ needs to be implemented in child class")
64
+
65
+    def __device_data__(self, device, key, data):
66
+        raise NotImplemented("Method __device_data__ needs to be implemented in child class")
67
+
68
+
69
+class base_routing(base):
70
+    def __init__(self, mqtt_client, topic, *args):
71
+        super().__init__(mqtt_client, topic, *args)
72
+        #
73
+        self.__device_key__ = {}
74
+        index = 0
75
+        for videv_key, device, device_key in args:
76
+            if self.__device_list__[videv_key] != device:
77
+                raise ReferenceError("Parent class generated a deviating device list")
78
+            self.__device_key__[videv_key] = device_key
79
+            index += 1
80
+        # add callbacks
81
+        for key in self.__device_list__:
82
+            self.__device_list__[key].add_callback(self.__device_key__[key], None, self.__device_data__, True)
83
+
84
+    def __rx_functionality__(self, key, data):
85
+        try:
86
+            self.__device_list__[key].set(self.__device_key__[key], data)
87
+        except KeyError:
88
+            self.logger.warning("RX passthrough not possible for key %s", key)
89
+
90
+    def __device_data__(self, device, key, data):
91
+        l1 = [k for k, v in self.__device_list__.items() if v == device]
92
+        l2 = [k for k, v in self.__device_key__.items() if v == key]
93
+        try:
94
+            videv_key = [k for k in l1 if k in l2][0]
95
+        except IndexError:
96
+            self.logger.warning("videv_key not available for %s::%s", device.__class__.__name__, device.topic)
97
+        else:
98
+            self.set(videv_key, data)
99
+            self.__tx__(videv_key, data)
100
+
101
+
102
+class videv_switching(base_routing):
103
+    KEY_STATE = 'state'
104
+    #
105
+    DEFAULT_VALUES = {
106
+        KEY_STATE: False,
107
+    }
108
+
109
+    def __init__(self, mqtt_client, topic, sw_device, sw_key):
110
+        #
111
+        super().__init__(mqtt_client, topic, (self.KEY_STATE, sw_device, sw_key))
112
+
113
+
114
+class videv_switch_brightness(base_routing):
115
+    KEY_STATE = 'state'
116
+    KEY_BRIGHTNESS = 'brightness'
117
+    #
118
+    DEFAULT_VALUES = {
119
+        KEY_STATE: False,
120
+        KEY_BRIGHTNESS: 0
121
+    }
122
+
123
+    def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key):
124
+        #
125
+        super().__init__(mqtt_client, topic, (self.KEY_STATE, sw_device, sw_key), (self.KEY_BRIGHTNESS, br_device, br_key))
126
+
127
+
128
+class videv_switch_brightness_color_temp(base_routing):
129
+    KEY_STATE = 'state'
130
+    KEY_BRIGHTNESS = 'brightness'
131
+    KEY_COLOR_TEMP = 'color_temp'
132
+    #
133
+    DEFAULT_VALUES = {
134
+        KEY_STATE: False,
135
+        KEY_BRIGHTNESS: 0,
136
+        KEY_COLOR_TEMP: 0,
137
+    }
138
+
139
+    def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key, ct_device, ct_key):
140
+        #
141
+        super().__init__(
142
+            mqtt_client, topic,
143
+            (self.KEY_STATE, sw_device, sw_key),
144
+            (self.KEY_BRIGHTNESS, br_device, br_key),
145
+            (self.KEY_COLOR_TEMP, ct_device, ct_key)
146
+        )

Loading…
Cancel
Save