moroen / IKEA-Tradfri-plugin

A Python plugin for Domoticz to controll IKEA Gateway
68 stars 23 forks source link

ikeatradfri error when setting dim level. #87

Closed mrblond18 closed 4 years ago

mrblond18 commented 4 years ago

I have recently installed the "new" ikea-tradfri-plugin for domoticz. I had a version of more than a year old which was running OK, but did not detect new lights anymore. I am still running an old raspbian (jessie) but I was able to update to the latest master and compile everything including the ikea-tradfri command line utility.

Turning lights and groups on and off from domoticz works just fine. However, when I try to set a certain dim level nothing happens and I can not turn lights on or off anymore either from domoticz. When I restart ikea-tradfri service it works again, using: sudo service ikea-tradfri restart

I can set a dim level using the command line utility without any problems. When domoticz does not work with ikea-tradfri anymore I can sill use the command line utility just fine.

I ran ikea-tradfri with debug loggin enabled and got the following error when setting dim level:

INFO:Received {"transition_time": "IDENT2", "deviceID": "65552", "action": "setLevel", "level": 45} from ('127.0.0.1', 46190) decrypt_verify(): found 24 bytes cleartext decrypt_verify(): found 299 bytes cleartext IDENT2 ERROR:Task exception was never retrieved future: <Task finished coro=<tcp_server.handle_echo() done, defined at /usr/local/lib/python3.7/site-packages/ikeatradfri-0.0.1-py3.7.egg/ikeatradfri/tcp_server.py:44> exception=AttributeError("module 'pytradfri.error' has no attribute 'RequestTimeOut'")> Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/ikeatradfri-0.0.1-py3.7.egg/ikeatradfri/tcp_server.py", line 75, in handle_echo returnData = await self.set_level(command) File "/usr/local/lib/python3.7/site-packages/ikeatradfri-0.0.1-py3.7.egg/ikeatradfri/tcp_server.py", line 213, in set_level command["level"], transition_time=command["transition_time"] File "/usr/local/lib/python3.7/site-packages/ikeatradfri-0.0.1-py3.7.egg/ikeatradfri/devices.py", line 131, in set_level int(level), transition_time=int(transition_time) ValueError: invalid literal for int() with base 10: 'IDENT2' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/ikeatradfri-0.0.1-py3.7.egg/ikeatradfri/tcp_server.py", line 92, in handle_echo except Error.RequestTimeOut: AttributeError: module 'pytradfri.error' has no attribute 'RequestTimeOut' ^CINFO:Received signal SIGINT: exiting ERROR:Exception CancelledError() can not be represented as errno, setting -1.

It might be similar to this issue.

My config: uname -a Linux RaspberryDomoticz 4.9.35-v7+ #1014 SMP Fri Jun 30 14:47:43 BST 2017 armv7l GNU/Linux python3 --version Python 3.7.4

moroen commented 4 years ago

If you're using the "new" pycoap version, there is no need for the IKEA tradfri-service or the command line utility any more... It look's like you're still using the old version of the plugin?

mrblond18 commented 4 years ago

If you're using the "new" pycoap version, there is no need for the IKEA tradfri-service or the command line utility any more... It look's like you're still using the old version of the plugin?

I am not aware I am using an old version. I am using master/default version. I would expect that is the advised one...

Anyway, I tried installing the pycoap version on my RPi3B running raspbian jessie with python 3.7.4 and go version go1.12.14 linux/arm.

When I follow the raspberry pi instructions from here, I get the error $ GOPATH=/tmp/tmpnpr7vjz7 go get -d /tmp/ccWT95yJ.o: In function "main": test.c:(.text+0xc): undefined reference to "f" collect2: error: ld returned 1 exit status

full log here

When I follow the general instructions from here I get a similar error: /tmp/ccjCD1rj.o: In function "main": test.c:(.text+0xc): undefined reference to "f" collect2: error: ld returned 1 exit status $ CGO_CFLAGS=-I/usr/local/include/python3.7m CGO_LDFLAGS=-Wl,--unresolved-symbols=ignore-all GOPATH=/tmp/tmp4qd2u5iu go build -buildmode=c-shared -o /tmp/pip-install-jwdhqsn9/pycoap/build/lib.linux-armv7l-3.7/_pycoap.cpython-37m-arm-linux-gnueabihf.so # github.com/moroen/pycoap/src/pycoap ./pycoap.go:4:20: fatal error: pycoap.h: No such file or directory #include "pycoap.h" ^ compilation terminated.

