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

TPLink Kasa Integration: KL430 - Persistent Effect Behavior #69039

Closed
SergeantPup opened this issue Mar 31, 2022 · 19 comments · Fixed by python-kasa/python-kasa#330 or #69308
Closed

TPLink Kasa Integration: KL430 - Persistent Effect Behavior #69039

SergeantPup opened this issue Mar 31, 2022 · 19 comments · Fixed by python-kasa/python-kasa#330 or #69308

Comments

@SergeantPup
Copy link

SergeantPup commented Mar 31, 2022

The problem

The defect has been replicated consistently on KL430 Hardware V2 Firmware 1.0.9 (approximately 16 devices) AND KL430 Hardware V1 Firmware 1.0.10. (approximately 22 devices). I previously reported that it was not happening on Firmware 1.0.10; however, upon further investigation the problem is occurring with all strips, it's just that the Firmware 1.0.10 come out of the effect with the adaptive lighting integration whereas Firmware 1.0.9 does not come out of the effect even with adaptive lighting.

For the purpose of these tests, I have removed adaptive lighting on the KL430's.
Reproduction steps on KL430 Hardware V1 Firmware 1.0.9 and KL430 Hardware V2 Firmware 1.0.10:

  1. Enter the device screen>select any effect (christmas)
  2. Change the device to color or CT (blue)
  3. Turn off the device
  4. Turn on the device > Light comes on as last effect set (christmas) not blue
  5. Every time the light is turned on via HA, it will come on with the last effect selected even if you were last in CT or color

What version of Home Assistant Core has the issue?

2022.4.0b0

What was the last working version of Home Assistant Core?

New Feature Functionality

What type of installation are you running?

Home Assistant OS

Integration causing the issue

TPLink Kasa

Link to integration documentation on our website

https://www.home-assistant.io/integrations/tplink/

Diagnostics information

Isolated to KL430 effects. If effects are never used/selected, it's not a problem.

Example YAML snippet

NA

Anything in the logs that might be useful for us?

No response

Additional information

No response

@probot-home-assistant
Copy link

tplink documentation
tplink source
(message by IssueLinks)

@probot-home-assistant
Copy link

Hey there @rytilahti, @TheGardenMonkey, mind taking a look at this issue as it has been labeled with an integration (tplink) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@SergeantPup SergeantPup changed the title TPLink Kasa Integration: KL430 Hardware V2 Firmware 1.0.9 - Persistent Effect Behavior TPLink Kasa Integration: KL430 - Persistent Effect Behavior Mar 31, 2022
@rytilahti
Copy link
Member

Maybe @bdraco has an idea what's wrong.

@bdraco bdraco self-assigned this Apr 1, 2022
@bdraco
Copy link
Member

bdraco commented Apr 1, 2022

I can replicate the issue.

Unfortunately I don't think there is enough information to fix it.

This is the state when a color is set and then turned off

                            'light_state': {'dft_on_state': {'brightness': 100,
                                                             'color_temp': 0,
                                                             'hue': 255,
                                                             'mode': 'normal',
                                                             'saturation': 100},
                                            'on_off': 0},
                            'lighting_effect_state': {'brightness': 100,
                                                      'custom': 0,
                                                      'enable': 0,
                                                      'id': 'izRhLCQNcDzIKdpMPqSTtBMuAIoreAuT',
                                                      'name': 'Rainbow'},

This is the state when an effect is set and its turned off

                            'light_state': {'dft_on_state': {'brightness': 100,
                                                             'color_temp': 0,
                                                             'hue': 255,
                                                             'mode': 'normal',
                                                             'saturation': 100},
                                            'on_off': 0},
                            'lighting_effect_state': {'brightness': 100,
                                                      'custom': 0,
                                                      'enable': 0,
                                                      'id': 'izRhLCQNcDzIKdpMPqSTtBMuAIoreAuT',
                                                      'name': 'Rainbow'},

They are identical unless I missed something

@bdraco
Copy link
Member

bdraco commented Apr 1, 2022

We might be able to fix it if there is a get_lighting_effect that returns more data in smartlife.iot.lighting_effect

I think we have to wait for modularize for that

@bdraco
Copy link
Member

bdraco commented Apr 1, 2022

