moroen / IKEA-Tradfri-plugin

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

Error after updating #132

Open redfirebrooks opened 3 years ago

redfirebrooks commented 3 years ago

Hello I have errors after updating the plugin

(IKEA TRADFRI) Failed to initialize tradfri module.
(IKEA TRADFRI) Unable to find tradfricoap
(IKEA TRADFRI) Failed to initialize tradfri module.
(IKEA TRADFRI) Unable to find tradfricoap
(IKEA TRADFRI) 'onHeartbeat' failed 'NameError':'name 'close_connection' is not defined'.
(IKEA TRADFRI) ----> Line 811 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat
(IKEA TRADFRI) ----> Line 767 in '/home/pi/domoticz/plugins/IKEA-Tradfri/plugin.py', function onHeartbeat

Raspbian GNU/Linux 10 (buster) Domoticz V2020.2 (c)2012-2020 GizMoCuz Build Hash: b63341bc0, Date: 2020-04-26 13:47:55 (IKEA TRADFRI) Initialized version 0.10.6, author 'moroen'

Already tried https://github.com/moroen/IKEA-Tradfri-plugin/wiki/Common-errors but no improvement, still have the errors...

Then I've checked the version :

pi@domoticz:~/domoticz/plugins/IKEA-Tradfri $ python3 plugin.py version
IKEA Tradfri Plugin: 0.10.6
Tradfricoap: 0.0.17
Py3coap: Not found
coapcmd: (/home/pi/domoticz/plugins/IKEA-Tradfri/bin/coapcmd) 0.1.7

but coapcmd was alreday installed (under sudo I guess)...

pi@domoticz:~/domoticz/plugins/IKEA-Tradfri $ pip3 install py3coap
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: py3coap in /usr/local/lib/python3.7/dist-packages (0.9.2)

so I uninstalled it

