Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add callback functions to scene registration #113

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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