I just moved my HA from docker within Ubuntu, to a KVM image. I have a shell_command
that runs a python script that I wrote to get an image from a camera and save it to a directory. If I run this script manually (in the SSH Terminal add-on), everything works great. If I run it through HA under Developer Tools > Services, it fails to write the file. The automation also fails, and I’m assuming it’s the same reason. I have the following in my HA /config directory:
The image should save to the ha_www_images
directory. If I change my script to write just to /config/
, it works just fine, but I can’t have the file saved there. The ha_www_images
directory is an NFS share from the Ubuntu host.
My shell_command
script looks like this (I commented out a ton trying to figure out why it was failing):
#import argparse
import urllib.request
import uuid
import requests
#from PIL import Image
# parser = argparse.ArgumentParser(description='This scrtipt is used to get a snapshot image and save it with a unique name. It then updates home assistant with the file name to use later.')
# parser.add_argument("--img_url", default="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", help="http link for image")
# parser.add_argument("--img_ext", default="jpeg", help="image type, ie: jpeg, png, etc")
# parser.add_argument("--save_loc", default="/config", help="where to save the image on the hassio instance")
# parser.add_argument("--ha_url", default="", help="url for home assistant")
# parser.add_argument("--entity_id", default="", help="entity_id to update with new file name")
# parser.add_argument("--ha_token", default="", help="long life token from Profile > Long-Lived Access Tokens")
# args = parser.parse_args()
def main():
file_name = str(uuid.uuid4()) + '.jpeg'
# print(file_name)
urllib.request.urlretrieve('http://192.168.2.31/snap.jpeg', '/config/ha_www_images/' + file_name)
# img = Image.open(args.save_loc + file_name)
# new_img = img.crop((600,0,1200,800))
# new_img.save(args.save_loc + file_name)
ha_url = 'http://192.168.2.102:8124/api/states/sensor.front_camera_snap'
ha_headers = {
'Authorization': 'Bearer <removed token>'
# 'Authorization': 'Bearer ' + args.ha_token
, 'content-type': 'application/json'
}
ha_payload = '{"state":"'+file_name+'"}'
x = requests.request("POST",ha_url,headers=ha_headers, data=ha_payload)
if __name__ == '__main__':
main()
The fact that it works if I change the urlretrieve
line to '/config/'
instead of '/config/ha_www_images/'
makes me believe it’s a permissions issue, but as you can see above, this folder is 777’d. Is there anything you see wrong with this script?