Skip to content

Commit

Permalink
fix activation conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
zim514 committed Dec 27, 2023
1 parent 214763b commit 4f611b0
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 47 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.5.0">
<addon id="script.service.hue" name="Hue Service" provider-name="zim514" version="1.5.1">
<requires>
<import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.requests" version="2.27.1"/>
Expand Down
150 changes: 104 additions & 46 deletions script.service.hue/resources/lib/lightgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ def reload_settings(self):
self.music_video_setting = ADDON.getSettingBool("video_MusicVideo")

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

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute music_video_setting defined outside __init__
self.pvr_setting = ADDON.getSettingBool("video_PVR")

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

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute pvr_setting defined outside __init__
self.other_setting = ADDON.getSettingBool("video_Other")

Check notice on line 53 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute other_setting defined outside __init__
self.enable_if_already_active = ADDON.getSettingBool('enable_if_already_active')
self.keep_lights_off = ADDON.getSettingBool('keep_lights_off')

self.skip_time_check_if_light_on = ADDON.getSettingBool('enable_if_already_active')

Check notice on line 54 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute skip_time_check_if_light_on defined outside __init__
self.skip_scene_if_all_off = ADDON.getSettingBool('keep_lights_off')

Check notice on line 55 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute skip_scene_if_all_off defined outside __init__

if type(self) is LightGroup:
# Load LightGroup specific settings
Expand All @@ -71,7 +70,9 @@ def reload_settings(self):
self.stop_transition = int(ADDON.getSettingNumber(f"group{self.light_group_id}_stopTransition") * 1000)

Check notice on line 70 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

An instance attribute is defined outside `__init__`

Instance attribute stop_transition defined outside __init__

xbmc.log(f"[script.service.hue] LightGroup[{self.light_group_id}] Reloaded settings. Group enabled: {self.enabled}, Bridge connected: {self.bridge.connected}, mediaType: {self.media_type}")

Check notice on line 72 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (201 \> 120 characters)

def onAVStarted(self):

self.state = STATE_PLAYING
self.last_media_type = self._playback_type()
xbmc.log(f"[script.service.hue] LightGroup[{self.light_group_id}] onPlaybackStarted. Group enabled: {self.enabled}, Bridge connected: {self.bridge.connected}, mediaType: {self.media_type}")

Check notice on line 78 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (197 \> 120 characters)
Expand Down Expand Up @@ -185,29 +186,7 @@ class ActivationChecker:
def __init__(self, parent):
self.parent = parent

def _is_within_schedule(self):
# Fetch daytime status
daytime = cache_get("daytime")
# Check if daylight disable setting is on and it's daytime
if self.parent.daylight_disable and daytime:
xbmc.log("[script.service.hue] Disabled by daytime")
return False

# Check if schedule setting is enabled
if self.parent.schedule_enabled:
# Check if current time is within start and end times
if self.parent.schedule_start < datetime.now().time() < self.parent.schedule_end:
xbmc.log("[script.service.hue] Enabled by schedule")
return True
else:
xbmc.log("[script.service.hue] Disabled by schedule")
return False

xbmc.log("[script.service.hue] Schedule not enabled, ignoring")
return True

def _is_video_activation_valid(self):

def _video_activation_rules(self):
# Fetch video info tag
info_tag = self.parent.video_info_tag
# Get duration in minutes
Expand All @@ -220,36 +199,63 @@ def _is_video_activation_valid(self):

# Check if file is a PVR file
is_pvr = file_name[0:3] == "pvr"
# Check if duration is above minimum or if it's a PVR file, and if media type matches settings
xbmc.log(f"[script.service.hue] _is_video_activation_valid settings: minimum_duration: {self.parent.minimum_duration}, movie_setting: {self.parent.movie_setting}, episode_setting: {self.parent.episode_setting}, music_video_setting: {self.parent.music_video_setting}, pvr_setting: {self.parent.pvr_setting}, other_setting: {self.parent.other_setting}")
xbmc.log(f"[script.service.hue] _is_video_activation_valid values: duration: {duration}, is_pvr: {is_pvr}, media_type: {media_type}, file_name: {file_name}")

