Skip to content

Commit

Permalink
configurable sunset/sunrise times
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Aug 17, 2024
1 parent 09f7e8b commit 34998a8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
62 changes: 48 additions & 14 deletions ovos_gui_plugin_shell_companion/brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 34998a8

Please sign in to comment.