New Sony Bravia TV integration for 2015 - 2016 alpha

i might try removing the poll function from the DH and manually call it myself, seems as you have to call for a status update manually because the tv doesnt automatically send one out

I tried that few months back with no luck :frowning: There’s a standalone smartapp for polling devices think it’s called pollster and I also used WebCore to try the polling but neither worked so I just left it.
Let me know how you get on.

yeah i tried pollster too didnt work, maybe it might be worth tinkering with the pollster code to listen for something specific?

Yeah maybe I’ve never really dug into it as I’m not that bothered as I only use the on/off in routines which still works no matter what the status is showing.

exactly the same as me tbh, only integrated it with ST so that I could switch everything off when I go to bed without going downstairs haha

1 Like

y==ive been the same it works to turn the tv on and off vai voice command so ive not had too much motivation to fix it, but also the poll code was in the orirional code i found for the tv that I added too, so i dont really know how it works,

@Steveuk23: I’m using WebCoRE for this, and it works like a charm. If you want, you can PM me the piston and I can have a look, what I did different to get it work.

Yeah that would be great I did try webcore a little bit but didn’t have much luck.
Cheers

I noticed your older post as well.
How do you know the various status names?
I’m looking at WebCore to dim my lights if the TV starts playing something like Netflix etc.
So I put.
If TV status
changes to "playing"
Set level to 50% on "room lamp
But I’m just guessing on what the status miget be for that just wondered if there’s any documentation anywhere.
Cheers

To update the TV status in the deviceHandler installed() and updated() definition I added runEvery1Minute(ā€œpushPowerUpdateā€)
that run a json command ā€œgetPowerStatusā€. Then in parse() definition I added the code to handle the answer.
This obviously update the TV status every minute, not instantly, but it is the best I was able to do.

This is my deviceHandler, but it works only with the service manager I’m trying to create to simplify the connection.
/**
* Sony TV
*
* Copyright 2017 Simone
*
* Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Based on Steve Bratt’s code:
* https://gist.github.com/steveAbratt/43133bf9011febf6437a662eb5998ec8
*
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an ā€œAS ISā€ BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
metadata {
definition (name: ā€œSony TVā€, namespace: ā€œSanfe75ā€, author: ā€œSimoneā€) {
capability "Actuator"
capability "Switch"
capability ā€œTVā€

        command "volumeUp"
        command "volumeDown"
        command "channelUp"
        command "channelDown"
        command "cursorUp"
        command "cursorDown"
        command "cursorLeft"
        command "cursorRight"
        command "home"
        command "mute"
	}

	tiles(scale: 1) {
		standardTile("switch", "device.switch", width: 1, height: 1, canChangeIcon: true, decoration: "flat") {
        	state "off", label: '${name}', action: "switch.on", icon: "st.Entertainment.entertainment14", backgroundColor: "#ffffff"
            state "on", label: 'ON', action: "switch.off", icon: "st.Entertainment.entertainment14", backgroundColor: "#79b821"
		}
        
		standardTile("volumeUp", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"Volume", action:"volumeUp", icon:"st.thermostat.thermostat-up"
		} 

		standardTile("volumeDown", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"Volume", action:"volumeDown", icon:"st.thermostat.thermostat-down"
		} 

		standardTile("channelUp", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"Channel", action:"channelUp", icon:"st.thermostat.thermostat-up"
		}

		standardTile("channelDown", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"Channel", action:"channelDown", icon:"st.thermostat.thermostat-down"
		}
        
                
        standardTile("cursorUp", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"", action:"cursorUp", icon:"st.thermostat.thermostat-up"
		}
        
        standardTile("cursorDown", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"", action:"cursorDown", icon:"st.thermostat.thermostat-down"
		}
                
        standardTile("cursorLeft", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"", action:"cursorLeft", icon:"st.thermostat.thermostat-left"
		}
                
        standardTile("cursorRight", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"", action:"cursorRight", icon:"st.thermostat.thermostat-right"
		}
                        
        standardTile("cursorEnter", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"āŽ", action:"cursorEnter", icon:""
		}
        
        standardTile("home", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"", action:"home", icon:"st.Home.home2"
		}
        
        standardTile("mute", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"Mute", action:"mute", icon:"st.custom.sonos.muted"
    	}
        
        standardTile("exit", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
			state "default", label:"", action:"exit", icon:"st.samsung.da.washer_ic_cancel"
    	}
        
        main "switch"
        details(["volumeUp", "switch", "channelUp", "volumeDown", "cursorUp", "channelDown", "cursorLeft", "cursorEnter", "cursorRight", "exit", "cursorDown", "home"])
	}
}

def installed() {

	initialize()
}

 def updated() {

	//unsubscribe()
    unschedule()
	initialize()
}

def initialize() {

    runEvery1Minute("pushPowerUpdate")
}

// parse events into attributes
def parse(description) {

    def msg = parseLanMessage(description)
    log.debug "parse Message.json '${msg.json}'"
    if (msg.json?.id == 2) {
        def tv = (msg.json.result[0]?.status == "active") ? "on" : "off"
        if (tv != state.tv) {
        	log.debug "parse change state: ${tv}"
        	state.tv = tv
        	sendEvent(name: "switch", value: tv)
        }
    }
	/*
    def volumeEvent = createEvent(name: "volume", value: "on")
    def channelEvent = createEvent(name: "channel", value: "on")
	def powerEvent = createEvent(name: "power", value: "on")
	def pictureEvent = createEvent(name: "picture", value: "on")
	def soundEvent = createEvent(name: "sound", value: "on")
	def movieModeEvent = createEvent(name: "movieMode", value: "on")
	*/
}

