Problem with Shell Command in Hassio

Hi guys. I tried to implement a way to load custom entity_picture to my Sonos media_players when streaming from an internet radio (*.mp3 source)

Followed the example from: https://community.home-assistant.io/t/chromecast-radio-with-station-and-player-selection/12732/237

Im however getting the following error and have been trying to change to different paths in the config. (for example /config/custom_components/radio_cast/ and /home/hass/.homeassistant/custom_components/radio_cast/ etc)

Log Details (ERROR)

Mon Jun 18 2018 13:18:49 GMT+0200 (W. Europe Daylight Time)

Error executing service <ServiceCall shell_command.radio_picture: my_media_player=media_player.livingroom, my_radio_station=Jayan.se_Radio>

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 1007, in _event_to_service_call
    await service_handler.func(service_call)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/shell_command.py", line 88, in async_service_handler
    process = yield from create_process
  File "/usr/lib/python3.6/asyncio/subprocess.py", line 225, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "uvloop/loop.pyx", line 2368, in __subprocess_run
  File "uvloop/handles/process.pyx", line 564, in uvloop.loop.UVProcessTransport.new
  File "uvloop/handles/process.pyx", line 87, in uvloop.loop.UVProcess._init
FileNotFoundError: [Errno 2] No such file or directory

My config looks as following:

Shell command

radio_picture: /home/hass/.homeassistant/custom_components/radio_cast/radio.py {{ my_media_player }} {{ my_radio_station }}

automation

alias: Set Radio Picture
trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: or
    conditions:
      #list all media players here
      - condition: template 
        value_template: "{{ trigger.event.data.entity_id == 'media_player.kitchen' }}"
      - condition: template
        value_template: "{{ trigger.event.data.entity_id == 'media_player.livingroom' }}"
      - condition: template
        value_template: "{{ trigger.event.data.entity_id == 'media_player.bedroom' }}"
  - condition: template
    value_template: "{{ trigger.event.data.new_state.state  == 'playing' }}"
  - condition: template
    value_template: "{{ trigger.event.data.new_state.attributes is not none }}"
  #only run if script triggered in last 30 sec to not overwrite other music services
  - condition: template
    value_template: "{{(as_timestamp(now()) - as_timestamp(states.script.radio_cast.attributes.last_triggered | default(0)) | int < 30 )}}"
action:
  - service: shell_command.radio_picture
    data_template:
      my_media_player: '{{ trigger.event.data.entity_id }}'
      my_radio_station: '{{ states.input_select.radio_station.state | replace(" ", "_") }}'

/custom_components/radio_cast/radio.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import datetime
import sys
import json, requests, simplejson

HA_URL = "!secret URL"
HA_PWD = "!secret PASSWORD"

##########################################
# Call the script with 2 parameters:
# parameter 1 is the full media_player entity name, e.g. media_player.bedroom
# Parameter 2 is the input_select of the selected radio station with spaces replaced with underscores, e.g. Absolute_Radio
##########################################

def HASS_API_State(Entity, State, Media_Title, Entity_Picture, media_content_id, friendly_name):
	url = HA_URL + "/api/states/" + str(Entity) + "?api_password=" + HA_PWD
	data={"state":"" + str(State) + "", "attributes": {"media_title": "" + str(Media_Title) + "", "entity_picture": "" + str(Entity_Picture) + "", "is_volume_muted": "false", "media_content_id": "" + str(media_content_id) + "", "friendly_name": "" + str(friendly_name) + "", "supported_features: ": "21437", "app_name: ": "Radio Cast", "media_position: ": "0"}}
	headers = {'Content-type':'application/json', 'Accept-Encoding': 'text/plain', 'Accept': 'text/plain'}
	r = requests.post(url, data=json.dumps(data), headers=headers)
	c = r.content
	result = simplejson.loads(c)


if(sys.argv[2]=="Jayan.se_Radio"):
	HASS_API_State(sys.argv[1],"playing",sys.argv[2].replace("_"," "), HA_URL +"/local/radio.jpg", "http://jayan.se:8000/stream.mp3", sys.argv[1].replace("media_player.","").capitalize())

If anyone can point me towards whats wrong i would be soooo thankful.

// Jayan

I’m not sure if this is due to a new HA version of the fact that I moved to docker, but I no longer appear to be able to do this, I get a white picture now instead of the pic i’m looking for…

Well thats unfortunate, i really loved the idea of what you created.

