From 89f579a402f75304ed691d2eeea3cdbd636d34c8 Mon Sep 17 00:00:00 2001 From: Andreas Backx Date: Thu, 18 Apr 2024 23:32:36 +0100 Subject: [PATCH 1/4] Fix: noon and midnight adjustment --- .../adaptive_lighting/color_and_brightness.py | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/custom_components/adaptive_lighting/color_and_brightness.py b/custom_components/adaptive_lighting/color_and_brightness.py index 52386bf7..a880f6e8 100644 --- a/custom_components/adaptive_lighting/color_and_brightness.py +++ b/custom_components/adaptive_lighting/color_and_brightness.py @@ -105,30 +105,21 @@ 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) + + solar_noon = self.astral_location.noon(dt, local=False) + solar_midnight = self.astral_location.midnight(dt, local=False) + + solar_sunrise= self.astral_location.sunrise(dt, local=False) + solar_sunset= self.astral_location.sunset(dt, local=False) + + sunrise_offset = solar_sunrise - sunrise + sunset_offset = solar_sunset - sunset + + noon = solar_noon - sunrise_offset + midnight = solar_midnight - sunset_offset + return noon, midnight def sun_events(self, dt: datetime.datetime) -> list[tuple[str, float]]: From 2bb5ea0bab90728b6d8a8a576e8a2e42cb0e57d0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 18 Apr 2024 23:08:09 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- custom_components/adaptive_lighting/color_and_brightness.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/adaptive_lighting/color_and_brightness.py b/custom_components/adaptive_lighting/color_and_brightness.py index a880f6e8..4db50b4f 100644 --- a/custom_components/adaptive_lighting/color_and_brightness.py +++ b/custom_components/adaptive_lighting/color_and_brightness.py @@ -111,8 +111,8 @@ def noon_and_midnight( solar_noon = self.astral_location.noon(dt, local=False) solar_midnight = self.astral_location.midnight(dt, local=False) - solar_sunrise= self.astral_location.sunrise(dt, local=False) - solar_sunset= self.astral_location.sunset(dt, local=False) + solar_sunrise = self.astral_location.sunrise(dt, local=False) + solar_sunset = self.astral_location.sunset(dt, local=False) sunrise_offset = solar_sunrise - sunrise sunset_offset = solar_sunset - sunset From 4dfed2d224fa31748d83c45ff8b2df3c837e8fcc Mon Sep 17 00:00:00 2001 From: Andreas Backx Date: Sat, 19 Oct 2024 14:56:37 +0100 Subject: [PATCH 3/4] 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, } From 9737ad7d04e831af9f9f8a6fd12ffe2098a0d25d Mon Sep 17 00:00:00 2001 From: Andreas Backx Date: Sat, 19 Oct 2024 14:57:40 +0100 Subject: [PATCH 4/4] Remove commented code. --- .../adaptive_lighting/color_and_brightness.py | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/custom_components/adaptive_lighting/color_and_brightness.py b/custom_components/adaptive_lighting/color_and_brightness.py index a24f82b5..4db50b4f 100644 --- a/custom_components/adaptive_lighting/color_and_brightness.py +++ b/custom_components/adaptive_lighting/color_and_brightness.py @@ -105,30 +105,6 @@ 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)