Skip to content

Commit 5fe06c6

Browse files
fix timezone and location detection for dayloght detection under special cirumstances were country was england by default choosen. (#208)
1 parent 05f82bc commit 5fe06c6

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

camera/camera.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def detect_daylight(self) -> bool:
386386
Detects whether it is currently daylight based on sunrise and sunset times.
387387
388388
The method uses either the configured coordinates (latitude and longitude)
389-
or the configured location to calculate sun times.
389+
or the configured country to calculate sun times.
390390
391391
Returns:
392392
bool: True if it is daylight (between sunrise and sunset),
@@ -399,17 +399,19 @@ def detect_daylight(self) -> bool:
399399
if (self.config.lat is not None and self.config.lon is not None):
400400
self.logger.debug("using coordinates for daylight detection")
401401
location = LocationInfo(
402+
name=self.config.city,
403+
region=self.config.country,
402404
latitude=self.config.lat,
403405
longitude=self.config.lon,
404406
timezone=self.config.timezone
405407
)
406408
s = sun(location.observer, date=local_date)
407-
elif self.config.location is not None:
408-
self.logger.debug("using location - city for daylight detection")
409-
location = LocationInfo(name=self.config.location)
409+
elif self.config.country is not None:
410+
self.logger.debug("using country - city for daylight detection")
411+
location = LocationInfo(name=self.config.country)
410412
s = sun(location.observer, date=local_date)
411413
else:
412-
raise ValueError("No valid location data provided")
414+
raise ValueError("No valid country data provided")
413415

414416
self.logger.debug(f"Sunrise: {s['sunrise']}, Sunset: {s['sunset']}")
415417
time_now = datetime.now(tz=ZoneInfo(self.config.timezone)) # Konvertiere String zu ZoneInfo

config/config_util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ def __init__(self) -> None:
5252
self.photo_image_path: str = self.config["photo_general"]["image_path"]
5353
self.default_camera_type = DefaultCam(self.config["photo_general"]["default_camera_type"].upper())
5454
self.enable_detect_daylight: bool = self.config["photo_general"]["enable_detect_daylight"]
55-
self.timezone: str = self.config["photo_general"]["timezone"]
56-
self.location: str = self.config["photo_general"]["location"]
55+
self.timezone: str = self.config["photo_general"]["timezone"] # Timezone name for daylight detection
56+
self.country: str = self.config["photo_general"]["country"] # Country name for timezone and daylight detection
57+
self.city: str = self.config["photo_general"].get("city", None) # City name for timezone and daylight detection
5758
self.lat: float = self.config["photo_general"].get("lat", None) # optional value see config_template.yaml for more information
58-
self.lon: float = self.config["photo_general"].get("lat", None) # optional value see config_template.yaml for more information
59+
self.lon: float = self.config["photo_general"].get("lon", None) # optional value see config_template.yaml for more information
5960
self.image_brightness: float = self.config["photo_general"].get("brightness_enhancer", None) # optional value see config_template.yaml for more information
6061
self.image_contrast: float = self.config["photo_general"].get("contrast_enhancer", None) # optional value see config_template.yaml for more information
6162

config_template.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ photo_general:
2828
default_camera_type: "blink" # "blink" or "picam" careful - case senitive
2929
image_path: "/tmp/foto.jpg" # path where downloaded foto from camera get intermediate stored
3030
timezone: "Europe/Berlin" # timezone for daylight detection
31-
location: "Berlin" # location for daylight detection
32-
lat: 1.1111111 # latitude for sunrise/sunset calculation if wanted based on lat instead of location timezone
33-
lon: 2.2222222 # longitude for sunrise/sunset calculation if wanted based on lat instead of location timezone
31+
country: "Germany" # country for daylight detection
32+
city: "Berlin" # city for daylight detection
33+
lat: 1.1111111 # latitude for sunrise/sunset calculation if wanted based on lat instead of country timezone
34+
lon: 2.2222222 # longitude for sunrise/sunset calculation if wanted based on lat instead of country timezone
3435
brightness_enhancer: 3 # brightness enhancer for image if camera does not provide night_vision (1.0 is normal, <1 darker, >1 lighter)
3536
contrast_enhancer: 2 # contrast enhancer for image if camera does not provide night_vision (1.0 is normal, <1 darker, >1 lighter)
3637

38+
3739
blink:
3840
enabled: True # blink cam enabled ?
3941
username: "blink_account_email_address_here" # blink account name here

test/test_camera_testsuite.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ def test_night_detected_with_coordinates(self):
195195
def test_night_detected_with_location(self):
196196
self.config.lat = None
197197
self.config.lon = None
198-
self.config.location = "Berlin"
198+
self.config.country = "Berlin"
199199
with patch('camera.camera.sun', new_callable=MagicMock) as mock_sun:
200200
with patch('camera.camera.datetime', new_callable=MagicMock) as mock_datetime:
201201
mock_datetime.now.return_value = datetime(2023, 6, 21, 2, 0, 0, tzinfo=ZoneInfo('Europe/Berlin'))
202202
mock_sun.return_value = {'sunrise': datetime(2023, 6, 21, 4, 0, 0, tzinfo=ZoneInfo('Europe/Berlin')),
203203
'sunset': datetime(2023, 6, 21, 22, 0, 0, tzinfo=ZoneInfo('Europe/Berlin'))}
204204
self.assertFalse(self.camera.detect_daylight())
205205
self.mock_logger_info.assert_any_call(msg="Is daylight detected: False")
206-
self.mock_logger_debug.assert_any_call("using location - city for daylight detection")
206+
self.mock_logger_debug.assert_any_call("using country - city for daylight detection")
207207

208208
def test_invalid_location_data(self):
209209
with patch('camera.camera.sun', new_callable=MagicMock) as mock_sun:
@@ -214,9 +214,9 @@ def test_invalid_location_data(self):
214214
def test_invalid_location_data(self):
215215
self.config.lat = None
216216
self.config.lon = None
217-
self.config.location = None
217+
self.config.country = None
218218
self.assertTrue(self.camera.detect_daylight())
219-
self.mock_logger_error.assert_any_call("Invalid Location data: No valid location data provided")
219+
self.mock_logger_error.assert_any_call("Invalid Location data: No valid country data provided")
220220

221221
def test_exception_handling(self):
222222
with patch('camera.camera.sun', new_callable=MagicMock) as mock_sun:

test/test_config_util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def setUp(self, mock_telebot, mock_define_config_file, mock_read_config, mock_ge
4242
'default_camera_type': 'blink',
4343
'enable_detect_daylight': True,
4444
'timezone': 'Europe/Berlin',
45-
'location': 'Berlin',
45+
'country': 'Germany',
46+
'city': 'Berlin',
4647
'lat': 1.1111111,
4748
'lon': 2.2222222,
4849
'brightness_enhancer': 3,

0 commit comments

Comments
 (0)