From 34998a8063a5f2b497e4487245dd2b5cf58b26dc Mon Sep 17 00:00:00 2001 From: miro Date: Sat, 17 Aug 2024 18:58:38 +0100 Subject: [PATCH] configurable sunset/sunrise times --- README.md | 4 ++ ovos_gui_plugin_shell_companion/brightness.py | 62 ++++++++++++++----- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 9aac056..b013481 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ provides various bus APIs that integrate with [ovos-shell](https://github.com/Op { "gui": { "ovos-gui-plugin-shell-companion": { + "sunrise_time": "auto", + "sunset_time": "auto", "default_brightness": 100, "night_default_brightness": 70, "low_brightness": 20, @@ -34,6 +36,8 @@ night mode will perform actions based on sunset/sunrise times - default brightness is reduced. - auto-dim is enabled +`sunrise_time` and `sunset_time` will be automatically calculated based on location if set to `"auto"`, specific times can be explicitly set with the format `"HH:MM"`, eg. if you are an early riser you may want `"sunrise_time": "05:30"` + brightness level during nighttime can be set via `"night_default_brightness"` ### Auto Dim diff --git a/ovos_gui_plugin_shell_companion/brightness.py b/ovos_gui_plugin_shell_companion/brightness.py index 143abc0..4a90cca 100644 --- a/ovos_gui_plugin_shell_companion/brightness.py +++ b/ovos_gui_plugin_shell_companion/brightness.py @@ -379,23 +379,57 @@ def stop_auto_night_mode(self): update_config("auto_nightmode", False) def get_suntimes(self) -> Tuple[datetime.datetime, datetime.datetime]: - location = Configuration()["location"] - lat = location["coordinate"]["latitude"] - lon = location["coordinate"]["longitude"] - tz = location["timezone"]["code"] - city = LocationInfo("Some city", "Some location", tz, lat, lon) + sunrise = self.config.get("sunrise_time", "auto") + sunset = self.config.get("sunset_time", "auto") + sunset_time = None + sunrise_time = None reference = now_local() # now_local() is tz aware - s = sun(city.observer, date=reference) - s2 = sun(city.observer, date=reference + timedelta(days=1)) - sunset_time = s["sunset"] - sunrise_time = s["sunrise"] - if reference > sunrise_time: # get next sunrise, today's already happened - sunrise_time = s2["sunrise"] - if reference > sunset_time: # get next sunset, today's already happened - sunset_time = s2["sunset"] - + # check if sunrise has been explicitly configured by user + if ":" in sunrise: + hours, mins = sunrise.split(":") + sunrise_time = datetime.datetime(hour=int(hours), + minute=int(mins), + day=reference.day, + month=reference.month, + year=reference.year, + tzinfo=reference.tzinfo) + if reference > sunrise_time: + sunrise_time += timedelta(days=1) + + # check if sunset has been explicitly configured by user + if ":" in sunset: + hours, mins = sunset.split(":") + sunset_time = datetime.datetime(hour=int(hours), + minute=int(mins), + day=reference.day, + month=reference.month, + year=reference.year, + tzinfo=reference.tzinfo) + if reference > sunset_time: + sunset_time += timedelta(days=1) + + # auto determine sunrise/sunset + if sunrise_time is None or sunset_time is None: + location = Configuration()["location"] + lat = location["coordinate"]["latitude"] + lon = location["coordinate"]["longitude"] + tz = location["timezone"]["code"] + city = LocationInfo("Some city", "Some location", tz, lat, lon) + + s = sun(city.observer, date=reference) + s2 = sun(city.observer, date=reference + timedelta(days=1)) + if not sunset_time: + sunset_time = s["sunset"] + if reference > sunset_time: # get next sunset, today's already happened + sunset_time = s2["sunset"] + if not sunrise_time: + sunrise_time = s["sunrise"] + if reference > sunrise_time: # get next sunrise, today's already happened + sunrise_time = s2["sunrise"] + + # info logs if self.sunrise_time is None or self.sunrise_time != sunrise_time: LOG.info(f"Sunrise time: {sunrise_time}") if self.sunset_time is None or self.sunset_time != sunset_time: