forked from thernst-de/smarthome.plugin.autoblind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoBlindCondition.py
352 lines (309 loc) · 15 KB
/
AutoBlindCondition.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
#!/usr/bin/env python3
# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab
#########################################################################
# Copyright 2014-2015 Thomas Ernst [email protected]
#########################################################################
# This file is part of SmartHome.py.
#
# 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/>.
#########################################################################
from . import AutoBlindTools
from . import AutoBlindLogger
from . import AutoBlindCurrent
from . import AutoBlindConditionValue
# Class representing a single condition
class AbCondition:
# Name of condition
@property
def name(self):
return self.__name
# Error in condition
@property
def error(self):
return self.__error
# Initialize the condition
# smarthome: Instance of smarthome.py-class
# name: Name of condition
def __init__(self, smarthome, name: str):
self.__sh = smarthome
self.__name = name
self.__item = None
self.__eval = None
self.__value = AutoBlindConditionValue.AbConditionValue(self.__sh, "value")
self.__min = AutoBlindConditionValue.AbConditionValue(self.__sh, "min")
self.__max = AutoBlindConditionValue.AbConditionValue(self.__sh, "max")
self.__negate = False
self.__agemin = AutoBlindConditionValue.AbConditionValue(self.__sh, "agemin")
self.__agemax = AutoBlindConditionValue.AbConditionValue(self.__sh, "agemax")
self.__agenegate = None
self.__error = None
# set a certain function to a given value
# func: Function to set ('item', 'eval', 'value', 'min', 'max', 'negate', 'agemin', 'agemax' or 'agenegate'
# value: Value for function
def set(self, func, value):
if func == "as_item":
self.__set_item(value)
elif func == "as_eval":
self.__eval = value
if func == "as_value":
self.__value.set(value, self.__name)
elif func == "as_min":
self.__min.set(value, self.__name)
elif func == "as_max":
self.__max.set(value, self.__name)
elif func == "as_agemin":
self.__agemin.set(value, self.__name)
elif func == "as_agemax":
self.__agemax.set(value, self.__name)
elif func == "as_negate":
self.__negate = value
elif func == "as_agenegate":
self.__agenegate = value
# Complete condition (do some checks, cast value, min and max based on item or eval data types)
# item_state: item to read from
# abitem_object: Related AbItem instance for later determination of current age and current delay
# logger: Instance of AbLogger to write log messages to
def complete(self, item_state, abitem_object):
# check if it is possible to complete this condition
if self.__min.is_empty() and self.__max.is_empty() and self.__value.is_empty() \
and self.__agemin.is_empty() and self.__agemax.is_empty():
return False
# set 'eval' for some known conditions if item and eval are not set, yet
if self.__item is None and self.__eval is None:
if self.__name == "weekday":
self.__eval = AutoBlindCurrent.values.get_weekday
elif self.__name == "sun_azimut":
self.__eval = AutoBlindCurrent.values.get_sun_azimut
elif self.__name == "sun_altitude":
self.__eval = AutoBlindCurrent.values.get_sun_altitude
elif self.__name == "age":
self.__eval = abitem_object.get_age
elif self.__name == "delay":
self.__eval = abitem_object.get_delay
elif self.__name == "time":
self.__eval = AutoBlindCurrent.values.get_time
elif self.__name == "random":
self.__eval = AutoBlindCurrent.values.get_random
elif self.__name == "month":
self.__eval = AutoBlindCurrent.values.get_month
elif self.__name == "laststate":
self.__eval = abitem_object.get_laststate_id
# missing item in condition: Try to find it
if self.__item is None:
result = AutoBlindTools.find_attribute(self.__sh, item_state, "as_item_" + self.__name)
if result is not None:
self.__set_item(result)
# missing eval in condition: Try to find it
if self.__eval is None:
result = AutoBlindTools.find_attribute(self.__sh, item_state, "as_eval_" + self.__name)
if result is not None:
self.__eval = result
# no we should have either 'item' or 'eval' set. If not ... very bad ....
if self.__item is None and self.__eval is None:
self.__error = "Condition {}: Neither 'item' nor 'eval' given!".format(self.__name)
raise ValueError(self.__error)
# cast stuff
try:
if self.__item is not None:
self.__cast_all(self.__item.cast)
elif self.__name in ("weekday", "sun_azimut", "sun_altitude", "age", "delay", "random", "month"):
self.__cast_all(AutoBlindTools.cast_num)
elif self.__name == "laststate":
self.__cast_all(AutoBlindTools.cast_str)
elif self.__name == "time":
self.__cast_all(AutoBlindTools.cast_time)
except Exception as ex:
self.__error = str(ex)
raise ValueError(self.__error)
# 'min' must not be greater than 'max'
if self.__min.get_type() == "value" and self.__max.get_type() == "value":
if self.__min.get() > self.__max.get():
self.__error = "Condition {}: 'min' must not be greater than 'max'!".format(self.__name)
raise ValueError(self.__error)
# 'agemin' and 'agemax' can only be used for items, not for eval
if self.__item is None and not (self.__agemin.is_empty() and self.__agemax.is_empty()):
self.__error = "Condition {}: 'agemin'/'agemax' can not be used for eval!".format(self.__name)
raise ValueError(self.__error)
return True
# Check if condition is matching
# logger: Instance of AbLogger to write log messages
def check(self, logger: AutoBlindLogger.AbLogger):
# Ignore if errors occured during preparing
if self.__error is not None:
logger.info("condition'{0}': Ignoring because of error: {1}", self.__name, self.__error)
return True
# Ignore if no current value can be determined (should not happen as we check this earlier, but to be sure ...)
if self.__item is None and self.__eval is None:
logger.info("condition '{0}': No item or eval found! Considering condition as matching!", self.__name)
return True
if not self.__check_value(logger):
return False
if not self.__check_age(logger):
return False
return True
# Write condition to logger
# logger: Instance of AbLogger to write to
def write_to_logger(self, logger: AutoBlindLogger.AbLogger):
if self.__error is not None:
logger.debug("error: {0}", self.__error)
if self.__item is not None:
logger.debug("item: {0}", self.__item.id())
if self.__eval is not None:
logger.debug("eval: {0}", self.__get_eval_name())
self.__value.write_to_logger(logger)
self.__min.write_to_logger(logger)
self.__max.write_to_logger(logger)
if self.__negate is not None:
logger.debug("negate: {0}", self.__negate)
self.__agemin.write_to_logger(logger)
self.__agemax.write_to_logger(logger)
if self.__agenegate is not None:
logger.debug("age negate: {0}", self.__agenegate)
# Cast 'value', 'min' and 'max' using given cast function
# cast_func: cast function to use
def __cast_all(self, cast_func):
self.__value.set_cast(cast_func)
self.__min.set_cast(cast_func)
self.__max.set_cast(cast_func)
if self.__negate is not None:
self.__negate = AutoBlindTools.cast_bool(self.__negate)
self.__agemin.set_cast(AutoBlindTools.cast_num)
self.__agemax.set_cast(AutoBlindTools.cast_num)
if self.__agenegate is not None:
self.__agenegate = AutoBlindTools.cast_bool(self.__agenegate)
# Check if value conditions match
# logger: Instance of AbLogger to write to
def __check_value(self, logger: AutoBlindLogger.AbLogger):
current = self.__get_current()
try:
if not self.__value.is_empty():
# 'value' is given. We ignore 'min' and 'max' and check only for the given value
value = self.__value.get()
# If current and value have different types, convert both to string
if type(value) != type(current):
value = str(value)
current = str(current)
logger.debug("Condition '{0}': value={1} negate={2} current={3}", self.__name, value,
self.__negate, current)
logger.increase_indent()
if self.__negate:
if current != value:
logger.debug("not OK but negated -> matching")
return True
else:
if current == value:
logger.debug("OK -> matching")
return True
logger.debug("not OK -> not matching")
return False
else:
min_value = self.__min.get()
max_value = self.__max.get()
# 'value' is not given. We check 'min' and 'max' (if given)
logger.debug("Condition '{0}': min={1} max={2} negate={3} current={4}",
self.__name, min_value, max_value, self.__negate, current)
logger.increase_indent()
if min_value is None and max_value is None:
logger.debug("no limit given -> matching")
return True
if not self.__negate:
if min_value is not None and current < min_value:
logger.debug("to low -> not matching")
return False
if max_value is not None and current > max_value:
logger.debug("to high -> not matching")
return False
else:
if min_value is not None and current > min_value and (max_value is None or current < max_value):
logger.debug("not lower than min -> not matching")
return False
if max_value is not None and current < max_value and (min_value is None or current > min_value):
logger.debug("not higher than max -> not matching")
return False
logger.debug("given limits ok -> matching")
return True
finally:
logger.decrease_indent()
# Check if age conditions match
# logger: Instance of AbLogger to write to
def __check_age(self, logger: AutoBlindLogger.AbLogger):
# No limits given -> OK
if self.__agemin.is_empty() and self.__agemax.is_empty():
logger.info("Age of '{0}': No limits given", self.__name)
return True
# Ignore if no current value can be determined (should not happen as we check this earlier, but to be sure ...)
if self.__item is None:
logger.info("Age of '{0}': No item found! Considering condition as matching!", self.__name)
return True
current = self.__item.age()
agemin = None if self.__agemin.is_empty() else self.__agemin.get()
agemax = None if self.__agemax.is_empty() else self.__agemax.get()
try:
# We check 'min' and 'max' (if given)
logger.debug("Age of '{0}': min={1} max={2} negate={3} current={4}",
self.__name, agemin, agemax, self.__agenegate, current)
logger.increase_indent()
if not self.__agenegate:
if agemin is not None and current < agemin:
logger.debug("to young -> not matching")
return False
if agemax is not None and current > agemax:
logger.debug("to old -> not matching")
return False
else:
if agemin is not None and current > agemin and (agemax is None or current < agemax):
logger.debug("not younger than min -> not matching")
return False
if agemax is not None and current < agemax and (agemin is None or current > agemin):
logger.debug("not older than max -> not matching")
return False
logger.debug("given age limits ok -> matching")
return True
finally:
logger.decrease_indent()
# set item
# item: value for item
def __set_item(self, item):
if isinstance(item, str):
self.__item = self.__sh.return_item(item)
else:
self.__item = item
# Current value of condition (based on item or eval)
def __get_current(self):
if self.__item is not None:
# noinspection PyCallingNonCallable
return self.__item()
if self.__eval is not None:
if isinstance(self.__eval, str):
# noinspection PyUnusedLocal
sh = self.__sh
try:
value = eval(self.__eval)
except Exception as e:
raise ValueError("Condition {}: problem evaluating {}: {}".format(self.__name, str(self.__eval), e))
else:
return value
else:
# noinspection PyCallingNonCallable
return self.__eval()
raise ValueError("Condition {}: Neither 'item' nor eval given!".format(self.__name))
# Name of eval-Object to be displayed in log
def __get_eval_name(self):
if self.__item is not None or self.__eval is None:
return None
if self.__eval is not None:
if isinstance(self.__eval, str):
return self.__eval
else:
return self.__eval.__module__ + "." + self.__eval.__name__