Browse Source

bug-fix for brightness_choose_n_action

tags/v1.0.0
Dirk Alders 2 years ago
parent
commit
8ba63349fb
5 changed files with 27 additions and 51 deletions
  1. 0
    24
      devices/__init__.py
  2. 4
    4
      function/__init__.py
  3. 2
    2
      function/first_floor_east.py
  4. 3
    3
      function/ground_floor_west.py
  5. 18
    18
      function/modules.py

+ 0
- 24
devices/__init__.py View File

215
     def __init__(self, mqtt_client, topic):
215
     def __init__(self, mqtt_client, topic):
216
         super().__init__(mqtt_client, topic)
216
         super().__init__(mqtt_client, topic)
217
 
217
 
218
-    def add_on_off_callback_0(self, callback):
219
-        self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
220
-
221
-    def add_on_off_callback_1(self, callback):
222
-        self.add_callback(self.KEY_OUTPUT_1, None, callback, True)
223
-
224
     #
218
     #
225
     # WARNING CALL
219
     # WARNING CALL
226
     #
220
     #
313
     def __init__(self, mqtt_client, topic):
307
     def __init__(self, mqtt_client, topic):
314
         super().__init__(mqtt_client, topic)
308
         super().__init__(mqtt_client, topic)
315
 
309
 
316
-    def add_default_on_off_callback(self, callback):
317
-        self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
318
-
319
     #
310
     #
320
     # RX
311
     # RX
321
     #
312
     #
389
     def __init__(self, mqtt_client, topic):
380
     def __init__(self, mqtt_client, topic):
390
         super().__init__(mqtt_client, topic)
381
         super().__init__(mqtt_client, topic)
391
 
382
 
392
-    def add_on_off_callback_0(self, callback):
393
-        self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
394
-
395
-    def add_on_off_callback_1(self, callback):
396
-        self.add_callback(self.KEY_OUTPUT_1, None, callback, True)
397
-
398
-    def add_on_off_callback_2(self, callback):
399
-        self.add_callback(self.KEY_OUTPUT_2, None, callback, True)
400
-
401
-    def add_on_off_callback_3(self, callback):
402
-        self.add_callback(self.KEY_OUTPUT_3, None, callback, True)
403
-
404
     #
383
     #
405
     # RX
384
     # RX
406
     #
385
     #
511
     def __init__(self, mqtt_client, topic):
490
     def __init__(self, mqtt_client, topic):
512
         super().__init__(mqtt_client, topic)
491
         super().__init__(mqtt_client, topic)
513
 
492
 
514
-    def add_on_off_callback_0(self, callback):
515
-        self.add_callback(self.KEY_OUTPUT_0, None, callback, True)
516
-
517
     def unpack_filter(self, key):
493
     def unpack_filter(self, key):
518
         if key == self.KEY_BRIGHTNESS:
494
         if key == self.KEY_BRIGHTNESS:
519
             self[key] = (self[key] - 1) * 100 / 254
495
             self[key] = (self[key] - 1) * 100 / 254

+ 4
- 4
function/__init__.py View File

59
 
59
 
60
     def init_off_functionality(self):
60
     def init_off_functionality(self):
61
         # Off Buttons
61
         # Off Buttons
62
-        self.gui_button_all_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/all/common/off/button")
63
-        self.gui_button_gfw_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/gfw/common/off/button")
64
-        self.gui_button_ffw_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/ffw/common/off/button")
65
-        self.gui_button_ffe_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/ffe/common/off/button")
62
+        self.gui_button_all_off = devices.nodered_gui_button(self.mqtt_client, "gui/all/common/off/button")
63
+        self.gui_button_gfw_off = devices.nodered_gui_button(self.mqtt_client, "gui/gfw/common/off/button")
64
+        self.gui_button_ffw_off = devices.nodered_gui_button(self.mqtt_client, "gui/ffw/common/off/button")
65
+        self.gui_button_ffe_off = devices.nodered_gui_button(self.mqtt_client, "gui/ffe/common/off/button")
66
         #
66
         #
67
         self.gui_button_all_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.all_off)
67
         self.gui_button_all_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.all_off)
68
         self.gui_button_gfw_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.gfw_off)
68
         self.gui_button_gfw_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.gfw_off)

+ 2
- 2
function/first_floor_east.py View File

63
         self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_FFE_SLEEP_INPUT_DEVICE)
63
         self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_FFE_SLEEP_INPUT_DEVICE)
64
         #
64
         #
65
         self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_FFE_SLEEP_DEVICE_CHOOSER_LED)
65
         self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_FFE_SLEEP_DEVICE_CHOOSER_LED)
66
-        self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly.add_on_off_callback_0)
67
-        self.brightness_functions.add(self.bed_light_di_tradfri, self.bed_light_di_tradfri.add_on_off_callback_0)
66
+        self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0)
67
+        self.brightness_functions.add(self.bed_light_di_tradfri, self.bed_light_di_tradfri, devices.tradfri_light.KEY_OUTPUT_0)
68
         #
68
         #
69
         # Callback initialisation
69
         # Callback initialisation
70
         #
70
         #

+ 3
- 3
function/ground_floor_west.py View File

77
         self.remote_amplifier = devices.remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE)
77
         self.remote_amplifier = devices.remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE)
78
         #
78
         #
79
         self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_LED)
79
         self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_LED)
