Skip to content

Commit

Permalink
feat: force store entity state when turning on and do not restore att…
Browse files Browse the repository at this point in the history
…ributes when state is 'off' (#145)
  • Loading branch information
hugobloem authored Dec 4, 2024
1 parent e7c8af8 commit 3567fd1
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions custom_components/stateful_scenes/StatefulScenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ def turn_on(self):
"Cannot find entity_id for: " + self.name + self._entity_id
)

# Store the current state of the entities
for entity_id in self.entities:
self.store_entity_state(entity_id)

self.hass.services.call(
domain="scene",
service="turn_on",
Expand Down Expand Up @@ -398,8 +402,13 @@ def check_all_states(self):

self._is_on = all(states)

def store_entity_state(self, entity_id, state):
"""Store the state of an entity."""
def store_entity_state(self, entity_id, state=None):
"""Store the state of an entity.
If the state is not provided, the current state of the entity is used.
"""
if state is None:
state = self.hass.states.get(entity_id)
self.restore_states[entity_id] = state

def restore(self):
Expand All @@ -408,7 +417,15 @@ def restore(self):
for entity_id, state in self.restore_states.items():
if state is None:
continue

# restore state
entities[entity_id] = {"state": state.state}

# do not restore attributes if the entity is off
if state.state == "off":
continue

# restore attributes
if state.domain in ATTRIBUTES_TO_CHECK:
entity_attrs = state.attributes
for attribute in ATTRIBUTES_TO_CHECK.get(state.domain):
Expand Down

0 comments on commit 3567fd1

Please sign in to comment.