Skip to content

Commit

Permalink
fix: Add callback functions to scene registration (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugobloem authored Oct 2, 2024
2 parents 0d40246 + 827d3f3 commit e6738b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 6 additions & 1 deletion custom_components/stateful_scenes/StatefulScenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def __init__(self, hass: HomeAssistant, scene_conf: dict) -> None:
self._debounce_time: float = 0

self.callback = None
self.callback_funcs = {}
self.schedule_update = None
self.states = {entity_id: False for entity_id in self.entities}
self.restore_states = {entity_id: None for entity_id in self.entities}
Expand Down Expand Up @@ -295,8 +296,12 @@ def set_restore_on_deactivate(self, restore_on_deactivate):
"""Set the restore on deactivate flag."""
self._restore_on_deactivate = restore_on_deactivate

def register_callback(self, state_change_func, schedule_update_func):
def register_callback(self):
"""Register callback."""
schedule_update_func = self.callback_funcs.get("schedule_update_func", None)
state_change_func = self.callback_funcs.get("state_change_func", None)
if schedule_update_func is None or state_change_func is None:
raise ValueError("No callback functions provided for scene.")
self.schedule_update = schedule_update_func
self.callback = state_change_func(
self.hass, self.entities.keys(), self.update_callback
Expand Down
9 changes: 5 additions & 4 deletions custom_components/stateful_scenes/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def __init__(self, scene) -> None:
self._icon = scene.icon
self._attr_unique_id = f"stateful_{scene.id}"

self._scene.callback_funcs = {
"state_change_func":async_track_state_change_event,
"schedule_update_func":self.schedule_update_ha_state
}
self.register_callback()

@property
Expand Down Expand Up @@ -156,10 +160,7 @@ def update(self) -> None:

def register_callback(self) -> None:
"""Register callback to update hass when state changes."""
self._scene.register_callback(
state_change_func=async_track_state_change_event,
schedule_update_func=self.schedule_update_ha_state,
)
self._scene.register_callback()

def unregister_callback(self) -> None:
"""Unregister callback."""
Expand Down

0 comments on commit e6738b3

Please sign in to comment.