-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telldus.py
287 lines (216 loc) · 7.62 KB
/
Telldus.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
import logging
import platform
from Settings import Settings
from ctypes import c_int, c_ubyte, c_void_p, c_char_p, POINTER, string_at
#Device methods
TELLSTICK_TURNON = 1
TELLSTICK_TURNOFF = 2
TELLSTICK_BELL = 4
TELLSTICK_TOGGLE = 8
TELLSTICK_DIM = 16
TELLSTICK_LEARN = 32
TELLSTICK_EXECUTE = 64
TELLSTICK_UP = 128
TELLSTICK_DOWN = 256
TELLSTICK_STOP = 512
methodsReadable = {1: 'ON',
2: 'OFF',
4: 'BELL',
8: 'TOGGLE',
16: 'DIM',
32: 'LEARN',
64: 'EXECUTE',
128: 'UP',
256: 'DOWN',
512: 'STOP'}
#Sensor value types
TELLSTICK_TEMPERATURE = 1
TELLSTICK_HUMIDITY = 2
sensorValueTypeReadable = {TELLSTICK_TEMPERATURE: 'Temperature',
TELLSTICK_HUMIDITY: 'Humidity'}
#Error codes
TELLSTICK_SUCCESS = 0
TELLSTICK_ERROR_NOT_FOUND = -1
TELLSTICK_ERROR_PERMISSION_DENIED = -2
TELLSTICK_ERROR_DEVICE_NOT_FOUND = -3
TELLSTICK_ERROR_METHOD_NOT_SUPPORTED = -4
TELLSTICK_ERROR_COMMUNICATION = -5
TELLSTICK_ERROR_CONNECTING_SERVICE = -6
TELLSTICK_ERROR_UNKNOWN_RESPONSE = -7
TELLSTICK_ERROR_SYNTAX = -8
TELLSTICK_ERROR_BROKEN_PIPE = -9
TELLSTICK_ERROR_COMMUNICATING_SERVICE = -10
TELLSTICK_ERROR_CONFIG_SYNTAX = -11
TELLSTICK_ERROR_UNKNOWN = -99
#Controller typedef
TELLSTICK_CONTROLLER_TELLSTICK = 1
TELLSTICK_CONTROLLER_TELLSTICK_DUO = 2
TELLSTICK_CONTROLLER_TELLSTICK_NET = 3
#Device changes
TELLSTICK_DEVICE_ADDED = 1
TELLSTICK_DEVICE_CHANGED = 2
TELLSTICK_DEVICE_REMOVED = 3
TELLSTICK_DEVICE_STATE_CHANGED = 4
#Change types
TELLSTICK_CHANGE_NAME = 1
TELLSTICK_CHANGE_PROTOCOL = 2
TELLSTICK_CHANGE_MODEL = 3
TELLSTICK_CHANGE_METHOD = 4
TELLSTICK_CHANGE_AVAILABLE = 5
TELLSTICK_CHANGE_FIRMWARE = 6
methodsSupportedDefault = 0
class Telldus:
def __init__(self, logLevel):
if platform.system() is 'Windows':
from ctypes import windll, WINFUNCTYPE
try:
self.tdlib = windll.LoadLibrary('TelldusCore.dll')
except WindowsError:
raise Exception('Telldus Core was not found, are you sure it is installed?')
else:
from ctypes import cdll
try:
self.tdlib = cdll.LoadLibrary('/usr/local/lib/libtelldus-core.so')
except OSError:
try:
self.tdlib = cdll.LoadLibrary('/usr/lib/libtelldus-core.so')
except OSError:
raise Exception('Telldus Core was not found, are you sure it is installed?')
if self.tdlib is None:
raise Exception('No Telldus Core library was found')
self.tdlib.tdInit()
logging.basicConfig(level=logLevel)
fileLog = logging.FileHandler(Settings.loggingPath)
self.logger = logging.getLogger('telldus')
self.logger.addHandler(fileLog)
def getNumberOfDevices(self):
return self.tdlib.tdGetNumberOfDevices()
def getDeviceId(self, idx):
return self.tdlib.tdGetDeviceId(int(idx))
def getDeviceIdFromStr(self, s):
try:
id = int(s)
deviceid = getDeviceId(id)
return deviceid, getName(deviceid)
except:
pass
for i in range(getNumberOfDevices()):
if s == getName(getDeviceId(i)):
return getDeviceId(i), s
return -1, 'UNKNOWN'
def getName(self, id):
getNameFunc = self.tdlib.tdGetName
getNameFunc.restype = c_void_p
vp = getNameFunc(id)
cp = c_char_p(vp)
s = cp.value
self.tdlib.tdReleaseString(vp)
return s
def turnOn(self, id):
self.logger.debug('sending turnOn() to device %d', int(id))
retval = self.tdlib.tdTurnOn(id)
if retval is not 0:
getErrorStringFunc = self.tdlib.tdGetErrorString
getErrorStringFunc.restype = c_void_p
vp = getErrorStringFunc(retval)
cp = c_char_p(vp)
s = cp.value
self.logger.error('error while trying to turn on device: %s', s)
self.tdlib.tdReleaseString(vp)
return retval
def turnOff(self, id):
self.logger.debug('sending turnOff() to device %d', id)
retval = self.tdlib.tdTurnOff(id)
if retval is not 0:
getErrorStringFunc = self.tdlib.tdGetErrorString
getErrorStringFunc.restype = c_void_p
vp = getErrorStringFunc(retval)
cp = c_char_p(vp)
s = cp.value
self.logger.error('error while trying to turn off device: %s', s)
self.tdlib.tdReleaseString(vp)
return retval
def dim(self, id, level):
self.logger.debug('sending dim(...) to device %d', id)
retval = self.tdlib.tdDim(id, level)
if retval is not 0:
getErrorStringFunc = self.tdlib.tdGetErrorString
getErrorStringFunc.restype = c_void_p
vp = getErrorStringFunc(retval)
cp = c_char_p(vp)
s = cp.value
self.logger.error('error while trying to dim device: %s', s)
self.tdlib.tdReleaseString(vp)
return retval
def learn(self, id):
self.logger.debug('sending learn(...) to device %d', id)
retval = self.tdlib.tdLearn(id)
if retval is not 0:
getErrorStringFunc = self.tdlib.tdGetErrorString
getErrorStringFunc.restype = c_void_p
vp = getErrorStringFunc(retval)
cp = c_char_p(vp)
s = cp.value
self.logger.error('error while trying to learn device: %s', s)
self.tdlib.tdReleaseString(vp)
return int(retval)
def getStatus(self, id):
state = self.tdlib.tdLastSentCommand(id)
self.logger.debug('state retrieved from device %s (id: %d)', state, id)
return state
def addDevice(self):
self.logger.debug('creating new device')
from Device import Device
return Device(0, self.tdlib.tdAddDevice(), self)
def removeDevice(self, id):
self.logger.debug('removing device %d', id)
return self.tdlib.tdRemoveDevice(id)
def setName(self, id, name):
if not isinstance(name, str):
raise ValueError('name needs to be str')
if not isinstance(id, int):
raise ValueError('id needs to be int')
self.logger.debug('setting name to device %d, new name %s', id, name)
return self.tdlib.tdSetName(id, name)
def setProtocol(self, id, protocol):
return self.tdlib.tdSetProtocol(id, protocol)
def setModel(self, id, model):
return self.tdlib.tdSetModel(id, model)
def setParameter(self, id, param, value):
return self.tdlib.tdSetDeviceParameter(id, param, value)
def getProtocol(self, id):
getProtocolFunc = self.tdlib.tdGetProtocol
getProtocolFunc.restype = c_void_p
vp = getProtocolFunc(id)
cp = c_char_p(vp)
s = cp.value
self.tdlib.tdReleaseString(vp)
return s
def getModel(self, id):
getModelFunc = self.tdlib.tdGetModel
getModelFunc.restype = c_void_p
vp = getModelFunc(id)
cp = c_char_p(vp)
s = cp.value
self.tdlib.tdReleaseString(vp)
return s
def getParameter(self, id, param):
getParameterFunc = self.tdlib.tdGetDeviceParameter
getParameterFunc.restype = c_void_p
vp = getParameterFunc(id, param, None)
cp = c_char_p(vp)
s = cp.value
self.tdlib.tdReleaseString(vp)
return s
def findDeviceByName(self, name):
nrDevices = self.tdlib.tdGetNumberOfDevices()
for i in range(nrDevices):
devid = self.tdlib.tdGetDeviceId(i)
devname = self.getName(devid)
if devname == name:
self.logger.debug('setting device id %i to device with name %s', devid, devname)
return devid
self.logger.debug('no device id was found for device with name %s', name)
return -1
def close(self):
self.tdlib.tdClose()