Skip to content

Commit

Permalink
stateengine: further issue handling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onkelandy committed Nov 27, 2024
1 parent 7f1434a commit cc78896
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions stateengine/StateEngineAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ def __repr__(self):
# value: Value of the set_(action_name) attribute
def update(self, value):
self.__byattr = value
_issue = {self._name: {'issue': None, 'attribute': self.__byattr,
_issue = {self._name: {'issue': None, 'attribute': [self.__byattr],
'issueorigin': [{'state': self._state.id, 'action': self._function}]}}
return _issue

Expand All @@ -938,7 +938,7 @@ def complete(self, evals_items=None, use=None):
self._abitem.set_variable('current.action_name', self._name)
self._abitem.set_variable('current.state_name', self._state.name)
self._scheduler_name = "{}-SeByAttrDelayTimer".format(self.__byattr)
_issue = {self._name: {'issue': None, 'attribute': self.__byattr,
_issue = {self._name: {'issue': None, 'attribute': [self.__byattr],
'issueorigin': [{'state': self._state.id, 'action': self._function}]}}
self._abitem.set_variable('current.action_name', '')
self._abitem.set_variable('current.state_name', '')
Expand Down
26 changes: 12 additions & 14 deletions stateengine/StateEngineActions.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ def update(self, attribute, value):
# If we do not have the action yet (mode-attribute before action-attribute), ...
self.__unassigned_modes[name] = value
else:
_val, _issue = self.__actions[name].update_mode(value)
_issue = self.__actions[name].update_mode(value)
if _issue:
_issue_list.append(_issue)
_issue, _action = self.__check_mode_setting(name, _val, self.__actions[name].function, self.__actions[name])
_issue, _action = self.__check_mode_setting(name, value, self.__actions[name].function, self.__actions[name])
if _issue:
_issue_list.append(_issue)
if _action:
Expand All @@ -194,7 +194,7 @@ def update(self, attribute, value):
_count += 1
_issue = StateEngineTools.flatten_list(_issue_list)
except ValueError as ex:
_issue = {name: {'issue': [str(ex)], 'attribute': func, 'issueorigin': [{'state': self.__state.id, 'action': self.__actions[name].function}], 'ignore': True}}
_issue = {name: {'issue': [str(ex)], 'attribute': [func], 'issueorigin': [{'state': self.__state.id, 'action': self.__actions[name].function}], 'ignore': True}}
if name in self.__actions:
del self.__actions[name]
self._log_warning("Ignoring action {0} because: {1}", attribute, ex)
Expand All @@ -208,8 +208,7 @@ def __check_force_setting(self, name, value, function):
if function not in ["set", "force"]:
_issue = {
name: {'issue': ['Parameter force not supported for this function'],
'attribute': 'force', 'issueorigin': [{'state': self.__state.id, 'action': function}]}}
_issue = "Parameter 'force' not supported for this function"
'attribute': ['force'], 'issueorigin': [{'state': self.__state.id, 'action': function}]}}
self._log_warning("Attribute 'se_action_{0}': Parameter 'force' not supported "
"for function '{1}'", name, function)
elif value and function == "set":
Expand All @@ -230,7 +229,7 @@ def __check_mode_setting(self, name, value, function, action):
_issue = None
# Parameter mode is supported only for type "remove"
if "remove" not in function:
_issue = {name: {'issue': ['Parameter mode only supported for remove function'], 'attribute': 'mode',
_issue = {name: {'issue': ['Parameter mode only supported for remove function'], 'attribute': ['mode'],
'issueorigin': [{'state': self.__state.id, 'action': function}]}}
self._log_warning("Attribute 'se_action_{0}': Parameter 'mode' not supported for function '{1}'",
name, function)
Expand All @@ -246,7 +245,7 @@ def __check_mode_setting(self, name, value, function, action):
self._log_info("Attribute 'se_action_{0}': Function 'remove' changed to '{1}'", name, value)
else:
_issue = {
name: {'issue': ['Parameter {} not allowed for mode!'.format(value)], 'attribute': 'mode',
name: {'issue': ['Parameter {} not allowed for mode!'.format(value)], 'attribute': ['mode'],
'issueorigin': [{'state': self.__state.id, 'action': function}]}}
self._log_warning(
"Attribute 'se_action_{0}': Parameter '{1}' for 'mode' is wrong - can only be {2}",
Expand Down Expand Up @@ -365,9 +364,9 @@ def __handle_combined_action_attribute(self, name, value_list):
def remove_action(e):
if name in self.__actions:
del self.__actions[name]
i = {name: {'issue': [str(e)], 'attribute': 'unknown', 'issueorigin': [{'state': self.__state.id, 'action': parameter['function']}], 'ignore': True}}
i = {name: {'issue': [str(e)], 'attribute': [f'se_action_{name}'], 'issueorigin': [{'state': self.__state.id, 'action': parameter['function']}], 'ignore': True}}
_issue_list.append(i)
self._log_warning("Ignoring action {0} because: {1}", name, e)
self._log_warning("Removed action {0} because: {1}.", name, e)

parameter = {'function': None, 'force': None, 'repeat': None, 'delay': 0, 'order': None, 'nextconditionset': None, 'conditionset': None,
'previousconditionset': None, 'previousstate_conditionset': None, 'mode': None, 'instanteval': None, 'mindelta': None, 'minagedelta': None}
Expand Down Expand Up @@ -395,18 +394,18 @@ def remove_action(e):
else:
parameter[key] = val
except Exception as ex:
remove_action("Problem with entry {} for action {}: {}".format(entry, name, ex))
remove_action("Problem with entry {}: {}".format(entry, ex))
if _issue_list:
return _issue_list
parameter['action'] = name

# function given and valid?
if parameter['function'] is None:
remove_action("Attribute 'se_action_{0}: Parameter 'function' must be set!".format(name))
remove_action("Parameter 'function' must be set!")
return _issue_list
if parameter['function'] not in ('set', 'force', 'run', 'byattr', 'trigger', 'special',
'add', 'remove', 'removeall', 'removefirst', 'removelast'):
remove_action("Attribute 'se_action_{0}: Invalid value '{1}' for parameter 'function'!".format(name, parameter['function']))
remove_action("Invalid value '{}' for parameter 'function'!".format(parameter['function']))
return _issue_list

_issue, parameter['function'] = self.__check_force_setting(name, parameter['force'], parameter['function'])
Expand Down Expand Up @@ -571,8 +570,7 @@ def remove_action(e):
# noinspection PyMethodMayBeStatic
def __raise_missing_parameter_error(self, parameter, param_name):
if param_name not in parameter or parameter[param_name] is None:
raise ValueError("Attribute 'se_action_{0}: Parameter '{1}' must be set for "
"function '{2}'!".format(parameter['action'], param_name, parameter['function']))
raise ValueError("Parameter '{0}' must be set for function '{1}'!".format(param_name, parameter['function']))

# Check the actions optimize and complete them
# state: state (item) to read from
Expand Down
13 changes: 12 additions & 1 deletion stateengine/StateEngineState.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,19 +592,29 @@ def filter_issues(input_dict):
return
for itm, dct in action_status.items():
if itm not in self.__action_status[actn_type]:
self.__action_status[actn_type].update({itm: dct})
self.__action_status[actn_type][itm] = dct
else:
for key, value in dct.items():
if key not in self.__action_status[actn_type][itm]:
self.__action_status[actn_type][itm][key] = value

for (itm, dct) in action_status.items():
issues = dct.get('issue')
attributes = dct.get('attribute')
if not isinstance(attributes, list):
attributes = [attributes]
if not isinstance(self.__action_status[actn_type][itm].get('attribute'), list):
self.__action_status[actn_type][itm]['attribute'] = [self.__action_status[actn_type][itm].get('attribute')]
if issues:
if isinstance(issues, list):
attributes = (attributes + [None] * len(issues))[:len(issues)]
for i, issue in enumerate(issues):
if issue not in self.__action_status[actn_type][itm]['issue']:
self.__action_status[actn_type][itm]['issue'].append(issue)
self.__action_status[actn_type][itm]['attribute'].append(attributes[i])

flattened_dict = {}

for key, action_type_dict in self.__action_status.items():
# Iterate through the inner dictionaries
for inner_key, nested_dict in action_type_dict.items():
Expand All @@ -616,6 +626,7 @@ def filter_issues(input_dict):
flattened_dict[inner_key].update(nested_dict)

self.__used_attributes = deepcopy(flattened_dict)

self.__action_status = filter_issues(self.__action_status)
self._abitem.update_attributes(self.__unused_attributes, self.__used_attributes)

Expand Down

0 comments on commit cc78896

Please sign in to comment.