Skip to content

Commit

Permalink
Fix sunset/morning calculation and daytime determination
Browse files Browse the repository at this point in the history
  • Loading branch information
zim514 committed May 5, 2024
1 parent 65606a4 commit 2f566ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion script.service.hue/addon.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<addon id="script.service.hue" name="Hue Service" provider-name="zim514" version="2.0.7">
<addon id="script.service.hue" name="Hue Service" provider-name="zim514" version="2.0.8">
<requires>
<import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.requests" version="2.31.0"/>
Expand Down
20 changes: 14 additions & 6 deletions script.service.hue/resources/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,31 @@ def _task_loop(self):

while not self.settings_monitor.abortRequested() and not self.stop_timers.is_set(): #todo: Update timers if sunset offset changes.

Check warning on line 218 in script.service.hue/resources/lib/core.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Line is longer than allowed by code style

Line is longer than allowed by code style (\> 120 columns)

now = datetime.now() + timedelta(seconds=1)
now = datetime.now() #+ timedelta(seconds=5)
today = date.today()
# Convert self.morning_time to a datetime object #todo: Do this in settings.py, or something

morning_datetime = datetime.combine(today, self.settings_monitor.morning_time)
# Convert self.bridge.sunset to a datetime object and apply the sunset offset
sunset_datetime = datetime.combine(today, self.bridge.sunset) + timedelta(minutes=self.settings_monitor.sunset_offset)

Check warning on line 225 in script.service.hue/resources/lib/core.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

Line is longer than allowed by code style

Line is longer than allowed by code style (\> 120 columns)

time_to_sunset = self._time_until(now, sunset_datetime)
time_to_morning = self._time_until(now, morning_datetime)
if sunset_datetime < now:
sunset_datetime += timedelta(days=1)
if morning_datetime < now:
morning_datetime += timedelta(days=1)

time_to_sunset = (sunset_datetime - now).total_seconds()
time_to_morning = (morning_datetime - now).total_seconds()

if time_to_sunset <= 0 or time_to_sunset > time_to_morning:
# Log the calculated times
log(f"[SCRIPT.SERVICE.HUE] Time to sunset: {time_to_sunset}, Time to morning: {time_to_morning}")

# Determine which event is next based on the time calculations
if time_to_morning < time_to_sunset:
# Morning is next
log(f"[SCRIPT.SERVICE.HUE] Timers: Morning is next. wait_time: {time_to_morning}")
if self.settings_monitor.waitForAbort(time_to_morning):
break
self._run_morning()

else:
# Sunset is next
log(f"[SCRIPT.SERVICE.HUE] Timers: Sunset is next. wait_time: {time_to_sunset}")
Expand Down

0 comments on commit 2f566ab

Please sign in to comment.