Ensure bulb state is restored when turning back on (#330)

* Ensure state is restored when turning back on

Fixes https://github.com/home-assistant/core/issues/69039

* Update kasa/tests/test_bulb.py

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

Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
J. Nick Koston 2022-04-05 06:51:36 -10:00 committed by GitHub
parent a744af46ab
commit 766819a2a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -34,6 +34,9 @@ TPLINK_KELVIN = {
r"KL430": ColorTempRange(2500, 9000), r"KL430": ColorTempRange(2500, 9000),
} }
NON_COLOR_MODE_FLAGS = {"transition_period", "on_off"}
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -211,6 +214,12 @@ class SmartBulb(SmartDevice):
if "on_off" not in state: if "on_off" not in state:
state["on_off"] = 1 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 # This is necessary to allow turning on into a specific state
state["ignore_default"] = 1 state["ignore_default"] = 1

View File

@ -232,3 +232,16 @@ async def test_non_dimmable(dev):
assert dev.brightness == 0 assert dev.brightness == 0
with pytest.raises(SmartDeviceException): with pytest.raises(SmartDeviceException):
await dev.set_brightness(100) await dev.set_brightness(100)
@bulb
async def test_ignore_default_not_set_without_color_mode_change_turn_on(dev, mocker):
query_helper = mocker.patch("kasa.SmartBulb._query_helper")
# When turning back without settings, ignore default to restore the state
await dev.turn_on()
args, kwargs = query_helper.call_args_list[0]
assert args[2] == {"on_off": 1, "ignore_default": 0}
await dev.turn_off()
args, kwargs = query_helper.call_args_list[1]
assert args[2] == {"on_off": 0, "ignore_default": 1}