{"smartlife.iot.lighting_effect":{"get_lighting_effect":{}},"context":{"source":"46a4d58b-6279-432c-ae23-e115c2db8354"}}
{"smartlife.iot.lighting_effect":{"get_lighting_effect":{"custom":0,"id":"oJjUMosgEMrdumfPANKbkFmBcAdEQsPy","brightness":7,"name":"Ocean","segments":[0],"expansion_strategy":1,"enable":0,"type":"sequence","duration":0,"transition":2000,"direction":3,"spread":16,"repeat_times":0,"sequence":[[198,84,30],[198,70,30],[198,10,30]],"err_code":0}}}

@SergeantPup
Copy link
Author

I can replicate the issue.

Unfortunately I don't think there is enough information to fix it.

This is the state when a color is set and then turned off

                            'light_state': {'dft_on_state': {'brightness': 100,
                                                             'color_temp': 0,
                                                             'hue': 255,
                                                             'mode': 'normal',
                                                             'saturation': 100},
                                            'on_off': 0},
                            'lighting_effect_state': {'brightness': 100,
                                                      'custom': 0,
                                                      'enable': 0,
                                                      'id': 'izRhLCQNcDzIKdpMPqSTtBMuAIoreAuT',
                                                      'name': 'Rainbow'},

This is the state when an effect is set and its turned off

                            'light_state': {'dft_on_state': {'brightness': 100,
                                                             'color_temp': 0,
                                                             'hue': 255,
                                                             'mode': 'normal',
                                                             'saturation': 100},
                                            'on_off': 0},
                            'lighting_effect_state': {'brightness': 100,
                                                      'custom': 0,
                                                      'enable': 0,
                                                      'id': 'izRhLCQNcDzIKdpMPqSTtBMuAIoreAuT',
                                                      'name': 'Rainbow'},

They are identical unless I missed something

If the top one is coming out of color, why does it have name rainbow? I agree they're identical but is that because it's hanging onto the effect name?

@bdraco
Copy link
Member

bdraco commented Apr 1, 2022

Thats the data from the device itself so its returning the effect that was previously set.

@bdraco
Copy link
Member

bdraco commented Apr 1, 2022

{"smartlife.iot.lighting_effect":{"set_lighting_effect":{"enable":0}},"smartlife.iot.lightStrip":{"set_light_state":{"on_off":0}}}

We might need to do something like this to augment the turn off

@bdraco
Copy link
Member

bdraco commented Apr 1, 2022

I'm out of time for today. I'll have to look when I get back home Monday

@bdraco
Copy link
Member

bdraco commented Apr 1, 2022

@SergeantPup if you can get dumps on how the app is turning it on and off locally without the cloud that would give me a head start

@SergeantPup
Copy link
Author

I reverted off beta for my own sanity so there's no rush. I took tomorrow off anyway and I'll get you the data dump. Thank you for confirming replication of issue.

@SergeantPup
Copy link
Author

SergeantPup commented Apr 2, 2022

I was looking for the instructions for the dump (because I knew I saw them in other threads) and I think somebody already came to a conclusion on the effects:

@iprodanovbg glad that you figured it out! I think the proper way to support this would be adding some new functionality (e.g. set_effect(Effect) for setting the effect) and adapt is_on (and others like turn_on if we can differentiate between the regular mode and the effect mode?) to take the effect on state into account when reporting on the state state.
From
LED strips effects are not supported (was LEDs is not turning on after switching on) #191

I learned some additional behavior that may point to the problem via Alexa:
From HA, select an effect, it gets stuck in the effect with the on/off (we know this) > turn off the device
From Alexa, call blue > lights come on as blue > from blue, call CT and it works. Turn off/on> light will be rainbow
Repeat these steps but do CT and it fails. From HA select rainbow > turn off > from Alexa, set to daylight > device will come on as rainbow > select a color instead and it changes.

What this tells me:
The issue is definitely with the 'on' feature
Color seems to override it but not CT
The next time 'on' is used, it will contain the effect in all scenarios

I have the packet information but I'm not sure how you want me to get it to you.

@bdraco
Copy link
Member

bdraco commented Apr 2, 2022

You can email it to nick@koston.org

