A Question about Kodi, HTTP POST and jsonrpc

Hey everyone,

I’ve got two URL’s I use from tasker with http post to start playing a file, and also stop playback on Kodi.

Start Playback:
http://IP_ADDRESS:PORT/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22id%22:%221%22,%22method%22:%22Player.Open%22,%22params%22:{%22item%22:{%22file%22:%22PATHTOFILE.mp4%22}}}

Stop Playback:
http://IP_ADDRESS:PORT/jsonrpc?request={%22jsonrpc%22:%20%222.0%22,%20%22method%22:%20%22Player.Stop%22,%20%22params%22:%20{%22playerid%22:1},%20%22id%22:%201}

How would I go about implementing this as a start/stop or on/off switch in Home Assistant?

Nothing I’ve tried so far has worked, and the closest was setting them up as Rest switches, which just caused the play script to restart every few seconds. Using command_line I get a “failed” notification in the logs. Script linking to shell is also non responsive.

I’ve got a few different switches working with post commands to a lirc_web installation, but I’m assuming the
json info is probably handled differently somehow?

Is there a preferred way to do this, or should I just stick with tasker for now?

Thanks!

Not at home to try this but did you try a Command Line Switch and curl?

On that page is an example of controlling a Foscam Motion Sensor which may help along with some examples on the Kodi forum here using Curl to control it.

If you don’t get it working I will try to have a look tomorrow, but there must be a simpler way than with curl but should work.

Hey Steven,

Yes, I did try a command line switch, but no luck.

- platform: command_line
  switches:
    kodi_test:
      command_on: "/usr/bin/curl -X POST URL"
      command_off: "/usr/bin/curl -X POST URL"

Also tried this as just curl instead of /usr/bin/curl, and same happens with the foscam example.

log output:

16-12-23 08:54:49 homeassistant.components.switch.command_line: Command failed: curl -k "http://IP_ADDRESS:PORT/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22id%22:%221%22,%22method%22:%22Player.Open%22,%22params%22:{%22item%22:{%22file%22:%22PATHTOFILE.mp4%22}}}"

Also tried to use shell.yaml

kodi_start_playback: 'curl -k "http://IP_ADDRESS:PORT/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22id%22:%221%22,%22method%22:%22Player.Open%22,%22params%22:{%22item%22:{%22file%22:%22PATHTOFILE.mp4%22}}}"'
kodi_stop_playback: 'curl -k "http://IP_ADDRESS:PORT/jsonrpc?request={%22jsonrpc%22:%20%222.0%22,%20%22method%22:%20%22Player.Stop%22,%20%22params%22:%20{%22playerid%22:1},%20%22id%22:%201}"'

And then added this to script.yaml

Kodi_test1:
  sequence:
  - service: shell_command.kodi_start_playback
Kodi_test2:
  sequence:
  - service: shell_command.kodi_stop_playback

But when I activate them I’m getting more errors in the log

16-12-23 09:03:18 homeassistant.components.shell_command: Error rendering command template: TemplateSyntaxError: tag name expected
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 72, in ensure_valid
    self._compiled_code = ENV.compile(self.template)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/environment.py", line 565, in compile
    self.handle_exception(exc_info, source_hint=source_hint)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<unknown>", line 1, in template
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/environment.py", line 470, in _parse
    return Parser(self, source, name, encode_filename(filename)).parse()
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/parser.py", line 897, in parse
    result = nodes.Template(self.subparse(), lineno=1)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/parser.py", line 879, in subparse
    rv = self.parse_statement()
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/parser.py", line 116, in parse_statement
    self.fail('tag name expected', token.lineno)
  File "/srv/homeassistant/lib/python3.4/site-packages/jinja2/parser.py", line 50, in fail
    raise exc(msg, lineno, self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: tag name expected

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/shell_command.py", line 52, in service_handler
        rendered_args = args_compiled.render(call.data)
      File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 86, in render
        self.hass.loop, self.async_render, kwargs).result()
      File "/usr/lib/python3.4/concurrent/futures/_base.py", line 402, in result
        return self.__get_result()
      File "/usr/lib/python3.4/concurrent/futures/_base.py", line 354, in __get_result
        raise self._exception
      File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/util/async.py", line 167, in run_callback
        future.set_result(callback(*args))
      File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 93, in async_render
        self._ensure_compiled()
      File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 143, in _ensure_compiled
        self.ensure_valid()
      File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 74, in ensure_valid
        raise TemplateError(err)
    homeassistant.exceptions.TemplateError: TemplateSyntaxError: tag name expected

You have authentication (http password) turned off in Kodi?

I know that sounds like an “Of course I do” kind of question, but I’ve been bitten by that. In your browser, you authenticate once and all of the other json calls work without another challenge.

