瀏覽代碼

videv base class moved to base.py

tags/v1.2.4^0
Dirk Alders 1 年之前
父節點
當前提交
c076f516d8
共有 4 個文件被更改,包括 107 次插入107 次删除
  1. 94
    0
      base.py
  2. 1
    1
      devices/__init__.py
  3. 11
    105
      function/videv.py
  4. 1
    1
      smart_brain.py

+ 94
- 0
base.py 查看文件

@@ -1,3 +1,4 @@
1
+import json
1 2
 import logging
2 3
 
3 4
 try:
@@ -46,3 +47,96 @@ class mqtt_base(common_base):
46 47
         self.topic = topic
47 48
         for entry in self.topic.split('/'):
48 49
             self.logger = self.logger.getChild(entry)
50
+
51
+
52
+class videv_base(mqtt_base):
53
+    KEY_INFO = '__info__'
54
+    #
55
+    SET_TOPIC = "set"
56
+
57
+    def __init__(self, mqtt_client, topic, default_values=None):
58
+        super().__init__(mqtt_client, topic, default_values=default_values)
59
+        self.__display_dict__ = {}
60
+        self.__control_dict__ = {}
61
+        self.__capabilities__ = None
62
+
63
+    def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
64
+        """
65
+        listen to data changes of ext_device and update videv information
66
+        """
67
+        if my_key not in self.keys():
68
+            self[my_key] = None
69
+        if ext_device.__class__.__name__ == "group":
70
+            # store information to identify callback from ext_device
71
+            self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
72
+            # register a callback to listen for data from external device
73
+            ext_device[0].add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
74
+        else:
75
+            # store information to identify callback from ext_device
76
+            self.__display_dict__[(id(ext_device), ext_key)] = my_key
77
+            # register a callback to listen for data from external device
78
+            ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
79
+        # send default data to videv interface
80
+
81
+    def __rx_ext_device_data__(self, ext_device, ext_key, data):
82
+        my_key = self.__display_dict__[(id(ext_device), ext_key)]
83
+        self.set(my_key, data)
84
+        self.__tx__(my_key, data)
85
+
86
+    def __tx__(self, key, data):
87
+        if type(data) not in (str, ):
88
+            data = json.dumps(data)
89
+        self.mqtt_client.send('/'.join([self.topic, key]), data)
90
+        self.__tx_capabilities__()
91
+
92
+    def __tx_capabilities__(self):
93
+        self.mqtt_client.send(self.topic + '/' + self.KEY_INFO, json.dumps(self.capabilities))
94
+
95
+    def add_control(self, my_key, ext_device, ext_key, on_change_only=True):
96
+        """
97
+        listen to videv information and pass data to ext_device
98
+        """
99
+        if my_key not in self.keys():
100
+            self[my_key] = None
101
+        # store information to identify callback from videv
102
+        self.__control_dict__[my_key] = (ext_device, ext_key, on_change_only)
103
+        # add callback for videv changes
104
+        self.mqtt_client.add_callback('/'.join([self.topic, my_key, self.SET_TOPIC]), self.__rx_videv_data__)
105
+
106
+    def __rx_videv_data__(self, client, userdata, message):
107
+        my_key = message.topic.split('/')[-2]
108
+        try:
109
+            data = json.loads(message.payload)
110
+        except json.decoder.JSONDecodeError:
111
+            data = message.payload
112
+        ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
113
+        if my_key in self.keys():
114
+            if data != self[my_key] or not on_change_only:
115
+                ext_device.send_command(ext_key, data)
116
+        else:
117
+            self.logger.info("Ignoring rx message with topic %s", message.topic)
118
+
119
+    def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
120
+        """
121
+        listen to data changes of ext_device and update videv information
122
+        and
123
+        listen to videv information and pass data to ext_device
124
+        """
125
+        # add display
126
+        self.add_display(my_key, ext_device, ext_key, on_change_only_disp)
127
+        self.add_control(my_key, ext_device, ext_key, on_change_only_videv)
128
+
129
+    @property
130
+    def capabilities(self):
131
+        if self.__capabilities__ is None:
132
+            self.__capabilities__ = {}
133
+            self.__capabilities__['__type__'] = self.__class__.__name__
134
+            for key in self.__control_dict__:
135
+                if not key in self.__capabilities__:
136
+                    self.__capabilities__[key] = {}
137
+                self.__capabilities__[key]['control'] = True
138
+            for key in self.__display_dict__.values():
139
+                if not key in self.__capabilities__:
140
+                    self.__capabilities__[key] = {}
141
+                self.__capabilities__[key]['display'] = True
142
+        return self.__capabilities__

