From d23f6ec63a713e9428328023bd405fcf2b7c4fd2 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sun, 7 Apr 2024 14:01:05 +0200 Subject: [PATCH] Fix `test_proactive_adaptation_with_separate_commands` (#970) * Add link script * Set run_immediately=False * Add await hass.async_block_till_done() * Use scripts/link * Rename scripts * Links in devcontainer * Install from setup script --- .../workflows/install_dependencies/action.yml | 3 ++- .github/workflows/pytest.yaml | 24 ------------------- .github/workflows/update-readme.yml | 5 ---- Dockerfile | 7 ++---- custom_components/adaptive_lighting/switch.py | 3 +++ requirements.txt | 10 -------- scripts/{install_ha => setup-dependencies} | 0 scripts/setup-devcontainer | 9 ++++++- scripts/setup-symlinks | 13 ++++++++++ tests/test_switch.py | 1 + 10 files changed, 29 insertions(+), 46 deletions(-) delete mode 100644 requirements.txt rename scripts/{install_ha => setup-dependencies} (100%) create mode 100755 scripts/setup-symlinks diff --git a/.github/workflows/install_dependencies/action.yml b/.github/workflows/install_dependencies/action.yml index 16c0a6ec..badc6566 100644 --- a/.github/workflows/install_dependencies/action.yml +++ b/.github/workflows/install_dependencies/action.yml @@ -34,4 +34,5 @@ runs: - name: Install dependencies shell: bash run: | - ./scripts/install_ha + ./scripts/setup-dependencies + ./scripts/setup-symlinks diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml index 59a46c1c..ed5de50c 100644 --- a/.github/workflows/pytest.yaml +++ b/.github/workflows/pytest.yaml @@ -62,30 +62,6 @@ jobs: python-version: ${{ matrix.python-version }} core-version: ${{ matrix.core-version }} - - name: Click here for troubleshooting steps if tests break again. - run: | - echo "::notice::### If tests fail, try these debug steps: ###" - echo "::notice::### 1. Replace '-qq' from .github/workflow/pytest.yaml. with '-v' for extra verbosity. ###" - echo "::notice::### 2. Push or run action again. ###" - echo "::notice::### 3. Check for any log messages in github actions resembling the following using CTRL+F ### - echo "::notice::### 4. ERROR:homeassistant.setup:Setup failed for 'component': Unable to import component: No module named ''module'' ###" - echo "::notice::### 5. add 'component'.'module' (without the '') from the above log into the 'required' list inside of 'test_dependencies.py' ###" - echo "::notice::### 6. Try again! If more issues persist they should be easily solvable by reading the verbose logs now. ###" - - - name: Link custom_components/adaptive_lighting - run: | - cd core - - # Link homeassitant.components.adaptive_lighting - cd homeassistant/components - ln -fs ../../../custom_components/adaptive_lighting adaptive_lighting - cd - - - # Link adaptive_lighting tests - cd tests/components/ - ln -fs ../../../tests adaptive_lighting - cd - - - name: Run pytest timeout-minutes: 60 run: | diff --git a/.github/workflows/update-readme.yml b/.github/workflows/update-readme.yml index 74f5e242..bf85fce3 100644 --- a/.github/workflows/update-readme.yml +++ b/.github/workflows/update-readme.yml @@ -26,11 +26,6 @@ jobs: run: | pip install markdown-code-runner==1.0.0 pandas tabulate - - name: Link custom_components/adaptive_lighting - run: | - cd core/homeassistant/components - ln -fs ../../../custom_components/adaptive_lighting adaptive_lighting - - name: Run markdown-code-runner run: markdown-code-runner --debug README.md diff --git a/Dockerfile b/Dockerfile index d3c5c79b..5232b4d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,13 +22,10 @@ RUN git clone --depth 1 --branch dev https://github.com/home-assistant/core.git COPY . /app/ # Setup symlinks in core -RUN ln -s /app/custom_components/adaptive_lighting /core/homeassistant/components/adaptive_lighting && \ - ln -s /app/tests /core/tests/components/adaptive_lighting && \ - # For test_dependencies.py - ln -s /core /app/core +RUN ln -s /core /app/core && /app/scripts/setup-symlinks # Install home-assistant/core dependencies -RUN /app/scripts/install_ha +RUN /app/scripts/setup-dependencies WORKDIR /core diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 23ca14f9..9408d90b 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -952,6 +952,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): self.hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STARTED, self._setup_listeners, + run_immediately=False, ) last_state: State | None = await self.async_get_last_state() is_new_entry = last_state is None # newly added to HA @@ -1658,10 +1659,12 @@ class AdaptiveLightingManager: self.hass.bus.async_listen( EVENT_CALL_SERVICE, self.turn_on_off_event_listener, + run_immediately=False, ), self.hass.bus.async_listen( EVENT_STATE_CHANGED, self.state_changed_event_listener, + run_immediately=False, ), ] diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index c560a858..00000000 --- a/requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -colorlog==6.7.0 -pip>=21.0,<23.2 -ruff==0.0.265 -pre-commit - -# Install HA and test dependencies (pytest, coverage) -# To pin the dev container to a specific HA version, set this dependency -# to the adequate version (add `==`) and rebuild the dev container. -# See https://github.com/MatthewFlamm/pytest-homeassistant-custom-component/releases for version mappings. -pytest-homeassistant-custom-component diff --git a/scripts/install_ha b/scripts/setup-dependencies similarity index 100% rename from scripts/install_ha rename to scripts/setup-dependencies diff --git a/scripts/setup-devcontainer b/scripts/setup-devcontainer index 9571972b..563d26ef 100755 --- a/scripts/setup-devcontainer +++ b/scripts/setup-devcontainer @@ -7,5 +7,12 @@ if [[ ! -d "core" ]]; then git clone --depth 1 --branch dev https://github.com/home-assistant/core.git fi -./scripts/install_ha +pip install \ + colorlog==6.7.0 \ + pip>=21.0,<23.2 \ + ruff==0.0.265 \ + pre-commit + +./scripts/setup-dependencies +./scripts/setup-symlinks pre-commit install-hooks diff --git a/scripts/setup-symlinks b/scripts/setup-symlinks new file mode 100755 index 00000000..91026b3a --- /dev/null +++ b/scripts/setup-symlinks @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -ex +cd "$(dirname "$0")/.." + +# Link custom components +cd core/homeassistant/components/ +ln -fs ../../../custom_components/adaptive_lighting adaptive_lighting +cd - + +# Link tests +cd core/tests/components/ +ln -fs ../../../tests/ adaptive_lighting +cd - diff --git a/tests/test_switch.py b/tests/test_switch.py index a6c4a2a8..e582a031 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -1684,6 +1684,7 @@ async def test_proactive_adaptation_transition_override(hass): {ATTR_ENTITY_ID: ENTITY_LIGHT_3, ATTR_TRANSITION: 456}, blocking=True, ) + await hass.async_block_till_done() # Assert that default is used when no transition is specified in service call assert patched_async_turn_on.call_args_list, patched_async_turn_on.call_args_list