Full log: here

moroen commented 4 years ago

As of yet, the master is the default, but the the pycoap version is the one I recommend using. When enough testing has been done, the pycoap-version will be the new master, and the old plugin/server solution retired...

There was an error in a previous commit to the pycoap repo, the pycoap.h file was missing. In the latest commit, this should be fixed. Try installing pycoap manually

$ git clone https://github.com/moroen/pycoap.git
$ cd pycoap
$ git pull
$ sudo -H python3 setup.py install
mrblond18 commented 4 years ago

As of yet, the master is the default, but the the pycoap version is the one I recommend using. When enough testing has been done, the pycoap-version will be the new master, and the old plugin/server solution retired...

There was an error in a previous commit to the pycoap repo, the pycoap.h file was missing. In the latest commit, this should be fixed. Try installing pycoap manually

$ git clone https://github.com/moroen/pycoap.git
$ cd pycoap
$ git pull
$ sudo -H python3 setup.py install

That does not seem to fix it...

$ GOPATH=/tmp/tmpeexmuarp go get -d
/tmp/ccO5fgEC.o: In function `main':
test.c:(.text+0xc): undefined reference to `f'
collect2: error: ld returned 1 exit status

Full log here.

moroen commented 4 years ago

What's the version of your go compiler?

From the logs, it appears that the go compiler chokes on the -buildmode flag, which as far as I know was introduced in go version 1.5.

$ go version
mrblond18 commented 4 years ago

What's the version of your go compiler?

From the logs, it appears that the go compiler chokes on the -buildmode flag, which as far as I know was introduced in go version 1.5.

$ go version
$ go version
go version go1.12.14 linux/arm

However, I have been struggling to get the correct version of go (already before this issue). First I installed golang using apt, that gave me version 1.3. The I used these instructions to get to 1.12: here.

However, there might be fragments of the old go left... Any idea how to force the new go?

mrblond18 commented 4 years ago
$ go version
go version go1.12.14 linux/arm

However, I have been struggling to get the correct version of go (already before this issue). First I installed golang using apt, that gave me version 1.3. The I used these instructions to get to 1.12: here.

However, there might be fragments of the old go left... Any idea how to force the new go?

Ok, fixed it myself. My pi user had the new go version installed, but when I did

sudo -H go version

I got the old version...

I completely wiped golang*, then sudo -H go version, indicated it could not find go. After changing a few paths and safepaths and exports it finally compiled :-)

Next step, starting in domoticz.... Domoticz is complaining that it can not find appdirs. I think this is caused by the fact that I compiled IKEA-tradfri with python 3.7 (as indicated in the readme), but domoticz uses python 3.4.2.

 2019-12-26 11:22:39.536 Status: Domoticz V4.10717 (c)2012-2019 GizMoCuz
2019-12-26 11:22:39.537 Status: Build Hash: b38b49e5, Date: 2019-05-09 13:04:08
2019-12-26 11:22:39.537 Status: Startup Path: /home/pi/domoticz/
2019-12-26 11:22:39.675 Status: PluginSystem: Started, Python version '3.4.2'. 
...
 2019-12-26 11:22:42.366 Error: (IKEA-Tradfri) failed to load 'plugin.py', Python Path used was '/home/pi/domoticz/plugins/IKEA-Tradfri/:/usr/lib/python3.4/:/usr/lib/python3.4/plat-arm-linux-gnueabihf:/usr/lib/python3.4/lib-dynload:/usr/local/lib/python3.4/dist-packages:/usr/lib/python3/dist-packages:/usr/lib/python3.4/dist-packages:/usr/lib/dist-python'.
2019-12-26 11:22:42.366 Error: (IkeaTradfri1) Module Import failed, exception: 'ImportError'
2019-12-26 11:22:42.366 Error: (IkeaTradfri1) Module Import failed: ' Name: appdirs'
2019-12-26 11:22:42.366 Error: (IkeaTradfri1) Error Line details not available. 

Any advice how to force domoticz to use python 3.7 (or use the IKEA-tradfri plugin with 3.4)?

moroen commented 4 years ago

Welcome to the "endless bucket of fun" that it python version management. There are good tools for this, for instance pyenv, but unfortunately, domoticz doesn't support this. It always loads the system python library, and it loads the lowest version it can find...

