A year ago I programmed this lambda, it works reliably:
val org.eclipse.xtext.xbase.lib.Functions$Function4<ColorItem, SwitchItem, StringItem, Object ,Boolean> yeelightCP = [
org.eclipse.smarthome.core.library.items.ColorItem colorYee,
org.eclipse.smarthome.core.library.items.SwitchItem switchYee,
org.eclipse.smarthome.core.library.items.StringItem Yee,
java.lang.Object commandYee |
var String data = ""
if(switchYee.state.toString == "ON") {
if(commandYee instanceof HSBType) {
var HSBType hsbValue = colorYee.state as HSBType
var hueValue = hsbValue.hue.intValue
var satValue = hsbValue.saturation.intValue
var briValue = hsbValue.brightness.intValue
data = String::format('{"id":1,"method":"set_hsv","params":[%1s, %2s,"smooth",150]}\r\n', hueValue, satValue)
data += String::format('{"id":2,"method":"set_bright","params":[%1s,"smooth",150]}\r\n', briValue)
}
if(commandYee instanceof PercentType) {
var HSBType hsbValue = colorYee.state as HSBType
var briValue = hsbValue.brightness.intValue
data = String::format('{"id":2,"method":"set_bright","params":[%1s,"smooth",150]}\r\n', briValue)
}
if(commandYee instanceof OnOffType) {
if(commandYee == ON ) {
data = '{"id":1,"method":"set_power","params":["on","smooth",150]}\r\n'
}else if (commandYee == OFF ) {
data = '{"id":1,"method":"set_power","params":["off","smooth",150]}\r\n'
}
}
var java.net.Socket clientSocket = new java.net.Socket(Yee.label, 55443)
var java.io.DataOutputStream outToServer = new java.io.DataOutputStream(clientSocket.getOutputStream())
outToServer.writeBytes(data)
clientSocket.close()
} else {
postUpdate(colorYee, "OFF" )
}
true
]
Example item:
Color FlurYeePicker "Flur Licht" <colorwheel>
String FlurYee "192.168.0.30"
Switch flurYeeWLAN "Yeelight Stromnetz [MAP(yeelight.map):%s]" <bulb>
Example rule:
rule "FlurYeeColorPicker"
when
Item FlurYeePicker received command
then
yeelightCP.apply(FlurYeePicker, flurYeeWLAN, FlurYee, receivedCommand)
end
Example sitemap:
Frame label="Licht" {
Switch item=FlurYeePicker label="Schalter Lampe" icon="switch" visibility=[flurYeeWLAN==ON]
Colorpicker item=FlurYeePicker label="Farbe Lampe" visibility=[flurYeeWLAN==ON]
Text item=flurYeeWLAN visibility=[flurYeeWLAN==OFF]
}
Everything works without a specific binding