Skip to content

Commit

Permalink
Add sunset offset
Browse files Browse the repository at this point in the history
Fix "force on at sunset" option
  • Loading branch information
zim514 committed Apr 15, 2024
1 parent 9da53b7 commit ff9b41b
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 26 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.3">
<addon id="script.service.hue" name="Hue Service" provider-name="zim514" version="2.0.4">
<requires>
<import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.requests" version="2.27.1"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,34 @@ msgctxt "#31335"
msgid "Transition time (seconds):"
msgstr ""


#. Number of minutes before or after sunset to consider as sunset
msgctxt "#31336"
msgid "Sunset Offset:"
msgstr ""

#. Help text for sunset offset setting
msgctxt "#31337"
msgid "Adjust sunset back or forwards by this many minutes"
msgstr ""

#. Help text for "morning disable time" setting
msgctxt "#31338"
msgid "Time at which to disable the service in the morning"
msgstr ""

#. Help text for 'Disable during daytime'
msgctxt "#31339"
msgid "Disable the service between configured time and sunset"
msgstr ""

#. Help text for 'activate at sunset'
msgctxt "#31340"
msgid "Activate scenes at sunset, during playback"
msgstr ""



msgctxt "#30002"
msgid "Bridge overloaded, stopping ambilight"
msgstr ""
Expand All @@ -482,3 +510,4 @@ msgstr ""
msgctxt "#30028"
msgid "Configure Hue Home location to use Sunset time, defaulting to 19:00"
msgstr ""

68 changes: 56 additions & 12 deletions script.service.hue/resources/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sys
import threading
from datetime import datetime
from datetime import datetime, timedelta, date

import xbmc

Expand Down Expand Up @@ -191,29 +191,43 @@ def _run_morning(self):
def _run_sunset(self):
xbmc.log(f"[SCRIPT.SERVICE.HUE] in run_sunset(): Sunset. ")
cache_set("daytime", False)
self.hue_service.activate()
if self.settings_monitor.force_on_sunset:
self.hue_service.activate()


def _set_daytime(self):
now = datetime.now()
xbmc.log(f"[SCRIPT.SERVICE.HUE] _set_daytime(): Morning Time: {self.morning_time}, Now: {now.time()}, bridge.sunset: {self.bridge.sunset}")
xbmc.log(f"[SCRIPT.SERVICE.HUE] _set_daytime(): Morning Time: {type(self.morning_time)}, Now: {type(now.time())}, bridge.sunset: {type(self.bridge.sunset)}")
if self.morning_time <= now.time() < self.bridge.sunset:
cache_set("daytime", True)
xbmc.log(f"[SCRIPT.SERVICE.HUE] _set_daytime(): Morning Time: {self.morning_time}, Now: {now.time()}, bridge.sunset: {self.bridge.sunset}, Sunset offset: {self.settings_monitor.sunset_offset}")

Check warning on line 200 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)

# Convert self.bridge.sunset to a datetime object by combining it with today's date
sunset_datetime = datetime.combine(datetime.today(), self.bridge.sunset)

# Apply the sunset offset
sunset_with_offset = sunset_datetime + timedelta(minutes=self.settings_monitor.sunset_offset)

# Compare times
if self.morning_time <= now.time() < sunset_with_offset.time():
daytime=True
else:
cache_set("daytime", False)
daytime = False
cache_set("daytime", daytime)
xbmc.log(f"[SCRIPT.SERVICE.HUE] in _set_daytime(): Sunset with offset: {sunset_with_offset}, Daytime: {daytime} ")

Check warning on line 214 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)

def _task_loop(self):

while not self.settings_monitor.abortRequested() and not self.stop_timers.is_set():
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()
self.morning_time = self.settings_monitor.morning_time
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, self.bridge.sunset)
time_to_morning = self._time_until(now, self.morning_time)
time_to_sunset = self._time_until(now, sunset_datetime)
time_to_morning = self._time_until(now, morning_datetime)

if time_to_sunset <= 0 or time_to_sunset > time_to_morning:

# Morning is next
xbmc.log(f"[SCRIPT.SERVICE.HUE] Timers: Morning is next. wait_time: {time_to_morning}")
if self.settings_monitor.waitForAbort(time_to_morning):
Expand All @@ -234,3 +248,33 @@ def _time_until(current, target):
now = datetime(1, 1, 1, current.hour, current.minute, current.second)
then = datetime(1, 1, 1, target.hour, target.minute, target.second)
return (then - now).seconds

'''
def _task_loop(self):
while not self.settings_monitor.abortRequested() and not self.stop_timers.is_set():
now = datetime.now()
self.morning_time = self.settings_monitor.morning_time
time_to_sunset = self._time_until(now, self.bridge.sunset + timedelta(minutes=self.settings_monitor.sunset_offset))

Check warning on line 260 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_morning = self._time_until(now, self.morning_time)
if time_to_sunset <= 0 or time_to_sunset > time_to_morning:
# Morning is next
xbmc.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
xbmc.log(f"[SCRIPT.SERVICE.HUE] Timers: Sunset is next. wait_time: {time_to_sunset}")
if self.settings_monitor.waitForAbort(time_to_sunset):
break
self._run_sunset()
xbmc.log("[SCRIPT.SERVICE.HUE] Timers stopped")
'''


