This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Sony Bravia Authentication
#5
I cleaned up your code a little to be tailored more to my needs. Also included a status object checker, convenient to get a feedback of the actual settings of the TV. 

   

Common Functions:
Code:
function ipControlSonyTV(service, command) 
  local http = require("socket.http")
  local ltn12 = require("ltn12")
  require("json")

  local ip = "192.168.1.135"
 
  local response_body = { }
  local res, code, response_headers, status = http.request
  {
    url = "http://" .. ip .. "/sony/" .. service,
    method = "POST",
    headers =
    {
      ["HOST"] = ip,
      ["Accept"] = "*/*",
      ["Content-Type"] = "application/x-www-form-urlencoded",
      ["X-Auth-PSK"] = "************",
      ["Content-Length"] = command:len()
    },
    source = ltn12.source.string(command),       
    sink = ltn12.sink.table(response_body)
  }
 
  response = json.decode(response_body[1])
  return response["result"][1]
end

Resident Script (every 2 seconds or as much as needed):
Code:
command = [[ {"method": "getPowerStatus","id": 50,"params": [],"version": "1.0"} ]]
powerStatus = ipControlSonyTV("system", command)
powerStatus = powerStatus["status"]

if (powerStatus == "active") then
  grp.checkwrite('5/0/2', 1, 1)
elseif (powerStatus == "standby") then
  grp.checkwrite('5/0/2', 0, 1)
end

if (powerStatus == "active") then
  command = [[ {"method": "getVolumeInformation", "id": 33, "params": [], "version": "1.0"} ]]
  volumeInfo = ipControlSonyTV("audio", command)
  volumeStatus = volumeInfo[1]["volume"]
  muteStatus = volumeInfo[1]["mute"]
  grp.checkwrite('5/0/5', volumeStatus, 1)
  grp.checkwrite('5/0/7', muteStatus, 1)
 
  command = [[ {"method": "getPlayingContentInfo","id": 103,"params": [],"version": "1.0"} ]]
  sourceStatus = ipControlSonyTV("avContent", command)
  sourceStatus = sourceStatus["uri"]
  sourceStatus = tonumber(string.sub(sourceStatus, -1))
  grp.checkwrite('5/0/9', sourceStatus, 1)
end

Every control object has it's own event script
Power:
Code:
power = event.getvalue()

if (power) then
  command = [[ {"method": "setPowerStatus","id": 55,"params": [{"status": true}],"version": "1.0"} ]]
else
  command = [[ {"method": "setPowerStatus","id": 55,"params": [{"status": false}],"version": "1.0"} ]]
end

ipControlSonyTV("system", command)

Volume Step:
Code:
volumeDown = event.getvalue()  -- down = 1 | up = 0

if (volumeDown) then
  command = [[ {"method": "setAudioVolume","id": 98,"params": [{"volume": "-1","ui": "on","target": "speaker"}],"version": "1.2"} ]]
else
  command = [[ {"method": "setAudioVolume","id": 98,"params": [{"volume": "+1","ui": "on","target": "speaker"}],"version": "1.2"} ]]
end

ipControlSonyTV("audio", command)

Volume:
Code:
volume = event.getvalue()

command = [[ {"method": "setAudioVolume","id": 98,"params": [{"volume": "]] .. volume .. [[","ui": "on","target": "speaker"}],"version": "1.2"} ]]

ipControlSonyTV("audio", command)

Mute:
Code:
mute = event.getvalue()

if (mute) then
  command = [[ {"method": "setAudioMute","id": 601,"params": [{"status": true}],"version": "1.0"} ]]
else
  command = [[ {"method": "setAudioMute","id": 601,"params": [{"status": false}],"version": "1.0"} ]]
end

ipControlSonyTV("audio", command)

Source Select:
Code:
source = event.getvalue()
power = grp.getvalue('5/0/2')

if (not power) then
  grp.write('5/0/1', 1)
  os.sleep(1)
end

command = [[ {"method": "setPlayContent","id": 101,"params": [{"uri": "extInput:hdmi?port=]] .. source .. [["}],"version": "1.0"} ]]

ipControlSonyTV("avContent", command)
Reply


Messages In This Thread
Sony Bravia Authentication - by arnocl - 17.02.2020, 09:58
RE: Sony Bravia Authentication - by admin - 17.02.2020, 10:14
RE: Sony Bravia Authentication - by arnocl - 21.02.2020, 21:08
RE: Sony Bravia Authentication - by arnocl - 22.02.2020, 09:29

Forum Jump: