Преглед изворни кода

Added some devives and commands

master
Dirk Alders пре 1 година
родитељ
комит
294527832d
7 измењених фајлова са 198 додато и 23 уклоњено
  1. 1
    1
      devdi
  2. 4
    4
      devices/__init__.py
  3. 0
    14
      devices/livarno.py
  4. 56
    0
      devices/silvercrest.py
  5. 134
    0
      devices/tradfri.py
  6. 1
    1
      home.py
  7. 2
    3
      home_emulation.py

+ 1
- 1
devdi

@@ -1 +1 @@
1
-Subproject commit 773d0a6679810b365bbd4537156c513b0f496c5a
1
+Subproject commit 5a94efbc8c4ca7e1a7ef6f40df53177717bb7c16

+ 4
- 4
devices/__init__.py Прегледај датотеку

@@ -1,14 +1,14 @@
1 1
 from devices.brennenstuhl import vlv as brennenstuhl_heatingvalve
2
-from devices.livarno import sw as silvercrest_powerplug
3
-from devices.livarno import sw_br_ct as livarno_sw_br_ct
2
+from devices.silvercrest import sw as silvercrest_powerplug
3
+from devices.silvercrest import sw_br_ct as livarno_sw_br_ct
4
+from devices.silvercrest import motion_sensor as silvercrest_motion_sensor
4 5
 from devices.my import powerplug as my_powerplug
5 6
 from devices.shelly import shelly_sw1
6 7
 from devices.tradfri import sw as tradfri_sw
7 8
 from devices.tradfri import sw_br as tradfri_sw_br
8 9
 from devices.tradfri import sw_br_ct as tradfri_sw_br_ct
10
+from devices.tradfri import button as tradfri_button
9 11
 
10
-tradfri_button = None
11
-silvercrest_motion_sensor = None
12 12
 audio_status = None
13 13
 remote = None
14 14
 

+ 0
- 14
devices/livarno.py Прегледај датотеку

@@ -1,14 +0,0 @@
1
-from devices.tradfri import sw as tradfri_sw
2
-from devices.tradfri import sw_br_ct as tradfri_sw_br_ct
3
-
4
-
5
-class sw(tradfri_sw):
6
-    pass
7
-
8
-
9
-class sw_br_ct(tradfri_sw_br_ct):
10
-    def set_state(self, value):
11
-        self.__set__("state", "on" if value else "off")
12
-
13
-    def power_on_action(self):
14
-        self["state"] = "on"

+ 56
- 0
devices/silvercrest.py Прегледај датотеку

@@ -0,0 +1,56 @@
1
+from devices.base import base
2
+from devices.tradfri import sw as tradfri_sw
3
+from devices.tradfri import sw_br_ct as tradfri_sw_br_ct
4
+import json
5
+
6
+
7
+class sw(tradfri_sw):
8
+    pass
9
+
10
+
11
+class sw_br_ct(tradfri_sw_br_ct):
12
+    def set_state(self, value):
13
+        self.__set__("state", "on" if value else "off")
14
+
15
+    def power_on_action(self):
16
+        self["state"] = "on"
17
+
18
+
19
+class motion_sensor(base):
20
+    """ Communication (MQTT)
21
+
22
+        silvercrest_motion_sensor {
23
+                                      battery: [0...100] %
24
+                                      battery_low: [True, False]
25
+                                      linkquality: [0...255] lqi
26
+                                      occupancy: [True, False]
27
+                                      tamper: [True, False]
28
+                                      voltage: [0...] mV
29
+                                  }
30
+    """
31
+    KEY_BATTERY = "battery"
32
+    KEY_BATTERY_LOW = "battery_low"
33
+    KEY_LINKQUALITY = "linkquality"
34
+    KEY_OCCUPANCY = "occupancy"
35
+    KEY_UNMOUNTED = "tamper"
36
+    KEY_VOLTAGE = "voltage"
37
+    #
38
+    PROPERTIES = [KEY_OCCUPANCY, KEY_UNMOUNTED]
39
+
40
+    def __init__(self, mqtt_client, topic, **kwargs):
41
+        super().__init__(mqtt_client, topic, **kwargs)
42
+        #
43
+        self[self.KEY_OCCUPANCY] = False
44
+        #
45
+        cmd_base = self.topic.replace('/', '.') + '.'
46
+        self.user_cmds = {
47
+            cmd_base + 'toggle_occupancy': self.__ui_toggle_occupancy__,
48
+        }
49
+
50
+    def send_device_status(self):
51
+        self.logger.info("Sending status: %s", repr(self))
52
+        self.mqtt_client.send(self.topic, json.dumps(self))
53
+
54
+    def __ui_toggle_occupancy__(self, *args):
55
+        self.__set__(self.KEY_OCCUPANCY, not self.get(self.KEY_OCCUPANCY))
56
+        self.send_device_status()

