Squashed commit of the following:

commit 7069d693b8
Merge: adde5b5 a888afd
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 12:52:19 2023 -0500

    Merge branch 'main' into add-watched-lights

commit adde5b5645
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 10:34:24 2023 -0500

    fix pytest

commit 2c5eaec2d3
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 07:27:25 2023 -0500

    Revert "Update const.py"

    This reverts commit e773af07ca.

commit e773af07ca
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 07:20:23 2023 -0500

    Update const.py

commit c167f59ccd
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 06:09:14 2023 -0500

    Update docs

commit 6220df57d1
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:59:26 2023 -0500

    fix the test

commit ff292c2c1b
Merge: 4fdf42b 19d4468
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:39:17 2023 -0500

    Merge branch 'add-watched-lights' of https://github.com/basnijholt/adaptive-lighting into add-watched-lights

commit 4fdf42bea1
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:39:05 2023 -0500

    small fixes

commit 19d4468f63
Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Date:   Mon Apr 10 10:36:54 2023 +0000

    Update README.md, strings.json, and services.yaml

commit 70c90181f0
Merge: e30b7de b6a9265
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 10:34:49 2023 +0000

    Merge b6a92650a9 into e30b7debe5

commit b6a92650a9
Merge: 784eed0 ae04ab4
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:34:43 2023 -0500

    Merge branch 'add-watched-lights' of https://github.com/basnijholt/adaptive-lighting into add-watched-lights

commit 784eed09b3
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:34:33 2023 -0500

    squash merge `alt_detect_method` again

commit ae04ab49a8
Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Date:   Mon Apr 10 10:34:12 2023 +0000

    Update README.md, strings.json, and services.yaml

commit cfbd65f976
Merge: e30b7de 3c5ea73
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 10:32:34 2023 +0000

    Merge 3c5ea73341 into e30b7debe5

commit 3c5ea73341
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:32:27 2023 -0500

    Update switch.py

commit 2f750694d9
Merge: 429251b 7a9dc8f
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:31:13 2023 -0500

    Merge branch 'add-watched-lights' of https://github.com/basnijholt/adaptive-lighting into add-watched-lights

commit 429251b45f
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:31:04 2023 -0500

    update docs

commit 7a9dc8f24a
Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Date:   Mon Apr 10 10:29:37 2023 +0000

    Update README.md, strings.json, and services.yaml

commit 37de4244b1
Merge: e30b7de d52ea53
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 10:28:01 2023 +0000

    Merge d52ea53dcc into e30b7debe5

commit d52ea53dcc
Merge: dbc47c8 e30b7de
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:27:43 2023 -0500

    Merge branch 'main' into add-watched-lights

commit dbc47c8a36
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:26:11 2023 -0500

    Update switch.py

commit 2db675a434
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:25:44 2023 -0500

    merge with `alt_detect_method`

    required for this PR

commit dced97b5ca
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Apr 10 05:21:51 2023 -0500

    merge with `origin\main`
This commit is contained in:
Benjamin Auquite 2023-04-11 19:08:49 -05:00
commit 7895075e76
6 changed files with 297 additions and 125 deletions

View file