If you issue that curl statement from a command line it works? If you can’t do it from a command line, then it won’t work in HASS.

I’m using curl in a bash script from HASS. I just have (in the bash script):

curl http://url

and in HASS (shell_command block):

nameofcommand: bash /home/pi/scripts/script.sh

@ih8gates

The authentication was disabled, but didn’t think to check if it works from command line.

It failed (Facepalm), so made some changes, and it’s working now!
Still getting errors in the log if I try and add it to shell.yaml or switch.yaml.

Code Used:

kodi_start_playback: curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","id":"1","method":"Player.Open","params":{"item":{"file":"22PATHTOFILE"}}}' "http://IP_ADDRESS:PORT/jsonrpc"
kodi_stop_playback: curl --header "Content-Type: application/json" --data '{"jsonrpc": "2.0", "method": "Player.Stop", "params": {"playerid":1}, "id": 1}' "http://IP_ADDRESS:PORT/jsonrpc"

Error:

16-12-24 14:03:27 homeassistant.util.yaml: mapping values are not allowed here
  in "/home/homeassistant/.homeassistant/shell.yaml", line 17, column 49

But, using curl in a bash script works 100%, and that’ll do the trick.

Thanks for the help!

Can you post line 17 (and the lines around it)?

You likely have an indentation problem or perhaps a quoting issue. It looks like HASS thinks you’re adding a list. Maybe put single quotes around that entire ‘curl…’ argument.

I’ve added the working bash commands to shell.yaml, so they’ve moved now. (not on line 17 anymore)
But here’s my entire shell.yaml (I know, it’s a bit messy… Work in progress)

amp_power: irsend SEND_ONCE pioneer POWER
amp_volume_up: irsend SEND_ONCE pioneer VOLUMEUP
amp_volume_down: irsend SEND_ONCE pioneer VOLUMEDOWN
amp_input_aux: irsend SEND_ONCE pioneer AUX
amp_input_dvd: irsend SEND_ONCE pioneer DVD
amp_input_tv: irsend SEND_ONCE pioneer TV
amp_mute: irsend SEND_ONCE pioneer MUTE
amp_volume_up_5: irsend SEND_ONCE pioneer VOLUMEUP VOLUMEUP VOLUMEUP VOLUMEUP VOLUMEUP VOLUMEUP VOLUMEUP VOLUMEUP VOLUMEUP VOLUMEUP
amp_volume_down_5: irsend SEND_ONCE pioneer VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN VOLUMEDOWN
restart_ha: service hass-daemon restart &
pushbullet: python /home/homeassistant/.homeassistant/pb.py
tv_input_hdmi1: "/usr/bin/curl -X POST http://192.168.0.42:3000/remotes/Samsung/HDMI1"
tv_input_hdmi2: "/usr/bin/curl -X POST http://192.168.0.42:3000/remotes/Samsung/HDMI2"
fireplace_start: bash /home/homeassistant/.homeassistant/fireplace_start.sh
fireplace_stop: bash /home/homeassistant/.homeassistant/fireplace_stop.sh
kodi_start_playback: 'curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","id":"1","method":"Player.Open","params":{"file":"home/pi/Fireplace.mp4"}}}' "http://192.168.0.42:8080/jsonrpc"'
kodi_stop_playback: 'curl --header "Content-Type: application/json" --data '{"jsonrpc": "2.0", "method": "Player.Stop", "params": {"playerid":1}, "id": 1}' "http://192.168.0.42:8080/jsonrpc"'

Error on startup:

16-12-24 15:13:20 homeassistant.util.yaml: while parsing a block mapping
  in "/home/homeassistant/.homeassistant/shell.yaml", line 1, column 1
expected <block end>, but found '{'
  in "/home/homeassistant/.homeassistant/shell.yaml", line 16, column 78

I messed around with it and found if I change ’ to " around {"jsonrpc":"2.0","id":"1","method":"Player.Open","params":{"file":"home/pi/Fireplace.mp4"}}} then I don’t get startup errors, or errors when running it, but it’s still not working from there.

OK. You’ve got single quotes in that string. I didn’t notice that before. So you can’t wrap it in single quotes since the first time you hit a quote within the string, it’ll close. IIRC, you escape in Jinja with {{}}. You’ll need to research escaping characters a bit.

Alternately, you can sometimes use this style notation (that avoids quoting):

kodi_start_playback: >
  curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","id":"1","method":"Player.Open","params":{"file":"home/pi/Fireplace.mp4"}}}' "http://192.168.0.42:8080/jsonrpc"

Thanks, I’ll give that a try, but otherwise I’ll just stick to the bash scripts.