The Vera Community forums have moved!

Advanced => Programming => Scene Scripting => Topic started by: trouty00 on February 27, 2016, 04:14:09 pm

Title: Sony TV REST API
Post by: trouty00 on February 27, 2016, 04:14:09 pm
So this has been bought over here from the "what did you do with your vera today" thread as think I was annoying a few people over there.. but cybrmage was helping me out - so hopefully he will find me here...

I have a curl script which works but want to get into a function
Code: [Select]
curl --silent -XPOST http://192.168.1.xxx/sony/IRCC -d "<?xml version=\"1.0\"?><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>"AAAAAQAAAAEAAAATAw=="</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>" -H 'Content-Type: text/xml; charset=UTF-8' -H 'SOAPACTION: "urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"' -H "Cookie: auth=xxx" -o /dev/null

I have updated the URL in the below as with the original reccomendation i got a 404 and the url below is the one i use in a curl scripts which works


so essentially I want to send commands from my vera to my Sony Bravia 2015 android tv.
Code: [Select]
local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local SONY_IP = "192.168.1.225"
local AUTH_CODE = "75071BA72588155AD9FABC47EBDD353BA4ED40D7"
local IRCC_CODE = "AAAAAQAAAAEAAAAUAw=="
local REQUEST = "<?xml version=\"1.0\"?><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>"
REQUEST=REQUEST .. IRCC_CODE
REQUEST = REQUEST.."</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"
local SONY_URL = "http://"..SONY_IP.."/local_api.php?getDeviceList=true"