+ 134
- 0
devices/tradfri.py Прегледај датотеку

@@ -41,6 +41,8 @@ tradfri devices
41 41
 
42 42
 from devices.base import base
43 43
 import json
44
+import task
45
+import time
44 46
 
45 47
 
46 48
 class sw(base):
@@ -106,12 +108,29 @@ class sw_br(sw):
106 108
     """
107 109
     PROPERTIES = sw.PROPERTIES + [
108 110
         "brightness",
111
+        "brightness_move",
109 112
     ]
110 113
 
111 114
     def __init__(self, mqtt_client, topic):
112 115
         super().__init__(mqtt_client, topic)
116
+        self.task = task.periodic(0.1, self.__task__)
117
+        self.task.run()
118
+        #
113 119
         self["brightness"] = 64
114 120
 
121
+    def __task__(self, rt):
122
+        db = self.get('brightness_move', 0)
123
+        if db != 0:
124
+            b = self["brightness"]
125
+            b += (db / 10)
126
+            if b < 0:
127
+                b = 0
128
+            elif b > 254:
129
+                b = 254
130
+            if b != self["brightness"]:
131
+                self["brightness"] = b
132
+                self.send_device_status()
133
+
115 134
 
116 135
 class sw_br_ct(sw_br):
117 136
     """A tradfri device with switching, brightness and colortemp functionality
@@ -127,3 +146,118 @@ class sw_br_ct(sw_br):
127 146
     def __init__(self, mqtt_client, topic):
128 147
         super().__init__(mqtt_client, topic)
129 148
         self["color_temp"] = 413