pi@domoticz:~/domoticz/plugins/IKEA-Tradfri $ sudo pip3 uninstall py3coap
Found existing installation: py3coap 0.9.2
Uninstalling py3coap-0.9.2:
  Would remove:
    /usr/local/bin/pycoap-client
    /usr/local/lib/python3.7/dist-packages/_py3coap.cpython-37m-arm-linux-gnueabihf.h
    /usr/local/lib/python3.7/dist-packages/_py3coap.cpython-37m-arm-linux-gnueabihf.so
    /usr/local/lib/python3.7/dist-packages/py3coap-0.9.2.dist-info/*
    /usr/local/lib/python3.7/dist-packages/py3coap/*
    /usr/local/src/py3coap/py3coap.c
    /usr/local/src/py3coap/py3coap.h
Proceed (y/n)? y
  Successfully uninstalled py3coap-0.9.2

to re install it (with the -U option)

pi@domoticz:~/domoticz/plugins/IKEA-Tradfri $ pip3 install py3coap --user
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting py3coap
  Downloading py3coap-0.9.2.tar.gz (5.4 kB)
Building wheels for collected packages: py3coap
  Building wheel for py3coap (setup.py) ... done
  Created wheel for py3coap: filename=py3coap-0.9.2-cp37-cp37m-linux_armv7l.whl size=1631941 sha256=0bc435f9c3b9479b41a4f325a3239a18fed265abd02c1c9852ffa42573007858
  Stored in directory: /home/pi/.cache/pip/wheels/fe/83/a6/aaa8e84feda8a35938c0df71c86c0b843072a840a2cb5d3838
Successfully built py3coap
Installing collected packages: py3coap
Successfully installed py3coap-0.9.2
pi@domoticz:~/domoticz/plugins/IKEA-Tradfri $ python3 plugin.py version
IKEA Tradfri Plugin: 0.10.6
Tradfricoap: 0.0.17
Py3coap: 0.9.2
coapcmd: (/home/pi/domoticz/plugins/IKEA-Tradfri/bin/coapcmd) 0.1.7

then to be sure to use py3coap

pi@domoticz:~/domoticz/plugins/IKEA-Tradfri $ python3 plugin.py config api py3coap

So I made a test

pi@domoticz:~/domoticz/plugins/IKEA-Tradfri $ python3 plugin.py list
Lights:
65546: Ampoule à spectre blanc 1 (State: 0 - Level: 127 - Hex: f1e0b5)
65547: TRADFRI bulb E14 WS opal 400lm 2 (State: 0 - Level: 127 - Hex: f1e0b5)
65558: Ampoule à spectre blanc 3 (State: 0 - Level: 127 - Hex: f1e0b5)
65560: Ampoule chambre 01 (State: 1 - Level: 1 - Hex: None)
65561: Ampoule chambre 02 (State: 1 - Level: 1 - Hex: None)

Groups:
131075: Salon
131081: Chambre
131082: TRADFRI GROUP 3
131083: TRADFRI GROUP 4
131084: SuperGroup

Others:
65565: TRADFRI wireless dimmer (State: None - Level: None - Hex: None)
65567: TRADFRI remote control (State: None - Level: None - Hex: None)

Batteries:
65565: TRADFRI wireless dimmer - 21
65567: TRADFRI remote control - 100

Everything seems to be OK but I still have the errors in Domoticz logs...

moroen commented 3 years ago

@redfirebrooks

Sorry for the late reply!

This is a ongoing plague with imports on raspbian, domoticz can't find the same modules as the command line.

Try switching to the debug branch, it is the same version as master (0.10.6) with import error handling disabled. It will fail just like the regular master, but should provide a bit more information and context, hopefully this will enable us to find out why it fails...

driehuis commented 3 years ago

I have a similar issue it seems: python3 plugin.py list shows all my devices, and I can even control the lights from the command line, but Domoticz doesn't see any Tradfri devices. I have switched to the debug branch, and see this in my Domoticz log, repeatedly: 2021-02-03 00:52:43.412 Error: IKEA-Tradfri hardware (11) thread seems to have ended unexpectedly Apart from the startup message, no other log info is produced. It seems that Domoticz doesn't do much logging if a plugin dies a horrible death.

In my case, it turns out that domoticz is running as root despite being started with start-stop-daemon --chuid, which means that my user-installed tradfricoap library wasn't accessible. I haven't yet tracked down the root cause of why chuid is being ignored. In my case, a good test to see if tradfricoap was installed successfully was to run sudo su - -c "python3 $HOME/domoticz/plugins/IKEA-Tradfri/plugin.py list". I will eventually replace the domoticz startup script with a systemd unit file; this is not the first time start-stop-daemon has surprised me. Ubuntu 20.04, but probably not release dependent.

Anyway, I do see my Tradfri devices in Domoticz now.

TGMivo commented 3 years ago

Hi people, I fixed this error in my Pi/Domoticz

I installed Tradfricoap local:

 $ cd domoticz/plugins/IKEA-Tradfri
 $ git clone https://github.com/moroen/tradfricoap.git

Now i got the error that Tradfricoap is 2 times on my machine. So uninstall it

$ cd domoticz/plugins
$ pip3 uninstall tradfricoap
$ cd IKEA-Tradfri
$ python3 plugin.py version
IKEA Tradfri Plugin: 0.10.6
Tradfricoap: 0.0.17
Py3coap: 0.9.2
coapcmd: Not found (Looking for /home/pi/domoticz/plugins/IKEA-Tradfri/bin/coapcmd).

And restarted the plugin in Domoticz and my IKEA-Tradfri plugin is running again :)

ZatarraNL commented 3 years ago

I'm working on a fresh install of domoticz in an proxmox VM with debian, and experience the same error. python3 plugin.py list does indeed list my devices. But Domoticz cannot communicatie with coap.

I use the py3coap module. I noticed the following:

So my conclusion would be something like: Tradfri-coap should be run as Root. What are your thoughts on this? Any idea how to check this?

ZatarraNL commented 3 years ago

Update: all previous suggestions didn't work for me, but I found te following sollution:

Follow the install manual exactly as described, but everything with sudo. Everything works as expected. Allthough, my Domoticz runs as root. Not my preverence.

moroen commented 3 years ago

@ZatarraNL

You should not run domoticz as root. On most linux systems, including debian, adding the user to the plugdev group allows that user's processes to access the USB-devices:

sudo usermod -aG plugdev $USER

will add your current user to this group, and allow domoticz started as that user to access the UBS zwave stick. Remember to log out and log in again to activate the membership in the group.

So if you're able to get tradfri to work when running domoticz as user Zatarra, i really recommend going this route!

As a general rule, nothing in userspace should be run as root. Bad code in such a program, malicious or unintended, could really wreck havoc on your system, or present a serious security threat!

I try to make py3coap, tradfricoap and the plugin, as safe as possible, but I really would not recommend running either as root, so no, tradfri-coap should definitely not be run as root!

If you really want to, installing py3coap with the root user should solve the issue you're having:

$ sudo pip3 install py3coap tradfricoap
driehuis commented 3 years ago

The issue that I ran into may be the same one that @ZatarraNL ran in to: the init.d script that comes with domoticz does try to start domoticz as a non-root user with start-stop-daemon --chuid $USERNAME, but for some reason the chuid is ignored. This may well be a limitation of systemd. I have not yet come around to rewriting the startup script to a native systemd unit, which I expect would solve my issue.

moroen commented 3 years ago

@driehuis

Perhaps you could use my systemd unit-file as a starting point?

[Unit]
Description=Domoticz Home Automation
After=network.target ikea-tradfri.service

[Service]
User=moroen
WorkingDirectory=/opt/domoticz
ExecStart=/opt/domoticz/domoticz -www 8080 -sslwww 443
KillMode=process
Restart=on-failure
Nice=-18

[Install]
WantedBy=multi-user.target

With this, installing py3coap and tradfricoap as moroen, and using the --user option of pip3, domoticz finds py3coap and tradfricoap.

driehuis commented 3 years ago

Ooh, lovely! I do not have a separate ikea-tradfri.service, but this is an excellent starting point. How did you solve using port 443 under a non-root user? I was planning to put nginx or lighttpd in front of domoticz for that reason.

ZatarraNL commented 3 years ago

@ZatarraNL

You should not run domoticz as root. On most linux systems, including debian, adding the user to the plugdev group allows that user's processes to access the USB-devices:

sudo usermod -aG plugdev $USER

I agree, but the /etc/init.d/domoticz.sh seems to runs domoticz as root. This is despite that the domoticz.sh describes Zatarra as user. I find it surprizing that domoticz runs in root out of the box...

The systemd solution does run domoticz as Zatarra. But somehow adding the user Zatarra to the dialout group still gives me problems with the zwave-stick. I'll try to find a solution some other day.

Thanks for your effort with this plugin. I've used it so far for over a year.

moroen commented 3 years ago

@driehuis

Ooh, lovely! I do not have a separate ikea-tradfri.service, but this is an excellent starting point. How did you solve using port 443 under a non-root user? I was planning to put nginx or lighttpd in front of domoticz for that reason.

I didn't... I don't expose domoticz (or anything on my network) to the outside, so I don't care that SSL is not working... But I would imagine it should be possible to use a non-privileged port for SSL, by changing the following line in the unit file:

ExecStart=/opt/domoticz/domoticz -www 8080 -sslwww 8090