if ((duration >= self.parent.minimum_duration or is_pvr) and
((self.parent.movie_setting and media_type == "movie") or
(self.parent.episode_setting and media_type == "episode") or
(self.parent.music_video_setting and media_type == "MusicVideo") or
(self.parent.pvr_setting and is_pvr) or
(self.parent.other_setting and media_type not in ["movie", "episode", "MusicVideo"] and not is_pvr))):
xbmc.log("[script.service.hue] _is_video_activation_valid activation: True")

# Log settings and values
xbmc.log(f"[script.service.hue] _video_activation_rules settings: minimum_duration: {self.parent.minimum_duration}, movie_setting: {self.parent.movie_setting}, episode_setting: {self.parent.episode_setting}, music_video_setting: {self.parent.music_video_setting}, pvr_setting: {self.parent.pvr_setting}, other_setting: {self.parent.other_setting}")

Check notice on line 204 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (362 \> 120 characters)
xbmc.log(f"[script.service.hue] _video_activation_rules values: duration: {duration}, is_pvr: {is_pvr}, media_type: {media_type}, file_name: {file_name}")

Check notice on line 205 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (166 \> 120 characters)

# Check if media type matches settings
media_type_match = ((self.parent.movie_setting and media_type == "movie") or
(self.parent.episode_setting and media_type == "episode") or
(self.parent.music_video_setting and media_type == "MusicVideo") or
(self.parent.pvr_setting and is_pvr) or
(self.parent.other_setting and media_type not in ["movie", "episode", "MusicVideo"] and not is_pvr))

Check notice on line 212 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (132 \> 120 characters)

if duration >= self.parent.minimum_duration and media_type_match:
xbmc.log("[script.service.hue] _video_activation_rules activation: True")
return True

xbmc.log("[script.service.hue] _is_video_activation_valid activation: False")
xbmc.log("[script.service.hue] _video_activation_rules activation: False")
return False

def _is_scene_light_already_active(self, scene_id):
# Fetch all light states
all_light_states = self.parent.bridge.make_api_request("GET", "light")
def _is_within_schedule(self):
# Check if daylight disable setting is on
if self.parent.daylight_disable:
# Fetch daytime status
daytime = cache_get("daytime")
# Check if it's daytime
if daytime:
xbmc.log("[script.service.hue] Disabled by daytime")
return False

# Check if schedule setting is enabled
if self.parent.schedule_enabled:
xbmc.log(f"[script.service.hue] Schedule enabled: {self.parent.schedule_enabled}, start: {self.parent.schedule_start}, end: {self.parent.schedule_end}")

Check notice on line 233 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (168 \> 120 characters)
# Check if current time is within start and end times
if self.parent.schedule_start < datetime.now().time() < self.parent.schedule_end:
xbmc.log("[script.service.hue] _is_within_schedule: True, Enabled by schedule")
return True
else:
xbmc.log("[script.service.hue] _is_within_schedule. False, Not within schedule")
return False

# If schedule is not enabled, always return True
xbmc.log("[script.service.hue] _is_within_schedule: True, Schedule not enabled")
return True

def skip_time_check_if_light_on(self, scene_id, all_light_states):
if not self.parent.enable_if_already_active:
xbmc.log("[script.service.hue] _is_scene_already_active: Not enabled")
return False

# Find the current scene from the scene data
current_scene = next((scene for scene in self.parent.bridge.scene_data['data'] if scene['id'] == scene_id), None)

Check notice on line 252 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (125 \> 120 characters)
#xbmc.log(f"[script.service.hue] _is_scene_already_active: Current scene: {current_scene}")
if not current_scene:
xbmc.log("[script.service.hue] _is_scene_already_active: Current scene not found in scene data")
return False

# Check if any light in the current scene is on
for action in current_scene['actions']:
#xbmc.log(f"[script.service.hue] _is_scene_already_active: Checking light {action['target']['rid']} in the scene")
light_id = action['target']['rid']
light_state = next((state for state in all_light_states['data'] if state['id'] == light_id), None)
if light_state and 'on' in light_state and light_state['on']['on']:
Expand All @@ -259,10 +265,62 @@ def _is_scene_light_already_active(self, scene_id):
xbmc.log("[script.service.hue] _is_scene_already_active: No lights in the scene are on")
return False

def skip_scene_if_all_off(self, scene_id, all_light_states):
# Find the current scene from the scene data
current_scene = next((scene for scene in self.parent.bridge.scene_data['data'] if scene['id'] == scene_id), None)

Check notice on line 270 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (125 \> 120 characters)
if not current_scene:
xbmc.log("[script.service.hue] _is_any_light_off: Current scene not found in scene data")
return False

# Check if any light in the current scene is on
for action in current_scene['actions']:
light_id = action['target']['rid']
light_state = next((state for state in all_light_states['data'] if state['id'] == light_id), None)
if light_state and 'on' in light_state and light_state['on']['on']:
xbmc.log(f"[script.service.hue] _is_any_light_off: Light {light_id} in the scene is on")
return True

return False

def validate(self, scene=None):
xbmc.log(f"[script.service.hue] LightGroup[{self.parent.light_group_id}] ActivationChecker.validate(): scene: {scene}, media_type: {self.parent.media_type}, skip_time_check_if_light_on: {self.parent.skip_time_check_if_light_on}, skip_scene_if_all_off: {self.parent.skip_scene_if_all_off}")

Check notice on line 286 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (301 \> 120 characters)

all_light_states = None
if scene and (self.parent.skip_time_check_if_light_on or self.parent.skip_scene_if_all_off):
# Fetch all light states
all_light_states = self.parent.bridge.make_api_request("GET", "light")
#xbmc.log(f"[script.service.hue] validate: all_light_states {all_light_states}")

Check notice on line 292 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E265 block comment should start with '# '

if self.parent.media_type == VIDEO and scene:
return (self._is_within_schedule() and self._is_video_activation_valid()) or self._is_scene_light_already_active(scene)
if self.parent.skip_scene_if_all_off and not self.skip_scene_if_all_off(scene, all_light_states):
xbmc.log("[script.service.hue] validate: All lights are off, not activating scene")
return False
if not (self._is_within_schedule() and self._video_activation_rules()):
xbmc.log("[script.service.hue] validate: Not within schedule or video activation rules not met, not activating scene")

Check notice on line 299 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (138 \> 120 characters)
return False
xbmc.log("[script.service.hue] validate: Activating scene for VIDEO")
return True

elif self.parent.media_type == VIDEO: # if no scene is set, use the default activation. This is the case for ambilight.

Check notice on line 304 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (132 \> 120 characters)
return self._is_within_schedule() and self._is_video_activation_valid()
if not (self._is_within_schedule() and self._video_activation_rules()):
xbmc.log("[script.service.hue] validate: Not within schedule or video activation rules not met, not activating scene")

Check notice on line 306 in script.service.hue/resources/lib/lightgroup.py

View workflow job for this annotation

GitHub Actions / Qodana for Python

PEP 8 coding style violation

PEP 8: E501 line too long (138 \> 120 characters)
return False
xbmc.log("[script.service.hue] validate: Activating scene for VIDEO")
return True

elif self.parent.media_type == AUDIO and scene:
if self.parent.skip_scene_if_all_off and not self.skip_scene_if_all_off(scene, all_light_states):
xbmc.log("[script.service.hue] validate: All lights are off, not activating scene")
return False
if not self._is_within_schedule():
xbmc.log("[script.service.hue] validate: Not within schedule, not activating scene")
return False
xbmc.log("[script.service.hue] validate: Activating scene for AUDIO media type")
return True

elif self.parent.media_type == AUDIO:
return self._is_within_schedule() and self._is_scene_light_already_active(scene)
if not self._is_within_schedule():
xbmc.log("[script.service.hue] validate: Not within schedule, not activating scene")
return False
xbmc.log("[script.service.hue] validate: Activating scene for AUDIO")
return True

0 comments on commit 4f611b0

Please sign in to comment.