adaptive-lighting/tests
Roee Hendel 6cebe14a69
fix: merge last_service_data across split calls to fix detect_non_ha_changes with separate_turn_on_commands (#1426)
* test: regression test — AL must not override manual brightness with separate_turn_on_commands

End-to-end scenario: user adjusts brightness via a directly-bound Zigbee
switch (e.g. IKEA RODRET).  No HA service call is made; ZHA reports the new
brightness via async_update_entity.  On the next adaptation interval AL must
detect the change and stop overriding the user's brightness.

The test verifies the user-visible symptom: after two adaptation cycles
following a simulated direct-Zigbee brightness change, the light's brightness
must still be the manually set value — not AL's own target.

NOTE: this test FAILS on the current code.  It is committed here to document
the bug before the fix is applied in the next commit.

* fix: merge last_service_data across split calls to fix detect_non_ha_changes with separate_turn_on_commands

When separate_turn_on_commands=True, each adaptation cycle makes two
light.turn_on calls (brightness, then color_temp).  Previously each call
overwrote last_service_data[light], so after the cycle only the color_temp
key remained.  _attributes_have_changed() then saw old_brightness=None and
silently skipped the brightness comparison, so a manually-set brightness was
never detected and AL kept overriding it.

Fix: merge instead of overwrite so all split-call attributes accumulate:

    self.manager.last_service_data[light] = {
        **self.manager.last_service_data.get(light, {}),
        **service_data,
    }

* test: add intermediate assertions to regression test

Two assertions were promised in the PR description but missing:
1. After the force-adapt, assert that last_service_data contains BOTH
   brightness AND color — directly proving the merge fix works.
2. After the first non-forced update, assert that BRIGHTNESS is in
   manual_control — proving detection fired, not just that the final
   state is right.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor: remove spurious comments, trim test docstring and assertions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor: strip verbose comments from test, trim assert messages

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test: add message to bare assert
2026-03-16 15:54:53 -07:00
..
__init__.py Copy tests from https://github.com/home-assistant/core/pull/40626 2022-08-28 12:05:31 -07:00
conftest.py Bump to 1.27.0 for fixes in 2025.12 (#1293) 2025-11-27 09:06:41 -08:00
README.md docs: clarify Docker test setup requirements (#1383) 2026-01-11 21:57:52 +01:00
test_adaptation_utils.py Individual manual control of brightness and color (#1356) 2025-12-22 22:55:16 -08:00
test_color_and_brightness.py Code cleanup (#1348) 2025-12-12 13:37:42 -08:00
test_config_flow.py feat: add option to duplicate existing lighting instance (#1329) 2025-12-05 04:15:24 -08:00
test_hass_utils.py Test HA core v2023.6 until v2024.2 (#942) 2024-04-06 10:14:54 +02:00
test_init.py Test HA core v2023.6 until v2024.2 (#942) 2024-04-06 10:14:54 +02:00
test_switch.py fix: merge last_service_data across split calls to fix detect_non_ha_changes with separate_turn_on_commands (#1426) 2026-03-16 15:54:53 -07:00

Developer notes for the tests directory

To run the tests, check out the CI configuration to see how they are executed in the CI pipeline. Alternatively, you can use the provided Docker image to run the tests locally or run them with VS Code directly in the dev container.

Prerequisites

Before running tests with Docker, you need a local Home Assistant core checkout with symlinks:

# Clone HA core (one-time setup)
git clone --depth 1 https://github.com/home-assistant/core.git core

# Setup symlinks (one-time setup)
./scripts/setup-symlinks

Running tests with Docker

Navigate to the adaptive-lighting repo folder and execute the following command.

Important: Mount the entire repo (-v $(pwd):/app), not individual directories, or the symlinks will break.

Linux / MacOS / Windows PowerShell:

docker run -v ${PWD}:/app basnijholt/adaptive-lighting:latest
  • In windows command prompt, the command is:
    docker run -v %cd%:/app basnijholt/adaptive-lighting:latest
    

This command will download the Docker image from the adaptive-lighting Docker Hub repo and run the tests.

If you prefer to build the image yourself, use the following command:

docker build -t basnijholt/adaptive-lighting:latest --no-cache --progress=plain .

This might be necessary if the image on Docker Hub is outdated or if the test_dependencies.py file is updated.

Passing arguments to pytest

You can pass arguments to pytest by appending them to the command:

For example, to run the tests with a custom log format, use the following command (this also gets rid of the captured stderr output):

docker run -v $(pwd):/app basnijholt/adaptive-lighting:latest --show-capture=log --log-format="%(asctime)s %(levelname)-8s %(name)s:%(filename)s:%(lineno)s %(message)s" --log-date-format="%H:%M:%S" tests/components/adaptive_lighting/