Skip to content

Commit

Permalink
Fix service not disabling properly
Browse files Browse the repository at this point in the history
  • Loading branch information
zim514 committed Jan 9, 2023
1 parent 8385def commit de983f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 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="1.4.0~alpha2">
<addon id="script.service.hue" name="Hue Service" provider-name="zim514" version="1.4.0~alpha6">
<requires>
<import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.requests" version="2.27.1"/>
Expand Down
3 changes: 2 additions & 1 deletion script.service.hue/resources/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ def _service(monitor):
# check if service was just re-enabled and if so activate groups
prev_service_enabled = service_enabled
service_enabled = cache_get(f"{ADDONID}.service_enabled")

if service_enabled and not prev_service_enabled:
activate(light_groups)

# if service disabled, stop ambilight._ambi_loop thread
if not service_enabled:
AMBI_RUNNING.clear()

# process CACHEd waiting commands
# process CACHED waiting commands
action = cache_get(f"{ADDONID}.action")
if action:
_process_actions(action, light_groups)
Expand Down
72 changes: 35 additions & 37 deletions script.service.hue/resources/lib/lightgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import xbmcgui
from qhue import QhueException

import resources.lib.kodiutils
from resources.lib import ADDON, reporting, ADDONID
from resources.lib.kodiutils import convert_time, notification, cache_get, cache_set
from resources.lib.kodiutils import convert_time, notification, cache_get
from .language import get_string as _

STATE_STOPPED = 0
Expand All @@ -24,7 +23,6 @@
AUDIO = 1



class LightGroup(xbmc.Player):
def __init__(self, light_group_id, hue_connection, media_type, initial_state=STATE_STOPPED, video_info_tag=xbmc.InfoTagVideo):
self.light_group_id = light_group_id
Expand Down Expand Up @@ -73,6 +71,7 @@ def onAVStarted(self):
else:
self.video_info_tag = None

xbmc.log(f"[script.service.hue] onAVStarted: check_active_time: {self.check_active_time()}, check_already_active: {self.check_already_active(self.start_scene)}")
if (self.check_active_time() or self.check_already_active(self.start_scene)) and self.check_keep_lights_off_rule(self.start_scene) and self.start_behavior and self.media_type == self.playback_type():
self.run_action("play")

Expand Down Expand Up @@ -116,24 +115,27 @@ def onPlayBackEnded(self):
self.onPlayBackStopped()

def run_action(self, action):
if action == "play":
scene = self.start_scene
elif action == "pause":
scene = self.pause_scene
elif action == "stop":
scene = self.stop_scene
else:
xbmc.log(f"[script.service.hue] Unknown action type: {action}")
raise RuntimeError
try:
self.group0.action(scene=scene)
except QhueException as exc:
xbmc.log(f"[script.service.hue] run_action: Hue call fail: {exc.type_id}: {exc.message} {traceback.format_exc()}")
if "3" in exc.type_id or "7" in exc.type_id:
xbmc.log("[script.service.hue] Scene not found")
notification(_("Hue Service"), _("ERROR: Scene not found"), icon=xbmcgui.NOTIFICATION_ERROR)

service_enabled = cache_get(f"{ADDONID}.service_enabled")
if service_enabled:
if action == "play":
scene = self.start_scene
elif action == "pause":
scene = self.pause_scene
elif action == "stop":
scene = self.stop_scene
else:
reporting.process_exception(exc)
xbmc.log(f"[script.service.hue] Unknown action type: {action}")
raise RuntimeError
try:
self.group0.action(scene=scene)
except QhueException as exc:
xbmc.log(f"[script.service.hue] run_action: Hue call fail: {exc.type_id}: {exc.message} {traceback.format_exc()}")
if "3" in exc.type_id or "7" in exc.type_id:
xbmc.log("[script.service.hue] Scene not found")
notification(_("Hue Service"), _("ERROR: Scene not found"), icon=xbmcgui.NOTIFICATION_ERROR)
else:
reporting.process_exception(exc)

def activate(self):
xbmc.log(f"[script.service.hue] Activate group [{self.light_group_id}]. State: {self.state}")
Expand All @@ -158,28 +160,24 @@ def playback_type(self):
def check_active_time():
service_enabled = cache_get(f"{ADDONID}.service_enabled")
daylight = cache_get("script.service.hue.daylight")
# xbmc.log("[script.service.hue] Schedule: {}, daylightDisable: {}, daylight: {}, startTime: {}, endTime: {}".format(ADDON.getSettingBool("enableSchedule"), ADDON.getSettingBool("daylightDisable"), daylight,
# ADDON.getSettingBool("startTime"), ADDON.getSettingBool("endTime")))
xbmc.log("[script.service.hue] Schedule: {}, daylightDisable: {}, daylight: {}, startTime: {}, endTime: {}".format(ADDON.getSettingBool("enableSchedule"), ADDON.getSettingBool("daylightDisable"), daylight,
ADDON.getSettingString("startTime"), ADDON.getSettingString("endTime")))

if ADDON.getSettingBool("daylightDisable") and daylight:
xbmc.log("[script.service.hue] Disabled by daylight")
return False

if service_enabled:
if ADDON.getSettingBool("enableSchedule"):
start = convert_time(ADDON.getSettingString("startTime"))
end = convert_time(ADDON.getSettingString("endTime"))
now = datetime.datetime.now().time()
if (now > start) and (now < end):
# xbmc.log("[script.service.hue] Enabled by schedule")
return True
# xbmc.log("[script.service.hue] Disabled by schedule")
return False
# xbmc.log("[script.service.hue] Schedule not enabled")
return True

# xbmc.log("[script.service.hue] Service disabled")
return False
if ADDON.getSettingBool("enableSchedule"):
start = convert_time(ADDON.getSettingString("startTime"))
end = convert_time(ADDON.getSettingString("endTime"))
now = datetime.datetime.now().time()
if (now > start) and (now < end):
xbmc.log("[script.service.hue] Enabled by schedule")
return True
xbmc.log("[script.service.hue] Disabled by schedule")
return False
xbmc.log("[script.service.hue] Schedule not enabled")
return True

def check_video_activation(self, info_tag):
try:
Expand Down

0 comments on commit de983f5

Please sign in to comment.