def update(tvPort, tvPSK){

    def existingPort = getDataValue("port")
    def existingPSK = getDataValue("tvPSK")

    if (tvPort && tvPort != existingPort) {
    	updateDataValue("port", String.valueOf(tvPort))
	}
    if (tvPSK && tvPSK != existingPSK) {
    	updateDataValue("tvPSK", tvPSK)
	}
}

def sync(ip) {
	def existingIp = getDataValue("ip")
	if (ip && ip != existingIp) {
		updateDataValue("ip", ip)
	}
}

private pushPowerUpdate() {

    def powerJson = "{\"id\":2,\"method\":\"getPowerStatus\",\"version\":\"1.0\",\"params\":[]}"
    def result = sendJsonRpcCommand(powerJson)
}
/*
private subscribeAction(path, callbackPath="") {
    //def address = getCallBackAddress()
    //def ip = getHostAddress()
    def result = new physicalgraph.device.HubAction(
        method: "SUBSCRIBE",
        path: path,
        headers: [
            HOST: getDataValue("ip"),
            //CALLBACK: "<http://${address}/notify${callbackPath}>",
            CALLBACK: "<http://192.168.1.166/notify${callbackPath}>",
            NT: "upnp:event",
            TIMEOUT: "Second-28800"
        ]
    )
    log.trace "SUBSCRIBE $path"
    return result
}*/

// handle commands

private sendremotecommand(rawcmd){

	def sonycmd = new physicalgraph.device.HubSoapAction(
            path:    '/sony/IRCC',
            urn:     "urn:schemas-sony-com:service:IRCC:1",
            action:  "X_SendIRCC",
            body:    ["IRCCCode":rawcmd],
            headers: [Host:"${getDataValue("ip")}:${getDataValue("port")}", 'X-Auth-PSK':"${getDataValue("tvPSK")}"]
     )
     sendHubCommand(sonycmd)
}

private sendJsonRpcCommand(json) {

  def result = new physicalgraph.device.HubAction(
    method: 'POST',
    path: '/sony/system',
    body: json,
    headers: ['HOST':"${getDataValue("ip")}:${getDataValue("port")}", 'Content-Type': "application/json", 'X-Auth-PSK':"${getDataValue("tvPSK")}"]
  )
  sendHubCommand(result)
}

def on() {

	WOLC()
    def json = "{\"method\":\"setPowerStatus\",\"version\":\"1.0\",\"params\":[{\"status\":true}],\"id\":102}"
    def result = sendJsonRpcCommand(json)
    sendEvent(name: "switch", value: "on")
    state.tv = "on"
}

def off() {

	def json = "{\"method\":\"setPowerStatus\",\"version\":\"1.0\",\"params\":[{\"status\":false}],\"id\":102}"
    def result = sendJsonRpcCommand(json)
    sendEvent(name: "switch", value: "off")
    state.tv = "off"
}

def volumeUp() {

	log.debug "Executing 'volumeUp'"
    
	def rawcmd = "AAAAAQAAAAEAAAASAw=="
	sendremotecommand(rawcmd)
}

def volumeDown() {

	log.debug "Executing 'volumeDown'"

	def rawcmd = "AAAAAQAAAAEAAAATAw=="
	sendremotecommand(rawcmd)
}

def channelUp() {

	log.debug "Executing 'channelUp'"

	def rawcmd = "AAAAAQAAAAEAAAAQAw=="
	sendremotecommand(rawcmd)
}

def channelDown() {

	log.debug "Executing 'channelDown'"

	def rawcmd = "AAAAAQAAAAEAAAARAw=="
	sendremotecommand(rawcmd)
}

def cursorUp() {

	log.debug "Executing 'cursorUp'"

	def rawcmd = "AAAAAQAAAAEAAAB0Aw=="
	sendremotecommand(rawcmd)
}

def cursorDown() {

	log.debug "Executing 'cursorDown'"

	def rawcmd = "AAAAAQAAAAEAAAB1Aw=="
	sendremotecommand(rawcmd)
}

def cursorLeft() {

	log.debug "Executing 'cursorLeft'"

	def rawcmd = "AAAAAQAAAAEAAAA0Aw=="
	sendremotecommand(rawcmd)
}

def cursorRight() {

	log.debug "Executing 'cursorRight'"

	def rawcmd = "AAAAAQAAAAEAAAAzAw=="
	sendremotecommand(rawcmd)
}

def cursorEnter() {

	log.debug "Executing 'cursorEnter'"

	def rawcmd = "AAAAAgAAAJcAAAAjAw=="
	sendremotecommand(rawcmd)
}

def home() {

	log.debug "Executing 'home'"
    
    def rawcmd = "AAAAAQAAAAEAAABgAw=="
    sendremotecommand(rawcmd)
}

def exit() {

	log.debug "Executing 'exit'"
    
    def rawcmd = "AAAAAQAAAAEAAABjAw=="
    sendremotecommand(rawcmd)
}

def mute() {

	log.debug "Executing 'mute'"
    
    def rawcmd = "AAAAAQAAAAEAAAAUAw=="
    sendremotecommand(rawcmd)
}

def WOLC() {

    //log.debug "WOLC mac: ${getDataValue("mac")}"
    log.debug "WOLC mac: ${device.deviceNetworkId}"
    
	def result = new physicalgraph.device.HubAction (
        "wake on lan ${device.deviceNetworkId}",
   		physicalgraph.device.Protocol.LAN,
   		null,
    	[secureCode: "111122223333"]
	)
	return result
}

Cheers I’ll look at that.
Does it have to be set to every 1 minute ?
It seems quite often I’d be happy with every 5 mins or more.
Also what’s the call back for and does the IP need to be set to mine?

You can use runEvery5Minutes()
runEvery5Minutes()

HI @Steveuk23, please find below the very simple piston I use with webCoRE to refresh my Sony TV.

A second, very simple piston switches my music on or off depends on the state change of my TV.

That works perfect for me. You can change the time or refresh interval and instead of switching music on or off, you can add your prefered lights. Switch 1 is my TV.

Hopefully that helps!

1 Like

i tried that piston and didn’t work for me :frowning: even tried using a poll command too

Yeah thanks I’ll try that.
I’m wondering whether I’m making a full connection to my TV as the power never changes colour at all and always shows off but it does turn on by my routine.
Would it affect it if I got the hex key slightly wrong as I want sure how to convert it and Google gave me different ways.
Can anyone confirm what 192.168.0.31 would work out as hex?
Cheers

Great work on this DTH, I was trying to clean up for my tv as I dont use a large bit of the tiles. I am unable to get it to turn back on with either the turn on or the wake on lan (even though I have it set to allow on tv). Any thoughts…

Also using switch for power, but it is not updating state (on,off).

Model XBR X850D

DTH Image

My TV is a X830C, older than yours, and to turn it on I always send a WOL
first then the json command
"{ā€œmethodā€:ā€œsetPowerStatusā€,ā€œversionā€:ā€œ1.0ā€,ā€œparamsā€:[{ā€œstatusā€:true}],ā€œidā€:102}ā€œ
Does your TV support the setPowerStatus? you could send a command
"getSystemSupportedFunctionā€ and log the answer to see the possible
commands and parameters.
For the switch, I update it every 1 minute sending a ā€œgetPowerStatusā€ json
command, then when the TV answer I update the switch in the parse(), but
also in this case you need to check if your TV has the "getPowerStatus"
command.
I really like the layout of your remote, can I get a look at the code and
copy it? LOL

@pmjoen usually if it doesnt come back on its because the wake on lan isnt firing I have the same issue, i tend to use the wake on lan button.

however if you dont want to do that, you can turn of the power save function on the tv and then the normal power on will work.

the status doesnt need to be correct for the on to work which is why so many people use it fine in automatons.

my suggestion woudl be to turn off the powersave functions you can still use standby, but the TV wont go fully to sleep

also your device handler looks really tidy, do you have the code for it ill update mine to match. one of these days ill sit down and try to make it all work a bit more seamlessly,

Is it possible to get a state from this TV for play/pause?