5 changes: 5 additions & 0 deletions script.service.hue/resources/lib/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def get_string(t):
_strings['reconnected'] = 30033
_strings['morning disable time:'] = 31334
_strings['transition time (seconds):'] = 31335
_strings['sunset offset:'] = 31336
_strings['adjust sunset back or forwards by this many minutes'] = 31337
_strings['time at which to disable the service in the morning'] = 31338
_strings['disable the service between configured time and sunset'] = 31339
_strings['activate scenes at sunset, during playback'] = 31340
_strings['bridge overloaded, stopping ambilight'] = 30002
_strings['bridge unauthorized, please reconfigure.'] = 30025
_strings['connection failed, retrying...'] = 30026
Expand Down
6 changes: 4 additions & 2 deletions script.service.hue/resources/lib/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ def reload_settings(self):
# scheduling settings

self.daylight_disable = ADDON.getSettingBool("daylightDisable")

Check notice on line 45 in script.service.hue/resources/lib/settings.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute daylight_disable defined outside __init__
self.schedule_enabled = ADDON.getSettingBool("enableSchedule")

self.force_on_sunset = ADDON.getSettingBool("forceOnSunset")

Check notice on line 46 in script.service.hue/resources/lib/settings.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute force_on_sunset defined outside __init__
self.morning_time = convert_time(ADDON.getSettingString("morningTime"))

Check notice on line 47 in script.service.hue/resources/lib/settings.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute morning_time defined outside __init__
self.sunset_offset = ADDON.getSettingNumber("sunsetOffset")

Check notice on line 48 in script.service.hue/resources/lib/settings.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute sunset_offset defined outside __init__

self.schedule_enabled = ADDON.getSettingBool("enableSchedule")

Check notice on line 50 in script.service.hue/resources/lib/settings.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute schedule_enabled defined outside __init__
self.schedule_start = convert_time(ADDON.getSettingString("startTime"))

Check notice on line 51 in script.service.hue/resources/lib/settings.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute schedule_start defined outside __init__
self.schedule_end = convert_time(ADDON.getSettingString("endTime"))

Check notice on line 52 in script.service.hue/resources/lib/settings.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute schedule_end defined outside __init__

Expand Down
53 changes: 42 additions & 11 deletions script.service.hue/resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -584,35 +584,52 @@
</category>
<category id="schedule" label="30511" help="">
<group id="1">
<setting id="forceOnSunset" type="boolean" label="30509" help="">
<setting id="forceOnSunset" type="boolean" label="30509" help="31340">
<level>1</level>
<default>True</default>
<control type="toggle"/>
</setting>
<setting id="daylightDisable" type="boolean" label="30508" help="">
<setting id="daylightDisable" type="boolean" label="30508" help="31339">
<level>1</level>
<default>False</default>
<control type="toggle"/>
</setting>
<setting id="morningTime" type="time" label="31334" help="">
<setting id="morningTime" type="time" label="31334" help="31338">
<level>2</level>
<default>08:00</default>
<control type="button" format="time">
<heading>31334</heading>
</control>
<dependencies>
<dependency type="enable">
<condition operator="is" setting="daylightDisable">true</condition>
</dependency>
</dependencies>

</setting>
<setting id="enable_if_already_active" type="boolean" label="30516" help="">
<level>1</level>
<default>False</default>
<control type="toggle"/>
</setting>
<setting id="keep_lights_off" type="boolean" label="30517" help="">


<setting id="sunsetOffset" type="number" label="31336" help="31337">
<level>1</level>
<default>False</default>
<control type="toggle"/>
<default>-30</default>
<constraints>
<minimum>-120</minimum>
<step>1</step>
<maximum>120</maximum>
</constraints>
<control type="slider" format="number">
<popup>false</popup>
</control>
<dependencies>
<dependency type="enable">
<condition operator="is" setting="daylightDisable">true</condition>
</dependency>
</dependencies>
</setting>


</group>

<group id="2">
<setting id="enableSchedule" type="boolean" label="30505" help="">
<level>1</level>
Expand Down Expand Up @@ -644,6 +661,20 @@
</control>
</setting>
</group>

<group id="3">
<setting id="enable_if_already_active" type="boolean" label="30516" help="">
<level>1</level>
<default>False</default>
<control type="toggle"/>
</setting>
<setting id="keep_lights_off" type="boolean" label="30517" help="">
<level>1</level>
<default>False</default>
<control type="toggle"/>
</setting>
</group>

</category>
<category id="ambilight" label="30523" help="30521">
<group id="1" label="30521">
Expand Down

0 comments on commit ff9b41b

Please sign in to comment.