@SergeantPup
Copy link
Author

I sent it over. Let me know if there's anything else I can do to help.

@bdraco
Copy link
Member

bdraco commented Apr 4, 2022

Here is the problem

The app sends
{"smartlife.iot.lightStrip":{"set_light_state":{"ignore_default":0,"on_off":1}}}

We send:
{"smartlife.iot.lightStrip":{"set_light_state":{"ignore_default":1,"on_off":1}}}

@bdraco
Copy link
Member

bdraco commented Apr 4, 2022

This fixes the lib to not clear the effect when turning back on

diff --git a/kasa/smartbulb.py b/kasa/smartbulb.py
index e5dcfbe..c2d0953 100644
--- a/kasa/smartbulb.py
+++ b/kasa/smartbulb.py
@@ -34,6 +34,9 @@ TPLINK_KELVIN = {
     r"KL430": ColorTempRange(2500, 9000),
 }
 
+
+NON_COLOR_MODE_FLAGS = {"transition_period", "on_off"}
+
 _LOGGER = logging.getLogger(__name__)
 
 
@@ -211,6 +214,12 @@ class SmartBulb(SmartDevice):
         if "on_off" not in state:
             state["on_off"] = 1
 
+        # If we are turning on without any color mode flags,
+        # we do not want to set ignore_default to ensure
+        # we restore the previous state.
+        if state["on_off"] and NON_COLOR_MODE_FLAGS.issuperset(state):
+            state["ignore_default"] = 0
+        else:
             # This is necessary to allow turning on into a specific state
             state["ignore_default"] = 1
 

This fixes the color temp not being able to be set when an effect is in progress

diff --git a/homeassistant/components/tplink/light.py b/homeassistant/components/tplink/light.py
index 89b954491e..22d2f2563f 100644
--- a/homeassistant/components/tplink/light.py
+++ b/homeassistant/components/tplink/light.py
@@ -352,6 +352,11 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
         """Turn the light on."""
         brightness, transition = self._async_extract_brightness_transition(**kwargs)
         if ATTR_COLOR_TEMP in kwargs:
+            if self.effect:
+                # If there is an effect in progress
+                # we have to set an HSV value to clear the effect
+                # before we can set a color temp
+                await self.device.set_hsv(0, 0, brightness)
             await self._async_set_color_temp(
                 int(kwargs[ATTR_COLOR_TEMP]), brightness, transition
             )
@@ -359,20 +364,6 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
             await self._async_set_hsv(kwargs[ATTR_HS_COLOR], brightness, transition)
         elif ATTR_EFFECT in kwargs:
             await self.device.set_effect(kwargs[ATTR_EFFECT])
-        elif (
-            self.device.is_off
-            and self.device.effect
-            and self.device.effect["enable"] == 0
-            and self.device.effect["name"]
-        ):
-            if not self.device.effect["custom"]:
-                await self.device.set_effect(self.device.effect["name"])
-            elif self._last_custom_effect:
-                await self.device.set_custom_effect(self._last_custom_effect)
-            # The device does not remember custom effects
-            # so we must set a default value or it can never turn back on
-            else:
-                await self.device.set_hsv(0, 0, 100, transition=transition)
         else:
             await self._async_turn_on_with_brightness(brightness, transition)
 

@bdraco
Copy link
Member

bdraco commented Apr 4, 2022

I need to pull out some of the bulbs and test that it doesn't break anything

@bdraco
Copy link
Member

bdraco commented Apr 5, 2022

confirmed ok with the bulbs

also custom effects now get restored 👍

bdraco added a commit to bdraco/python-kasa that referenced this issue Apr 5, 2022
bdraco added a commit to bdraco/home-assistant that referenced this issue Apr 5, 2022
bdraco added a commit to bdraco/python-kasa that referenced this issue Apr 5, 2022
rytilahti added a commit to python-kasa/python-kasa that referenced this issue Apr 5, 2022
* Ensure state is restored when turning back on

Fixes home-assistant/core#69039

* Update kasa/tests/test_bulb.py

Co-authored-by: Teemu R. <tpr@iki.fi>

Co-authored-by: Teemu R. <tpr@iki.fi>
@bdraco bdraco reopened this Apr 5, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.