149
+
150
+
151
+class button(base):
152
+    """ Communication (MQTT)
153
+
154
+        tradfri_button {
155
+                            "action": [
156
+                                           "arrow_left_click",
157
+                                           "arrow_left_hold",
158
+                                           "arrow_left_release",
159
+                                           "arrow_right_click",
160
+                                           "arrow_right_hold",
161
+                                           "arrow_right_release",
162
+                                           "brightness_down_click",
163
+                                           "brightness_down_hold",
164
+                                           "brightness_down_release",
165
+                                           "brightness_up_click",
166
+                                           "brightness_up_hold",
167
+                                           "brightness_up_release",
168
+                                           "toggle"
169
+                                      ]
170
+                            "action_duration": [0...] s
171
+                            "battery": [0...100] %
172
+                            "linkquality": [0...255] lqi
173
+                            "update": []
174
+                       }
175
+    """
176
+    ACTION_TOGGLE = "toggle"
177
+    ACTION_BRIGHTNESS_UP = "brightness_up_click"
178
+    ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
179
+    ACTION_RIGHT = "arrow_right_click"
180
+    ACTION_LEFT = "arrow_left_click"
181
+    ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
182
+    ACTION_BRIGHTNESS_UP_RELEASE = "brightness_up_release"
183
+    ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
184
+    ACTION_BRIGHTNESS_DOWN_RELEASE = "brightness_down_release"
185
+    ACTION_RIGHT_LONG = "arrow_right_hold"
186
+    ACTION_RIGHT_RELEASE = "arrow_right_release"
187
+    ACTION_LEFT_LONG = "arrow_left_hold"
188
+    ACTION_LEFT_RELEASE = "arrow_left_release"
189
+    ACTION_RELEASE = {
190
+        ACTION_BRIGHTNESS_DOWN_LONG: ACTION_BRIGHTNESS_DOWN_RELEASE,
191
+        ACTION_BRIGHTNESS_UP_LONG: ACTION_BRIGHTNESS_UP_RELEASE,
192
+        ACTION_LEFT_LONG: ACTION_LEFT_RELEASE,
193
+        ACTION_RIGHT_LONG: ACTION_RIGHT_RELEASE
194
+    }
195
+    #
196
+    KEY_LINKQUALITY = "linkquality"
197
+    KEY_BATTERY = "battery"
198
+    KEY_ACTION = "action"
199
+    KEY_ACTION_DURATION = "action_duration"
200
+
201
+    def __init__(self, mqtt_client, topic, **kwargs):
202
+        super().__init__(mqtt_client, topic, **kwargs)
203
+        self.__device_in_use__ = False
204
+        #
205
+        cmd_base = self.topic.replace('/', '.') + '.'
206
+        self.user_cmds = {
207
+            cmd_base + 'toggle': self.__ui_button_toggle__,
208
+            cmd_base + 'left': self.__ui_button_left__,
209
+            cmd_base + 'left_long': self.__ui_button_left_long__,
210
+            cmd_base + 'right': self.__ui_button_right__,
211
+            cmd_base + 'right_long': self.__ui_button_right_long__,
212
+            cmd_base + 'up': self.__ui_button_up__,
213
+            cmd_base + 'up_long': self.__ui_button_up_long__,
214
+            cmd_base + 'down': self.__ui_button_down__,
215
+            cmd_base + 'down_long': self.__ui_button_down_long__,
216
+        }
217
+
218
+    def send_device_status(self, data):
219
+        self.logger.info("Sending status: %s", repr(data))
220
+        self.mqtt_client.send(self.topic, json.dumps(data))
221
+
222
+    def __ui_button_toggle__(self, *args):
223
+        self.send_device_status({self.KEY_ACTION: self.ACTION_TOGGLE})
224
+
225
+    def __ui_button_left__(self, *args):
226
+        self.send_device_status({self.KEY_ACTION: self.ACTION_LEFT})
227
+
228
+    def __ui_button_left_long__(self, *args):
229
+        self.__ui_button_long_press__(self.ACTION_LEFT_LONG, *args)
230
+
231
+    def __ui_button_right__(self, *args):
232
+        self.send_device_status({self.KEY_ACTION: self.ACTION_RIGHT})
233
+
234
+    def __ui_button_right_long__(self, *args):
235
+        self.__ui_button_long_press__(self.ACTION_RIGHT_LONG, *args)
236
+
237
+    def __ui_button_up__(self, *args):
238
+        self.send_device_status({self.KEY_ACTION: self.ACTION_BRIGHTNESS_UP})
239
+
240
+    def __ui_button_up_long__(self, *args):
241
+        self.__ui_button_long_press__(self.ACTION_BRIGHTNESS_UP_LONG, *args)
242
+
243
+    def __ui_button_down__(self, *args):
244
+        self.send_device_status({self.KEY_ACTION: self.ACTION_BRIGHTNESS_DOWN})
245
+
246
+    def __ui_button_down_long__(self, *args):
247
+        self.__ui_button_long_press__(self.ACTION_BRIGHTNESS_DOWN_LONG, *args)
248
+
249
+    def __ui_button_long_press__(self, action, *args):
250
+        try:
251
+            dt = float(args[0])
252
+        except (IndexError, ValueError):
253
+            print("You need to give a numeric argument to define the period.")
254
+        else:
255
+            if not self.__device_in_use__:
256
+                self.__device_in_use__ = True
257
+                self.send_device_status({self.KEY_ACTION: action})
258
+                time.sleep(dt)
259
+                self.send_device_status({self.KEY_ACTION: self.ACTION_RELEASE[action]})
260
+                self.__device_in_use__ = False
261
+
262
+    def __ui_button_long_release__(self):
263
+        self.__device_in_use__ = False

+ 1
- 1
home.py Прегледај датотеку

@@ -1,4 +1,4 @@
1
-import devdi.props as props
1
+from devdi import topic as props
2 2
 
3 3
 
4 4
 def functions(pd):

+ 2
- 3
home_emulation.py Прегледај датотеку

@@ -1,5 +1,5 @@
1 1
 import config
2
-import devdi
2
+import devdi.devices
3 3
 import home
4 4
 import logging
5 5
 import mqtt
@@ -7,7 +7,6 @@ import os
7 7
 import report
8 8
 import user_interface
9 9
 
10
-# TODO: Add some more ui commands for devices
11 10
 # TODO: Add some test for smart_brain
12 11
 # TODO: Implementation of missing devices in devices/__init__.py
13 12
 
@@ -33,7 +32,7 @@ if __name__ == "__main__":
33 32
     #
34 33
     # Smarthome physical Devices
35 34
     #
36
-    pd = devdi.physical_devices(mc)
35
+    pd = devdi.devices.physical_devices(mc)
37 36
 
38 37
     #
39 38
     # Smart Home Functionality

Loading…
Откажи
Сачувај