80
-        self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly.add_on_off_callback_0)
81
-        self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common.add_on_off_callback_1)
82
-        self.brightness_functions.add(self.remote_amplifier, self.powerplug_common.add_on_off_callback_0)
80
+        self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0)
81
+        self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT)
82
+        self.brightness_functions.add(self.remote_amplifier, self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER)
83
         #
83
         #
84
         self.spotify_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_SPOTIFY)
84
         self.spotify_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_SPOTIFY)
85
         self.mpd_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_MPD)
85
         self.mpd_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_MPD)

+ 18
- 18
function/modules.py View File

28
         button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
28
         button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
29
         button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
29
         button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
30
         #
30
         #
31
-        self.device_list = []
31
+        self.brightness_device_list = []
32
         self.callback_device_list = []
32
         self.callback_device_list = []
33
         self.device_states = []
33
         self.device_states = []
34
         self.active_device_state = None
34
         self.active_device_state = None
35
         self.update_active_device_led()
35
         self.update_active_device_led()
36
 
36
 
37
-    def add(self, device, add_callback):
37
+    def add(self, brightness_device, callback_device, callback_key):
38
         """
38
         """
39
-        device: A device for brightness function needs to have the following methods:
39
+        brightness_device: A device for brightness function needs to have the following methods:
40
         * .default_inc()
40
         * .default_inc()
41
         * .default_dec()
41
         * .default_dec()
42
         * .default_stop()
42
         * .default_stop()
43
-        add_callback: A on/off callback which tages the <function_to_be_executed> as argument and
44
-            - which calls the callback only on changes
45
-            - which calls with the following arguments: device, key, data
43
+        callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method:
44
+        * .add_callback(key, data or None, callback, on_changes_only)
46
         """
45
         """
47
-        if len(self.device_list) >= len(devices.nodered_gui_leds.RX_KEYS):
46
+        if len(self.brightness_device_list) >= len(devices.nodered_gui_leds.RX_KEYS):
48
             raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.")
47
             raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.")
49
-        self.device_list.append(device)
50
-        self.callback_device_list.append(add_callback.__self__)
48
+        self.brightness_device_list.append(brightness_device)
49
+        self.callback_device_list.append((callback_device, callback_key))
51
         self.device_states.append(False)
50
         self.device_states.append(False)
52
-        add_callback(self.device_state_action)
51
+        callback_device.add_callback(callback_key, True, self.device_state_action, True)
52
+        callback_device.add_callback(callback_key, False, self.device_state_action, True)
53
 
53
 
54
     def device_state_action(self, device, key, data):
54
     def device_state_action(self, device, key, data):
55
-        self.device_states[self.callback_device_list.index(device)] = data
55
+        self.device_states[self.callback_device_list.index((device, key))] = data
56
         if data is True:
56
         if data is True:
57
-            self.active_device_state = self.callback_device_list.index(device)
57
+            self.active_device_state = self.callback_device_list.index((device, key))
58
             self.update_active_device_led()
58
             self.update_active_device_led()
59
         else:
59
         else:
60
             self.choose_next_device()
60
             self.choose_next_device()
61
 
61
 
62
     def update_active_device_led(self):
62
     def update_active_device_led(self):
63
-        for i in range(0, len(self.device_list)):
63
+        for i in range(0, len(self.brightness_device_list)):
64
             self.gui_led_active_device.set_led(devices.nodered_gui_leds.RX_KEYS[i], self.active_device_state == i)
64
             self.gui_led_active_device.set_led(devices.nodered_gui_leds.RX_KEYS[i], self.active_device_state == i)
65
 
65
 
66
     def choose_prev_device(self, device=None, key=None, data=None):
66
     def choose_prev_device(self, device=None, key=None, data=None):
67
         if self.active_device_state is not None:
67
         if self.active_device_state is not None:
68
             start_value = self.active_device_state
68
             start_value = self.active_device_state
69
-            for i in range(0, len(self.device_list)):
70
-                target_state = (start_value - i - 1) % (len(self.device_list))
69
+            for i in range(0, len(self.brightness_device_list)):
70
+                target_state = (start_value - i - 1) % (len(self.brightness_device_list))
71
                 if self.device_states[target_state]:
71
                 if self.device_states[target_state]:
72
                     self.active_device_state = target_state
72
                     self.active_device_state = target_state
73
                     self.update_active_device_led()
73
                     self.update_active_device_led()
78
     def choose_next_device(self, device=None, key=None, data=None):
78
     def choose_next_device(self, device=None, key=None, data=None):
79
         if self.active_device_state is not None:
79
         if self.active_device_state is not None:
80
             start_value = self.active_device_state
80
             start_value = self.active_device_state
81
-            for i in range(0, len(self.device_list)):
82
-                target_state = (start_value + i + 1) % (len(self.device_list))
81
+            for i in range(0, len(self.brightness_device_list)):
82
+                target_state = (start_value + i + 1) % (len(self.brightness_device_list))
83
                 if self.device_states[target_state]:
83
                 if self.device_states[target_state]:
84
                     self.active_device_state = target_state
84
                     self.active_device_state = target_state
85
                     self.update_active_device_led()
85
                     self.update_active_device_led()
89
 
89
 
90
     def brightness_action(self, device, key, data):
90
     def brightness_action(self, device, key, data):
91
         if self.active_device_state is not None:
91
         if self.active_device_state is not None:
92
-            target = self.device_list[self.active_device_state]
92
+            target = self.brightness_device_list[self.active_device_state]
93
             if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
93
             if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
94
                 logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
94
                 logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
95
                 target.default_inc()
95
                 target.default_inc()

Loading…
Cancel
Save