RESTful Command XML post to DIAL service for TV control

Hi!

Having a hard time understanding the RESTful Command service.

I’m trying to automate my TV via the DIAL API. Posting the following through a restful client Im able to control my TV, but I can’t get my head around how to get it to work using the restful service.

The endpoint to my TV is the following:
http://192.168.1.241:56791/apps/vr/remote

Posting the following as raw data will increase the volume (where the key code is the variable)
<?xml version="1.0" ?><remote><key code="1017"/></remote>

Have tried the following in the config but it just throws errors:

rest_command:
  tv_remote:
    method: POST
    url: 'http://192.168.1.241:56791/apps/vr/remote'
    payload: '<?xml version="1.0" ?><remote><key code="1017"/></remote>'

Would love to get some help on how ot integrate this with templating so I could get full control over my TV.

What errors does it throw?

What “restful client” are you using successfully, and can you provide more details about exactly what parameters you pass it?

Does the TV REST API require a login (i.e., username & password)?

Have you tried adding the rest_command’s content_type parameter? Might you need to specify any headers?

Sun Jul 15 2018 14:43:59 GMT+0200 (CEST)

Error executing service <ServiceCall rest_command.tv_remote>
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/components/rest_command.py", line 105, in async_service_handler
headers=headers
  File "/usr/local/lib/python3.6/site-packages/aiohttp/client.py", line 387, in _request
await resp.start(conn)
  File "/usr/local/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 748, in start
message, payload = await self._protocol.read()
  File "/usr/local/lib/python3.6/site-packages/aiohttp/streams.py", line 533, in read
await self._waiter
aiohttp.client_exceptions.ServerDisconnectedError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/app/homeassistant/core.py", line 1007, in _event_to_service_call
await service_handler.func(service_call)
  File "/usr/src/app/homeassistant/components/rest_command.py", line 118, in async_service_handler
_LOGGER.error("Client error %s.", request.url)
UnboundLocalError: local variable 'request' referenced before assignment

I tried Advanced Rest Client add-on for Chrome with the following:

  • Default headers
  • Body content type: application/XML

No need for authentication.

I have a hard time understanding the documentation for the service so I have no idea what the content type parameter should be?

Thanks for helping out!

Edit - Source from the Rest Client:

POST /apps/vr/remote HTTP/1.1
HOST: 192.168.1.241:56791
content-type: application/xml
content-length: 57

<?xml version="1.0" ?><remote><key code="1017"/></remote>

Try:

rest_command:
  tv_remote:
    method: POST
    url: 'http://192.168.1.241:56791/apps/vr/remote'
    payload: '<?xml version="1.0" ?><remote><key code="1017"/></remote>'
    content_type: 'application/xml'

Sucess! That did the trick!

Do you by any chance know how to turn this into something I could call from a template with different variables (key codes)?

Yep, was saving that until you got it working. :slight_smile:

rest_command:
  tv_remote:
    method: POST
    url: 'http://192.168.1.241:56791/apps/vr/remote'
    payload: '<?xml version="1.0" ?><remote><key code="{{ code }}"/></remote>'
    content_type: 'application/xml'

script:
  test:
    sequence:
      service: rest_command.tv_remote
      data:
        code: 1017

At least I think that should work. If not, then might need to use an input_text as a “variable” to pass the code. Let me know if the above works for you or not.

1 Like

Works great, thanks a bunch!

Tried passing the key code as a variable from the automation by editing the script as following:

test:
  sequence:
    service: rest_command.tv_remote
    data:
      code: "{{ keycode }}"

And the automation as by the following:

- alias: 'ATV-automation'
  trigger:
    - platform: state
      entity_id: remote.harmony_hub
  condition:
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Watch Apple TV" }}'
  action:
    service: script.test
    data:
      keycode: '1017'

This does not work, is it something obvious Im doing wrong?

Edit: Got it working by changing to data_template from data in the script.

1 Like