IP control of Sony TV with HA

In my opinion, the braviatv-Integration should be extended with this commands (from the soap-interface I think?)

The REST API on which the integration is apparently based does not provide that much functions.

I need to simulate remote-input like arrow keys…

https://pro-bravia.sony.net/develop/integrate/ircc-ip/overview/index.html

I made a remote in the past and I am still using it. You can read all about it here:

This could be a workaround for you. It is for me…

Thank you, I stumbled over this while google-ing. I will have a look at it - as it is a native sony feature, I guess IRCC-IP is quite responsive.
I set up ADB and the androidtv integration, but this is laggy and input takes 2 seconds to happen on the TV…

The thing with the REST API of the Sony TVs is, that quite a few things aren’t documented. Depending on what you want to do there might even be an interface for that.

With this payload

{"method":"getMethodTypes","params":[""],"id":1,"version":"1.0"}

You can get all possible methods from an API endpoint. With this command I was able to find the method getSceneSetting from the /videoScreen endpoint to get all “Scenes” (picture modes) from my Bravia as well as the current one. With that I can now easily automate switching my TV into “game” mode when my AVR has a gaming console selected as source or “cinema” mode when watching Netflix since the picture mode enables or disables certain post processing effects which reduce input lag.

Building a REST command around the /ircc interface wouldn’t be so difficult. I gave it a try:
rest_commands.yml

  sony_bravia_tv_send_ircc_code:
    url: 'http://{{ host }}/sony/ircc'
    method: POST
    headers:
      Content-Type: 'text/xml; charset=UTF-8'
      SOAPACTION: 'urn:schemas-sony-com:service:IRCC:1#X_SendIRCC'
      X-Auth-PSK: '{{ psk }}'
    payload: >-
      <s:Envelope
          xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
          s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
          <s:Body>
              <u:X_SendIRCC xmlns:u="urn:schemas-sony-com:service:IRCC:1">
                  <IRCCCode>{{ ircccode }}</IRCCCode>
              </u:X_SendIRCC>
          </s:Body>
      </s:Envelope>

And here is a simple button card which now sends the ‘AAAAAQAAAAEAAABgAw==’ IRCC code which maps to the home for my Bravia.

type: button
tap_action:
  action: call-service
  service: rest_command.sony_bravia_tv_send_ircc_code
  service_data:
    host: <your bravia ip>
    psk: <your private shared key>
    ircccode: AAAAAQAAAAEAAABgAw==
hold_action:
  action: more-info
show_icon: true
show_name: true
name: Home
icon: 'hass:home'

This works fine for my KDL-55W805B, you might have to change the URL path to /sony/IRCC instead of /sony/ircc in the rest_command as my older model seems to use /sony/IRCC where as the documentation from sony suggests /sony/ircc.

If you want a full list of IRCC commands that your TV supports just send the payload

{"method":"getRemoteControllerInfo","params":[""],"id":1,"version":"1.0"}

to the /sony/system endpoint

If you want to try out stuff just use a rest client for testing, like Advanced REST Client or Postman.

And about that, I’m afraid this won’t happen as the integration is a simple media_player entity and the media_player component only supports changing sound modes, sources, volume, media playback and media information. Adding functionality that is specific to Sony Bravias or other TVs would mean the devs had to extend the default media player entity to support that as well.

In my opinion we should have more specific components for TVs and AVRs, then the other devs who do integrations could add more features as well since they wouldn’t be bound by the available methods of media_player entity interface.

2 Likes

@shawly
Great, thanks a lot! :beers:
I created a feature request to extend the braviatv integration.

However, I will follow your example and build a rest bridge which I can utilize in the meantime (I am quite new to HA, but I think I see what you did there :slightly_smiling_face:).

I kickstartered for a YIO-remote (Touchscreen remote for HA) and in the end I want to be able to get rid of the ugly original sony remote and control everything with YIO.

Glad I could help.

Don’t get your hopes up too high for your feature request to go through though, but you might want to link it here so everybody can upvote it to increase the chance. :slight_smile:

The YIO-remote thingy looks pretty damn cool, thanks for sharing!

Edit Oh I forgot, the interfaces mentioned in the Sony documentation aren’t all available interfaces. But you can get all possible API endpoints by sending the following payload to the /sony/guide interface

{"method":"getServiceProtocols","params":[""],"id":1,"version":"1.0"}

Which will return an overview of all possible endpoints, then you can just crawl through every endpoint by sending the {"method":"getMethodTypes","params":[""],"id":1,"version":"1.0"} payload I mentioned before. With that you can map the complete API of your TV and use rest sensors and rest commands to control everything.

Hi @shawly

Thank you very much for your input, I will set this up in my environment. I think after this I don’t even need the requested features!

Don’t get your hopes up too high for your feature request to go through though, but you might want to link it here so everybody can upvote it to increase the chance.

braviatv-Integration - support IRCC-IP :slight_smile:

Best regards

Pete

Hi @shawly
I set up the rest_command. I forgot the PIN I had to enter when I set up ADB, so I created a PSK which I think should work too?

I am trying to send

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <u:X_SendIRCC xmlns:u="urn:schemas-sony-com:service:IRCC:1">
            <IRCCCode comment="Home">AAAAAQAAAAEAAABgAw==</IRCCCode>
        </u:X_SendIRCC>
    </s:Body>
</s:Envelope>

with header

SOAPACTION:urn:schemas-sony-com:service:IRCC:1#X_SendIRCC
X-Auth-PSK:1717

But I am getting the following error:

<?xml version="1.0"?>
<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <s:Fault>
            <faultcode>s:Client</faultcode>
            <faultstring>UPnPError</faultstring>
            <detail>
                <UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
                    <errorCode>401</errorCode>
                    <errorDescription>Invalid Action</errorDescription>
                </UPnPError>
            </detail>
        </s:Fault>
    </s:Body>
