Page 1 of 3

Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
Hi
I have put togheter a wrapper for python linux users that want to skip tdtool and work directly against the libtelldus.

Source is available here:
http://bitbucket.org/davka003/pytelldus/

There is also an example that mimics tdtool but is written in python.

Currently I am lacking support for the API commands for changing the configuration and event handling.

Configuration will come, just missing answer to a question on permissions here: http://www.telldus.com/forum/viewtopic.php?f=8&t=2107

Event handling will probably also come, but as I am lacking a DUO it doesnt make much sense to develop that yet.

Please let me know what you think. Contributions are allways welcome.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
Now configuration and most event handling is added as well.
Currently missing support for:

Code: Select all

int tdRegisterRawDeviceEvent( TDRawDeviceEvent eventFunction, void *context );
int tdRegisterControllerEvent( TDControllerEvent eventFunction, void *context);
int tdSendRawCommand(const char *command, int reserved);
All the remaining APIs are in there, as well as untested Windows support.

An example tdtool is there as well that is able to serve as an example of how it can be used.

Let me know if you have any questions.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by micke.prag
Great wrapper. I have just one note.

Regarding your init function. You may never assume which methods the library client will implement. This goes against the purpose of the supportedMethods variable. You should define the ini function like this:

Code: Select all

def init(defaultMethods = 0):
It is always up to the end software to decide the methods.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by Klas
Noticed a bug in __main__

Code: Select all

    for i in range(getNumberOfDevices()):
        print getDeviceId(i), getName(i), methods(i)
Should be

Code: Select all

   for i in range(getNumberOfDevices()):
        id = getDeviceId(i)
        print id, getName(id), methods(id)
Same problem exists in getDeviceIdFromStr()

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
Thanks for the bugreport, i Will look into it when i am back from vacation.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
Thanks Klas and Micke for pointing out these problems. They are now fixed and pushed to the git repository. At the same time I added support for raw device event's as I just got hold of an DUO.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by Klas
Great,

A suggestion, it would be nice with a debug-flag. To be used instead of if 0: in the callbacks.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by Mahlstrom
Great wrapper!

I've tried it out using mac os x adding

Code: Select all

elif (platform.system() == 'Darwin'):
    #Mac
    from ctypes import cdll, CFUNCTYPE
    tdlib = cdll.LoadLibrary('/Library/Frameworks/TelldusCore.framework/TelldusCore')
The only problem I've got was when calling tdlib.tdReleaseString(vp)
python2.6(10014,0x7fff752b2180) malloc: *** error for object 0xffffffffb2601c50: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Do you guys know how to solve this?
Or is skipping tdReleaseString the only solution?

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
If you dont mind I will include your code snippet in the git repository. Is that ok?

At least once the memory handling has been strighten out.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by Mahlstrom
No problem at all.
Found it while running thou the forums to learn about python/telldus.
And btw, while adding, what i did was just adding

Code: Select all

if (platform.system() != 'Darwin'):
before tdReleaseString

Right now i'm thinking about tackle the tdSendRawCommand
How far have you come so far?

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
I include your source contributions then. (With a comment about potentially memory leak).

For Raw Command sending I havent tried anything at all. (I dont know any good way of testing if I am correct). But I think it should be as simple as:

Code: Select all

def sendRawCommand(command, reserved = 0):
    return tdlib.tdSendRawCommand(command, reserved)
I allways push all my changes to the git repo as soon as I have done anything so all I got is in there.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by UglyBob
Hi!

I can't get this to work. If I run the python script with -t option and trigger a sensor, nothing happens. Am I missing something? I can't add the sensor to tellstick.conf as I have no information about it. I think I have installed all libraries from the repo, so what am I missing? Is there a guide for getting sensors to work in linux with tellstick duo? I can't seem to find any useful information...

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
You dont have to configure anything in Linux to get sensors to work. But be aware that the range may not be very good. My temp sensor sends a signal every 50 sec. If the duo receive the signal it flashes blue. Also you could try pressing the button on a ordinary nexa remote or similar. This should also give you blue blink.

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by UglyBob
Yes, I do see it flashes when triggering my motion sensor or pressing the remote. But how will I access the data? Is using the API the only way or are there other tools for testing?

Re: Python wrapper of libtelldus

Posted: Fri Mar 17, 2023 9:45 am
by davka003
I think there is a clarification needed.

Sensors in the tellstick world are not Nexa motion detectors or light sensor. It is only temperature and humidity that is sensors. All others are deviceevent or rawdeviceevent.
If you see the duo flashes you should see a rawDeviceEvent (or many) if you run python tdtool.py --event

To make it device event you need to create a device entry in /etc/tellstick.conf matching the protocol/house/unit code reported in the rawdeviceevent.