Im not even getting a picture tho, just the Shell Service Call error, so i might have done something wrong.

Are you running Hassio? What does your shell command look like now? and whats the path to your ChromeCast_Radio.py file?

can you format your code using the blue bar on top of the page?
I’m not sure but I might have picked up a formatting issue with quotes, for example:
HASS_API_State(sys.argv[1],“playing”,sys.argv[2].replace("_"," “ <----), HA_URL +”/local/radio.jpg", “<----http://jayan.se:8000/stream.mp3”, sys.argv[1].replace(“media_player.”<----- ,"").capitalize())

Edited OP.

thanks, so no issues with the formatting it appears.
What happens when you run the script manually from command line?
I wonder if the “.” in your input select might be the culprit.
Can you try and rename the option to “jayan_se_radio”?
Also have you adapted the media_player options to reflect the names of your media_players?

The script i.e the radio runs just fine, yes the media_player options reflect to the names of my media_players.

Testing to remove the “.” and changed it to “jayan_radio” now to see if we get a difference.

Same problem.

Log Details (ERROR)

Mon Jun 18 2018 13:29:02 GMT+0200 (W. Europe Daylight Time)

Error executing service <ServiceCall shell_command.radio_picture: my_media_player=media_player.livingroom, my_radio_station=Jayan_Radio>

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 1007, in _event_to_service_call
    await service_handler.func(service_call)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/shell_command.py", line 88, in async_service_handler
    process = yield from create_process
  File "/usr/lib/python3.6/asyncio/subprocess.py", line 225, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "uvloop/loop.pyx", line 2368, in __subprocess_run
  File "uvloop/handles/process.pyx", line 564, in uvloop.loop.UVProcessTransport.new
  File "uvloop/handles/process.pyx", line 87, in uvloop.loop.UVProcess._init
FileNotFoundError: [Errno 2] No such file or directory

Really wondering if the path is correct for Hassio

radio_picture: /home/hass/.homeassistant/custom_components/radio_cast/radio.py {{ my_media_player }} {{ my_radio_station }}

Tried different paths since log says No such file or directory.

I did however try to replicate your .py path without any success also.

If i run shell_command.radio_picture without any JSON data i get.


Log Details (ERROR)

Mon Jun 18 2018 13:33:59 GMT+0200 (W. Europe Daylight Time)

Error executing service <ServiceCall shell_command.radio_picture>

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 1007, in _event_to_service_call
    await service_handler.func(service_call)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/shell_command.py", line 88, in async_service_handler
    process = yield from create_process
  File "/usr/lib/python3.6/asyncio/subprocess.py", line 225, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "uvloop/loop.pyx", line 2368, in __subprocess_run
  File "uvloop/handles/process.pyx", line 564, in uvloop.loop.UVProcessTransport.new
  File "uvloop/handles/process.pyx", line 87, in uvloop.loop.UVProcess._init
FileNotFoundError: [Errno 2] No such file or directory

In other words, the shell_command simply dosnt talk to radio.py because the path is wrong?

yeah looks like it’s pointing to the wrong location.
Just to confirm, what happens when you run the script manually from the command line?
Have you tried to create a dummy script and call it from HASS to see if it works? (a script that will for example create a dummy text file so you can confirm it ran successfully)

I can run other shell commands fine, however neither of these point to a path.

What is your current shell_command path for this script?

I don’t use hass.io so don’t know, and since I now run docker I can’t just point to the script as there are required libraries that don’t come with HA by default. So I changed my setup to call the script via MQTT, but like I said it no longer works. No idea if the issue is docker or a new version of HA that no longer accepts the hack

1 Like

Alright, ill put this on hold and hopefully someone will figure out a solution.

Cheers and thanks for your input.

by the way what happens when you run the script manually?
also you need to find the way to call a script from HASS, if you don’t point to the right path it’ll never work…

this path simply doesn’t exist in hassio.

Your config resides at /config so your path should be /config/custom_components

1 Like

I have tried this also. :frowning: i wrote that in the op

radio_picture: /config/custom_components/Radio_Cast/radio.py {{ my_media_player }} {{ my_radio_station }}

Same problem in the logs.

Could it be because custom_components is a special folder for, well, custom components?

Have you tried putting your script in a scripts folder?

I think i tried this before, but could have been with the wrong intial path.

Gonna try this again.

Edit: Same error.

/config/shell_scripts/radio.py

Same here.

1 Like