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