BobRak / OpenHAB-Smartthings

53 stars 54 forks source link

Color Channel support for On / Off #102

Open SJRepos opened 3 years ago

SJRepos commented 3 years ago

Within OpenHab the color channel supports On/Off which is not supported by the 'actionColorControl' capability and hence we get the following error in the log:

Error message from Smartthings: Error occured while calling action: {actionColorControl} for Capability: Color Control with device name: ItemName changed to: off. Exception java.lang.NumberFormatException: For input string: "o"

BobRak commented 3 years ago

OpenHAB supports on/off for the color control but smartthings defines Color, Hue and Saturation. See the definition here. I've tried to follow the Smartthings Capabilities reference in my implementation. That said, it would be possible for me to extend the Color Control but what would I name the on/off attribute and how would I then document that?

I have a Sengled bulb and it supports the switch and switchLevel capabilities and I use those to control on/off and brightness. Can you use that? Here are my things and items definitions:

// Sengled Bulb colorControl SengledColorControl [ smartthingsName="Sengled Bulb"] colorTemperature SengledColorTemperature [ smartthingsName="Sengled Bulb"] switch SengledSwitch [ smartthingsName="Sengled Bulb"] switchLevel SengledSwitchLevel [ smartthingsName="Sengled Bulb"]

// Sengled bulb Color SengledColorControl "Sengled bulb color" <colorpicker> {channel="smartthings:colorControl:Home:SengledColorControl:color"} Number SengledTemperature "Sengled bulb color temperature" {channel="smartthings:colorTemperature:Home:SengledColorTemperature:colorTemperature"} Switch SengledSwitch "Sengled bulb switch" <switch> {channel="smartthings:switch:Home:SengledSwitch:switch"} Dimmer SengledDimmer "Sengled bulb dimmer" <slider> {channel="smartthings:switchLevel:Home:SengledSwitchLevel:level"}

If you want me to look into this further, please send me the steps I could use to reproduce this error.

Thanks,

Bob

SJRepos commented 3 years ago

I understand that you have followed the Smartthings definitions, and I have actually worked around this myself by modifying the code for the 'OpenHabAppV2' smart app. In particular I changed the 'actionColorControl' routine so that it would check for color values of 'off' and 'on':

// This is the original color control
def actionColorControl(device, attribute, value) {
    log.debug "actionColorControl: attribute \"${attribute}\", value \"${value}\""
    switch (attribute) {
        case "hue":
            device.setHue(value as int)
        break
        case "saturation":
            device.setSaturation(value as int)
        break
        case "color":
            if (value == "off") {
                device.off()
            } else if (value == "on") {
                device.on()
            } else {
                def colormap = ["hue": value[0] as int, "saturation": value[1] as int]
                // log.debug "actionColor: Setting device \"${device}\" with attribute \"${attribute}\" to colormap \"${colormap}\""
                device.setColor(colormap)
                device.setLevel(value[2] as int)
            }
        break
    }
}

I also added 'Off' an 'On' to the 'actionColor' routine:

// This is the new "proposed" color. Here hue is 0-360
def actionColor(device, attribute, value) {
    log.debug "[SJ2] actionColor: attribute \"${attribute}\", value \"${value}\""
    switch (attribute) {
        case "hue":
            device.setHue(value as int)
        break
        case "saturation":
            device.setSaturation(value as int)
        break
        case "colorValue":
            def colormap = ["hue": value[0] as int, "saturation": value[1] as int]
            // log.debug "actionColor: Setting device \"${device}\" with attribute \"${attribute}\" to colormap \"${colormap}\""
            device.setColor(colormap)
            device.setLevel(value[2] as int)
        break
        case "off":
            device.off()
        break
        case "on":
            device.on()        
        break
    }
}

FYI, I do have Switch channels defined for the bulbs I am using, its just that the OH3 color picker sends the 'off' value to the color channel if the brightness is reduced to 0%. I believe my modification is sane since it maps the OH color channel to the Smartthings device.

BobRak commented 3 years ago

I am just starting to experiment with OH3 so hadn't discovered that yet. Actually doing OH3 setup is where I discovered that the discovery service is broken. And, I've gotten slowed down because the Eclipse IDE has problems AGAIN. I was just about to bag doing anything and I tried setting up the VSCode IDE and that works well.

Couple of questions:

  1. Assuming the color is currently 0 and off has been sent to the device. Then if you change the color to 10 does OH send 10 or on and then 10? Have you experimented with that and know that what you have done is what OH expects.
  2. Have you been able to test the actionColor changes you made?
  3. Is this backwards compatible with OH2?

I agree that you changes make complete since since that is the use case that OH is providing.

Thanks for your help.

Bob

SJRepos commented 3 years ago

To answer your questions:

  1. I haven't experimented - but my changes shouldn't affect that since the 'value' is checked for 'off' or 'on' which was being sent by the default oh3 color picker (it has a toggle next to the box which opens a color picker when viewing the bulb as a group).
  2. The error message about not being able to convert 'off' or 'on' no longer appears in my logs - ie. the changes have cleaned things up. I haven't seen any adverse effects.
  3. Yes, as the changes add checks for additional values whilst not altering anything - the original functionality is intact.
BobRak commented 3 years ago

@sjhangiani What version are you using? I've been able to partially reproduce the issue you are describing using version 3.0. But in version 3.1 the color picker seems to work differently. Your enhancement doesn't seem necessary under 3.1. I'm wondering if you have found this true or not?