Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scene support for lights. #146

Merged
merged 9 commits into from
Nov 18, 2020
Merged

Conversation

ultratoto14
Copy link
Collaborator

@ultratoto14 ultratoto14 commented Nov 9, 2020

This PR is adding basic supports of scenes for tuya lights.

     - platform: light
        friendly_name: Device Light
        id: 4 # Usually 1 or 20
...
        scene: 25 # Optional, usually 6 (RGB_HSV) or 25 (HSV), default: "none"
        music_mode: False # Optional, some use internal mic, others, phone mic. Only internal mic is supported, default: "False"

We cannot get the scenes from the lights.
So this PR uses default scenes stored in the app for both RGB and HSV light bulbs.
If a scene is modified in the app, it appears as custom and but could not be use in a home assistant scene.

The music mode is also added but should only works with specific hardware that have a mic.

@ultratoto14 ultratoto14 mentioned this pull request Nov 9, 2020
@SmartM-ui
Copy link

follow

@ultratoto14
Copy link
Collaborator Author

@SmartM-ui to be notified on this PR you can just use the subscribe option on the right side of the PRs.

@ultratoto14
Copy link
Collaborator Author

@SmartM-ui feel free to test this one if you have time 😄

@SerAlbi
Copy link

SerAlbi commented Nov 15, 2020

With my standard rgb bulbs it works fine. The scene is correctly set from the drop down menu and color changes without problems. However with my LED strips, scenes does not work, or at least, 1 or 2 scenes seems to work but the others don't and if i try to change the color i get an error and nothing happens.

I'm looking at the code and i see that for my particular LED strips, the scenes codes are wrong, for example: the 'Music' scene is actually the same code of the 'Beautiful' scene.

The strange thing is that in my Tuya app, if i set an automation to set my LEDs to 'Music' it doesn't work. I have to manually change the scene to 'Music' and then set an automation to set my LEDs to 'Scene' mode.

So, for what i discovered, It seems that for my LEDs i only have 2 options for the 'colour_mode' parameter, which are: colour and scene.

  • When set to colour, the leds can freely change color from the UI or tuya app.
  • When set to scene, the leds will instead 'listen' to the 'scene' parameter on 25.

So the reason it doesn't work for my strips it could be because the manifacturer use a different approach in changing the scene mode.
Could it be just my unfortunate led strips?

For more info:
This is what i see in the configuration:
image

This is my scene list that i have corrected to match what i see in the Tuya app:

SCENE_LIST_1 = {
    "Night": "000e0d0000000000000000c80000",
    "Read": "010e0d0000000000000003e801f4",
    "Working": "020e0d0000000000000003e803e8",
    "Leisure": "030e0d0000000000000001f401f4",
    "Soft": "04464602007803e803e800000000464602007803e8000a00000000",
    "Colorful": "05464601000003e803e800000000464601007803e803e80000000046460100f003e803"
    + "e800000000",
    "Dazzling": "06464601000003e803e800000000464601007803e803e80000000046460100f003e803e8"
    + "00000000",
    "Music": "07464602000003e803e800000000464602007803e803e80000000046460200f003e8"
    + "03e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e80"
    + "3e800000000",
}

@ultratoto14
Copy link
Collaborator Author

ultratoto14 commented Nov 15, 2020

Hi @SerAlbi, some steps you can do:

  • Remove your led strip from HA, then re-add it, do not set the scene id to 25, let it empty. On the next config_flow step, choose to add a sensor and choose the id 25.
  • Now you have your bulb, not working with scene and the scene value is reflected in the sensor.
  • Using the tuya app, change the scene, and each time you select a scene, write down the value associated. Sometimes, the bulbs/strips do not let your device have two concurrent connections. you may need to close the tuya app each time you change the scene and reload the localtuya integration.

Once you saved the mapping, post the result here, please.
Thanks for testing.

P.S. can you also check your logs and put here the error you receive when you change the color ?

@SerAlbi
Copy link

SerAlbi commented Nov 15, 2020

Hi @ultratoto14 , done all the steps. I simply added a sensor to the yaml config since that's what i use an this is what i got (already replaced in the dictionary):

SCENE_LIST_1 = {
    "Night": "000e0d00002e03e802cc00000000",
    "Read": "010e0d000084000003e800000000",
    "Working": "020e0d00001403e803e800000000",
    "Leisure": "030e0d0000e80383031c00000000",
    "Soft": "04464602007803e803e800000000464602007803e8000a00000000",
    "Colorful": "05464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000464601003d03e803e80000000046460100ae03e803e800000000464601011303e803e800000000",
    "Dazzling": "06464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000",
    "Music": "07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000",
}

