add tdUnregisterCallback

This commit is contained in:
Øyvind Saltvik 2012-08-07 16:08:13 +02:00
parent 6496e71026
commit 71e13196f3
4 changed files with 127 additions and 48 deletions

View file

@ -1,42 +0,0 @@
import telldus
import time
telldus.tdInit()
devices = telldus.tdGetNumberOfDevices()
print "Devices: %d\n" % devices
allMethods = telldus.TELLDUS_TURNON | telldus.TELLDUS_TURNOFF | telldus.TELLDUS_BELL | telldus.TELLDUS_DIM
for i in xrange(devices):
deviceid = telldus.tdGetDeviceId(i)
name = telldus.tdGetName(deviceid)
print "%s - %s\n" % (deviceid, name)
methods = telldus.tdMethods(deviceid, allMethods)
if methods & telldus.TELLDUS_TURNON:
print " * TurnOn\n"
telldus.tdTurnOn(deviceid)
time.sleep(1)
if methods & telldus.TELLDUS_TURNOFF:
print " * TurnOff\n"
telldus.tdTurnOff(deviceid)
time.sleep(1)
if methods & telldus.TELLDUS_BELL:
echo " * Bell\n"
telldus.tdBell(deviceid)
time.sleep(1)
if methods & telldus.TELLDUS_TOGGLE:
print " * Toggle\n"
if methods & telldus.TELLDUS_DIM:
print " * Dim\n"
telldus.tdDim(deviceid, 128)
time.sleep(1)
telldus.tdClose()

View file

@ -0,0 +1,42 @@
import telldus
import time
telldus.tdInit()
devices = telldus.tdGetNumberOfDevices()
print "Devices: %d\n" % devices
allMethods = telldus.TELLDUS_TURNON | telldus.TELLDUS_TURNOFF | telldus.TELLDUS_BELL | telldus.TELLDUS_DIM
for i in xrange(devices):
deviceid = telldus.tdGetDeviceId(i)
name = telldus.tdGetName(deviceid)
print "%s - %s\n" % (deviceid, name)
methods = telldus.tdMethods(deviceid, allMethods)
if methods & telldus.TELLDUS_TURNON:
print " * TurnOn\n"
telldus.tdTurnOn(deviceid)
time.sleep(1)
if methods & telldus.TELLDUS_TURNOFF:
print " * TurnOff\n"
telldus.tdTurnOff(deviceid)
time.sleep(1)
if methods & telldus.TELLDUS_BELL:
echo " * Bell\n"
telldus.tdBell(deviceid)
time.sleep(1)
if methods & telldus.TELLDUS_TOGGLE:
print " * Toggle\n"
if methods & telldus.TELLDUS_DIM:
print " * Dim\n"
telldus.tdDim(deviceid, 128)
time.sleep(1)
telldus.tdClose()

View file

@ -0,0 +1,60 @@
import telldus
import time
from threading import Timer
telldus.tdInit()
timers = {} #timerlist
def turnOn():
print "turning on"
telldus.tdTurnOn(1)
def turnOff():
print "turning off"
telldus.tdTurnOff(1)
def callback(deviceId, method, value, callbackId):
global timers
print "callback!"
if (deviceId == 1):
# is turning on deviceId 1 here, so just return if events for that device are picked up
return
t = 0
print "Received event for device %d" % (deviceId,)
if (deviceId in timers):
# a timer already exists for this device, it might be running so interrupt it
# Many devices (for example motion detectors) resends their messages many times to ensure that they
# are received correctly. In this example, we don't want to run the turnOn/turnOff methods every time, instead we
# start a timer, and run the method when the timer is finished. For every incoming event on this device, the timer
# is restarted.
t = timers[deviceId]
t.cancel()
if (method == 1):
#on
t = Timer(0.5, turnOn) #start timer with 0.5 second delay (adjust the delay to suit your needs), then turn on
else:
#off
t = Timer(0.5, turnOff) #start timer with 0.5 second delay (adjust the delay to suit your needs), then turn off
t.start()
timers[deviceId] = t #put timer in list, to allow later cancellation
#function to be called when device event occurs, even for unregistered devices
def rawcallback(data, controllerId, callbackId):
print str(data)
callbackid = telldus.tdRegisterDeviceEvent(callback)
rawcallbackid = telldus.tdRegisterRawDeviceEvent(rawcallback)
try:
while(1):
time.sleep(0.5) #don't exit
except KeyboardInterrupt:
print "Exiting"
telldus.tdUnregisterCallback(callbackid)
telldus.tdUnregisterCallback(rawcallbackid)
telldus.tdClose()

View file

@ -490,13 +490,20 @@ telldus_tdRegisterSensorEvent(PyObject *self, PyObject *args)
return PyLong_FromLong((long) tdRegisterSensorEvent((TDSensorEvent) telldus_sensorEventCallback, &func));
}
static PyObject *
telldus_tdUnregisterCallback(PyObject *self, PyObject *args)
{
long id;
if (!PyArg_ParseTuple(args, "l", &id));
return NULL;
return PyBool_FromLong((long) tdUnregisterCallback(id));
}
*/
static PyMethodDef telldus_methods[] = {
/* The cast of the function is necessary since PyCFunction values
* only take two PyObject* parameters, and keywdarg_parrot() takes
* three.
*/
{"tdInit", (PyCFunction) telldus_tdInit, METH_NOARGS, "Initiate telldus."},
{"tdClose", (PyCFunction) telldus_tdClose, METH_NOARGS, "Close telldus."},
{"tdTurnOn", (PyCFunction) telldus_tdTurnOn, METH_VARARGS, "Turn on device."},
@ -527,6 +534,7 @@ static PyMethodDef telldus_methods[] = {
{"tdRegisterDeviceChangeEvent", (PyCFunction) telldus_tdRegisterDeviceChangeEvent, METH_VARARGS, "RegisterDeviceChangeEvent comment."},
{"tdRegisterRawDeviceEvent", (PyCFunction) telldus_tdRegisterRawDeviceEvent, METH_VARARGS, "RegisterRawDeviceEvent comment."},
{"tdRegisterSensorEvent", (PyCFunction) telldus_tdRegisterSensorEvent, METH_VARARGS, "RegisterSensorEvent comment."},
{"tdUnregisterCallback", (PyCFunction) telldus_tdUnregisterCallback, METH_VARARGS, "UnregisterCallback comment."},
*/
{NULL, NULL, 0, NULL} /* sentinel */
@ -552,7 +560,9 @@ inittelldus(void)
PyObject *TELLSTICK_ERROR_UNKNOWN_GLUE;
PyObject *TELLSTICK_TYPE_DEVICE_GLUE;
PyObject *TELLSTICK_TYPE_GROUP_GLUE;
PyObject *TELLSTICK_TEMPERATURE_GLUE;
PyObject *TELLSTICK_HUMIDITY_GLUE;
/* Create the module and add the functions */
/* PyEval_InitThreads(); */
@ -617,5 +627,14 @@ inittelldus(void)
TELLSTICK_TYPE_GROUP_GLUE = PyLong_FromLong((long) TELLSTICK_TYPE_GROUP);
PyObject_SetAttrString(module, "TELLSTICK_TYPE_GROUP", TELLSTICK_TYPE_GROUP_GLUE);
Py_DECREF(TELLSTICK_TYPE_GROUP_GLUE);
Py_DECREF(TELLSTICK_TYPE_GROUP_GLUE);
PyObject *TELLSTICK_TEMPERATURE_GLUE = PyLong_FromLong((long) TELLSTICK_TEMPERATURE);
PyObject_SetAttrString(module, "TELLSTICK_TEMPERATURE", TELLSTICK_TEMPERATURE_GLUE);
Py_DECREF(TELLSTICK_TEMPERATURE_GLUE);
PyObject *TELLSTICK_HUMIDITY_GLUE = PyLong_FromLong((long) TELLSTICK_HUMIDITY);
PyObject_SetAttrString(module, "TELLSTICK_HUMIDITY", TELLSTICK_HUMIDITY_GLUE);
Py_DECREF(TELLSTICK_HUMIDITY_GLUE);
}