Running Python Scripts from Shell.yaml

Hey everyone,

Trying to get a python script to run. When I click on “Activate” it says it’s activated, but nothing happens.
If I run the script manually from command line, it works fine. Using the pi image if that makes any difference.

shell.yaml contains:

pushbullet: "python2 /home/homeassistant/.homeassistant/pushbullet_send.py"

script.yaml contains:

pushbullet:
  sequence:
  - service: shell_command.pushbullet

I’ve tried with and without quotes, python/python2/python3, adding sudo (Not sure if that makes a difference?)
Also made the script executable, tried removing python from the path, but still no change.

Any help would be greatly appreciated.

I use shell_command’s for some bash scripts, but not any Python scripts. In mine, I have no quotes around anything - not the script itself or any arguments I might pass it.

To help diagnose ownership/permission issues, you could sudo to the HASS user:
sudo su -s /bin/bash hass

then run your script. That may not help, but it would rule out permissions. Do you see anything in home_assistant.log?

If I try sudo su -s /bin/bash hass I get: No passwd entry for user ‘hass’

No output from the log, except when I try to run the script immediately a second or a third time.

16-12-12 23:06:16 homeassistant.components.script: Script script.pushbullet already running.
16-12-12 23:22:16 homeassistant.core: Error doing job: Fatal read error on socket transport
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/selector_events.py", line 582, in _read_ready
    data = self._sock.recv(self.max_size)
TimeoutError: [Errno 110] Connection timed out

Not sure if the homeassistant.core error is relevant?

You might need to tweak this per your installation. I’m running All-in-one. That’ll affect the location of the hass user. I’m not a linux guy, so can’t give you the exact info.

Weird that you’re getting that error on subsequent clicks. It almost seems like the previous command hangs. But it might also depend on how long you wait between clicks. Running the command from the command line will give you a feel for how long it takes to complete. I wonder if it’s a problem with the py script itself? Since it’s running as hass instead of pi (which you are likely logged in as), it may not get the same environment. Maybe it’s missing a dependency for the pushbullet code (or are you using native python rather than importing any libraries).

If you’re installed in a virtual env, you’ll need to make sure that any necessary code is also installed to that environment. With the A-I-O install, you enter the VENV with:
source /srv/hass/hass_venv/bin/activate

Okay, think you were right about permissions. User I was looking for is homeassistant
sudo su -s /bin/bash homeassistant

Switching over and trying to run the script gives me this output:

Traceback (most recent call last):
  File "pushbullet_send.py", line 8, in <module>
    urllib.urlretrieve ("http://192.168.0.50/snapshot.jpg", "snapshot.jpg")
  File "/usr/lib/python2.7/urllib.py", line 98, in urlretrieve
    return opener.retrieve(url, filename, reporthook, data)
  File "/usr/lib/python2.7/urllib.py", line 249, in retrieve
    tfp = open(filename, 'wb')
IOError: [Errno 13] Permission denied: 'snapshot.jpg'

So seems to be a write permission error from inside of the script.

pushbullet_send.py

#!/usr/bin/env python

from pushbullet import Pushbullet
import urllib

pb = Pushbullet("api_key_goes_here")

urllib.urlretrieve ("http://192.168.0.50/snapshot.jpg", "snapshot.jpg")
with open("snapshot.jpg", "rb") as pic:
        file_data = pb.upload_file(pic, "snapshot.jpg")
push = pb.push_file(**file_data)

Any idea what I need to change?

Okay, found the issue…

When I set up the script originally, and ran tests, it created the snapshot.jpg with root privileges.
Deleted that, and now it seems to be working, so I assume the homeassistant user couldn’t modify the file.

@ih8gates, Thanks for the assistance