Skip to content

Commit

Permalink
Fix daylight detection from timezon utc to local timezone defined in …
Browse files Browse the repository at this point in the history
…config.yaml file

See config_template.yaml section:
photo_general:
  timezone: "Europe/Berlin" # timezone for daylight detection
  location: "Berlin" # location for daylight detection

This is a breaking change!
Config file must be adjusted to this accordingly.
  • Loading branch information
OliverDrechsler committed Feb 2, 2025
1 parent ff4a2d2 commit 208f3b0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
12 changes: 6 additions & 6 deletions camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import queue
import time
from datetime import datetime, timezone

Check failure on line 9 in camera/camera.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

camera/camera.py:9:32: F401 `datetime.timezone` imported but unused

Check failure on line 9 in camera/camera.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

camera/camera.py:9:32: F401 `datetime.timezone` imported but unused

from zoneinfo import ZoneInfo
import aiohttp
import requests

Expand Down Expand Up @@ -374,11 +374,11 @@ def detect_daylight(self) -> bool:
"""
Detects daylight and returns a boolean value.
"""
loc = LocationInfo(name="Berlin", region="Germany", timezone="Europe/Berlin")
time_now: datetime = datetime.now(tz=timezone.utc)
s: dict[str, datetime] = sun(
observer=loc.observer, date=time_now, tzinfo=loc.timezone
)
location = LocationInfo(name=self.config.location)
local_date = datetime.now(ZoneInfo(self.config.timezone))
s = sun(location.observer, date=local_date)
time_now = datetime.now(tz=ZoneInfo(self.config.timezone)) # Konvertiere String zu ZoneInfo

daylight: bool = s["sunrise"] <= time_now <= (s["sunset"])
self.logger.info(msg=f"Is daylight detected: {daylight}")
return daylight
Expand Down
2 changes: 2 additions & 0 deletions config/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def __init__(self) -> None:
self.photo_image_path: str = self.config["photo_general"]["image_path"]
self.default_camera_type = DefaultCam(self.config["photo_general"]["default_camera_type"].upper())
self.enable_detect_daylight: bool = self.config["photo_general"]["enable_detect_daylight"]
self.timezone: str = self.config["photo_general"]["timezone"]
self.location: str = self.config["photo_general"]["location"]

self.blink_enabled: bool = self.config["blink"]["enabled"]
self.blink_username: str = self.config["blink"]["username"]
Expand Down
2 changes: 2 additions & 0 deletions config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ photo_general:
enable_detect_daylight: False
default_camera_type: "blink" # "blink" or "picam" careful - case senitive
image_path: "/tmp/foto.jpg" # path where downloaded foto from camera get intermediate stored
timezone: "Europe/Berlin" # timezone for daylight detection
location: "Berlin" # location for daylight detection

blink:
enabled: True # blink cam enabled ?
Expand Down
5 changes: 4 additions & 1 deletion test/test_camera2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import queue
from unittest.mock import Mock, patch, AsyncMock
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
from config.config_util import Configuration, DefaultCam
from config.data_class import Camera_Task, Message_Task
from camera.camera import Camera
Expand All @@ -16,6 +17,8 @@ def config():
config.default_camera_type = DefaultCam.BLINK
config.photo_image_path = "/tmp/test.jpg"
config.blink_name = "test_camera"
config.timezone = "Europe/Berlin"
config.location = "Berlin"
return config


Expand Down Expand Up @@ -75,7 +78,7 @@ async def test_picam_foto_helper_success(camera):
def test_detect_daylight(camera):
with patch('camera.camera.sun') as mock_sun:
# Simuliere Tageslicht
current_time = datetime.now(tz=timezone.utc)
current_time = datetime.now(tz=ZoneInfo(camera.config.timezone)) # Konvertiere String zu ZoneInfo
mock_sun.return_value = {
'sunrise': current_time.replace(hour=6),
'sunset': current_time.replace(hour=20)
Expand Down
4 changes: 3 additions & 1 deletion test/test_config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def setUp(self, mock_telebot, mock_define_config_file, mock_read_config, mock_ge
'photo_general': {
'image_path': '/path/to/image',
'default_camera_type': 'blink',
'enable_detect_daylight': True
'enable_detect_daylight': True,
'timezone': 'Europe/Berlin',
'location': 'Berlin'
},
'blink': {
'enabled': True,
Expand Down

0 comments on commit 208f3b0

Please sign in to comment.