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

Breaking change fix daylight detection from timezone utc to local timezone needs adjustment in config file #204

Merged
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
12 changes: 6 additions & 6 deletions camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import os
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 @@
"""
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 @@
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 Expand Up @@ -109,7 +111,7 @@
except FileNotFoundError:
self.logger.error("Could not find %s", self.config_file)
raise FileNotFoundError("Could not find config file")
except:

Check failure on line 114 in config/config_util.py

View workflow job for this annotation

GitHub Actions / build

Ruff (E722)

config/config_util.py:114:9: E722 Do not use bare `except`

Check failure on line 114 in config/config_util.py

View workflow job for this annotation

GitHub Actions / build

Ruff (E722)

config/config_util.py:114:9: E722 Do not use bare `except`
self.logger.error("a YAML error is occured during parsing file %s ", self.config_file)
raise YamlReadError("a YAML error is occured during parsing file")

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
Loading