HubAction JSON response

Pretty new the whole custom DTH.

Am trying to write a DTH for Yeelight bulbs and have a working solution, kind of.

All seems to work OK, albeit a liitle slowly, but I can’t get a response from the bulb to show up in def parse()

It’s supposed to be in JSON format but I only get:

debug Response 'index:56, mac:34CE008233AA, ip:C0A80183, port:D893, requestId:7feb7d02-17f9-428c-b05a-ce7a1783006f'

I’ve searched these forums and find some similar problems but can’t seem to get it to work.

Response that I should get on an “on” command (confimed with Packetsender):

{"id":1, "result":["ok"]}\r\n
{"id": 1, "method": "set_power", "params":["on", "smooth", 500]}\r\n

Thanks is advance

metadata {
	definition (name: "Yeelight", namespace: "smartthings", author: "Simon Tether", ocfDeviceType: "oic.d.light", mnmn: "SmartThings", vid: "generic-rgbw-color-bulb") {
		capability "Switch Level"
		capability "Color Control"
		capability "Color Temperature"
		capability "Switch"
		capability "Refresh"
		capability "Actuator"
		capability "Sensor"
        capability "Health Check"
        capability "polling"

		command "reset"
	}

	simulator {
	}

	standardTile("switch", "device.switch", width: 1, height: 1, canChangeIcon: true) {
		state "on", label:'${name}', action:"switch.off", icon:"st.lights.philips.hue-single", backgroundColor:"#00a0dc", nextState:"turningOff"
		state "off", label:'${name}', action:"switch.on", icon:"st.lights.philips.hue-single", backgroundColor:"#ffffff", nextState:"turningOn"
		state "turningOn", label:'${name}', action:"switch.off", icon:"st.lights.philips.hue-single", backgroundColor:"#00a0dc", nextState:"turningOff"
		state "turningOff", label:'${name}', action:"switch.on", icon:"st.lights.philips.hue-single", backgroundColor:"#ffffff", nextState:"turningOn"
	}
	standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
		state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
	}
	standardTile("reset", "device.reset", inactiveLabel: false, decoration: "flat") {
		state "default", label:"Reset Color", action:"reset", icon:"st.lights.philips.hue-single"
	}
	controlTile("levelSliderControl", "device.level", "slider", height: 1, width: 2, inactiveLabel: false, range:"(0..100)") {
		state "level", action:"switch level.setLevel"
	}
	controlTile("rgbSelector", "device.color", "color", height: 3, width: 3, inactiveLabel: false) {
		state "color", action:"setColor"
	}
	valueTile("level", "device.level", inactiveLabel: false, decoration: "flat") {
		state "level", label: 'Level ${currentValue}%'
	}
	controlTile("colorTempControl", "device.colorTemperature", "slider", height: 1, width: 1, inactiveLabel: false, range:"(1700..6500)") {
		state "colorTemperature", action:"setColorTemperature"
	}
	valueTile("hue", "device.hue", inactiveLabel: false, decoration: "flat") {
		state "hue", label: 'Hue ${currentValue}   '
	}

	main(["switch"])
	details(["switch", "levelSliderControl", "rgbSelector", "reset", "colorTempControl", "refresh"])
}

def installed()  {
}

def updated()  {
}

def parse(String description) {
	log.debug "Response '${description}'"
}

def transmit(yeelightCommand) {
    def String ipaddr = "192.168.1.131"
    def port = 55443
    def String hexIp = ipaddr.tokenize('.').collect {
        String.format('%02X', it.toInteger())
    }.join()
    def String hexPort = String.format('%04X', port)
    def String myNetworkID = "${hexIp}:${hexPort}"
    //log.debug "network ID: " + myNetworkID
    device.deviceNetworkId = myNetworkID
	def transmittedData = new physicalgraph.device.HubAction(yeelightCommand, physicalgraph.device.Protocol.LAN, myNetworkID)
    transmittedData.options = [type:'LAN_TYPE_CLIENT', protocol:'LAN_PROTOCOL_TCP']
	log.debug "Sent " + transmittedData
    sendHubCommand(transmittedData)
    return transmittedData
}

def getProp() {
	transmit("""{"id":1,"method":"get_prop","params":["power", "bright", "ct", "rgb", "name"]}\r\n""")
}

def on() {
    delayBetween([
        transmit("""{"id": 1, "method": "set_power", "params":["on", "smooth", 500]}\r\n"""),
        transmit("""{"id": 1, "method": "set_bright", "params":[75, "smooth", 100]}\r\n""")
    ], 100)
    //transmit("""{"id": 1, "method": "set_rgb", "params":[16777215, "smooth", 500]}\r\n""")
    sendEvent(name: "switch", value: "on")
    sendEvent(name: "level", value: 75)
    //sendEvent(name: "color", value: 16777215)
}

def off() {
	transmit("""{"id": 1, "method": "set_power", "params":["off", "smooth", 500]}\r\n""")
    sendEvent(name: "switch", value: "off")
}

def setLevel(level) {
	delayBetween([
		transmit("""{"id": 1, "method": "set_power", "params":["on", "sudden", 50]}\r\n"""),
    	transmit("""{"id": 1, "method": "set_bright", "params":[$level, "smooth", 100]}\r\n""")
    ], 100)
    sendEvent(name: "switch", value: "on")
    sendEvent(name: "level", value: level)
}

def setColor(value) {
	def result = []
    transmit("""{"id": 1, "method": "set_power", "params":["on", "smooth", 500]}\r\n""")
    def red = value.red
    def green = value.green
    def blue = value.blue
	log.debug "setColor: ${value}"
    //log.debug red, green, blue
    def rgb = (red*65536)+(green*256)+blue
    transmit("""{"id": 1, "method": "set_rgb", "params":[${rgb}, "smooth", 500]}\r\n""")
	if(value.hue) sendEvent(name: "hue", value: value.hue)
	if(value.hex) sendEvent(name: "color", value: value.hex)
	if(value.switch) sendEvent(name: "switch", value: value.switch)
	if(value.saturation) sendEvent(name: "saturation", value: value.saturation)
}

def setColorTemperature(kelvin) {
	if(kelvin > 6500) percent = 6500
    log.debug "setColorTemperature: ${kelvin}K"
	transmit("""{"id": 1, "method": "set_ct_abx", "params":[${kelvin}, "smooth", 500]}\r\n""")
    sendEvent(name: "colorTemperature", value: kelvin)
}

def reset() {
	log.debug "reset"
    delayBetween([
        transmit("""{"id": 1, "method": "set_power", "params":["on", "smooth", 100]}\r\n"""),
        transmit("""{"id": 1, "method": "set_bright", "params":[75, "smooth", 100]}\r\n"""),
        transmit("""{"id": 1, "method": "set_rgb", "params":[16777215, "smooth", 100]}\r\n"""),
        transmit("""{"id": 1, "method": "set_ct_abx", "params":6000, "smooth", 100]}\r\n""")
    ], 100)
    sendEvent(name: "switch", value: "on")
    sendEvent(name: "level", value: 75)
    sendEvent(name: "color", value: 16777215)
    sendEvent(name: "colorTemperature", value: 6000)
}
2 Likes

There is a bug in the platform where it’s stripping out of the body of the response to a hubAction before it reaches the DTH/App. Sounds like you may be running into it from the looks of it.

@rappleg has a potential patch for it, waiting for it to go into production

Ok thanks for letting me know.

@rappleg any idea when this may be released ?

having same issue, subscribed to the post