+ 1
- 1
devices/__init__.py 查看文件

@@ -27,7 +27,7 @@ devices (DEVICES)
27 27
 """
28 28
 
29 29
 from base import mqtt_base
30
-from function.videv import base as videv_base
30
+from base import videv_base
31 31
 import json
32 32
 import logging
33 33
 import math

+ 11
- 105
function/videv.py 查看文件

@@ -10,9 +10,8 @@ Targets:
10 10
   * No functionality should be implemented here
11 11
 """
12 12
 
13
-from base import mqtt_base
13
+from base import videv_base
14 14
 from function.rooms import room, room_collection
15
-import json
16 15
 import time
17 16
 
18 17
 try:
@@ -21,100 +20,7 @@ except ImportError:
21 20
     ROOT_LOGGER_NAME = 'root'
22 21
 
23 22
 
24
-class base(mqtt_base):
25
-    KEY_INFO = '__info__'
26
-    #
27
-    SET_TOPIC = "set"
28
-
29
-    def __init__(self, mqtt_client, topic, default_values=None):
30
-        super().__init__(mqtt_client, topic, default_values=default_values)
31
-        self.__display_dict__ = {}
32
-        self.__control_dict__ = {}
33
-        self.__capabilities__ = None
34
-
35
-    def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
36
-        """
37
-        listen to data changes of ext_device and update videv information
38
-        """
39
-        if my_key not in self.keys():
40
-            self[my_key] = None
41
-        if ext_device.__class__.__name__ == "group":
42
-            # store information to identify callback from ext_device
43
-            self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
44
-            # register a callback to listen for data from external device
45
-            ext_device[0].add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
46
-        else:
47
-            # store information to identify callback from ext_device
48
-            self.__display_dict__[(id(ext_device), ext_key)] = my_key
49
-            # register a callback to listen for data from external device
50
-            ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
51
-        # send default data to videv interface
52
-
53
-    def __rx_ext_device_data__(self, ext_device, ext_key, data):
54
-        my_key = self.__display_dict__[(id(ext_device), ext_key)]
55
-        self.set(my_key, data)
56
-        self.__tx__(my_key, data)
57
-
58
-    def __tx__(self, key, data):
59
-        if type(data) not in (str, ):
60
-            data = json.dumps(data)
61
-        self.mqtt_client.send('/'.join([self.topic, key]), data)
62
-        self.__tx_capabilities__()
63
-
64
-    def __tx_capabilities__(self):
65
-        self.mqtt_client.send(self.topic + '/' + self.KEY_INFO, json.dumps(self.capabilities))
66
-
67
-    def add_control(self, my_key, ext_device, ext_key, on_change_only=True):
68
-        """
69
-        listen to videv information and pass data to ext_device
70
-        """
71
-        if my_key not in self.keys():
72
-            self[my_key] = None
73
-        # store information to identify callback from videv
74
-        self.__control_dict__[my_key] = (ext_device, ext_key, on_change_only)
75
-        # add callback for videv changes
76
-        self.mqtt_client.add_callback('/'.join([self.topic, my_key, self.SET_TOPIC]), self.__rx_videv_data__)
77
-
78
-    def __rx_videv_data__(self, client, userdata, message):
79
-        my_key = message.topic.split('/')[-2]
80
-        try:
81
-            data = json.loads(message.payload)
82
-        except json.decoder.JSONDecodeError:
83
-            data = message.payload
84
-        ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
85
-        if my_key in self.keys():
86
-            if data != self[my_key] or not on_change_only:
87
-                ext_device.send_command(ext_key, data)
88
-        else:
89
-            self.logger.info("Ignoring rx message with topic %s", message.topic)
90
-
91
-    def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
92
-        """
93
-        listen to data changes of ext_device and update videv information
94
-        and
95
-        listen to videv information and pass data to ext_device
96
-        """
97
-        # add display
98
-        self.add_display(my_key, ext_device, ext_key, on_change_only_disp)
99
-        self.add_control(my_key, ext_device, ext_key, on_change_only_videv)
100
-
101
-    @property
102
-    def capabilities(self):
103
-        if self.__capabilities__ is None:
104
-            self.__capabilities__ = {}
105
-            self.__capabilities__['__type__'] = self.__class__.__name__
106
-            for key in self.__control_dict__:
107
-                if not key in self.__capabilities__:
108
-                    self.__capabilities__[key] = {}
109
-                self.__capabilities__[key]['control'] = True
110
-            for key in self.__display_dict__.values():
111
-                if not key in self.__capabilities__:
112
-                    self.__capabilities__[key] = {}
113
-                self.__capabilities__[key]['display'] = True
114
-        return self.__capabilities__
115
-
116
-
117
-class videv_switching(base):
23
+class videv_switching(videv_base):
118 24
     KEY_STATE = 'state'
