Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: noon and midnight adjustment #980

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
39 changes: 15 additions & 24 deletions custom_components/adaptive_lighting/color_and_brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
8 changes: 4 additions & 4 deletions scripts/setup-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
9 changes: 2 additions & 7 deletions scripts/setup-symlinks
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
Loading