n8henrie / fauxmo

Emulated Belkin WeMo devices that work with the Amazon Echo
https://n8henrie.com/2016/02/fauxmo-home-automation-with-the-amazon-echo-raspberry-pi-and-homeassistant/
Other
376 stars 78 forks source link

problem starting fauxmo from Raspian service #82

Closed johngo7470 closed 5 years ago

johngo7470 commented 5 years ago

My Issue

Similar to another user, I'm able to start up fauxmo flawlessly, by using the following command, logged in as user 'pi':

 fauxmo -c /home/pi/config.json

pi@raspberrypi:/lib/systemd/system $ fauxmo --version v0.4.9 pi@raspberrypi:/lib/systemd/system $ whereis fauxmo fauxmo: /home/pi/.local/bin/fauxmo

The system file contains: ExecStart=/home/pi/.local/bin/fauxmo -c /home/pi/config.json

The problem noted in the system status, is:

 Sep 09 21:23:52 raspberrypi fauxmo[696]:   File "/home/pi/.local/bin/fauxmo", line 6, in <module>
 Sep 09 21:23:52 raspberrypi fauxmo[696]:     from fauxmo.cli import cli
 Sep 09 21:23:52 raspberrypi fauxmo[696]: ModuleNotFoundError: No module named 'fauxmo'
 Sep 09 21:23:52 raspberrypi systemd[1]: myfauxmo.service: Main process exited, code=exited, status=1/FAILURE
 Sep 09 21:23:52 raspberrypi systemd[1]: myfauxmo.service: Failed with result 'exit-code'.

The contents of /home/pi/.local/bin/fauxmo:

 pi@raspberrypi:~/.local/bin $ cat fauxmo
 #!/usr/bin/python3.7
 # -*- coding: utf-8 -*-
 import re
 import sys

 from fauxmo.cli import cli

 if __name__ == '__main__':
     sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
     sys.exit(cli())

==================

On a related note, if fauxmo is installed in a venv (e.g. /home/pi/.venv), how can this be accommodated in a module.service startup script? Do I need to add an activate command to the .service file?

WHYT


Please make sure you've taken these steps before submitting a new issue:

johngo7470 commented 5 years ago

I might have figured this one out...

I just noticed your .service example has the .venv version in there - will try this later this evening.

However, what path should be used for the "Working Directory"? (which should probably be moved below the line "#Fix the paths below:")

pi@raspi:/ $ sudo find . -name fauxmo 2>/dev/null ./home/pi/venv/bin/fauxmo ./home/pi/venv/lib/python3.6/site-packages/fauxmo ./usr/local/bin/fauxmo ./usr/local/lib/python3.6/site-packages/fauxmo ./opt/pyenv/versions/3.7.3/bin/fauxmo ./opt/pyenv/versions/3.7.3/lib/python3.7/site-packages/fauxmo ./opt/pyenv/shims/fauxmo

In my particular case, I think I'm using the non-venv version (still trying to understand venv, before I switch to it).

n8henrie commented 5 years ago

You can ignore workingdirectory (leave it out) if you just use absolute paths, which may be the easiest.

You can probably get the full path to your fauxmo executable with which fauxmo or command -v fauxmo. Use that absolute path and the absolute path to your config and see if that works.

Read up on venvs, they're important to understand!

johngo7470 commented 5 years ago

All paths are fully qualified... does seem like it's missing something from my environment, since it works fine if I execute it unqualified (or fully qualified), from the command line, when logged in as 'pi'.

pi@raspberrypi:~ $ which fauxmo /home/pi/.local/bin/fauxmo pi@raspberrypi:~ $ fauxmo --version v0.4.9

pi@raspberrypi:/ $ sudo systemctl start myfauxmo pi@raspberrypi:/ $ sudo systemctl status myfauxmo ● myfauxmo.service - Fauxmo Loaded: loaded (/lib/systemd/system/myfauxmo.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Thu 2019-09-12 19:18:52 CDT; 18s ago Process: 1507 ExecStart=/home/pi/.local/bin/fauxmo -c /home/pi/fauxmo_config.json (code=exited, status=1 Main PID: 1507 (code=exited, status=1/FAILURE)

Sep 12 19:18:52 raspberrypi systemd[1]: Started Fauxmo. Sep 12 19:18:52 raspberrypi fauxmo[1507]: Traceback (most recent call last): Sep 12 19:18:52 raspberrypi fauxmo[1507]: File "/home/pi/.local/bin/fauxmo", line 6, in Sep 12 19:18:52 raspberrypi fauxmo[1507]: from fauxmo.cli import cli Sep 12 19:18:52 raspberrypi fauxmo[1507]: ModuleNotFoundError: No module named 'fauxmo' Sep 12 19:18:52 raspberrypi systemd[1]: myfauxmo.service: Main process exited, code=exited, status=1/FAILU Sep 12 19:18:52 raspberrypi systemd[1]: myfauxmo.service: Failed with result 'exit-code'.

johngo7470 commented 5 years ago

[does seem like it's missing something from my environment]

I created a .venv, and that did the trick... (also seeming to confirm something was missing from my non-venv env)

● myfauxmo.service - Fauxmo Loaded: loaded (/lib/systemd/system/myfauxmo.service; enabled; vendor preset: Active: active (running) since Thu 2019-09-12 19:43:29 CDT; 11s ago Main PID: 1717 (fauxmo) Tasks: 1 (limit: 4035) Memory: 7.1M CGroup: /system.slice/myfauxmo.service └─1717 /home/pi/.venv/bin/python3 /home/pi/.venv/bin/fauxmo -c /home/

Sep 12 19:43:29 raspberrypi systemd[1]: Started Fauxmo.

n8henrie commented 5 years ago

How did you install fauxmo previously (before the venv)? And is this your system Python install?

johngo7470 commented 5 years ago

I first installed fauxmo using your instructions under "Simple install: From PyPI".

Then, being unsure of using venv, I did a "python3 -m pip remove fauxmo", followed by "python3.7 -m pip install fauxmo".

[And is this your system Python install?]

If I understand your question, yes... It's a Raspberry Pi 4, created a few weeks ago, so it would have likely been created with Python 3.7 from the get-go. When I type 'python3', it shows the version is 3.7.3, whereas on another Raspberry I'm working on (RPi 3), when I start up 'python3', it comes back with 3.5.3, even though I know 3.7.3 is available somewhere (I think via pyenv).

n8henrie commented 5 years ago

I first installed fauxmo using your instructions under "Simple install: From PyPI".

I don't think this is possible, since my simple install instructions install into a venv, so your fauxmo CLI script would not be in ~/.local/bin. It should definitely have .venv somewhere in its path if you had followed those instructions verbatim.

My guess is at some point you ran into a permissions error and instead ran: python3 -m pip install --user fauxmo. Is that possible?

And then when you ran pip uninstall fauxmo (remove is not a valid pip command to my knowledge) perhaps you got left with an artifact in ~/.local/bin.

Regardless, glad you got it working. venv is the way to go, although Fauxmo doesn't have any dependencies, so theoretically it would probably work fine in a system-wide installation.