-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
453 lines (383 loc) · 16.9 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python3
# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab
#########################################################################
# Copyright 2013 KNX-User-Forum e.V. http://knx-user-forum.de/
#########################################################################
# This file is part of SmartHome.py. http://mknx.github.io/smarthome/
#
# SmartHome.py is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SmartHome.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SmartHome.py. If not, see <http://www.gnu.org/licenses/>.
#########################################################################
import logging
import time
from lib.model.smartplugin import *
from lib.item import Items
IDENTICICATION = 'Roto'
IMPULS = 1
UP = 0
DOWN = 1
OFF = 2
class Roto(SmartPlugin):
ALLOW_MULTIINSTANCE = False
PLUGIN_VERSION = "1.6.2"
def __init__(self, smarthome):
self.logger.info('Init roto plugin')
self.logger.debug(f"init {__name__}")
self.alive = False
self._sh = smarthome
self.itemsApi = Items.get_instance()
self.__roto_items = {}
def run(self):
self.alive = True
count_items = 0
for item in self.itemsApi.find_items("roto_plugin"):
if item.conf["roto_plugin"] == "active":
try:
roto_item = RotoItem(self._sh, item)
self.__roto_items[roto_item.id] = roto_item
count_items += 1
except ValueError as ex:
self.logger.error("Item: {0}: {1}".format(item.id(), str(ex)))
self.logger.info("Using Roto for {0} Items".format(count_items))
def stop(self):
self.alive = False
def parse_item(self, item):
if 'roto_plugin' in item.conf and item.conf["roto_plugin"] == "active":
self.logger.debug("parse item: {0}".format(item))
return self.update_item
else:
return None
def parse_logic(self, logic):
if 'xxx' in logic.conf:
# self.function(logic['name'])
pass
def update_item(self, item, caller=None, source=None, dest=None):
orig_caller, orig_source, orig_item = self.get_original_caller(self._sh, caller, source, item)
if orig_caller == IDENTICICATION or caller == IDENTICICATION:
self.logger.debug("Ignoring changes from " + IDENTICICATION)
return
item_source = self._sh.return_item(source)
self.logger.info("update item: {0} orig_caller: {1} source: {2} item_source: {3}".format(item.id(), orig_caller, source, item_source))
if item.id() in self.__roto_items:
roto_item = self.__roto_items[item.id()]
else:
return
if (source == item.conf['roto_stop']):
if (item_source() == UP):
roto_item.roto_step_up()
else:
roto_item.roto_step_down()
if (source == item.conf['roto_up_down']):
if (item_source() == UP):
roto_item.roto_up_total()
else:
roto_item.roto_down_total()
if (source == item.conf['roto_position']):
value = 0
if caller != 'Eval':
value = item()
else:
value = orig_item()
self.logger.debug("roto set position: {0}".format(value))
#self.logger.info("roto set position orig_caller: {0}".format(orig_caller))
roto_item.roto_position(value)
# determine original caller/source
# smarthome: instance of smarthome.py
# caller: caller
# source: source
def get_original_caller(self, smarthome, caller, source, item=None):
original_caller = caller
original_source = source
original_item = item
while original_caller == "Eval":
original_item = smarthome.return_item(original_source)
if original_item is None:
break
original_changed_by = original_item.changed_by()
if ":" not in original_changed_by:
break
original_caller, __, original_source = original_changed_by.partition(":")
if item is None:
return original_caller, original_source
else:
return original_caller, original_source, original_item
# Class
class RotoItem:
# return item id
@property
def id(self):
return self.__id
# time counter
@property
def time_on(self):
return self.__time_on
@time_on.setter
def time_on(self, value):
self.__time_on = value
# time counter
@property
def time_off(self):
return self.__time_off
@time_off.setter
def time_off(self, value):
self.__time_off = value
@property
def time_up(self):
return self.__time_up
@property
def time_down(self):
return self.__time_down
@property
def time_step(self):
return self.__time_step
@property
def item_up(self):
return self.__item_up
@property
def item_down(self):
return self.__item_down
# time counter
@property
def position(self):
return self.__position
@position.setter
def position(self, value):
self.__position = value
# return instance of smarthome.py class
@property
def sh(self):
return self.__sh
# Constructor
# smarthome: instance of smarthome.py
# item: item to use
def __init__(self, smarthome, item):
self.logger = logging.getLogger(__name__)
self.__sh = smarthome
self.__item = item
self.__id = self.__item.id()
self.__name = str(self.__item)
self.__time_on = self.__sh.now()
self.__time_off = self.__sh.now()
self.__time_last_loop = self.__sh.now()
self.__actor_type = 'none'
self.__time_up = 60
if 'roto_time_up' in item.conf:
self.__time_up = int(item.conf['roto_time_up'])
self.__time_down = 60
if 'roto_time_down' in item.conf:
self.__time_down = int(item.conf['roto_time_down'])
self.__cycle_time = 5
if 'roto_cycle_time' in item.conf:
self.__cycle_time = int(item.conf['roto_cycle_time'])
self.__time_step = 10
if 'roto_time_step' in item.conf:
self.__time_step = int(item.conf['roto_time_step'])
if 'roto_actor_open' in item.conf and 'roto_actor_close' in item.conf:
self.__item_up = self.__sh.return_item(item.conf['roto_actor_open'])
self.__item_down = self.__sh.return_item(item.conf['roto_actor_close'])
self.__actor_type = 'roto'
elif 'roto_actor_up_down' in item.conf and 'roto_actor_stop' in item.conf:
self.__item_up = self.__sh.return_item(item.conf['roto_actor_up_down'])
self.__item_down = self.__sh.return_item(item.conf['roto_actor_stop'])
self.__actor_type = 'jalousie'
else:
self.logger.error("roto Aktor-Items fehlen oder fehlerhaft")
if 'roto_position' in item.conf:
self.__item_position = self.__sh.return_item(item.conf['roto_position'])
else:
self.logger.error("roto Positions-Items fehlt oder fehlerhaft")
self.__delays = []
self.__position = self.__item_position()
self.__position_time = self.__time_up / 100 * self.__position
self.__direction = OFF
def roto_position(self, value):
if self.__direction != OFF:
self.roto_stop()
time.sleep(IMPULS+1)
if self.__direction != OFF:
self.logger.info("roto Position kann nicht angefahren werden da der Antrieb bereits faehrt: {0}".format(self.__position))
return
new_pos = value
old_pos = self.__position
self.logger.debug("roto_position old:{0} new:{1}".format(old_pos, new_pos))
# Auffahren
if new_pos > old_pos:
diff_pos = new_pos - old_pos
diff_time = self.__time_up / 100 * diff_pos
self.roto_down(diff_time)
#Zufahren:
elif new_pos < old_pos:
diff_pos = old_pos - new_pos
diff_time = self.__time_down / 100 * diff_pos
self.roto_up(diff_time)
def roto_up(self, value):
if self.__direction != OFF:
self.roto_stop()
else:
if self.__actor_type == 'roto':
self.__item_down(0)
self.__item_up(1)
self.__direction = UP
self.__time_on = self.__sh.now()
self.__time_last_loop = self.__sh.now()
self.roto_add_delay(UP, 0, IMPULS)
self.roto_add_delay(UP, 1, value)
self.roto_add_delay(UP, 0, value + IMPULS)
elif self.__actor_type == 'jalousie':
self.__item_up(UP)
self.__direction = UP
self.__time_on = self.__sh.now()
self.__time_last_loop = self.__sh.now()
#self.roto_add_delay(DOWN, 0, IMPULS)
self.roto_add_delay(DOWN, 1, value)
#self.roto_add_delay(DOWN, 0, value + IMPULS)
def roto_down(self, value):
if self.__direction != OFF:
self.roto_stop()
else:
if self.__actor_type == 'roto':
self.__item_up(0)
self.__item_down(1)
self.__direction = DOWN
self.__time_on = self.__sh.now()
self.__time_last_loop = self.__sh.now()
self.roto_add_delay(DOWN, 0, IMPULS)
self.roto_add_delay(DOWN, 1, value)
self.roto_add_delay(DOWN, 0, value + IMPULS)
elif self.__actor_type == 'jalousie':
self.__item_up(DOWN)
self.__direction = DOWN
self.__time_on = self.__sh.now()
self.__time_last_loop = self.__sh.now()
#self.roto_add_delay(DOWN, 0, IMPULS)
self.roto_add_delay(DOWN, 1, value)
#self.roto_add_delay(DOWN, 0, value + IMPULS)
def roto_up_total(self):
self.roto_up(self.__time_up)
def roto_down_total(self):
self.roto_down(self.__time_down)
def roto_step_up(self):
self.roto_up(self.__time_step)
def roto_step_down(self):
self.roto_down(self.__time_step)
def roto_stop(self):
if self.__actor_type == 'roto':
if self.__direction == UP:
self.__item_down(0)
self.__item_up(1)
self.logger.debug("roto_stop Fahrt-AUS Position: {0}".format(self.__position))
#self.__direction = UP
#self.__time_on = self.__sh.now()
self.__delays = []
self.roto_add_delay(UP, 0, IMPULS)
if self.__direction == DOWN:
self.__item_up(0)
self.__item_down(1)
self.logger.debug("roto_stop Fahrt-AUS Position: {0}".format(self.__position))
#self.__direction = DOWN
#self.__time_on = self.__sh.now()
self.__delays = []
self.roto_add_delay(DOWN, 0, IMPULS)
elif self.__actor_type == 'jalousie':
if self.__direction is not OFF:
self.__item_down(1)
self.logger.debug("roto_stop Fahrt-AUS Position: {0}".format(self.__position))
self.__delays = []
self.__direction = OFF
def roto_add_delay(self, direction, value, delay):
#if not item in self.__delays:
x_delay = {}
x_delay.update({'delay': delay})
x_delay.update({'direction': direction})
x_delay.update({'value': value})
self.__delays.append(x_delay)
self.logger.debug("roto_add_delay: {0}".format(x_delay))
if self.__id not in self.__sh.scheduler:
s = self.__sh.scheduler.add(self.__id, self.roto_loop, prio=5, offset = 2, cycle=int(self.__cycle_time))
#else:
# self.__sh.scheduler.change(self.__id, active=True)
# wird ueber scheduler aufgerufen
def roto_loop(self):
# Wenn keine Eintraege vorhanden sind, kann scheduler deaktiviert werden
self.logger.debug("roto_loop: delays{0} ".format(len(self.__delays)))
if (len(self.__delays)) == 0:
if self.__id in self.__sh.scheduler:
self.logger.debug("roto_loop STOP scheduler")
self.__sh.scheduler.remove(self.__id)
self.__direction = OFF
#self.__sh.scheduler.change(self.__id, active=False)
else:
self.logger.debug("roto_loop keine Eintraege in delays vorhanden")
#self.__sh.scheduler.change(self.__id, active=False)
else:
time_on_sec = (self.__sh.now() - self.__time_on).total_seconds()
# delays-Liste sortieren
from operator import itemgetter
x_delays = [x for x in self.__delays]
x_delays.sort(key=itemgetter('delay'), reverse=False)
if len(x_delays) > 0:
x_delay = x_delays[0]
else:
self.logger.debug("roto_loop keine Eintraege in x_delays vorhanden")
return
# Berechnen der Position Ueber die Fahrzeit
self.roto_calc_pos()
self.__time_last_loop = self.__sh.now()
self.logger.debug("roto_loop Position: {0}".format(self.__position))
# Impuls ausschalten
if x_delay['value'] == 0 and time_on_sec > x_delay['delay']:
if self.__direction == UP:
self.__item_up(x_delay['value'])
elif self.__direction == DOWN:
self.__item_down(x_delay['value'])
else:
self.__item_up(0)
self.__item_down(0)
self.__delays.remove(x_delay)
self.logger.debug("roto_loop Impuls-AUS Position: {0}".format(self.__position))
#Fahrt aus
elif self.__direction != OFF and x_delay['value'] == 1 and time_on_sec > x_delay['delay']:
if self.__actor_type == 'roto': # Impuls geben fuer Fahr aus
if self.__direction == UP:
self.__item_up(x_delay['value'])
elif self.__direction == DOWN:
self.__item_down(x_delay['value'])
self.__time_off = self.__sh.now()
self.__direction = OFF
self.__delays.remove(x_delay)
self.logger.debug("roto_loop Fahrt-AUS Position: {0} ".format(self.__position))
elif self.__actor_type == 'jalousie':
if x_delay['direction'] == DOWN:
self.__item_down(x_delay['value'])
self.__time_off = self.__sh.now()
self.__direction = OFF
self.__delays.remove(x_delay)
self.logger.debug("roto_loop Fahrt-AUS Position: {0} ".format(self.__position))
#self.logger.info("roto_loop")
def roto_calc_pos(self):
time_loop_sec = (self.__sh.now() - self.__time_last_loop).total_seconds()
if self.__time_on > self.__time_off:
if self.__direction == DOWN:
self.__position_time += time_loop_sec
if self.__position_time > self.__time_up:
self.__position_time = self.__time_up
self.__position = self.__position_time * 100 / self.__time_up
elif self.__direction == UP:
self.__position_time -= time_loop_sec
if self.__position_time < 0:
self.__position_time = 0
self.__position = self.__position_time * 100 / self.__time_down
self.__item_position(self.__position, caller=IDENTICICATION) # aktualisiere das Item
return self.__position
else:
self.logger.error("roto aktuelle Position konnte nicht berechnet werden: {0} {1} ".format(self.__time_on, self.__time_off))