-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessory.py
executable file
·373 lines (320 loc) · 12.8 KB
/
accessory.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
#!/usr/bin/env python3
"""
accessory.py
Copyright (C) 2016 hjltu@ya.ru
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
import statdb
import time
import math
import datetime as dt
class Start(object):
""" add homekit name in database if its not exist
"""
def __init__(self, acc, prefix='/homekit/'):
self.prefix = prefix
self.name = acc['acc']
self.comm = acc['comm']
self.stat = acc['stat']
self.comm1 = acc['comm1']
self.stat1 = acc['stat1']
self.comm2 = acc['comm2']
self.stat2 = acc['stat2']
self.comm3 = acc['comm3']
self.stat3 = acc['stat3']
statdb.create(self.name)
if statdb.select(self.name) is None:
statdb.insert(self.name, 0)
if 'dimm' in self.name:
statdb.create(self.name+'_dimm')
if statdb.select(self.name+'_dimm') is None:
statdb.insert(self.name+'_dimm', 0)
if 'day' in self.stat2:
statdb.create(self.name+'_day')
if 'night' in self.stat3:
statdb.create(self.name+'_nigth')
def get_status(self, pause=.1):
time.sleep(pause)
stat = list(statdb.select(self.name).values())[0]
out = (self.prefix+self.name+'-stat', stat)
if 'dimm' in self.name and stat == 1:
dimm = list(statdb.select(self.name+'_dimm').values())[0]
out += (self.prefix+self.name+'-dimm', dimm)
return out
class Rele(Start):
def event(self, top, msg):
if top==self.stat:
statdb.insert(self.name, msg)
return (self.prefix+self.name+'-stat', msg)
if top==self.prefix+self.name:
return (self.comm, msg)
class Count(Start):
def event(self, top, msg):
if top==self.stat:
statdb.insert(self.name, msg)
return (self.prefix+self.name+'-stat', msg)
class Electro(Start):
""" 2-tariff electric counter """
def __init__(self, acc, dimm=None, day='_day', night='_night'):
super(Electro, self).__init__(acc, dimm=None, day='_day', night='_night')
self.countNew = 0
self.countOld = 0
self.countDay = (statdb.select(self.prefix+self.name+"_day")).values()[0]
self.countNight = (statdb.select(self.prefix+self.name+"_night")).values()[0]
def event(self, top, msg, buf=100):
if top=='/wemos'+wemos_name+'/out/'+wemos_pin:
self.countNew = self.countNew + 1
if self.countNew > self.countOld + buf:
statdb.insert(self.name, msg)
if self.tariff() == 1:
self.countDay = self.countDay+(self.countNew-self.countOld)
statdb.insert(self.name+"_day", self.countDay)
if self.tariff() == 2:
self.countNight = self.countNight+(self.countNew-self.countOld)
statdb.insert(self.name+"_night", self.countNight)
self.countOld = self.countNew
#return (self.prefix+self.name+'-stat',msg)
def tariff(self, t1=7, t2=23):
h = dt.datetime.now().hour
if h >= t1 and h < t2:
return 1 # Day
if h >= t2 or h < t1:
return 2 # Night
class Short(Start):
""" no homekit """
def __init__(self, acc):
super().__init__(acc)
try:
self.comm = self.comm.split('.')
except:
pass
def event(self, top, msg):
if top == self.stat and msg == 1:
stat = list(statdb.select(self.name).values())[0]
if stat==0:
val=1
if stat==1:
val=0
out=()
statdb.insert(self.name, val)
for comm in self.comm:
if comm[0] is "/":
out+=(comm, val)
else:
out += ("/knx/in",'{"dstgad":"'+comm+'","value":"'+str(val)+'","dpt":"1"}')
return out
class Scene(Short):
def event(self, top, msg):
if top == self.stat and msg == 1:
out, val = (), None
stat = list(statdb.select(self.name).values())[0]
if stat==0:
statdb.insert(self.name, 1)
val=self.comm1
else:
statdb.insert(self.name, 0)
val=self.stat1
for comm in self.comm:
if comm[0] is "/":
out+=(comm, val)
else:
out += ("/knx/in",'{"dstgad":"'+comm+'","value":"'+str(val)+'","dpt":"5"}')
return out
class Switch(Start):
def event(self, top, msg):
if top == self.stat and msg == 2:
stat = list(statdb.select(self.name).values())[0]
if stat == 0:
statdb.insert(self.name, 1)
return (self.prefix+self.name+'-stat', 1)
if stat == 1:
statdb.insert(self.name, 0)
return (self.prefix+self.name+'-stat', 0)
if top==self.prefix+self.name:
statdb.insert(self.name, msg)
return (self.prefix+self.name+'-stat', msg)
class Sensor(Start):
""" Temp, Hum, Leak, Motion """
def __init__(self, acc):
super().__init__(acc)
self.msg = None
def event(self, top, msg):
if top == self.stat:
if self.msg != msg:
self.msg = msg
statdb.insert(self.name, msg)
return (self.prefix+self.name+'-curr', msg)
class NTC_Sensor(Start):
""" for NTC 10kOhm thermistor, msg from analog input(ADC) """
def __init__(self, name):
super(NTC_Sensor, self).__init__(self.prefix+self.name)
# list temperatures
self.samples=[None]*8
# NTC sensor resistance from ADC
self.res=lambda x: 10000/(1025/x-1)
# Temperature. Steinhart-Hart equation
# B=?3430? for (-55..+125)
# B=4141 for (-55..+155)
self.temp=lambda b,y: 1/(1/298.3+math.log(self.res(y)/10000)/b)-273.3
def my_temp(self,msg,B):
if msg not in self.samples or None in self.samples:
self.samples.pop()
self.samples.insert(0,msg)
try:
sort = sorted(self.samples)
while len(sort)>4:
sort.pop()
sort.pop(0)
ma=sum(sort)/len(sort)
except:
return
try:
temp=self.temp(float(B),float(ma))
except Exception as e:
print("ERR:",str(e))
return round(temp, 1)
def event(acc,B,top,msg):
if top=="/"+name+"/out/"+pin:
temp=(self.my_temp(msg,B))
if temp<150 and temp>-50:
if self.db > temp+1 or self.db < temp-1: # step one degree
self.db=temp
statdb.insert(self.name, temp)
return (self.prefix+self.name+"-curr", temp)
class NTC_Term(Start):
""" Thermoregulator for NTC thermistor
inputs:
curr: input device
self.prefix+self.name: name
name2,pin2: output device
mode: 0-heating, 1-cooling """
def event(self, top, msg, mode=0):
if top == self.prefix+self.name+curr+"-curr":
target = (statdb.select(self.name)).values[0]
if type(target) is str:
target = float(target)
else:
print("ERR:", self.prefix+self.name, "target", target)
return
if target<150 and target>-50:
if float(msg)<target-0.5:
if mode==0:
out = ("/"+name2+"/in/"+pin2, 1)
if mode==1:
out = ("/"+name2+"/in/"+pin2, 0)
return out
if float(msg)>target+0.5:
if mode==0:
out = ("/"+name2+"/in/"+pin2, 0)
if mode==1:
out = ("/"+name2+"/in/"+pin2, 1)
return out
if top==self.prefix+self.name+'-target':
statdb.insert(self.name, msg)
return (self.prefix+self.name+'-stat', msg)
class Term(Start):
def __init__(self, acc, prefix='/homekit/'):
super().__init__(acc, prefix='/homekit/')
statdb.create(self.name+'_target')
statdb.create(self.name+'_mode')
if statdb.select(self.name+'_target') is None:
statdb.insert(self.name+'_target', 22)
if statdb.select(self.name+'_mode') is None:
statdb.insert(self.name+'_mode', 1)
def get_status(self, pause=.1):
time.sleep(pause)
self.target = list(statdb.select(self.name+'_target').values())[0]
self.mode = list(statdb.select(self.name+'_mode').values())[0]
out = (self.prefix+self.name+'-target-stat', self.target)
out += (self.prefix+self.name+'-mode-stat', self.mode)
return out
def event(self, top, msg):
if top == self.stat:
out = (self.prefix+self.name+'-curr', msg)
if self.mode == 1: # heatig
if int(msg) < self.target:
out += (self.comm2, 1)
if int(msg) >= self.target:
out += (self.comm2, 0)
if self.mode == 2: # cooling
if int(msg) > self.target:
out += (self.comm3, 1)
if int(msg) <= self.target:
out += (self.comm3, 0)
return out
if top==self.prefix+self.name+'-target':
self.target = int(msg)
statdb.insert(self.name+'_target', self.target)
return (self.prefix+self.name+'-target-stat', self.target)
if top==self.prefix+self.name+'-mode':
out = ()
if int(msg) == 1:
self.mode = int(msg)
out += (self.comm3, 0)
if int(msg) == 2:
self.mode = int(msg)
out += (self.comm2, 0)
if int(msg) == 0:
self.mode = int(msg)
out += (self.comm2, 0, self.comm3, 0)
statdb.insert(self.name+'_mode', self.mode)
out += (self.prefix+self.name+'-mode-stat', self.mode)
return out
# in use stat
if top==self.stat2: # heat
return (self.prefix+self.name+'-use-stat', msg)
if top==self.stat3: # cool
if float(msg) > 0:
return (self.prefix+self.name+'-use-stat', 2)
else:
return (self.prefix+self.name+'-use-stat', 0)
class Dimmer(Start):
def event(self, top, msg):
if top == self.stat:
if int(msg) == 0:
statdb.insert( self.name, msg)
return (self.prefix+self.name+'-stat',msg)
if int(msg) > 0:
statdb.insert(self.name, 1)
statdb.insert(self.name+'_dimm', msg)
return (self.prefix+self.name+'-stat',1,self.prefix+self.name+'-dimm-stat',msg)
if top==self.prefix+self.name:
if int(msg) == 0:
return (self.comm, 0)
if int(msg) == 1:
time.sleep(.2)
pwm = list(statdb.select(self.name+'_dimm').values())[0]
if pwm == 0:
pwm = 255
return (self.comm, pwm)
if top==self.prefix+self.name+'-dimm':
return (self.comm, int(float(msg)))
class Somfy(Start):
def __init__(self, acc, prefix='/homekit/'):
super().__init__(acc, prefix='/homekit/')
statdb.create(self.name)
if statdb.select(self.name) is None:
statdb.insert(self.name, 0)
def event(self, top, msg):
if top == self.stat:
msg = int(254./float(self.comm1)*float(msg))
statdb.insert(self.name, msg)
return (self.prefix+self.name+'-stat', msg)
if top == self.prefix+self.name:
msg = int(float(self.comm1)/254.*float(msg))
return (self.comm, int(float(msg)))