Applying this to the code seems to make the scenes work correctly in my case :)
Unfortunately the other error is still present, also @ultratoto14 i'm wondering what could be done to select the correct scene list for each light? What if each light has different scene codes like in this case?

This is the log when i try to change the color while in scene mode:

Log Details (ERROR)
Logger: homeassistant.components.websocket_api.http.connection.2884159928
Source: custom_components/localtuya/light.py:314
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 8:36:47 PM (9 occurrences)
Last logged: 8:36:58 PM

unsupported format string passed to NoneType.format
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 135, in handle_call_service
await hass.services.async_call(
File "/usr/src/homeassistant/homeassistant/core.py", line 1448, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1483, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
await self.hass.helpers.service.entity_service_call(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 482, in entity_service_call
future.result() # pop exception if have
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
await coro
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 519, in _handle_entity_call
await result
File "/usr/src/homeassistant/homeassistant/components/light/init.py", line 249, in async_handle_light_on_service
await light.async_turn_on(**params)
File "/config/custom_components/localtuya/light.py", line 314, in async_turn_on
color = "{:04x}{:04x}{:04x}".format(
TypeError: unsupported format string passed to NoneType.format

This is the log when i try to change the brightness:

Logger: homeassistant.components.websocket_api.http.connection.2884159928
Source: custom_components/localtuya/light.py:293
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 8:38:45 PM (2 occurrences)
Last logged: 8:38:45 PM

'NoneType' object is not subscriptable
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 135, in handle_call_service
await hass.services.async_call(
File "/usr/src/homeassistant/homeassistant/core.py", line 1448, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1483, in _execute_service
await handler.job.target(service_call)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
await self.hass.helpers.service.entity_service_call(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 482, in entity_service_call
future.result() # pop exception if have
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
await coro
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 519, in _handle_entity_call
await result
File "/usr/src/homeassistant/homeassistant/components/light/init.py", line 249, in async_handle_light_on_service
await light.async_turn_on(**params)
File "/config/custom_components/localtuya/light.py", line 293, in async_turn_on
round(self._hs[0]), round(self._hs[1] * 10.0), brightness
TypeError: 'NoneType' object is not subscriptable

@ultratoto14
Copy link
Collaborator Author

Hi @SerAlbi so, it confirms what i was scared about. I dumped the scenes from known devices (i have multiple ones from different manufacturers). It seems that yours are different in the light strip at least.
Will see if we can find a way to override the predefined scenes.

For the color/brightness error, will check tomorrow, I think I know why, stay tuned.

@SerAlbi
Copy link

SerAlbi commented Nov 15, 2020

Thank you very much :) i'll keep you updated if i discover something else.

@ultratoto14
Copy link
Collaborator Author

Hi @SerAlbi, tried to reproduce your error, and didn't succeed. Can you paste here the yaml configuration of the specific device ?
Can you describe the steps to reproduce ? The only way that i find to generate that would be that the device has just been added (HA restart or integration reload) and a try is done to set color/brightness before the integration gets its status, or the tuya application is running and HA does not get a first status.

I can still protect the code to ensure the exception will not throw but i would prefer understanding how it happened first 😄
Thanks.

@SerAlbi
Copy link

SerAlbi commented Nov 16, 2020

Ok, @ultratoto14 this is my configuration under 'localtuya':

  • host: 192.168.1.84
    device_id: ---
    local_key: ---
    friendly_name: LOCAL - LED PC
    protocol_version: "3.3"
    entities:
    • platform: light
      friendly_name: LOCAL - LED PC
      id: 20
      color_mode: 21
      color: 24
      scene: 25
      music_mode: False

The way the error is show is as follows:

  1. Turn on device
  2. Change scene mode to something
  3. Try and change the color of the LEDs using the colorwheel (or changing the brightness)
  4. The error is shown in the logs

Could it be that my leds do not return the color/brightness info if they are in scene mode? So they would need to be turned back to 'colour' before they can return any data... it's a bit confusing.

@ultratoto14
Copy link
Collaborator Author

Thanks, I reproduced it.

@ultratoto14
Copy link
Collaborator Author

@SerAlbi can you test last commit, it should fix the brightness/color errors.

@SerAlbi
Copy link

SerAlbi commented Nov 16, 2020

Hi, @ultratoto14 I tested the new version and it's working perfectly! I'm so pumped lol

@SmartM-ui
Copy link

@SmartM-ui feel free to test this one if you have time 😄

Hi @ultratoto14
here I am back home, tonight I check all the latest developments and try the latest version of the pull #146

See you later!

@ultratoto14
Copy link
Collaborator Author

Hi @SerAlbi I just added the scenes related to your RGB only strip, you can check if it works for you.

@SerAlbi
Copy link

SerAlbi commented Nov 16, 2020

Hi, @ultratoto14, after some testing it's all working perfectly :)

Copy link

@SerAlbi SerAlbi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested all the scenes for my light bulbs and led strips. They are all working correctly. Switching modes from scenes to colour/white is working aswell.

@reaper7
Copy link

reaper7 commented Nov 16, 2020

Works very well! TNX!

@postlund
Copy link
Collaborator

I will review later tonight.

@rospogrigio
Copy link
Owner

@rospogrigio can you give me rights to edit/add pages to the wiki please ?

@ultratoto14 ok how can I do that? @postlund maybe you can help?

@ultratoto14
Copy link
Collaborator Author

@rospogrigio, i already asked @postlund, he redirected me to you 😄

@ultratoto14
Copy link
Collaborator Author

@rospogrigio permissions

@rospogrigio
Copy link
Owner

Done... even if I'd prefer to add you as a collaborator, rather than letting anyone edit the wiki...

@ultratoto14
Copy link
Collaborator Author

@rospogrigio it could be that also, opening the wiki to every one is a bit big.

@rospogrigio
Copy link
Owner

OK you've been invited! Welcome onboard! 😄

@ultratoto14
Copy link
Collaborator Author

Thanks it will be more secure like that.

@postlund
Copy link
Collaborator

Great to have another pirate on the ship! 🏴‍☠️

@SmartM-ui
Copy link

@SerAlbi already found your brightness issue, @SmartM-ui it's not the same, i need to check more.

Hi @ultratoto14
Can I carry out some further tests to better describe the problem and get some details on the error to provide?

@ultratoto14
Copy link
Collaborator Author

@SmartM-ui no need it should be fixed,
You can test

Copy link
Collaborator

@postlund postlund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@postlund
Copy link
Collaborator

We go by the premises that you only approve PRs and let the author merge. So I leave that to you.

@ultratoto14 ultratoto14 merged commit 92f2372 into rospogrigio:master Nov 18, 2020
@SmartM-ui
Copy link

@SmartM-ui no need it should be fixed,
You can test

Hi @ultratoto14
Perfect, now it seems to work correctly on my bulbs too.
I left the scene value: 25 because all the scenes are viewed without having to change anything.
As soon as I can I also try the scene: 6 by changing the parameters as suggested a few posts above.
Good job!

@SmartM-ui
Copy link

Hi @SmartM-ui, some background about scenes:

* Scenes are stored in the tuya bulb

* When in scene mode, the bulb change the color and the brightness by itself and do no return the status, we have no way to update HA status and represent the changes

* As the scene is stored, there's no way to change, changing the brightness should change to color or white but this is arbitrary

About the scene: 6 can you dump the scenes you have on this light and post here, with name and values please ? you can use the procedure here

Hi @ultratoto14
I report the data for my scenes:

GEORGEUS
{"25":"07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000"}

DAZZLING
{"25":"06464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000"}

COLORFUL
{"25":"05464601000003e803e800000000464601007803e803e80000000046460100f003e803e800000000464601003d03e803e80000000046460100ae03e803e800000000464601011303e803e800000000"}

SOFT
{"25":"04464602007803e803e800000000464602007803e8000a00000000"}

LEISURE
{"25":"030e0d00001403e8000001f401f4"}

WORKING
{"25":"020e0d0000e80383000003e803e8"}

READ
{"25":"010e0d0000840000000003e801f4"}

NIGHT
{"25":"000e0d00002e03e8000000c80000"}

Where should I enter this data to try scene: 6 ?

Thanks

@ultratoto14
Copy link
Collaborator Author

For the moment not possible, in near future we will find a way to override predefined scenes.

@SmartM-ui
Copy link

For the moment not possible, in near future we will find a way to override predefined scenes.

Excuse the misunderstanding @ultratoto14
I thought it needed to be able to use preset scenes to use scene mode: 6 to force RGB_HSV.

@ultratoto14
Copy link
Collaborator Author

@ultratoto14 i stored some data from known device in the code, it is temporary.
If you have a bulb with DP ids in the range 1-15, then you can add a sensor with this id 6 and monitor your scenes and dump the values. As mentioned in another thread, the integration can just activate scenes that are available in the bulb, it cannot create scenes.

@SmartM-ui
Copy link

SmartM-ui commented Nov 23, 2020

@ultratoto14 @postlund

Hi,
I found an error on some bulbs that use the color: 5 instead of 24.

     color: 5 # Optional, usually 5 (RGB_HSV) or 24 (HSV), default: "none"
     scene: 25 # Optional, usually 6 (RGB_HSV) or 25 (HSV), default: "none"

In practice, when I choose a scene from the menu, instead of being shown the selected scene, a scene with the name "custom" is shown and, obviously, the scene is not reproduced and there is not even the possibility to turn off the light bulb through Home Assistant, except by restoring it to a recognized color

Log:
2020-11-23 14:47:28 ERROR (SyncWorker_35) [homeassistant.util.logging] Exception in _update_handler when dispatching 'localtuya_043...a40': ({'1': True, '2': 'scene', '3': 94, '4': 141, '5': '5b1600000fff5e', '6': '5b2d00001eff5e', '7': 'ffff500100ff00', '8': 'ffff8003ff000000ff000000ff000000000000000000', '9': 'ffff5001ff0000', '10': 'ffff0505ff000000ff00ffff00ff00ff0000ff000000'},)
Traceback (most recent call last):
File "/config/custom_components/localtuya/common.py", line 248, in _update_handler
self.status_updated()
File "/config/custom_components/localtuya/light.py", line 397, in status_updated
self._effect = self.__find_scene_by_scene_data(
File "/config/custom_components/localtuya/light.py", line 270, in __find_scene_by_scene_data
return next(
File "/config/custom_components/localtuya/light.py", line 271, in
(item for item in self._effect_list if self._scenes[item] == data),
KeyError: 'Custom'

I have no problem with bulbs that use 24 color mode

PS for info, I have pull # 171 and not pull # 146

Thanks

@ultratoto14
Copy link
Collaborator Author

@SmartM-ui Yep, it's because the scene of your light is not recognized by the integration.
Two possibilities, first, you modified the scene through the tuya app, second, the list of the scene of your bulb is not the same as the one used in the integration. No solution yet, it should be done when #181 will be designed and implemented.

@SmartM-ui
Copy link

@SmartM-ui Yep, it's because the scene of your light is not recognized by the integration.
Two possibilities, first, you modified the scene through the tuya app, second, the list of the scene of your bulb is not the same as the one used in the integration. No solution yet, it should be done when #181 will be designed and implemented.

Hi,
I haven't edited the scenes in the tuya app, but I could see that the bulbs in which the scenes work, have these modes (all titles written in English):
Night, Read, Working, Leisure, Soft, Colorful, Dazzling, Gorgeous

The bulbs in which the scenes do not work have these modes (some titles in English, others in Italian):
Night, Read, Meeting, Leisure, morbido, Arcobaleno, Brillare, Bellissima

Is it possible to add them in the integration?

@ultratoto14
Copy link
Collaborator Author

@SmartM-ui, sorry for that, but we will need to have the db, currently the scenes are stored as dictionary in the light.py. We don't want this file to become a db of all existing scenes. So we will need to wait for the #181 to be implemented.

@SmartM-ui
Copy link

@SmartM-ui, sorry for that, but we will need to have the db, currently the scenes are stored as dictionary in the light.py. We don't want this file to become a db of all existing scenes. So we will need to wait for the #181 to be implemented.

OK!
I saw that 181 was opened a few hours ago, I was not the first to encounter the problem! :-)

I tried trivially to put the names of my scenes in the light.py, but it still doesn't work.

I will wait for #181.
If I can be of any help with the tests, let me know.
Thanks in advance, you are the best :-)

PaulCavill pushed a commit to PaulCavill/localtuya that referenced this pull request May 9, 2024
* Revert and use target precision.
* Auto configuere Fix default name if prefer_type is dict.
* Fix max/min temps.
PaulCavill pushed a commit to PaulCavill/localtuya that referenced this pull request May 9, 2024
* Rather than send 2 commands each on it own, now sends both of them togather.
PaulCavill pushed a commit to PaulCavill/localtuya that referenced this pull request May 9, 2024
PaulCavill pushed a commit to PaulCavill/localtuya that referenced this pull request May 9, 2024
PaulCavill pushed a commit to PaulCavill/localtuya that referenced this pull request May 9, 2024
PaulCavill pushed a commit to PaulCavill/localtuya that referenced this pull request May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants