Skip to content

Commit

Permalink
Catch assertion fail for sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
acockburn committed Jan 30, 2025
1 parent 6370300 commit 93ff6e4
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions appdaemon/sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def __init__(self, ad: "AppDaemon"):

async def run_sequence_service(self, namespace, domain, service, kwargs):
if "entity_id" not in kwargs:
self.logger.warning("entity_id not given in service call, so will not be executing %s", service)
self.logger.warning(
"entity_id not given in service call, so will not be executing %s", service)
return

entity_id = kwargs["entity_id"]
Expand Down Expand Up @@ -134,7 +135,8 @@ async def prep_sequence(self, _name: str, namespace: str, sequence: str | list[s
if isinstance(sequence, str):
entity_id = sequence
if self.AD.state.entity_exists("rules", entity_id) is False:
self.logger.warning('Unknown sequence "%s" in run_sequence()', sequence)
self.logger.warning(
'Unknown sequence "%s" in run_sequence()', sequence)
return None

entity = await self.AD.state.get_state("_services", "rules", sequence, attribute="all")
Expand All @@ -146,7 +148,8 @@ async def prep_sequence(self, _name: str, namespace: str, sequence: str | list[s
#
# Assume it's a list with the actual commands in it
#
assert isinstance(sequence, list) and all(isinstance(s, str) for s in sequence)
assert isinstance(sequence, list) and all(
isinstance(s, str) for s in sequence)
entity_id = "sequence.{}".format(uuid.uuid4().hex)
# Create an ephemeral entity for it
ephemeral_entity = True
Expand Down Expand Up @@ -191,7 +194,8 @@ async def do_steps(self,

elif command == "wait_state":
if ephemeral_entity is True:
self.logger.warning("Cannot process command 'wait_state', as not supported in sequence")
self.logger.warning(
"Cannot process command 'wait_state', as not supported in sequence")
continue

_, entity_name = entity_id.split(".")
Expand All @@ -200,7 +204,8 @@ async def do_steps(self,
wait_entity = parameters.get("entity_id")

if wait_entity is None:
self.logger.warning("Cannot process command 'wait_state', as entity_id not given")
self.logger.warning(
"Cannot process command 'wait_state', as entity_id not given")
continue

state = parameters.get("state")
Expand All @@ -209,17 +214,20 @@ async def do_steps(self,
timeout = parameters.get("timeout", 15 * 60)

# now we create the wait entity object
entity_object = Entity(self.logger, self.AD, name, ns, wait_entity)
entity_object = Entity(
self.logger, self.AD, name, ns, wait_entity)
if not entity_object.exists():
self.logger.warning(
f"Waiting for an entity {wait_entity}, in sequence {entity_name}, that doesn't exist"
f"Waiting for an entity {wait_entity}, in sequence {
entity_name}, that doesn't exist"
)

try:
await entity_object.wait_state(state, attribute, duration, timeout)
except TimeOutException:
self.logger.warning(
f"{entity_name} sequence wait for {wait_entity} timed out, so continuing sequence"
f"{entity_name} sequence wait for {
wait_entity} timed out, so continuing sequence"
)

else:
Expand All @@ -228,11 +236,18 @@ async def do_steps(self,
params = copy.deepcopy(parameters)
await self.AD.services.call_service(ns, domain, service, entity_id, params)

if isinstance(loop_step, dict): # we need to loop this command multiple times
# we need to loop this command multiple times
if isinstance(loop_step, dict):
await self.loop_step(ns, command, parameters, loop_step)

if loop is not True:
break
except Exception:
self.logger.error("-" * 60)
self.logger.error("Unexpected error when attempting do_steps()")
self.logger.error("-" * 60)
self.logger.error(traceback.format_exc())
self.logger.error("-" * 60)
finally:
await self.AD.state.set_state("_sequences", "rules", entity_id, state="idle")

Expand Down

0 comments on commit 93ff6e4

Please sign in to comment.