</s:Envelope>

Does this ring any bells for you?

@shawly
Never mind, I found it. The action in the header needs to be within quotation marks:

"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"

Oh alright. :smiley:

Great solution, i tried to implement it but im gettin error.Does anyone has any solution for this? what should i check. I also tired with ircc lowercase, same error.

Logger: homeassistant.components.rest_command
Source: components/rest_command/init.py:132
Integration: rest_command (documentation, issues)

Error. Url: http://xxx.xxx.xxx.xxx/sony/IRCC. Status code 500.

Send your entity configs, nobody can guess whats wrong without seeing the configuration.

I have the same error as @meternich

WARNING (MainThread) [homeassistant.components.rest_command] Error. Url: http://192.168.0.9/sony/ircc. Status code 500.
sony_tv_ircc:
  url: 'http://192.168.0.9/sony/ircc'
  method: POST
  headers:
    Content-Type: 'text/xml; charset=UTF-8'
    SOAPACTION: 'urn:schemas-sony-com:service:IRCC:1#X_SendIRCC'
    X-Auth-PSK: "mike01"
  payload: >-
    <s:Envelope
        xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
        s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <s:Body>
            <u:X_SendIRCC xmlns:u="urn:schemas-sony-com:service:IRCC:1">
                <IRCCCode>{{ ircccode }}</IRCCCode>
            </u:X_SendIRCC>
        </s:Body>
    </s:Envelope>

I have tried all various things but cannot get passed this. The call works if I use another REST client (ie Postman). I suspect the Xml payload is not getting through…but not sure why. Any tips would be greatly appreciated.

The payload is fine, your issue is either that the PSK you sent is wrong or the ircccode you’ve sent is invalid.
How do you test your rest_command? Did you create a button in lovelace or are you using the developer tools for sending the data? Please post the other stuff as well.

I have tried a ton of stuff. If the PSK is incorrect the HTTP response is a 403. For testing, the ircc code is hardcoded and I have tested it with another REST client. TBH I am out of ideas. I might have to step through the rest_command and see what the response is along with the 500 status code.

On my KDL-55W805B, the error response is 500 if the wrong PSK is sent.
But if you say the same data works in Postman your best bet is to turn on debug logging to see what has been sent to your TV.
Or you could use an interceptor to send the request to so you can see what home assistant is sending to your TVs API with something like this https://intercept.rest/

Well mystery solved. This configuration now works for me:

sony_tv_ircc:
  url: !secret media_player_braviatv_ircc_url
  method: POST
  headers:
    Content-Type: "text/xml; charset=UTF-8"
    SOAPACTION: '"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"'
    X-Auth-PSK: !secret media_player_braviatv_psk_psk
  payload: >-
    <s:Envelope
        xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
        s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <s:Body>
            <u:X_SendIRCC xmlns:u="urn:schemas-sony-com:service:IRCC:1">
                <IRCCCode>{{ircc_code}}</IRCCCode>
            </u:X_SendIRCC>
        </s:Body>
    </s:Envelope>
  verify_ssl: false

Note the single plus double quotes around the value for SOAPACTION (who misses SOAP…)

To debug this I copied the rest_command over into custom_components folder and added a whole bunch of debug statements to the source code. The most important one was the actual response:

2020-04-24 23:00:09 INFO (MainThread) [custom_components.rest_command] Errrrrrrrrrrrrrror <?xml version="1.0"?>
<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <s:Fault>
      <faultcode>s:Client</faultcode>
      <faultstring>UPnPError</faultstring>
      <detail>
        <UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
          <errorCode>401</errorCode>
          <errorDescription>Invalid Action</errorDescription>
        </UPnPError>
      </detail>
    </s:Fault>
  </s:Body>
</s:Envelope>
2020-04-24 23:00:09 WARNING (MainThread) [custom_components.rest_command] Error. Url: http://192.168.0.57/sony/ircc. Status code 500.

Then after a bit of googling & tinkering I found that the SOAPACTION needed to be formatted as above. Thanks everyone who tried to help out.

Sorry that i didn’t write my config. I just copied @shawly code and tried. Confrming @Mike’s code, it’s working for me to. Great job Mike, thx.

Sorry for this horribly noobish question… Been searching for something like this. I have the bravia integration and while it gives me control, i wanted some buttons to go directly to certain things. Where does rest_commands.yml go and also how does one aquire a psk key.

I figured as this integration allows control of channel, volume and on/off you must be able to create individual UI components to do specific tasks.

Not sure if this will help anyone, but last year I upgraded to a new Sony Bravia 950H, and figured out how to control it via Node Red. I would expect most (maybe all) of what I do in NR you can do in an automation, I hardly use automations so I don’t know how to do it that way.

I wrote up a page on how to implement in NR, maybe someone can figure out how to use this in HA.

For the PSK, you need to setup in the TV settings. I show how to find it in my TV, but yours might be different menu system. Its a four digit number, and you can try 0000 and see if that works, I think its the default.

If you use the rest-api interface instead of IR commands, you can also get information from the TV, and its easier to send commands that way. Links are in my page.

Through rest-api, you can get information from the TV on all the loaded apps, and extract a string that you can use to start an app. I give example strings for starting Sling, Disney +, Amazon Prime, Hulu and HBO. This is if you use Sony’s built in android tv.
Then it shows how to start that app in Node Red. I expect if you send the same command and payload in HA, it would start the app.

One other thing I show is how I poll the TV every few seconds, to see if its been turned on. I have an initialization flow in Node Red that I run when the TV is turned on. I wish there was a better way to do this than having to poll the TV constantly, but I don’t know what it is.

Randy