xZetsubou / hass-localtuya

๐Ÿ”น A Home Assistant integration to handle Tuya devices locally "fork from localtuya"
https://xzetsubou.github.io/hass-localtuya/
GNU General Public License v3.0
428 stars 55 forks source link

[Bug]: Light scene list not assigned properly #392

Open curious-ninja opened 5 days ago

curious-ninja commented 5 days ago

LocalTuya Version

2024.9.0

Home Assistant Version

2024.11.2

Environment

What happened?

When I add my lights using the automatic add feature, they are assigned a set of scenes (DP 25). The scenes are incorrect though. My scene names in the Tuya app are: Night, Read, Working, Leisure, Soft, Colorful, Dazzling, and Gorgeous.

But this LocalTuya integration assigns these instead: Night, Read, Meeting, Leisure, Soft, Rainbow, Shine, and Beautiful.

I took a quick look at light.py and noticed that these are 2 separate lists named "SCENE_LIST_RGBW_1000" and "SCENE_LIST_RGB_1000", respectively. The deciding factor to assign "SCENE_LIST_RGB_1000" is whether or not "CONF_BRIGHTNESS" is None. For my lights, CONF_BRIGHTNESS is in fact assigned, DP 22 bright_value_v2. So maybe my devices aren't classified properly? My lights are being assigned to SCENE_LIST_RGBW_1000, but they should be SCENE_LIST_RGB_1000, even though the CONF_BRIGHTNESS is not None.

 elif self._config.get(CONF_BRIGHTNESS) is None:
                self._scenes = SCENE_LIST_RGB_1000

https://github.com/xZetsubou/hass-localtuya/blob/54593b5b0121b3bab72495b0ec19a2daf34b98c6/custom_components/localtuya/light.py#L180

My devices are "InfiBrite Slim LED Downlight", marketed as "RGBCW", Y-CB2L-RGBCW. https://www.infibrite.com/product/infibrite-6-inch-wifi-smart-ultra-thin-led-recessed-ceiling-light-12w-1100lm-dimmable-with-junction-box-easy-install-app-voice-control-alexa-google-compatible-etl-energy-star-wet-rated/

Also, as a side note, if I try to reconfigure an existing device manually, and modify the DP 20 entity, select DP 25 for the scene dropdown, and don't fill in anything for "(Optional) Scene values".. the typical automatically assigned default scenes are no longer there. They do no get assigned. I have to delete the device and re-add it to get them back. Is there a way around this?

Steps to reproduce.

  1. Used add new device option.
  2. Device was found. Used automatic config option. Device got added.
  3. Went to control the device, found the "effect" dropdown scene names did not match the ones in my Tuya app.

Relevant log output

No response

Diagnostics information.

localtuya-01JCPN3WY6GNV5EFSSZQ56ZBHM-Living Room 1-2a7b2df33bf3e20a29369523401aa4fc.json

xZetsubou commented 3 days ago

Is the names only are incorrect or the scenes not working as well. you can add RGBW as custom scenes to test as well.

    "Night": "000e0d00002e03e802cc00000000",
    "Read": "010e0d000084000003e800000000",
    "Working": "020e0d00001403e803e800000000",
    "Leisure": "030e0d0000e80383031c00000000",
    "Soft": "04464602007803e803e800000000464602007803e8000a00000000",
    "Colorful": "05464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000464601003d03e803e80000000046460100ae03e803e800000000464601011303e803e800000000",
    "Dazzling": "06464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000",
    "Gorgeous": "07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000",
}
curious-ninja commented 3 days ago

The scenes do work, but the colors are of course wrong. So for example, when I click "rainbow" it does work, with the 3 colors in that scene, but the Tuya app shows "colorful" as active, but only with 3 colors, which is wrong. If I exit that scene and then select "colorful" with the Tuya app, it loads the correct scene that has 6 colors, not 3.

I of course could just configure custom scenes for each device, and that does work. But wouldn't it make more sense to fix why my lights get the wrong scene list assigned?

Lurker00 commented 3 days ago

@curious-ninja Pardon for jumping in, but I have several Tuya bulbs, Zigbee, BLE, WiFi, and I also investigated how Tuya and LocalTuya provides an access to pre-defined scenes. It's me who added "Default", "Mode color", "Mode scene" into the list of effects in HA light UI for LocalTuya, to activate white, color and scene modes with parameters defined in the Tuya app. You can also use "white", "color", "scene" effects in automations for them.

The result of my research is:

The list of scenes in the Tuya applications (SmartLife in my case), both names of the scenes and their actual values, depend on the manufacturer of a particular bulb, and can't be detected from outside.

What Tuya app shows is an UI application that manufacturer has chosen, either from a set of made by Tuya, or modified, or made by himsef using Tuya developers tools. Tuya app is able to download the UI from the cloud and then run. LocalTuya can't.

LocalTuya has implemented some sets of scenes with names and values taken from some devices, and uses a logic to assign a set to a particular device. The logic is not perfect, but I see no other way. Considering that it corresponds to some existing bulbs, I failed to implement support for my bulbs without breaking support for those original samples.

By the way, I tried to add my Zigbee bulbs to both ZHA and Zigbee2mqtt intergations. ZHA offers no a scene list at all. Zigbee2mqtt offers a list of "effects" which is the same for different kinds of light entities, and is not relevant to RGBCW bulbs at all: blink, breathe, okay, channel_change, finish_effect, stop_effect, colorloop, stop_colorloop. You can read about them here.

So, LocalTuya offers the best of what is possible outside Tuya app! ๐Ÿ˜„

curious-ninja commented 2 days ago

@Lurker00 Ah ok that all makes sense. I'll just have to configure each device with the custom list of scenes then.

I don't know enough about tuya devices, but was assuming maybe the "product_key" values were unique to specific models and that could be used. But I guess that's not the case? My product_id is keytg5kq8gvkv9dh.

Appreciate the responses guys, thanks.

Lurker00 commented 2 days ago

My product_id is keytg5kq8gvkv9dh

Probably, it can be used. But, you see, I just checked and found out that:

It means, meaning, somebody should support a registry of keys and scene sets from hundreds of Chinese "white box" manufacturers! Would you start such a project? ๐Ÿ˜‰

xZetsubou commented 1 day ago

It's possible to add a select option where user able to choose pre-set however I don't know how many scenes sets out there and what the most used and if these two set are is actually most used one.

It's also possible to make "multi select option" where it's possible to choose the set one by one however due to conflict of sets name where "night" is actually used twice seems above yet the have different values.

edit: since I don't used scenes often just checked the both lights in my bedroom -- both has incorrect sets where one has similar issue to OP and other has completely different sets.

Lurker00 commented 1 day ago

I have quite a different issue: all my Zigbee bulbs have assigned the list of scenes by this condition: https://github.com/xZetsubou/hass-localtuya/blob/816bd41bc5cc0c84cc50d3beeca2c53bcc795e7f/custom_components/localtuya/light.py#L178-L179 and I don't understand what does it mean! If it is the DP number of scene_data_v2, yes, it is 6 for them.

The correct list is SCENE_LIST_RGB_1000, but it is RGBCW and, as such, https://github.com/xZetsubou/hass-localtuya/blob/816bd41bc5cc0c84cc50d3beeca2c53bcc795e7f/custom_components/localtuya/light.py#L180-L181 is False. So the logic is broken for these bulbs, but I can't offer something better, without creating database of different bulbs.

localtuya-944c1616039a1d1ec6b9745179bff74e-ะ’ะก_ Zigbee Bulb-6e477bc6604f0282c806c9399a0b3c50 (1).json

xZetsubou commented 16 hours ago

So I made some tests it seems the scenes are not static for specific device or something, It more like a bunch of colors effects that create a scene, I tried to create custom scene on one of my light and use it on another one and it worked as well. so at the end instead of making multi scenes sets and similar to the "Tuya APP" we could just make a one set for all the lights.

You can test it out: using SET DP Service quicker.

Kamzai Smart Bulb

``` Breathe 3146460200000000000003e80000464602000000000000000a0000 Step Change 32646401000000000000000a000064640100000000000001f4000064640100000000000003e8000064640100000000000001f40000 Rythm Of Breathing 33545402000000000000000a0000545402000000000000011c000054540200000000000003e80000545402000000000000000a0000 Flash 2c646401000000000000000a000064640100000000000003e80000 Forest 19464601007803e803e800000000464602006e0320025800000000464602005a038403e800000000 Dream 1C4646020104032003e800000000464602011802bc03e800000000464602011303e803e800000000 F Style 1E323201015e01f403e800000000323202003201f403e80000000032320200a001f403e800000000 A Style 1F46460100dc02bc03e800000000464602006e03200258000000004646020014038403e800000000464601012703e802ee0000000046460100000384028a00000000 Halloween 28464601011303e803e800000000464601001e03e803e800000000 Christmas 225a5a0100f003e803e8000000005a5a01003d03e803e800000000464601000003e803e8000000005a5a0100ae03e803e8000000005a5a01011303e803e800000000464601007803e803e800000000 Birthday 20646401003d03e803e800000000646401007803e803e8000000005a5a01011303e803e8000000005a5a0100ae03e803e800000000646401003201f403e800000000646401000003e803e800000000 Wedding Anniversary 21323202015e01f403e800000000323202011303e803e800000000 ```