You're system might have a python3.4 (or python34) command, that points to the 3.4 interpreter. Use python3.4 in the instructions, and to make sure pip uses this version, try:

 $ python3.4 -m pip install ... 

If you don't have a python3.4 command, you can create it by making a symbolic link to the correct python interpreter (probably /usr/bin/python3):

$ sudo ln -s /usr/bin/python3 /usr/bin/python3.4

However, the IKEA-Tradfri plugin doesn't support python3.4. It might work, but I haven't tested it!

You could force domoticz to use python3.7 with some clever symbolic linking of the library files, but this would probably break your system in novel, unexpected ways...

Unless you have a lot of other stuff on your Pi, which would be a total PITA to reinstall, I really would recommend starting with a fresh raspbian buster image, then you have support for go 1.11.6 and python 3.7.3 out of the box...

mrblond18 commented 4 years ago

Welcome to the "endless bucket of fun" that it python version management. There are good tools for this, for instance pyenv, but unfortunately, domoticz doesn't support this. It always loads the system python library, and it loads the lowest version it can find...

You're system might have a python3.4 (or python34) command, that points to the 3.4 interpreter. Use python3.4 in the instructions, and to make sure pip uses this version, try:

 $ python3.4 -m pip install ... 

If you don't have a python3.4 command, you can create it by making a symbolic link to the correct python interpreter (probably /usr/bin/python3):

$ sudo ln -s /usr/bin/python3 /usr/bin/python3.4

However, the IKEA-Tradfri plugin doesn't support python3.4. It might work, but I haven't tested it!

You could force domoticz to use python3.7 with some clever symbolic linking of the library files, but this would probably break your system in novel, unexpected ways...

Unless you have a lot of other stuff on your Pi, which would be a total PITA to reinstall, I really would recommend starting with a fresh raspbian buster image, then you have support for go 1.11.6 and python 3.7.3 out of the box...

Ok, thanks again.

I postponed going to raspbian buster for a while now and intend on keep doing that... I've got pihole and openvpn server running there as well.

I succeeded in compiling for python3.4. However, when the plugin starts it gets further, but still gives errors:

 2019-12-26 15:44:24.428 Error: (IkeaTradfri1) 'onStart' failed 'ValueError':'invalid literal for int() with base 10: 'True''.
2019-12-26 15:44:24.428 Error: (IkeaTradfri1) ----> Line 348 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onStart
2019-12-26 15:44:24.428 Error: (IkeaTradfri1) ----> Line 245 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onStart 
... shell
2019-12-26 15:44:34.310 Error: (IkeaTradfri1) 'onHeartbeat' failed 'TypeError':'unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType''.
2019-12-26 15:44:34.311 Error: (IkeaTradfri1) ----> Line 383 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
2019-12-26 15:44:34.311 Error: (IkeaTradfri1) ----> Line 336 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
2019-12-26 15:44:44.330 Error: (IkeaTradfri1) 'onHeartbeat' failed 'TypeError':'unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType''.
2019-12-26 15:44:44.331 Error: (IkeaTradfri1) ----> Line 383 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
2019-12-26 15:44:44.331 Error: (IkeaTradfri1) ----> Line 336 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
2019-12-26 15:44:54.297 Error: (IkeaTradfri1) 'onHeartbeat' failed 'TypeError':'unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType''.
2019-12-26 15:44:54.298 Error: (IkeaTradfri1) ----> Line 383 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
2019-12-26 15:44:54.299 Error: (IkeaTradfri1) ----> Line 336 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
2019-12-26 15:45:04.316 Error: (IkeaTradfri1) 'onHeartbeat' failed 'TypeError':'unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType''.
2019-12-26 15:45:04.316 Error: (IkeaTradfri1) ----> Line 383 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
2019-12-26 15:45:04.317 Error: (IkeaTradfri1) ----> Line 336 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat 

Any ideas?

moroen commented 4 years ago

You probably have some stale values from the old version in the domoticz database. If you select the IKEA-Tradfri line in the hardware page, change type to IKEA-Tradfri Plugin - pycoap version, make sure that the values are sane and press "Update", it should work...

mrblond18 commented 4 years ago

You probably have some stale values from the old version in the domoticz database. If you select the IKEA-Tradfri line in the hardware page, change type to IKEA-Tradfri Plugin - pycoap version, make sure that the values are sane and press "Update", it should work...