rBody, rCode, rHeaders, rStatus = http.request(
{
method = "POST",
url = SONY_URL,
headers = {
["Content-Type"] = "text/xml; charset=UTF-8",
["SOAPACTION"] = "urn:schemas-sony-com:service:IRCC:1#X_SendIRCC",
["Cookie"] = "auth="..AUTH_CODE
},
source = ltn12.source.string(REQUEST),
sink = ltn12.sink.table(respBody),
redirect = false
}
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")

i get the following in the log

Code: [Select]
0 02/27/16 21:19:12.518 luup_log:0: http response - code [500] html [

    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 
   
      s:Client
      UPnPError
     
       
          401
          Invalid Action
       
     
   
 
] <0x71dd2520>
04 02/27/16 21:19:20.209 <0x769d2520>
02 02/27/16 21:19:20.209 Device_Basic::AddPoll 10 poll list ful
Title: Re: Sony TV REST API
Post by: cybrmage on February 28, 2016, 01:33:10 pm
I have updated the URL in the below as with the original reccomendation i got a 404 and the url below is the one i use in a curl scripts which works

The URL in the script is the original URL from your original request...

You need to update it to point to the IRCC endpoint (the current URL points to a UPnP endpoint)

Also, I'm not sure about the auth header... A quick search shows it should be "X-Auth-PSK", not "Cookie". And, looking at the code I wrote for my Samsung TV (which also uses SOAP), the SOAP requests may need more headers (which may have been automatically added by curl command from the command prompt)...




Try:
Code: [Select]
local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local SONY_IP = "192.168.1.225"
local AUTH_CODE = "75071BA72588155AD9FABC47EBDD353BA4ED40D7"
local IRCC_CODE = "AAAAAQAAAAEAAAAUAw=="
local REQUEST = "<?xml version=\"1.0\"?><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>"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"

rBody, rCode, rHeaders, rStatus = http.request(
{
method = "POST",
url = SONY_URL,
headers = {
["Host"] = SONY_IP,
["Content-Type"] = "text/xml; charset=\"UTF-8\"0",
["Content-Length"] = tostring(#REQUEST)
["SOAPACTION"] = "urn:schemas-sony-com:service:IRCC:1#X_SendIRCC",
["X-Auth-PSK"] = AUTH_CODE,
["Cookie"] = "auth="..AUTH_CODE
},
source = ltn12.source.string(REQUEST),
sink = ltn12.sink.table(respBody),
redirect = false
}
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
Title: Re: Sony TV REST API
Post by: trouty00 on February 28, 2016, 03:37:35 pm
hmmm, the auth code seems to be a mix between the type of tv. running from curl it works fine with the cookie but never the PSK (pre shared key version)


tried with both as per below commenting out each option and  still no joy.

Code: [Select]
local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local SONY_IP = "192.168.1.225"
local AUTH_CODE1 = "1111"
local AUTH_CODE = "75071BA72588155AD9FABC47EBDD353BA4ED40D7"
local IRCC_CODE = "AAAAAQAAAAEAAAAUAw=="
local REQUEST = "<?xml version=\"1.0\"?><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>"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"

rBody, rCode, rHeaders, rStatus = http.request(
{
method = "POST",
url = SONY_URL,
headers = {
["Host"] = SONY_IP,
["Content-Type"] = "text/xml; charset=\"UTF-8\"0",
["Content-Length"] = tostring(#REQUEST),
["SOAPACTION"] = "urn:schemas-sony-com:service:IRCC:1#X_SendIRCC",
["X-Auth-PSK"] = AUTH_CODE1,
--["Cookie"] = "auth="..AUTH_CODE
},
source = ltn12.source.string(REQUEST),
sink = ltn12.sink.table(respBody),
redirect = false
}
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
always see the same error in the log
Code: [Select]
50 02/28/16 20:23:03.974 luup_log:0: http response - code [500] html [

    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 
   
      s:Client
      UPnPError
     
       
          401
          Invalid Action
       
     
   
 
] <0x70f85520>

edit-- so seems like an authorization issue. I have actually just got the PSK to work via curl but it is passed in a different way
Code: [Select]
-H "X-Auth-PSK:1111"  vs 
-H 'SOAPACTION: "urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"'

i dont really understand the construct here but guessing is it something to do with the x-auth not being a defined something or other and just need to send the whole thing as a header - sorry if im not making sense, i made an observation and am guessing..

Title: Re: Sony TV REST API
Post by: Brientim on February 28, 2016, 03:47:05 pm
They was some previous comments a while ago which eluded to another forum.

https://www.domoticz.com/forum/viewtopic.php?t=8301
Title: Re: Sony TV REST API
Post by: trouty00 on February 28, 2016, 04:07:30 pm
yeah i saw all this but they just seem to be sending the curl command - i know im close with this, just this slight issue need to get past..
Title: Re: Sony TV REST API
Post by: cybrmage on February 28, 2016, 06:01:22 pm
yeah i saw all this but they just seem to be sending the curl command - i know im close with this, just this slight issue need to get past..

Ok... so... The curl command work.... Modify the curl command... change "--silent" to "-v -v",run the command and post the output... It should show exactly what is going on with the curl command and we can see how it differs from the http.request...
Title: Re: Sony TV REST API
Post by: trouty00 on February 29, 2016, 09:52:20 am
here we are

Code: [Select]
curl -v -v -XPOST http://192.168.1.225/sony/IRCC -H "X-Auth-PSK:11
11" -d "<?xml version=\"1.0\"?><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>"
AAAAAQAAAAEAAAAUAw=="</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>" -H 'Conte
nt-Type: text/xml; charset=UTF-8' -H 'SOAPACTION: "urn:schemas-sony-com:service:
IRCC:1#X_SendIRCC"'
* About to connect() to 192.168.1.225 port 80 (#0)
*   Trying 192.168.1.225...
* Adding handle: conn: 0x50570
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x50570) send_pipe: 1, recv_pipe: 0
* Connected to 192.168.1.225 (192.168.1.225) port 80 (#0)
> POST /sony/IRCC HTTP/1.1
> User-Agent: curl/7.32.0
> Host: 192.168.1.225
> Accept: */*
> X-Auth-PSK:1111
> Content-Type: text/xml; charset=UTF-8
> SOAPACTION: "urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"
> Content-Length: 291
>
* upload completely sent off: 291 out of 291 bytes
< HTTP/1.1 200 OK
< Content-Type: text/xml; charset="utf-8"
< Content-Length: 292
< Connection: keep-alive
< Date: Mon, 29 Feb 2016 14:51:30 GMT+00:00
< Ext:
* Server Android/5.1.1 UPnP/1.0 IRCC/1.0 is not blacklisted
< Server: Android/5.1.1 UPnP/1.0 IRCC/1.0
<
<?xml version="1.0"?>
<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <u:X_SendIRCCResponse xmlns:u="urn:schemas-sony-com:service:IRCC:1">
    </u:X_SendIRCCResponse>
  </s:Body>
* Connection #0 to host 192.168.1.225 left intact
Title: Re: Sony TV REST API
Post by: cybrmage on February 29, 2016, 12:04:02 pm
Code: [Select]

local SONY_IP = "192.168.1.225"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
local AUTH_CODE = "1111"
local IRCC_CODE = "AAAAAQAAAAEAAAAUAw=="

local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local REQUEST = "<?xml version=\"1.0\"?><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\">\"".. IRCC_CODE .. "\"</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

rBody, rCode, rHeaders, rStatus = http.request(
{
method = "POST",
url = SONY_URL,
headers = {
["User-Agent"] = "curl/7.32.0",
["Host"] = SONY_IP,
["Accept"] = "*/*",
["X-Auth-PSK"] = AUTH_CODE,
["Content-Type"] = "text/xml; charset=UTF-8",
["Content-Length"] = tostring(#REQUEST),
["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
},
source = ltn12.source.string(REQUEST),
sink = ltn12.sink.table(respBody),
redirect = false
}
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")

Title: Re: Sony TV REST API
Post by: trouty00 on February 29, 2016, 02:25:45 pm
still getting the same error  ??? ???
Title: Re: Sony TV REST API
Post by: trouty00 on March 04, 2016, 02:33:32 pm
gave up in the end and am just using os.execute
Code: [Select]
os.execute([[curl --silent -XPOST http://ip.ip.ip.ip/sony/IRCC -H "X-Auth-PSK:****" -d "<?xml version=\"1.0\"?><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>"AAAAAQAAAAEAAAAvAw=="</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>" -H 'Content-Type: text/xml; charset=UTF-8' -H 'SOAPACTION: "urn:schemas-sony-com:service:IRCC:1#X_SendIRCC"']])
Title: Re: Sony TV REST API
Post by: asdf1nit on February 09, 2017, 08:30:26 pm
I wanted to do the same thing and make a plugin and found this. Did you ever get any farther with it? I'm going to play around with this and see what I can do but I haven't worked on the vera or with lua before so it'll be a learning experience. I Have read all the forum post both here and for other HA platforms about doing this and even have the python and bash versions working but I really want this as a plugin to the vera.
Title: Re: Sony TV REST API
Post by: asdf1nit on February 09, 2017, 10:43:18 pm
Ok it works. There were a couple errors in the local REQUEST string. I'm going to play with this some more and try to figure out a gui and get a plugin hammered out. It'll probably take me a little while but this function was the meat of the whole thing so thank you @trouty00 and @cybrmage for you work. I would have taken me a long while to figure that out(I couldn't find any documentation on sending soap request).


Code: [Select]
local SONY_IP = "10.0.1.5"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
local AUTH_CODE = "2020"
local IRCC = "AAAAAQAAAAEAAAAuAw=="

local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local REQUEST = "<?xml version=\"1.0\"?><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.."</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

rBody, rCode, rHeaders, rStatus = http.request(
{
method = "POST",
url = SONY_URL,
headers = {
["User-Agent"] = "curl/7.38.0",
["Host"] = SONY_IP,
["Accept"] = "*/*",
["X-Auth-PSK"] = AUTH_CODE,
["Content-Type"] = "text/xml; charset=UTF-8",
["Content-Length"] = tostring(#REQUEST),
["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
},
source = ltn12.source.string(REQUEST),
sink = ltn12.sink.table(respBody),
redirect = false
}
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")

And here's the LuaUpnp.log output
 
Code: [Select]
08      02/09/17 21:49:39.922   JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: RunLua <0x71e00520>
08      02/09/17 21:49:39.922   JobHandler_LuaUPnP::HandleActionRequest argument serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1 <0x71e00520>
08      02/09/17 21:49:39.922   JobHandler_LuaUPnP::HandleActionRequest argument action=RunLua <0x71e00520>
08      02/09/17 21:49:39.923   JobHandler_LuaUPnP::HandleActionRequest argument Code=local SONY_IP = "10.0.1.5"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
local AUTH_CODE = "2020"
local IRCC = "AAAAAQAAAAEAAAAuAw=="

local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local REQUEST = "<?xml version=\"1.0\"?><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.."</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

rBody, rCode, rHeaders, rStatus = http.request(
        {
                method = "POST",
                url = SONY_URL,
                headers = {
                        ["User-Agent"] = "curl/7.38.0",
                        ["Host"] = SONY_IP,
                        ["Accept"] = "*/*",
                        ["X-Auth-PSK"] = AUTH_CODE,
                        ["Content-Type"] = "text/xml; charset=UTF-8",
                        ["Content-Length"] = tostring(#REQUEST),
                        ["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
                },
                source = ltn12.source.string(REQUEST),
                sink = ltn12.sink.table(respBody),
                redirect = false
        }
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]") <0x71e00520>
50      02/09/17 21:49:39.938   luup_log:0: http response - code [200] html [<?xml version="1.0"?>
<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <u:X_SendIRCCResponse xmlns:u="urn:schemas-sony-com:service:IRCC:1">
    </u:X_SendIRCCResponse>
  </s:Body>
</s:Envelope>] <0x71e00520>
Title: Re: Sony TV REST API
Post by: asdf1nit on February 10, 2017, 12:38:33 am
And Just to make it easy to send commands
Code: [Select]
command = {
PowerOff="AAAAAQAAAAEAAAAvAw==",
["PowerOn"]="AAAAAQAAAAEAAAAuAw==",
["VolumeUp"]="AAAAAQAAAAEAAAASAw==",
["VolumeDown"]="AAAAAQAAAAEAAAATAw==",
["Mute"]="AAAAAQAAAAEAAAAUAw==",
["Input"]="AAAAAQAAAAEAAAAlAw==",
["GGuide"]="AAAAAQAAAAEAAAAOAw==",
["EPG"]="AAAAAgAAAKQAAABbAw==",
["Favorites"]="AAAAAgAAAHcAAAB2Aw==",
["Display"]="AAAAAQAAAAEAAAA6Aw==",
["Home"]="AAAAAQAAAAEAAABgAw==",
["Options"]="AAAAAgAAAJcAAAA2Aw==",
["Return"]="AAAAAgAAAJcAAAAjAw==",
["Up"]="AAAAAQAAAAEAAAB0Aw==",
["Down"]="AAAAAQAAAAEAAAB1Aw==",
["Right"]="AAAAAQAAAAEAAAAzAw==",
["Left"]="AAAAAQAAAAEAAAA0Aw==",
["Confirm"]="AAAAAQAAAAEAAABlAw==",
["Red"]="AAAAAgAAAJcAAAAlAw==",
["Green"]="AAAAAgAAAJcAAAAmAw==",
["Yellow"]="AAAAAgAAAJcAAAAnAw==",
["Blue"]="AAAAAgAAAJcAAAAkAw==",
["Num1"]="AAAAAQAAAAEAAAAAAw==",
["Num2"]="AAAAAQAAAAEAAAABAw==",
["Num3"]="AAAAAQAAAAEAAAACAw==",
["Num4"]="AAAAAQAAAAEAAAADAw==",
["Num5"]="AAAAAQAAAAEAAAAEAw==",
["Num6"]="AAAAAQAAAAEAAAAFAw==",
["Num7"]="AAAAAQAAAAEAAAAGAw==",
["Num8"]="AAAAAQAAAAEAAAAHAw==",
["Num9"]="AAAAAQAAAAEAAAAIAw==",
["Num0"]="AAAAAQAAAAEAAAAJAw==",
["Num11"]="AAAAAQAAAAEAAAAKAw==",
["Num12"]="AAAAAQAAAAEAAAALAw==",
["Tv"]="AAAAAQAAAAEAAAAkAw==",
["Hdmi1"]="AAAAAgAAABoAAABaAw==",
["Hdmi2"]="AAAAAgAAABoAAABbAw==",
["Hdmi3"]="AAAAAgAAABoAAABcAw==",
["Hdmi4"]="AAAAAgAAABoAAABdAw==",
["Google"]="AAAAAgAAAMQAAAA7Aw==",
["ActionMenu"]="AAAAAgAAAMQAAABLAw==",
["ChannelUp"]="AAAAAQAAAAEAAAAQAw==",
["ChannelDown"]="AAAAAQAAAAEAAAARAw==",
["SubTitle"]="AAAAAgAAAJcAAAAoAw==",
["ClosedCaption"]="AAAAAgAAAKQAAAAQAw==",
["Enter"]="AAAAAQAAAAEAAAALAw==",
["DOT"]="AAAAAgAAAJcAAAAdAw==",
["Analog"]="AAAAAgAAAHcAAAANAw==",
["Teletext"]="AAAAAQAAAAEAAAA/Aw==",
["Exit"]="AAAAAQAAAAEAAABjAw==",
["Analog2"]="AAAAAQAAAAEAAAA4Aw==",
["AD"]="AAAAAgAAABoAAAA7Aw==",
["Digital"]="AAAAAgAAAJcAAAAyAw==",
["Analog"]="AAAAAgAAAJcAAAAuAw==",
["BS"]="AAAAAgAAAJcAAAAsAw==",
["CS"]="AAAAAgAAAJcAAAArAw==",
["BSCS"]="AAAAAgAAAJcAAAAQAw==",
["Ddata"]="AAAAAgAAAJcAAAAVAw==",
["PicOff"]="AAAAAQAAAAEAAAA+Aw==",
["Tv_Radio"]="AAAAAgAAABoAAABXAw==",
["Theater"]="AAAAAgAAAHcAAABgAw==",
["SEN"]="AAAAAgAAABoAAAB9Aw==",
["InternetWidgets"]="AAAAAgAAABoAAAB6Aw==",
["InternetVideo"]="AAAAAgAAABoAAAB5Aw==",
["Netflix"]="AAAAAgAAABoAAAB8Aw==",
["SceneSelect"]="AAAAAgAAABoAAAB4Aw==",
["Mode3D"]="AAAAAgAAAHcAAABNAw==",
["iManual"]="AAAAAgAAABoAAAB7Aw==",
["Audio"]="AAAAAQAAAAEAAAAXAw==",
["Wide"]="AAAAAgAAAKQAAAA9Aw==",
["Jump"]="AAAAAQAAAAEAAAA7Aw==",
["PAP"]="AAAAAgAAAKQAAAB3Aw==",
["MyEPG"]="AAAAAgAAAHcAAABrAw==",
["ProgramDescription"]="AAAAAgAAAJcAAAAWAw==",
["WriteChapter"]="AAAAAgAAAHcAAABsAw==",
["TrackID"]="AAAAAgAAABoAAAB+Aw==",
["TenKey"]="AAAAAgAAAJcAAAAMAw==",
["AppliCast"]="AAAAAgAAABoAAABvAw==",
["acTVila"]="AAAAAgAAABoAAAByAw==",
["DeleteVideo"]="AAAAAgAAAHcAAAAfAw==",
["PhotoFrame"]="AAAAAgAAABoAAABVAw==",
["TvPause"]="AAAAAgAAABoAAABnAw==",
["KeyPad"]="AAAAAgAAABoAAAB1Aw==",
["Media"]="AAAAAgAAAJcAAAA4Aw==",
["SyncMenu"]="AAAAAgAAABoAAABYAw==",
["Forward"]="AAAAAgAAAJcAAAAcAw==",
["Play"]="AAAAAgAAAJcAAAAaAw==",
["Rewind"]="AAAAAgAAAJcAAAAbAw==",
["Prev"]="AAAAAgAAAJcAAAA8Aw==",
["Stop"]="AAAAAgAAAJcAAAAYAw==",
["Next"]="AAAAAgAAAJcAAAA9Aw==",
["Rec"]="AAAAAgAAAJcAAAAgAw==",
["Pause"]="AAAAAgAAAJcAAAAZAw==",
["Eject"]="AAAAAgAAAJcAAABIAw==",
["FlashPlus"]="AAAAAgAAAJcAAAB4Aw==",
["FlashMinus"]="AAAAAgAAAJcAAAB5Aw==",
["TopMenu"]="AAAAAgAAABoAAABgAw==",
["PopUpMenu"]="AAAAAgAAABoAAABhAw==",
["RakurakuStart"]="AAAAAgAAAHcAAABqAw==",
["OneTouchTimeRec"]="AAAAAgAAABoAAABkAw==",
["OneTouchView"]="AAAAAgAAABoAAABlAw==",
["OneTouchRec"]="AAAAAgAAABoAAABiAw==",
["OneTouchStop"]="AAAAAgAAABoAAABjAw==",
["DUX"]="AAAAAgAAABoAAABzAw==",
["FootballMode"]="AAAAAgAAABoAAAB2Aw==",
["Social"]="AAAAAgAAABoAAAB0Aw=="}


function SendCommand(Command)
local SONY_IP = "10.0.1.5"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
local AUTH_CODE = "2020"
local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local REQUEST = "<?xml version=\"1.0\"?><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>"..Command.."</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

rBody, rCode, rHeaders, rStatus = http.request(
{
method = "POST",
url = SONY_URL,
headers = {
["User-Agent"] = "curl/7.38.0",
["Host"] = SONY_IP,
["Accept"] = "*/*",
["X-Auth-PSK"] = AUTH_CODE,
["Content-Type"] = "text/xml; charset=UTF-8",
["Content-Length"] = tostring(#REQUEST),
["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
},
source = ltn12.source.string(REQUEST),
sink = ltn12.sink.table(respBody),
redirect = false
}
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
end

SendCommand(command["VolumeUp"])
Title: Re: Sony TV REST API
Post by: trouty00 on February 23, 2017, 01:11:45 pm
great work, are you still working on a plugin or just going to use the code. will probbaly look at tweaking my lua code to incorporate this rather tan the os request i was using.

Stu
Title: Re: Sony TV REST API
Post by: Quixote on March 15, 2017, 11:58:38 pm
This looks pretty interesting.
What model TVs will this work with? I have a Sony KDL-55W802A that I was hoping to control over my LAN.
Title: Re: Sony TV REST API
Post by: asdf1nit on March 19, 2017, 05:20:39 pm
This looks pretty interesting.
What model TVs will this work with? I have a Sony KDL-55W802A that I was hoping to control over my LAN.
This should work on all of the current Bravia series that use this protocol. All the way back to the A series in 2013

great work, are you still working on a plugin or just going to use the code. will probbaly look at tweaking my lua code to incorporate this rather tan the os request i was using.

Stu

I've been working on it some in my spare time but I have a big network project that's taking up a lot it. I've never made a plugin so I'm basing it around the Samsung TV plugin that I use for my other TV. I haven't worked on anything in a couple weeks. Right now I'm just using the script os.exec way. it actually works well when called from bwsSystems hue bridge via Alexa. I'm only using power and volume commands.
Title: Re: Sony TV REST API
Post by: Quixote on March 20, 2017, 12:16:24 am
Thanks!
A plugin sure would be a great help, but I'll see what I can figure out with what's been shared already. I only need a few functions as well.
Title: Re: Sony TV REST API
Post by: Quixote on March 21, 2017, 04:31:25 pm
Works like a charm!   This made my day!

The only thing that I'm ever-so-slightly disappointed about is the time it takes for a command to go through (not practical for volume changes), but that could just be the Vera UI that is causing the delay. I haven't experimented enough yet, but I'll test other input methods.

Thanks again!

Title: Re: Sony TV REST API
Post by: Styxman on June 29, 2017, 05:46:38 pm
Don't know if this thread is still alive....I installed the code provided above and it worked like a charm---for about 24 hours, now it won't run.  Seems like it's something in Vera.  Has anyone had this issue and if so, is there a fix?

Thanks.
Title: Re: Sony TV REST API
Post by: Derek Macias on June 30, 2017, 05:50:51 am
Don't know if this thread is still alive....I installed the code provided above and it worked like a charm---for about 24 hours, now it won't run.  Seems like it's something in Vera.  Has anyone had this issue and if so, is there a fix?

Thanks.
This is what I'm using in a few scenes.
Code: [Select]
function SendCommand(Command)
local SONY_IP = "xxx.xxx.xxx.xxx"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
local AUTH_CODE = "password"
local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local REQUEST = "<?xml version=\"1.0\"?><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>"..Command.."==</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

rBody, rCode, rHeaders, rStatus = http.request(
    {
        method = "POST",
        url = SONY_URL,
        headers = {
            ["User-Agent"] = "curl/7.38.0",
            ["Host"] = SONY_IP,
            ["Accept"] = "*/*",
            ["X-Auth-PSK"] = AUTH_CODE,
            ["Content-Type"] = "text/xml; charset=UTF-8",
            ["Content-Length"] = tostring(#REQUEST),
            ["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
        },
        source = ltn12.source.string(REQUEST),
        sink = ltn12.sink.table(respBody),
        redirect = false
    }
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
end

SendCommand("AAAAAQAAAAEAAAAuAw")


This is for tv power on. Just substitute your tv's ip address. You can change the send command with the appropriate code from the array for other functions. I just moved the == to the request and I thought the array was more appropriate for a plugin and just include the command in each scene minus the ==. I don't know if it matters but I didn't want to include that huge array I each scene's lua. This works fine for me using the HA bridge and alexa. It consistently turns the tv on and off and volume up and down.

I have some other functions for checking status and getting other info I can share when I get to my laptop. I can get the tv's power status. The only thing that sucks is there's not a function to set volume on my tv when I use curl to get a list of supported functions. E.g. You can't set volume to a value of 20.
Title: Re: Sony TV REST API
Post by: Quixote on August 05, 2017, 01:39:14 am
Don't know if this thread is still alive....I installed the code provided above and it worked like a charm---for about 24 hours, now it won't run.  Seems like it's something in Vera.  Has anyone had this issue and if so, is there a fix?

Thanks.

Is it possible that you haven't set a static IP for your TV on your LAN in your router settings and the TV is being assigned a new address? Just a thought.
Title: Re: Sony TV REST API
Post by: Don Phillips on August 05, 2017, 12:24:21 pm
Don't know if this thread is still alive....I installed the code provided above and it worked like a charm---for about 24 hours, now it won't run.  Seems like it's something in Vera.  Has anyone had this issue and if so, is there a fix?

Thanks.

Is it possible that you haven't set a static IP for your TV on your LAN in your router settings and the TV is being assigned a new address? Just a thought.

I tend to leave my devices on DHCP and set up the router to assign IP addresses based on that device's MAC address. It seems some devices prefer DHCP but I prefer to know it's IP address is the same when connected.
Title: Re: Sony TV REST API
Post by: conchordian on August 18, 2017, 12:48:44 am
http://www.eventghost.net/forum/viewtopic.php?f=9&t=6067 and
https://openremote.github.io/archive-dotorg/forums/Sony%20TV%20HTTP%20control.html have some really good info on the matter.

Title: Re: Sony TV REST API
Post by: kdschlosser on October 03, 2017, 01:49:44 pm
@conchordian

TY for linking to the eventghost website for this. I wanted to touch base on it a little bit. As we all know the Vera is very handicapped in the resources department so it is always a wise decision to keep the "extras" off of it. Through the use of virtual switches you can use the EventGhost plugin for the Vera  EventGhost plugin for the Vera (http://www.eventghost.net/forum/viewtopic.php?p=39323#p39323). the virtual switches will create "events" or notifications in EventGhost when it's status changes on the Vera. because of this you can then point that event to perform some specific task. In this case you would have the EventGhost plugin for Sony Bravia TV's (http://www.eventghost.net/forum/viewtopic.php?f=9&t=6067). installed into EventGhost and you can marry the state change of a virtual switch to a specific remote control function.

I am currently in the process of writing a whole API for the Sony TV's. this is a very long process as none of the API is really documented. But I do believe I have the most complete API available these TV's. It does everything right down to getting playing content information turning the TV on properly. as well as starting and stopping Android applications and listing installed applications along with grabbing the applications icon (application features only available on android versions of the TV) If you have a TV and you are familiar with Python and want to help/test please drop a line in my Sony API (https://github.com/kdschlosser/SonyAPI) GitHub repository. This could also help someone if they want to port it over to the Vera as well. The API is not yet complete as there are some things that i wish to change with it. But it does have TV auto discovery supports pre shared keys as well as pin input. automatic grabbing of the MAC address if your TV needs a WOL to turn it on. it will automatically change the WOL setting on the TV if it has to use it and it's turned off. You can also list and play content on a USB device. Get the station names and playing content of a station. set recording schedules. Honestly i can't even remember what else. Quite possibly if I get some help with it I would get out of being burnt out with working on it. and it will spark some new ideas on how to solve some issues. Would help to deliver a complete API quicker.

Thanks again and look for an update to this in the coming months
Title: Re: Sony TV REST API
Post by: Quixote on October 04, 2017, 08:42:34 pm
Hi kdschlosser,

Nice to see bright people making progress on things like this.
One thing I would like to point out, though, is that the Vera -- as limited as it is -- makes a great stand-alone controller for people that take trips and bring their computers with them. For that reason, it would be good to have a plugin that resides solely on the Vera. I fully understand that software solutions like EventGhost and Girder are waaay more powerful and versatile, but when I take off for a couple of weeks, I bring my laptop and leave my Vera and Elk alarm system to look after my place. Granted, television control would be a pretty elaborate method to feign occupancy, it would also be very convincing.
Please take this into consideration and thanks for your much appreciated efforts.
 
Title: Re: Sony TV REST API
Post by: kdschlosser on October 05, 2017, 11:49:09 am
yes the Vera is a standalone controller. But you have to remember it is a standalone ZWave controller not a control your whole house controller. I do have a question tho if you were in another country what would be the purpose to controlling you TV that is at home? Just a question I guess. Because for those users that have no need to do such a thing it would be a complete waste of resources on the Vera to have a plugin that would allow for full control of the TV.

I have 10 security cameras and the Vera can control the security cameras. but if i put anything more then 2 or 3 of them on the Vera . the Vera has a really difficult time. just because you can doesn't mean you should LOL. the Vera is a ZWave controller and that is what it's design was for. all the rest of the things it can control it really shouldn't be doing. it was never designed to handle it. I personally use it as a bridge between ZWave and the rest of my network. I have EventGhost tell the Vera to turn lights on or off and the Vera sends state changes back to EventGhost.

for the willing programmer they could port it over to the Vera if they wanted to. I am more then happy to explain how it works
Title: Re: Sony TV REST API
Post by: ember1205 on October 05, 2017, 01:28:35 pm
yes the Vera is a standalone controller. But you have to remember it is a standalone ZWave controller not a control your whole house controller. I do have a question tho if you were in another country what would be the purpose to controlling you TV that is at home? Just a question I guess. Because for those users that have no need to do such a thing it would be a complete waste of resources on the Vera to have a plugin that would allow for full control of the TV.

I have 10 security cameras and the Vera can control the security cameras. but if i put anything more then 2 or 3 of them on the Vera . the Vera has a really difficult time. just because you can doesn't mean you should LOL. the Vera is a ZWave controller and that is what it's design was for. all the rest of the things it can control it really shouldn't be doing. it was never designed to handle it. I personally use it as a bridge between ZWave and the rest of my network. I have EventGhost tell the Vera to turn lights on or off and the Vera sends state changes back to EventGhost.

for the willing programmer they could port it over to the Vera if they wanted to. I am more then happy to explain how it works

Your question is legit for people that live alone. But, what about when multiple people live in the house and only one is out of the house and has the computer with them? Those that aren't traveling lose the ability to control the TV with Vera?

How about the people that turn the TV on for pets? What if they want a way to turn it on automatically while they're away? Or off?

How about someone that travels and wants to be able to turn the TV on and off while they aren't home for theft deterrence?

You're being very short-sighted with the assumptions you're making about why it doesn't need to be self-contained on the Vera.
Title: Re: Sony TV REST API
Post by: Quixote on October 15, 2017, 07:08:48 pm
Well, it's a valid question and ember1205 has thought it through even more than I have, but my intention was to treat the TV as another light that would follow my regular schedule and maybe even control the volume so that it is lower after a certain time. Little details like this would fool most potential thieves. Anyone can pick up light timers at the local hardware store but few realize that a TV could be controlled similarly. A system like this would even fool my neighbors into believing I'm home. I'm not willing to buy another laptop just for this functionality.
To send out commands to the TV from the Vera would be a trivial task, resource-wise. It's no different than controlling an Internet-connected light. Most of us do not live in mansions and control 100s of lights.
Title: Re: Sony TV REST API
Post by: ember1205 on October 16, 2017, 08:10:36 am
You could also use a ZWave outlet in combination with a fake TV...

https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=fake+tv

Just sayin'  :)
Title: Re: Sony TV REST API
Post by: Quixote on October 17, 2017, 03:15:18 am
Well, I'm not about to shell out 90 bucks for a fake TV and a Zwave appliance module when I have everything I need already, aside from the plugin itself. Besides, I'm pretty sure the fake TV doesn't broadcast TV audio.
Worst case scenario I would just take the time to create scripts instead of using a plugin.
Title: Re: Sony TV REST API
Post by: kdschlosser on November 09, 2017, 01:37:42 am
as I state once again. You are simply looking to turn it on and off. that can be done very simply by sending a WOL packet to turn it on. and a simple web command to turn it off. The vera does not have the capacity to process/store the amount of information that these TV's contain. The API i wrote accesses every last part of it right down to grabbing the Android application icons. It is able to capture the content metadata from the TV as well as the channel listings.. I do not think that there is enough memory on the vera to handle it.

and for < 40 dollars USD a PC can be purchased that would supply more then enough power and resources that would be able to handle these kinds of things.. and it's footprint is not a whole lot bigger then the vera.

I am just making a suggestion because I have seen first hand what happens when the vera gets overloaded. It actually will soft brick on ya causing a whole lot of unneeded work. The vera was purpose built as a zwave controller. the ability to add "extras" came with time. and because of how its plugin and scripting work a lot of people think that it can handle controlling every single device in a house... and this it cannot.. it can't even handle the number of devices that Mios states it can (reliably).

so my suggestion is to let the vera do what it was originally intended to do.. control zwave devices. if you want to add extremely simple things like turning a TV on and off that would be fine. But to use this whole API would surly beat the vera with a rather large stick. I posted about it so if a user that did not have a large number of plugins on their vera and not a huge amount of devices wanted to have the ability to provide a little more functionality for controlling their TV the information is there so it can be used as a guide.

In the end I am simply making a suggestion. and you and anyone else can use the information contained in the API in any fashion they choose. that is the beauty of GNU licensing



Title: Re: Sony TV REST API
Post by: mnasri.jamaluddin on March 04, 2018, 12:06:24 pm
Don't know if this thread is still alive....I installed the code provided above and it worked like a charm---for about 24 hours, now it won't run.  Seems like it's something in Vera.  Has anyone had this issue and if so, is there a fix?

Thanks.
This is what I'm using in a few scenes.
Code: [Select]
function SendCommand(Command)
local SONY_IP = "xxx.xxx.xxx.xxx"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
local AUTH_CODE = "together"
local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local REQUEST = "<?xml version=\"1.0\"?><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>"..Command.."==</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

rBody, rCode, rHeaders, rStatus = http.request(
    {
        method = "POST",
        url = SONY_URL,
        headers = {
            ["User-Agent"] = "curl/7.38.0",
            ["Host"] = SONY_IP,
            ["Accept"] = "*/*",
            ["X-Auth-PSK"] = AUTH_CODE,
            ["Content-Type"] = "text/xml; charset=UTF-8",
            ["Content-Length"] = tostring(#REQUEST),
            ["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
        },
        source = ltn12.source.string(REQUEST),
        sink = ltn12.sink.table(respBody),
        redirect = false
    }
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
end

SendCommand("AAAAAQAAAAEAAAAuAw")


This is for tv power on. Just substitute your tv's ip address. You can change the send command with the appropriate code from the array for other functions. I just moved the == to the request and I thought the array was more appropriate for a plugin and just include the command in each scene minus the ==. I don't know if it matters but I didn't want to include that huge array I each scene's lua. This works fine for me using the HA bridge and alexa. It consistently turns the tv on and off and volume up and down.

I have some other functions for checking status and getting other info I can share when I get to my laptop. I can get the tv's power status. The only thing that sucks is there's not a function to set volume on my tv when I use curl to get a list of supported functions. E.g. You can't set volume to a value of 20.

Hi Derek! I found your lua code most useful to pause the TV when the doorbell rings (triggered by a z wave button).

I was wondering if you've gotten around sharing your lua code to check status of the TV. I'm looking to just have a virtual switch updated based on the TV on/off status. The idea is to cast the entrance cctv to the TV only if the TV is already switched on. If this check is not done, casting to a TV while it's off will inadvertently turn the TV on.

Really appreciate your guidance on this. Thanks!
Title: Re: Sony TV REST API
Post by: sefocuher on October 02, 2018, 04:11:36 pm
Hi,

Very good job! Now i can control my new tv Sony KD-65AF8 with Vera!
I make a virtual switch to control On and Off TV.
Now when i go "away" i'm sure the Tv is Off and when i make "Tv mode" all light off and TV On.

Thank you very very mutch
Title: Re: Sony TV REST API
Post by: Derek Macias on October 16, 2018, 07:33:00 pm
Don't know if this thread is still alive....I installed the code provided above and it worked like a charm---for about 24 hours, now it won't run.  Seems like it's something in Vera.  Has anyone had this issue and if so, is there a fix?

Thanks.
This is what I'm using in a few scenes.
Code: [Select]
function SendCommand(Command)
local SONY_IP = "xxx.xxx.xxx.xxx"
local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
local AUTH_CODE = "password"
local http = require("socket.http")
local ltn12 = require("ltn12")
local respBody = {}
local REQUEST = "<?xml version=\"1.0\"?><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>"..Command.."==</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

rBody, rCode, rHeaders, rStatus = http.request(
    {
        method = "POST",
        url = SONY_URL,
        headers = {
            ["User-Agent"] = "curl/7.38.0",
            ["Host"] = SONY_IP,
            ["Accept"] = "*/*",
            ["X-Auth-PSK"] = AUTH_CODE,
            ["Content-Type"] = "text/xml; charset=UTF-8",
            ["Content-Length"] = tostring(#REQUEST),
            ["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
        },
        source = ltn12.source.string(REQUEST),
        sink = ltn12.sink.table(respBody),
        redirect = false
    }
)
luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
end

SendCommand("AAAAAQAAAAEAAAAuAw")


This is for tv power on. Just substitute your tv's ip address. You can change the send command with the appropriate code from the array for other functions. I just moved the == to the request and I thought the array was more appropriate for a plugin and just include the command in each scene minus the ==. I don't know if it matters but I didn't want to include that huge array I each scene's lua. This works fine for me using the HA bridge and alexa. It consistently turns the tv on and off and volume up and down.

I have some other functions for checking status and getting other info I can share when I get to my laptop. I can get the tv's power status. The only thing that sucks is there's not a function to set volume on my tv when I use curl to get a list of supported functions. E.g. You can't set volume to a value of 20.

Hi Derek! I found your lua code most useful to pause the TV when the doorbell rings (triggered by a z wave button).

I was wondering if you've gotten around sharing your lua code to check status of the TV. I'm looking to just have a virtual switch updated based on the TV on/off status. The idea is to cast the entrance cctv to the TV only if the TV is already switched on. If this check is not done, casting to a TV while it's off will inadvertently turn the TV on.

Really appreciate your guidance on this. Thanks!

Sorry it has been a while since I messed with it. I was messing around with making a plugin using another as a template but this is what I had from the lua file. I didn't look into it more since I just turn the tv on and off using scenes and lua.

Code: [Select]
local command = {
    ["Num1"]="AAAAAQAAAAEAAAAAAw",
    ["Num2"]="AAAAAQAAAAEAAAABAw",
    ["Num3"]="AAAAAQAAAAEAAAACAw",
    ["Num4"]="AAAAAQAAAAEAAAADAw",
    ["Num5"]="AAAAAQAAAAEAAAAEAw",
    ["Num6"]="AAAAAQAAAAEAAAAFAw",
    ["Num7"]="AAAAAQAAAAEAAAAGAw",
    ["Num8"]="AAAAAQAAAAEAAAAHAw",
    ["Num9"]="AAAAAQAAAAEAAAAIAw",
    ["Num0"]="AAAAAQAAAAEAAAAJAw",
    ["Num11"]="AAAAAQAAAAEAAAAKAw",
    ["Num12"]="AAAAAQAAAAEAAAALAw",
    ["Enter"]="AAAAAQAAAAEAAAALAw",
    ["GGuide"]="AAAAAQAAAAEAAAAOAw",
    ["ChannelUp"]="AAAAAQAAAAEAAAAQAw",
    ["ChannelDown"]="AAAAAQAAAAEAAAARAw",
    ["VolumeUp"]="AAAAAQAAAAEAAAASAw",
    ["VolumeDown"]="AAAAAQAAAAEAAAATAw",
    ["Mute"]="AAAAAQAAAAEAAAAUAw",
    ["TvPower"]="AAAAAQAAAAEAAAAVAw",
    ["Audio"]="AAAAAQAAAAEAAAAXAw",
    ["MediaAudioTrack"]="AAAAAQAAAAEAAAAXAw",
    ["Tv"]="AAAAAQAAAAEAAAAkAw",
    ["Input"]="AAAAAQAAAAEAAAAlAw",
    ["TvInput"]="AAAAAQAAAAEAAAAlAw",
    ["TvAntennaCable"]="AAAAAQAAAAEAAAAqAw",
    ["WakeUp"]="AAAAAQAAAAEAAAAuAw",
    ["PowerOff"]="AAAAAQAAAAEAAAAvAw",
    ["Sleep"]="AAAAAQAAAAEAAAAvAw",
    ["Right"]="AAAAAQAAAAEAAAAzAw",
    ["Left"]="AAAAAQAAAAEAAAA0Aw",
    ["SleepTimer"]="AAAAAQAAAAEAAAA2Aw",
    ["Analog2"]="AAAAAQAAAAEAAAA4Aw",
    ["TvAnalog"]="AAAAAQAAAAEAAAA4Aw",
    ["Display"]="AAAAAQAAAAEAAAA6Aw",
    ["Jump"]="AAAAAQAAAAEAAAA7Aw",
    ["PicOff"]="AAAAAQAAAAEAAAA+Aw",
    ["PictureOff"]="AAAAAQAAAAEAAAA+Aw",
    ["Teletext"]="AAAAAQAAAAEAAAA/Aw",
    ["Video1"]="AAAAAQAAAAEAAABAAw",
    ["Video2"]="AAAAAQAAAAEAAABBAw",
    ["AnalogRgb1"]="AAAAAQAAAAEAAABDAw",
    ["Home"]="AAAAAQAAAAEAAABgAw",
    ["Exit"]="AAAAAQAAAAEAAABjAw",
    ["PictureMode"]="AAAAAQAAAAEAAABkAw",
    ["Confirm"]="AAAAAQAAAAEAAABlAw",
    ["Up"]="AAAAAQAAAAEAAAB0Aw",
    ["Down"]="AAAAAQAAAAEAAAB1Aw",
    ["ClosedCaption"]="AAAAAgAAAKQAAAAQAw",
    ["Component1"]="AAAAAgAAAKQAAAA2Aw",
    ["Component2"]="AAAAAgAAAKQAAAA3Aw",
    ["Wide"]="AAAAAgAAAKQAAAA9Aw",
    ["EPG"]="AAAAAgAAAKQAAABbAw",
    ["PAP"]="AAAAAgAAAKQAAAB3Aw",
    ["TenKey"]="AAAAAgAAAJcAAAAMAw",
    ["BSCS"]="AAAAAgAAAJcAAAAQAw",
    ["Ddata"]="AAAAAgAAAJcAAAAVAw",
    ["Stop"]="AAAAAgAAAJcAAAAYAw",
    ["Pause"]="AAAAAgAAAJcAAAAZAw",
    ["Play"]="AAAAAgAAAJcAAAAaAw",
    ["Rewind"]="AAAAAgAAAJcAAAAbAw",
    ["Forward"]="AAAAAgAAAJcAAAAcAw",
    ["DOT"]="AAAAAgAAAJcAAAAdAw",
    ["Rec"]="AAAAAgAAAJcAAAAgAw",
    ["Return"]="AAAAAgAAAJcAAAAjAw",
    ["Blue"]="AAAAAgAAAJcAAAAkAw",
    ["Red"]="AAAAAgAAAJcAAAAlAw",
    ["Green"]="AAAAAgAAAJcAAAAmAw",
    ["Yellow"]="AAAAAgAAAJcAAAAnAw",
    ["SubTitle"]="AAAAAgAAAJcAAAAoAw",
    ["CS"]="AAAAAgAAAJcAAAArAw",
    ["BS"]="AAAAAgAAAJcAAAAsAw",
    ["Digital"]="AAAAAgAAAJcAAAAyAw",
    ["Options"]="AAAAAgAAAJcAAAA2Aw",
    ["Media"]="AAAAAgAAAJcAAAA4Aw",
    ["Prev"]="AAAAAgAAAJcAAAA8Aw",
    ["Next"]="AAAAAgAAAJcAAAA9Aw",
    ["DpadCenter"]="AAAAAgAAAJcAAABKAw",
    ["CursorUp"]="AAAAAgAAAJcAAABPAw",
    ["CursorDown"]="AAAAAgAAAJcAAABQAw",
    ["CursorLeft"]="AAAAAgAAAJcAAABNAw",
    ["CursorRight"]="AAAAAgAAAJcAAABOAw",
    ["ShopRemoteControlForcedDynamic"]="AAAAAgAAAJcAAABqAw",
    ["FlashPlus"]="AAAAAgAAAJcAAAB4Aw",
    ["FlashMinus"]="AAAAAgAAAJcAAAB5Aw",
    ["AudioQualityMode"]="AAAAAgAAAJcAAAB7Aw",
    ["DemoMode"]="AAAAAgAAAJcAAAB8Aw",
    ["Analog"]="AAAAAgAAAHcAAAANAw",
    ["Mode3D"]="AAAAAgAAAHcAAABNAw",
    ["DigitalToggle"]="AAAAAgAAAHcAAABSAw",
    ["DemoSurround"]="AAAAAgAAAHcAAAB7Aw",
    ["*AD"]="AAAAAgAAABoAAAA7Aw",
    ["AudioMixUp"]="AAAAAgAAABoAAAA8Aw",
    ["AudioMixDown"]="AAAAAgAAABoAAAA9Aw",
    ["PhotoFrame"]="AAAAAgAAABoAAABVAw",
    ["Tv_Radio"]="AAAAAgAAABoAAABXAw",
    ["SyncMenu"]="AAAAAgAAABoAAABYAw",
    ["Hdmi1"]="AAAAAgAAABoAAABaAw",
    ["Hdmi2"]="AAAAAgAAABoAAABbAw",
    ["Hdmi3"]="AAAAAgAAABoAAABcAw",
    ["Hdmi4"]="AAAAAgAAABoAAABdAw",
    ["TopMenu"]="AAAAAgAAABoAAABgAw",
    ["PopUpMenu"]="AAAAAgAAABoAAABhAw",
    ["OneTouchTimeRec"]="AAAAAgAAABoAAABkAw",
    ["OneTouchView"]="AAAAAgAAABoAAABlAw",
    ["DUX"]="AAAAAgAAABoAAABzAw",
    ["FootballMode"]="AAAAAgAAABoAAAB2Aw",
    ["iManual"]="AAAAAgAAABoAAAB7Aw",
    ["Netflix"]="AAAAAgAAABoAAAB8Aw",
    ["Assists"]="AAAAAgAAAMQAAAA7Aw",
    ["FeaturedApp"]="AAAAAgAAAMQAAABEAw",
    ["FeaturedAppVOD"]="AAAAAgAAAMQAAABFAw",
    ["GooglePlay"]="AAAAAgAAAMQAAABGAw",
    ["ActionMenu"]="AAAAAgAAAMQAAABLAw",
    ["Help"]="AAAAAgAAAMQAAABNAw",
    ["TvSatellite"]="AAAAAgAAAMQAAABOAw",
    ["WirelessSubwoofer"]="AAAAAgAAAMQAAAB+Aw"
}

local SONY_IP = "xx.xx.xx.xx" --luup.devices[lul_device].ip
local AUTH_CODE = "password" --luup.devices[lul_device].authkey

function SendCommand(Command)
    local SONY_URL = "http://"..SONY_IP.."/sony/IRCC"
    local http = require("socket.http")
    local ltn12 = require("ltn12")
    local response = {}
    local REQUEST = "<?xml version=\"1.0\"?><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>"..command[Command].."==</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

    rBody, rCode, rHeaders, rStatus = http.request(
        {
            method = "POST",
            url = SONY_URL,
            headers = {
                ["User-Agent"] = "curl/7.38.0",
                ["Host"] = SONY_IP,
                ["Accept"] = "*/*",
                ["X-Auth-PSK"] = AUTH_CODE,
                ["Content-Type"] = "text/xml; charset=UTF-8",
                ["Content-Length"] = tostring(#REQUEST),
                ["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
            },
            source = ltn12.source.string(REQUEST),
            sink = ltn12.sink.table(response),
            redirect = false
        }
    )
    luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(response,"") or "NIL").."]")
end

function SetPower(Status)
    local SONY_URL = "http://"..SONY_IP.."/sony/system"
    local http = require("socket.http")
    local ltn12 = require("ltn12")
    local respBody = {}
    local REQUEST = '{"method":"setPowerStatus","version":"1.0","params":[{"status":'..Status..'}],"id":102}'

    rBody, rCode, rHeaders, rStatus = http.request(
      {
            method = "POST",
            url = SONY_URL,
            headers = {
                ["User-Agent"] = "curl/7.38.0",
                ["Host"] = SONY_IP,
                ["Accept"] = "*/*",
                ["X-Auth-PSK"] = AUTH_CODE,
                ["Content-Type"] = "application/json",
                ["Content-Length"] = tostring(#REQUEST),
            },
            source = ltn12.source.string(REQUEST),
            sink = ltn12.sink.table(respBody),
            redirect = false
        }
    )
    luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
end

function GetPowerStatus()
    local SONY_URL = "http://"..SONY_IP.."/sony/system"
    local http = require("socket.http")
    local ltn12 = require("ltn12")
    local respBody = {}
    local REQUEST =  '{"id":20,"method":"getPowerStatus","version":"1.0","params":[]}'

    rBody, rCode, rHeaders, rStatus = http.request(
        {
            method = "POST",
            url = SONY_URL,
            headers = {
                ["User-Agent"] = "curl/7.38.0",
                ["Host"] = SONY_IP,
                ["Accept"] = "*/*",
                ["X-Auth-PSK"] = AUTH_CODE,
                ["Content-Type"] = "text/xml; charset=UTF-8",
                ["Content-Length"] = tostring(#REQUEST),
                ["SOAPACTION"] = "\"urn:schemas-sony-com:service:IRCC:1#X_SendIRCC\""
            },
            source = ltn12.source.string(REQUEST),
            sink = ltn12.sink.table(respBody),
            redirect = false
        }
    )
    response = (table.concat(respBody,"") or "NIL")
    luup.log("http response - code ["..(rCode or "NIL").."] html ["..response.."]")

    if string.find(response, '{"status":"active"}') then
        return 'active'
    elseif string.find(response, '{"status":"standby"}') then
        return 'standby'
    else
        return 'unknown'
    end
end


This is directly from a scene to turn the tv off
Code: [Select]

local SONY_IP = "xxx.xxx.xxx.xxx" --luup.devices[lul_device].ip
local AUTH_CODE = "password" --luup.devices[lul_device].authkey

function SetPower(Status)
    local SONY_URL = "http://"..SONY_IP.."/sony/system"
    local http = require("socket.http")
    local ltn12 = require("ltn12")
    local respBody = {}
    local REQUEST = '{"method":"setPowerStatus","version":"1.0","params":[{"status":'..Status..'}],"id":102}'

    rBody, rCode, rHeaders, rStatus = http.request(
      {
            method = "POST",
            url = SONY_URL,
            headers = {
                ["User-Agent"] = "curl/7.38.0",
                ["Host"] = SONY_IP,
                ["Accept"] = "*/*",
                ["X-Auth-PSK"] = AUTH_CODE,
                ["Content-Type"] = "application/json",
                ["Content-Length"] = tostring(#REQUEST),
            },
            source = ltn12.source.string(REQUEST),
            sink = ltn12.sink.table(respBody),
            redirect = false
        }
    )
    luup.log("http response - code ["..(rCode or "NIL").."] html ["..(table.concat(respBody,"") or "NIL").."]")
end

SetPower("false")
Title: Re: Sony TV REST API
Post by: Quixote on October 17, 2018, 08:43:04 pm
A plugin would be pretty kick-ass!
It would be very useful to set variables that track the state of the television, including power/display state, input, volume, channel, etc. We could then use our IR equipment to watch for remote signals and adjust the variables accordingly. I used to use a similar method years ago when I was relying on Girder to do the heavy lifting in my automation setup. It rarely had issues.
Thanks for helping us out with this stuff. Wish I was capable of contributing.  :/