Default

``` WHITE SCENE 07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000 NIGHT 000e0d00002e03e802cc00000000 READ 010e0d000084000003e800000000 WORKING 020e0d00001403e803e800000000 LEISURE 030e0d0000e80383031c00000000 SOFT 04464602007803e803e800000000464602007803e8000a00000000 COLORFUL 05464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000464601003d03e803e80000000046460100ae03e803e800000000464601011303e803e800000000 DAZZLING 06464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000 GORGEOUS 07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000 FLASH 05464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000464601003d03e803e80000000046460100ae03e803e800000000464601011303e803e800000000 STROBE 06464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000 FADE 04464602007803e803e800000000464602007803e8000a00000000 SMOOTH 07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000 WHITE 05464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000464601003d03e803e80000000046460100ae03e803e800000000464601011303e803e800000000 ```

Tuya sources: default scenes 
https://github.com/tuya/tuya-panel-demo/blob/main/examples/lampGeneric/src/config/scenes.ts

So what's the goal, do we want to match the scenes with the same one as tuya app probably impossible? OR provide more scenes includes one in the app

goommie commented 15 hours ago

Hmm interesting.

I suppose if there are so many scene varieties out there, there really is no method to choose the proper default set, and no method to pull from tuya app.

So seems like creating a custom list manually is the solution. Would be amazing if there could be a UI that is similar to the tuya app scene designer page, and it would spit out the scene code that you can then use in the localtuya device config. This way scenes can easily be made in home assistant and the tuya app can be forgotten.

Lurker00 commented 12 hours ago

OR provide more scenes includes one in the app

This looks like the solution, providing we can't guess what is inside MiniApp of a particular device. We are speaking about "dj" devices, right?

According to Tuya description and my experience, for a scene capable bulb, WiFi and Zigbee bulbs use the same data format, but BLE bulbs are different. But BLE (which are write-only) bulbs is another story anyway...

In the current code, the set SCENE_LIST_RGBW_255 is for BLE blulbs, while SCENE_LIST_RGBW_1000 and SCENE_LIST_RGB_1000 are for Zigbee and WiFi bulbs, which can be replaced with one big set.

According to this document, DP for the scene value can be either 25 or 6, so that the current LocalTuya logic looks wrong.

I failed to find a link to the formal description of the byte sequence that encodes a scene. But is is here as JSON. Note, that the first byte is a "scene number" 0-255 (in binary format), whatever it means. As I understand, bulbs just ignore it.

Lurker00 commented 9 hours ago

@xZetsubou I realized that this condition https://github.com/xZetsubou/hass-localtuya/blob/816bd41bc5cc0c84cc50d3beeca2c53bcc795e7f/custom_components/localtuya/light.py#L178-L179 is an attempt to tell which DP name is in use: DPCode.SCENE_DATA (the condition is true) or DPCode.SCENE_DATA_V2 (then use what we are talking about). So, you have to somehow provide LocalTuyaLight object information about the name of the CONF_SCENE DP.

Then, this condition https://github.com/xZetsubou/hass-localtuya/blob/816bd41bc5cc0c84cc50d3beeca2c53bcc795e7f/custom_components/localtuya/light.py#L180-L181 has no sense, because scenes utilize only RGB LEDs, and they all compatible regardless of the while LEDs in a bulb. This condition shall be removed, and any DPCode.SCENE_DATA_V2-capable light should be assigned the common list of scenes.

This way it should still work for all the currently supported lights, but with even better support!

As I said before, the first byte (first two characters in the hex form of a scene) is a "scene number" and shall be ignored when comparing different scenes to create a set of unique scenes.

xZetsubou commented 6 hours ago

If the DP is always lower then 20 than it uses _255 but seems @Lurker00 bulbs has DP 6 with _1000 set.

I assume changing self._scenes = SCENE_LIST_RGB_1000 to merge any known scenes into that list hm?

And maybe add change default set from entity light config, in case _255 assigned as _1000 if it possible to happen not sure.