@ -11,6 +11,7 @@ from homeassistant.components.adaptive_lighting.const import (
ADAPT_BRIGHTNESS_SWITCH,
ADAPT_COLOR_SWITCH,
ATTR_TURN_ON_OFF_LISTENER,
CONF_ALT_DETECT_METHOD,
CONF_AUTORESET_CONTROL,
CONF_DETECT_NON_HA_CHANGES,
CONF_INITIAL_TRANSITION,
@ -22,6 +23,7 @@ from homeassistant.components.adaptive_lighting.const import (
CONF_SUNRISE_OFFSET,
CONF_SUNRISE_TIME,
CONF_SUNSET_TIME,
CONF_TAKE_OVER_CONTROL,
CONF_TRANSITION,
CONF_TURN_ON_LIGHTS,
CONF_USE_DEFAULTS,
@ -214,7 +216,9 @@ async def setup_lights_and_switch(hass, extra_conf=None):
CONF_SUNSET_TIME: datetime.time(SUNSET.hour),
CONF_INITIAL_TRANSITION: 0,
CONF_TRANSITION: 0,
CONF_DETECT_NON_HA_CHANGES: True,
CONF_ALT_DETECT_METHOD: False,
CONF_DETECT_NON_HA_CHANGES: False,
CONF_TAKE_OVER_CONTROL: True,
CONF_PREFER_RGB_COLOR: False,
CONF_MIN_COLOR_TEMP: 2500, # to not coincide with sleep_color_temp
**(extra_conf or {}),
@ -668,7 +672,7 @@ async def test_manual_control(hass):
@pytest.mark.dependency(depends=[*GLOBAL_TEST_DEPENDENCIES, "test_manual_control"])
async def test_auto_reset_manual_control(hass):
switch, (light, *_) = await setup_lights_and_switch(
hass, {CONF_AUTORESET_CONTROL: 0.1}
hass, {CONF_AUTORESET_CONTROL: 0.2}
)
context = switch.create_context("test") # needs to be passed to update method
manual_control = switch.turn_on_off_listener.manual_control
@ -913,7 +917,7 @@ async def test_state_change_handlers(hass):
# [Config options]:
transition_used = 2
total_events = 5
total_events = 6
async def set_brightness(val: int):
# 'Unsafe' set but we know what we're doing.
@ -1034,58 +1038,95 @@ async def test_state_change_handlers(hass):
assert listener.transition_timers.get(ENTITY_LIGHT)
# 5. Execute some checks during a transition
_LOGGER.debug("Test detect_non_ha_changes:")
switch._take_over_control = True
assert switch._take_over_control
switch._detect_non_ha_changes = True
assert switch._detect_non_ha_changes
await asyncio.sleep(transition_used / 3)
# Ensure the timer still exists
timer = listener.transition_timers.get(ENTITY_LIGHT)
assert timer and timer.is_running()
last_service_data = deepcopy(current_service_data)
await update()
assert not switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
await update()
assert not switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
timer = listener.transition_timers.get(ENTITY_LIGHT)
assert timer and timer.is_running()
# Ensure the light did not adapt during the transition.
assert last_service_data == current_service_data
for i in range(2):
if i == 0:
_LOGGER.debug("Test detect_non_ha_changes before a transition:")
switch._take_over_control = True
assert switch._take_over_control
switch._detect_non_ha_changes = True
assert switch._detect_non_ha_changes
switch._alt_detect_method = False
assert not switch._alt_detect_method
elif i == 1:
_LOGGER.debug("Test alt_detect_method before a transition:")
switch._take_over_control = True
assert switch._take_over_control
switch._detect_non_ha_changes = False
assert not switch._detect_non_ha_changes
switch._alt_detect_method = True
assert switch._alt_detect_method
await asyncio.sleep(transition_used / 3)
# Ensure the timer still exists
timer = listener.transition_timers.get(ENTITY_LIGHT)
assert timer and timer.is_running()
last_service_data = deepcopy(current_service_data)
await update()
assert not switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
await update()
assert not switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
timer = listener.transition_timers.get(ENTITY_LIGHT)
assert timer and timer.is_running()
# Ensure the light did not adapt during the transition.
assert last_service_data == current_service_data
# 6. Assert everything after the transition finishes.
await asyncio.sleep(transition_used)
assert listener.last_state_change.get(ENTITY_LIGHT)
assert len(listener.last_state_change[ENTITY_LIGHT]) == total_events
# Timer should be done and reset now.
# This is the assert that I can't fix.
timer = listener.transition_timers.get(ENTITY_LIGHT)
assert not timer or not timer.is_running()
# build last service data
await update(force=False)
for i in range(2):
if i == 0:
_LOGGER.debug("Test detect_non_ha_changes after a transition:")
switch._take_over_control = True
assert switch._take_over_control
switch._detect_non_ha_changes = True
assert switch._detect_non_ha_changes
switch._alt_detect_method = False
assert not switch._alt_detect_method
elif i == 1:
_LOGGER.debug("Test alt_detect_method after a transition:")
switch._take_over_control = True
assert switch._take_over_control
switch._detect_non_ha_changes = False
assert not switch._detect_non_ha_changes
switch._alt_detect_method = True
assert switch._alt_detect_method
# force=True should not reset manual control.
await turn_light(True, brightness=40)
await turn_light(True, brightness=20)
await update(force=False)
assert switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
await update(force=True)
assert switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
assert listener.last_state_change.get(ENTITY_LIGHT)
if i == 1:
total_events = 2
assert len(listener.last_state_change[ENTITY_LIGHT]) == total_events
# Timer should be done and reset now.
timer = listener.transition_timers.get(ENTITY_LIGHT)
assert not timer or not timer.is_running()
# turn light off then on should reset manual control.
await turn_light(False)
await turn_light(True)
assert not switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
# build last service data
await update(force=False)
await turn_light(True, brightness=50)
_LOGGER.debug("Test: Brightness set to %s", 50)
# force=True should not reset manual control.
await turn_light(True, brightness=40)
await turn_light(True, brightness=20)
await update(force=False)
assert switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
await update(force=True)
assert switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
# On next update ENTITY_LIGHT should be marked as manually controlled
await update(force=False)
assert switch.turn_on_off_listener.last_service_data.get(ENTITY_LIGHT) is not None
assert switch.turn_on_off_listener.last_state_change.get(ENTITY_LIGHT) is not None
assert switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
# turn light off then on should reset manual control.
await turn_light(False)
await turn_light(True)
assert not switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
await turn_light(True, brightness=50)
_LOGGER.debug("Test: Brightness set to %s", 50)
# On next update ENTITY_LIGHT should be marked as manually controlled
await update(force=False)
assert (
switch.turn_on_off_listener.last_service_data.get(ENTITY_LIGHT) is not None
)
assert (
switch.turn_on_off_listener.last_state_change.get(ENTITY_LIGHT) is not None
)
assert switch.turn_on_off_listener.manual_control[ENTITY_LIGHT]
@pytest.mark.dependency(