119 25
 
120 26
     def __init__(self, mqtt_client, topic, sw_device, sw_key):
@@ -124,7 +30,7 @@ class videv_switching(base):
124 30
         self.__tx_capabilities__()
125 31
 
126 32
 
127
-class videv_switching_timer(base):
33
+class videv_switching_timer(videv_base):
128 34
     KEY_STATE = 'state'
129 35
     KEY_TIMER = 'timer'
130 36
 
@@ -136,7 +42,7 @@ class videv_switching_timer(base):
136 42
         self.__tx_capabilities__()
137 43
 
138 44
 
139
-class videv_switching_motion(base):
45
+class videv_switching_motion(videv_base):
140 46
     KEY_STATE = 'state'
141 47
     #
142 48
     KEY_TIMER = 'timer'
@@ -155,7 +61,7 @@ class videv_switching_motion(base):
155 61
         self.__tx_capabilities__()
156 62
 
157 63
 
158
-class videv_switch_brightness(base):
64
+class videv_switch_brightness(videv_base):
159 65
     KEY_STATE = 'state'
160 66
     KEY_BRIGHTNESS = 'brightness'
161 67
 
@@ -167,7 +73,7 @@ class videv_switch_brightness(base):
167 73
         self.__tx_capabilities__()
168 74
 
169 75
 
170
-class videv_switch_brightness_color_temp(base):
76
+class videv_switch_brightness_color_temp(videv_base):
171 77
     KEY_STATE = 'state'
172 78
     KEY_BRIGHTNESS = 'brightness'
173 79
     KEY_COLOR_TEMP = 'color_temp'
@@ -181,7 +87,7 @@ class videv_switch_brightness_color_temp(base):
181 87
         self.__tx_capabilities__()
182 88
 
183 89
 
184
-class videv_heating(base):
90
+class videv_heating(videv_base):
185 91
     KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
186 92
     KEY_VALVE_TEMPERATURE_SETPOINT = 'valve_temperature_setpoint'
187 93
     KEY_AWAY_MODE = 'away_mode'
@@ -209,7 +115,7 @@ class videv_heating(base):
209 115
         self.__tx_capabilities__()
210 116
 
211 117
 
212
-class videv_multistate(base):
118
+class videv_multistate(videv_base):
213 119
     KEY_STATE = 'state_%d'
214 120
 
215 121
     def __init__(self, mqtt_client, topic, key_for_device, device, num_states, default_values=None):
@@ -230,7 +136,7 @@ class videv_multistate(base):
230 136
         self.__tx_capabilities__()
231 137
 
232 138
 
233
-class videv_audio_player(base):
139
+class videv_audio_player(videv_base):
234 140
     KEY_ACTIVE_PLAYER = 'player_%d'
235 141
     KEY_TITLE = 'title'
236 142
     NO_TITLE = '---'
@@ -255,7 +161,7 @@ class videv_audio_player(base):
255 161
         return self.__capabilities__
256 162
 
257 163
 
258
-class videv_warnings(base):
164
+class videv_warnings(videv_base):
259 165
     MAX_WARNINGS = 10
260 166
     KEY_WARNING = 'html_short'
261 167
 
@@ -270,7 +176,7 @@ class videv_warnings(base):
270 176
         self.__tx__(self.KEY_WARNING, txt)
271 177
 
272 178
 
273
-class all_off(base):
179
+class all_off(videv_base):
274 180
     ALLOWED_CLASSES = (room, room_collection, )
275 181
 
276 182
     def __init__(self, mqtt_client, topic, room_collection):

+ 1
- 1
smart_brain.py 查看文件

@@ -13,7 +13,7 @@ logger = logging.getLogger(config.APP_NAME)
13 13
 
14 14
 VERS_MAJOR = 1
15 15
 VERS_MINOR = 2
16
-VERS_PATCH = 3
16
+VERS_PATCH = 4
17 17
 
18 18
 INFO_TOPIC = "__info__"
19 19
 INFO_DATA = {

Loading…
取消
儲存