Still a no go...

 2019-12-27 12:12:09.440 Status: User: Admin initiated a switch command (260/KoffieSpot/On)
2019-12-27 12:12:09.484 Error: (IkeaTradfri1) 'onCommand' failed 'NameError':'name 'pycoap' is not defined'.
2019-12-27 12:12:09.485 Error: (IkeaTradfri1) ----> Line 370 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onCommand
2019-12-27 12:12:09.485 Error: (IkeaTradfri1) ----> Line 288 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onCommand
2019-12-27 12:12:09.485 Error: (IkeaTradfri1) ----> Line 147 in '/home/pi/domoticz/plugins/IKEA-Tradfri/tradfricoap.py', function State
2019-12-27 12:12:09.485 Error: (IkeaTradfri1) ----> Line 38 in '/home/pi/domoticz/plugins/IKEA-Tradfri/tradfri/pycoap_api.py', function request 

Looks like it can not find pycoap. However, when I start a python shell with $ python3.4, I can succesfully import pycoap.

p.s. I changed the values in the plugin config to: type: IKEA tradfri plugin - py version data timeout: disabled add groups as devices: yes observe changes: yes polling interval (seconds): yes transition time (tenth of a second): 10 debug: false

moroen commented 4 years ago

This is seriously strange, the previous error suggest that domoticz can find pycoap, the last one, as you say, pycoap can't be found... I'll take a look at custom installation of modules, and see if it's possible to load a module from the same directory regardless of which interpreter is used.. I'll keep you posted...

moroen commented 4 years ago

Custom locations for modules turned out to be a real hassle, but you might have some luck trying the alternative coap-transport, coapcmd. It's available in the development branch, please refer to the readme in that branch!

moroen commented 4 years ago

Support for coapcmd have been merged into the master branch.

mrblond18 commented 4 years ago

Hi moroen, thanks again for all the effort. With the updated master branch I was able to get the latest version running on my setup (with the default Pycoap). I had to remove all ikea remotes and motionsensors I had in my domoticz devices first (they were no use in the previous version anyway, status and battery level were not detected...). After that it actually worked quite well. What I notice is that the commands are executed and finished much faster now and they are immediately updated in the domoticz interface as well, I do not have to wait for another "heartbeat" to see that the light is on after I pressed on in domoticz...

What I occasionely get is this:

 2020-01-12 22:51:19.099 Status: User: Admin initiated a switch command (265/AanrechtBladVerlichting/Off)
2020-01-12 22:51:19.329 Status: Executing script: /home/pi/domoticz/scripts/changeSwitchMultiple.sh
2020-01-12 22:51:19.380 Status: User: Admin initiated a switch command (273/AanrechtbladRechts/Off)
2020-01-12 22:51:19.526 Status: User: Admin initiated a switch command (275/AanrechtbladMidden/Off)
2020-01-12 22:51:19.670 Status: User: Admin initiated a switch command (277/AanrechtLinks/Off)
2020-01-12 22:51:22.550 Error: (IkeaTradfri1) 'onCommand' failed 'HandshakeError':'DTLS Error: Handshake timeout'.
2020-01-12 22:51:22.550 Error: (IkeaTradfri1) ----> Line 378 in '/home/pi/domoticz/plugins/IKEA-Tradfri-plugin/plugin.py', function onCommand
2020-01-12 22:51:22.550 Error: (IkeaTradfri1) ----> Line 297 in '/home/pi/domoticz/plugins/IKEA-Tradfri-plugin/plugin.py', function onCommand
2020-01-12 22:51:22.551 Error: (IkeaTradfri1) ----> Line 156 in '/home/pi/domoticz/plugins/IKEA-Tradfri-plugin/tradfricoap.py', function State
2020-01-12 22:51:22.551 Error: (IkeaTradfri1) ----> Line 41 in '/home/pi/domoticz/plugins/IKEA-Tradfri-plugin/tradfri/pycoap_api.py', function request
2020-01-12 22:51:22.551 Error: (IkeaTradfri1) ----> Line 52 in '/usr/local/lib/python3.4/dist-packages/pycoap-0.6.2-py3.4-linux-armv7l.egg/pycoap/pycoap.py', function Request 

I press a button here (265/AanrechtBladVerlichting/Off). This is a dummy button with three individual tradfri lights under it. Sometimes it works sometimes I get such an error. I know you state python 3.5.3 or higer is needed, I use python 3.4, but all the other things are working just fine... Is it a timing issue maybe?