From 4dfed2d224fa31748d83c45ff8b2df3c837e8fcc Mon Sep 17 00:00:00 2001 From: Andreas Backx Date: Sat, 19 Oct 2024 14:56:37 +0100 Subject: [PATCH] Make Dockerfile handle caching better and fix 1 trivial test issue. --- Dockerfile | 24 ++++++++++--------- .../adaptive_lighting/color_and_brightness.py | 24 +++++++++++++++++++ scripts/setup-dependencies | 8 +++---- scripts/setup-symlinks | 9 ++----- tests/test_switch.py | 1 - 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5232b4d9..ff021c0b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,28 +9,30 @@ FROM python:3.12-bookworm +# Make 'custom_components/adaptive_lighting' imports available to tests +ENV PYTHONPATH="${PYTHONPATH}:/app" + RUN apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y \ git \ build-essential libssl-dev libffi-dev python3-dev \ && rm -rf /var/lib/apt/lists/* -# Clone home-assistant/core -RUN git clone --depth 1 --branch dev https://github.com/home-assistant/core.git /core - -# Copy the Adaptive Lighting repository -COPY . /app/ +WORKDIR /core -# Setup symlinks in core +# Clone home-assistant/core and setup symlinks +RUN git clone --depth 1 --branch dev https://github.com/home-assistant/core.git /core +RUN mkdir /app +COPY ./scripts /app/scripts RUN ln -s /core /app/core && /app/scripts/setup-symlinks # Install home-assistant/core dependencies +COPY ./test_dependencies.py /app/test_dependencies.py RUN /app/scripts/setup-dependencies -WORKDIR /core - -# Make 'custom_components/adaptive_lighting' imports available to tests -ENV PYTHONPATH="${PYTHONPATH}:/app" +# Copy the remaining Adaptive Lighting repository, do this last so most changes +# won't require rebuilding the entire image. +COPY . /app/ ENTRYPOINT ["python3", \ # Enable Python development mode @@ -57,4 +59,4 @@ ENTRYPOINT ["python3", \ "-o", "console_output_style=count"] # Run tests in the 'tests/components/adaptive_lighting' directory -CMD ["tests/components/adaptive_lighting"] +CMD ["/core/tests/components/adaptive_lighting"] diff --git a/custom_components/adaptive_lighting/color_and_brightness.py b/custom_components/adaptive_lighting/color_and_brightness.py index 4db50b4f..a24f82b5 100644 --- a/custom_components/adaptive_lighting/color_and_brightness.py +++ b/custom_components/adaptive_lighting/color_and_brightness.py @@ -105,6 +105,30 @@ def noon_and_midnight( sunrise: datetime.datetime | None = None, ) -> tuple[datetime.datetime, datetime.datetime]: """Return the (adjusted) noon and midnight times for the given datetime.""" + # if ( + # self.sunrise_time is None + # and self.sunset_time is None + # and self.min_sunrise_time is None + # and self.max_sunrise_time is None + # and self.min_sunset_time is None + # and self.max_sunset_time is None + # ): + # solar_noon = self.astral_location.noon(dt, local=False) + # solar_midnight = self.astral_location.midnight(dt, local=False) + # return solar_noon, solar_midnight + + # if sunset is None: + # sunset = self.sunset(dt) + # if sunrise is None: + # sunrise = self.sunrise(dt) + + # middle = abs(sunset - sunrise) / 2 + # if sunset > sunrise: + # noon = sunrise + middle + # midnight = noon + timedelta(hours=12) * (1 if noon.hour < 12 else -1) + # else: + # midnight = sunset + middle + # noon = midnight + timedelta(hours=12) * (1 if midnight.hour < 12 else -1) sunrise = sunrise or self.sunrise(dt) sunset = sunset or self.sunset(dt) diff --git a/scripts/setup-dependencies b/scripts/setup-dependencies index ca14045d..ffdd01b8 100755 --- a/scripts/setup-dependencies +++ b/scripts/setup-dependencies @@ -2,7 +2,7 @@ set -ex cd "$(dirname "$0")/.." -pip install -r core/requirements.txt +pip install -r core/requirements.txt --root-user-action if grep -q 'codecov' core/requirements_test.txt; then # Older HA versions still have `codecov` in `requirements_test.txt` @@ -14,8 +14,8 @@ if grep -q 'mypy-dev==1.10.0a3' core/requirements_test.txt; then # mypy-dev==1.10.0a3 seems to not be available anymore, HA 2024.4 and 2024.5 are affected sed -i 's/mypy-dev==1.10.0a3/mypy-dev==1.10.0b1/' core/requirements_test.txt fi -pip install -r core/requirements_test.txt +pip install -r core/requirements_test.txt --root-user-action -pip install -e core/ +pip install -e core/ --root-user-action pip install ulid-transform # this is in Adaptive-lighting's manifest.json -pip install $(python test_dependencies.py) +pip install $(python test_dependencies.py) --root-user-action diff --git a/scripts/setup-symlinks b/scripts/setup-symlinks index 91026b3a..443fd967 100755 --- a/scripts/setup-symlinks +++ b/scripts/setup-symlinks @@ -1,13 +1,8 @@ #!/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 - +ln -fs /app/custom_components/adaptive_lighting /core/homeassistant/components/adaptive_lighting # Link tests -cd core/tests/components/ -ln -fs ../../../tests/ adaptive_lighting -cd - +ln -fs /app/tests /core/tests/components/adaptive_lighting diff --git a/tests/test_switch.py b/tests/test_switch.py index e582a031..47710c86 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -1347,7 +1347,6 @@ def mock_area_registry( registry._area_data = {} area_kwargs = { "name": "Test Area", - "normalized_name": "test-area", "id": "test-area", "picture": None, }