Skip to content

Commit

Permalink
stateengine plugin: improve on_pass actions
Browse files Browse the repository at this point in the history
  • Loading branch information
onkelandy committed Sep 18, 2024
1 parent df90163 commit 2a0b5cc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
43 changes: 34 additions & 9 deletions stateengine/StateEngineItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def logger(self):
def instant_leaveaction(self):
return self.__instant_leaveaction.get()

@property
def last_run(self):
return self.__last_run

@last_run.setter
def last_run(self, value_dict):
self.__last_run.update(value_dict)

@property
def default_instant_leaveaction(self):
return self.__default_instant_leaveaction.get()
Expand Down Expand Up @@ -204,6 +212,8 @@ def __init__(self, smarthome, item, se_plugin):
self.__active_schedulers = []
self.__release_info = {}
self.__cache = {}
self.__last_run = {}
self.__pass_repeat = {}
self.__default_instant_leaveaction = StateEngineValue.SeValue(self, "Default Instant Leave Action", False, "bool")
self.__instant_leaveaction = StateEngineValue.SeValue(self, "Instant Leave Action", False, "num")
try:
Expand Down Expand Up @@ -640,6 +650,7 @@ def update_current_to_empty(d):

# find new state
_leaveactions_run = False
_pass_state = None

if _instant_leaveaction >= 1 and caller != "Released_by Retrigger":
evaluated_instant_leaveaction = True
Expand All @@ -649,7 +660,7 @@ def update_current_to_empty(d):
_previousstate_conditionset_name = ''

update_current_to_empty(self.__webif_infos)
self.__logger.develop("Resetted current info for webif info. It is now: {}", self.__webif_infos)
self.__logger.develop("Reset current info for webif info. It is now: {}", self.__webif_infos)
for state in self.__states:
if not self.__ab_alive:
self.__logger.debug("StateEngine Plugin not running (anymore). Stop state evaluation.")
Expand All @@ -673,14 +684,25 @@ def update_current_to_empty(d):
self.__conditionsets.update(
{state.state_item.property.path: [_last_conditionset_id, _last_conditionset_name]})
# New state is different from last state

if result is False and last_state == state and evaluated_instant_leaveaction is True:
self.__logger.info("Leaving {0} ('{1}'). Running actions immediately.", last_state.id,
last_state.name)
last_state.run_leave(self.__repeat_actions.get())
_leaveactions_run = True
if result is False and last_state == state:
if evaluated_instant_leaveaction is True:
self.__logger.info("Leaving {0} ('{1}'). Running actions immediately.", last_state.id,
last_state.name)
last_state.run_leave(self.__repeat_actions.get())
_leaveactions_run = True
elif result is False and last_state != state and state.actions_pass.count() > 0:
_pass_state = state
state.run_pass(self.__pass_repeat.get(state, False), self.__repeat_actions.get())
_key_pass = ['{}'.format(last_state.id), 'pass']
self.update_webif(_key_pass, True)
self.__pass_repeat.update({state: True})
if result is True:
new_state = state
for repeat_state in self.__pass_repeat:
if new_state.order < repeat_state.order:
self.__pass_repeat.update({repeat_state: False})
_key_pass = ['{}'.format(repeat_state.id), 'pass']
self.update_webif(_key_pass, False)
break

# no new state -> stay
Expand Down Expand Up @@ -743,10 +765,12 @@ def update_current_to_empty(d):
_key_leave = ['{}'.format(last_state.id), 'leave']
_key_stay = ['{}'.format(last_state.id), 'stay']
_key_enter = ['{}'.format(last_state.id), 'enter']
_key_pass = ['{}'.format(last_state.id), 'pass']

self.update_webif(_key_leave, True)
self.update_webif(_key_stay, False)
self.update_webif(_key_enter, False)
self.update_webif(_key_pass, False)
self.__handle_releasedby(new_state, last_state, _instant_leaveaction)

if self.update_lock.locked():
Expand Down Expand Up @@ -817,16 +841,19 @@ def update_current_to_empty(d):
new_state.id, new_state.name, _last_conditionset_id, _last_conditionset_name)

new_state.run_enter(self.__repeat_actions.get())

self.__laststate_set(new_state)
self.__previousstate_set(last_state)
if _leaveactions_run is True and self.__ab_alive:
_key_leave = ['{}'.format(last_state.id), 'leave']
_key_stay = ['{}'.format(last_state.id), 'stay']
_key_enter = ['{}'.format(last_state.id), 'enter']
_key_pass = ['{}'.format(last_state.id), 'pass']

self.update_webif(_key_leave, True)
self.update_webif(_key_stay, False)
self.update_webif(_key_enter, False)
self.update_webif(_key_pass, False)

self.__logger.debug("State evaluation finished")
all_released_by = self.__handle_releasedby(new_state, last_state, _instant_leaveaction)
Expand Down Expand Up @@ -1428,8 +1455,6 @@ def __update_check_can_enter(self, state, instant_leaveaction, refill=True):
if refill:
state.refill()
can_enter = state.can_enter()
if can_enter[0] is False:
state.run_pass(self.__repeat_actions.get())
return can_enter
except Exception as ex:
self.__logger.warning("Problem with currentstate {0}. Error: {1}", state.id, ex)
Expand Down
9 changes: 4 additions & 5 deletions stateengine/StateEngineState.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def run_stay(self, allow_item_repeat: bool):

# run actions when passing the state
# item_allow_repeat: Is repeating actions generally allowed for the item?
def run_pass(self, allow_item_repeat: bool):
def run_pass(self, is_repeat: bool, allow_item_repeat: bool):
self._log_info("Passing state {}, running pass actions.", self.id)
self._log_increase_indent()
_key_leave = ['{}'.format(self.id), 'leave']
Expand All @@ -348,7 +348,7 @@ def run_pass(self, allow_item_repeat: bool):
self._abitem.update_webif(_key_stay, False)
self._abitem.update_webif(_key_enter, False)
self._abitem.update_webif(_key_pass, True)
self.__actions_pass.execute(True, allow_item_repeat, self)
self.__actions_pass.execute(is_repeat, allow_item_repeat, self)
self._log_decrease_indent(50)

# run actions when leaving the state
Expand Down Expand Up @@ -642,7 +642,6 @@ def update_action_status(action_status, actiontype):
use = StateEngineTools.flatten_list(use)
self.__fill_list(use, recursion_depth, se_use, use)
# Get action sets and condition sets
self._log_develop("Use is {}", use)
parent_item = item_state.return_parent()
if parent_item == Items.get_instance():
parent_item = None
Expand Down Expand Up @@ -697,9 +696,9 @@ def update_action_status(action_status, actiontype):
if child_name in action_mapping:
action_name, action_method = action_mapping[child_name]
for attribute in child_item.conf:
self._log_develop("Filling state with {} action named {} based on {}", child_name, attribute, state)
self._log_develop("Filling state with {} action named {} for state {} with config {}", child_name, attribute, state.id, child_item.conf)
_action_counts[action_name] += 1
_, _action_status = action_method.update(attribute, child_item.conf[attribute])
_, _action_status = action_method.update(attribute, child_item.conf.get(attribute))
if _action_status:
update_action_status(_action_status, action_name)
self._abitem.update_action_status(self.__action_status)
Expand Down

0 comments on commit 2a0b5cc

Please sign in to comment.