jason0x43 / hacs-hubitat

A Hubitat integration for Home Assistant
MIT License
196 stars 48 forks source link

Color modes - 'RGB unsupported' error #119

Closed Desterly closed 3 years ago

Desterly commented 3 years ago

Looking through some of the closed events I think I might be running into the same issues that others have had in which all of my color bulbs reflect the color stuck in the center of the wheel with no reflection of the actual color.

Starting information:

Integration: 0.6.6 HomeAssistant: 2021.4.6 Hubitat FW: 2.2.6.140 Bulb: Inovelli LZW42 multi-color bulb Inovelli driver - the built in driver for this bulb doesn't send events for color change to makerapi so using Inovelli's driver.

Debugging this a little, I ran across this in the logs ( just a sample):

2021-05-03 21:16:55 DEBUG (MainThread) [hubitatmaker.hub] Updating saturation of 649 to 100
2021-05-03 21:16:55 DEBUG (MainThread) [homeassistant.components.light] light.test_light: set to unsupported color_mode: RGB, supported_color_modes: {'color_temp', 'hs'}

All color events are erroring due to the RGB mode issue.

The capabilities of the device:

"capabilities": [
        "Configuration",
        "Actuator",
        "Refresh",
        "ColorTemperature",
        {
            "attributes": [
                {
                    "name": "colorTemperature",
                    "dataType": null
                },
                {
                    "name": "colorName",
                    "dataType": null
                }
            ]
        },
        "ColorMode",
        {
            "attributes": [
                {
                    "name": "colorMode",
                    "dataType": null
                }
            ]
        },
        "ColorControl",
        {
            "attributes": [
                {
                    "name": "hue",
                    "dataType": null
                },
                {
                    "name": "saturation",
                    "dataType": null
                },
                {
                    "name": "color",
                    "dataType": null
                },
                {
                    "name": "colorName",
                    "dataType": null
                },
                {
                    "name": "RGB",
                    "dataType": null
                }
            ]
        },
        "ChangeLevel",
        "SwitchLevel",
        {
            "attributes": [
                {
                    "name": "level",
                    "dataType": null
                }
            ]
        },
        "Switch",
        {
            "attributes": [
                {
                    "name": "switch",
                    "dataType": null
                }
            ]
        },
        "Sensor"

Looking at the "Light Entity" docs, it looks like the integration is currently using "Support Features" which are slotted to be removed later this year.

I'm currently digging more into it but it looks like 1) need to figure out why RGB isn't set as allowed and/or: 2) convert to HSV.

I know there's code in there for this so I'm guessing this is somehow related to the capabilities on this driver.

Also thinking the "supported_color_modes" will need to be added over the "features" for when it's dropped this year.

While I'm digging into it myself if there's any other info or can add some debug logging for you let me know.

Desterly commented 3 years ago

Update:

Looks like HA is making changes to how lights work which is overall causing come issues specifically that supported Features is being removed in favor of the "supported_color_modes" option.

So.... below is what I did to get mine to work.
1) the constant for RGB in HA is actually "rgb". - as HU is passing it uppercase the method needs to be modified:

    @property
    def color_mode(self) -> Optional[str]:
        """Return this light's color mode."""
        c = self.get_str_attr("colorMode")
        if (c):
            c = c.lower()
        return c

^ This is probably true for other modes as well. It might be a driver setting in HU but regardless, it needs to be lowered for HA to accept it.

2) added a supported_color_modes property:

    @property
    def supported_color_modes(self) -> list:
        cm = [COLOR_MODE_COLOR_TEMP, COLOR_MODE_RGB]
        return cm

^ Obviously this would need to be expanded to determine what mode/modes the bulb actually supports but this was just proof of concept.

3) added a rgb_color property:

    @property
    def rgb_color(self):
        if COLOR_MODE_RGB in self.supported_color_modes:
            value = self.get_str_attr("color")
            value = value.lstrip('#')
            length = len(value)
            return tuple(int(value[i:i + length // 3], 16) for i in range(0, length, length // 3))
        return None

^ In my case, "color" comes over as a hex string. converting that to the rgb tuple returns either the value or None.. Again, somewhere it will need to be modified accordingly to check passed value.

With the above three, HA now correctly sees my color. The other method I can see is to use something similar to what's in the core "light" module for converting the value to the correct attribute:

https://github.com/home-assistant/core/blob/5ee373869a59266142001b62a5067d56c22bfddf/homeassistant/components/light/init.py#L291

jason0x43 commented 3 years ago

Thanks for the excellent writeup! I had seen that the color handling system for lights had changed, but I hadn't really looked into it yet. I really should get an RGB light that's not Hue, just to have something to experiment with through Hubitat. 😀

jason0x43 commented 3 years ago

I bought a Sengled RGB bulb. Now I too can experience the brokenness of my light support. 😁

Desterly commented 3 years ago

I haven't spent a lot more time with it once I got the above working as almost all my bulbs are the same... I'll see if I can look more this week. The one thing that I'm not sure of (and will be a huge pain if not) is the RGB value passed... The one I have passes RGB as a hex but I'm pretty sure another bulb I had passed it as either an int or tuple...

I'm wondering if it makes more sense to add the conversion/unification on the hubitatmaker side instead of the hacs side.

The "supported_color_modes" won't be too bad though. It's more or less a direct replacement for the features set. Biggest difference is that some modes are exclusive (such as UNKNOWN) but that can be fairly easily checked for.

The only other annoying thing with this bulb I have now is it technically can handle HSV, CT, RGB... the "modes" it uses are either CT or RGB but it passes both HSV and RGB when color changes so you end up with duplicate color events.

jason0x43 commented 3 years ago

Interesting. I wasn't going to bother with an explicit RGB color property on the HA side, because (based on the Maker API docs and how this Sengled bulb works) it seemed like Hubitat mostly handled the RGB mode through hue and sat, or at least hue and sat always worked for bulbs in RGB mode. I was going to have light just explicitly support HS, and let HA handle converting things between that and RGB (LightEntity already takes care of that).

As far as multiple events go, to some extent that's normal. Like, when I update the color if this Sengled bulb, I might get individual events for hue, saturation, and colorName. I don't get a separate event for an RGB color, though. Are you seeing functionally duplicative events, like you're getting events for both HS and RGB when you make a change?

jason0x43 commented 3 years ago

I updated the light support in a08ebd9, which is in v0.6.7. Give that a try.

Desterly commented 3 years ago

Apparently email decided to label github as spam....

For this bulb/driver yes, it passes both HS and RGB events. I'm not overly worried about that as it's a function of this driver and for the most part, HA ignores them as when the HS is sent and it sees it's in RGB mode, it ignores the values.

I'll take a look at the update and sees if there's any issues and/or if any other things pop up.

This is one thing that annoys me with different drivers/devices is that there seems to be no consistency in how the drivers store/send events... sometimes A, sometimes B, sometimes C... makes writing integrations painful :)

Thanks again.

jason0x43 commented 3 years ago

I'm going to close this since it seems to be working OK, but we can reopen it if there are still issues.