From 208f3b0ea1949725b77c199a94553271b256b243 Mon Sep 17 00:00:00 2001 From: oliver Date: Mon, 3 Feb 2025 00:07:53 +0100 Subject: [PATCH] Fix daylight detection from timezon utc to local timezone defined in 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. --- camera/camera.py | 12 ++++++------ config/config_util.py | 2 ++ config_template.yaml | 2 ++ test/test_camera2.py | 5 ++++- test/test_config_util.py | 4 +++- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/camera/camera.py b/camera/camera.py index ee5b978..1eef9cf 100644 --- a/camera/camera.py +++ b/camera/camera.py @@ -7,7 +7,7 @@ import queue import time from datetime import datetime, timezone - +from zoneinfo import ZoneInfo import aiohttp import requests @@ -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 diff --git a/config/config_util.py b/config/config_util.py index ce49606..5e58cf9 100755 --- a/config/config_util.py +++ b/config/config_util.py @@ -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"] diff --git a/config_template.yaml b/config_template.yaml index e84265b..f2278b1 100755 --- a/config_template.yaml +++ b/config_template.yaml @@ -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 ? diff --git a/test/test_camera2.py b/test/test_camera2.py index 5610c16..4534469 100644 --- a/test/test_camera2.py +++ b/test/test_camera2.py @@ -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 @@ -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 @@ -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) diff --git a/test/test_config_util.py b/test/test_config_util.py index f0265a3..9b1549d 100644 --- a/test/test_config_util.py +++ b/test/test_config_util.py @@ -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,