Skip to content

Commit

Permalink
stateengine plugin: minor code and logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
onkelandy committed Jul 30, 2024
1 parent 5b00ffe commit 9a358f8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions stateengine/StateEngineAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def real_execute(self, state, actionname: str, namevar: str = "", repeat_text: s
self._log_decrease_indent()
text = "{0}: Problem evaluating '{1}': {2}."
self.update_webif_actionstatus(state, self._name, 'False', 'Problem evaluating: {}'.format(ex))
self._log_error(text.format(actionname, StateEngineTools.get_eval_name(self.__eval), ex))
self._log_error(text, actionname, StateEngineTools.get_eval_name(self.__eval), ex)
else:
try:
if returnvalue:
Expand All @@ -967,7 +967,7 @@ def real_execute(self, state, actionname: str, namevar: str = "", repeat_text: s
self._log_decrease_indent()
self.update_webif_actionstatus(state, self._name, 'False', 'Problem calling: {}'.format(ex))
text = "{0}: Problem calling '{0}': {1}."
self._log_error(text.format(actionname, StateEngineTools.get_eval_name(self.__eval), ex))
self._log_error(text, actionname, StateEngineTools.get_eval_name(self.__eval), ex)

def get(self):
result = {'function': str(self._function), 'eval': str(self.__eval),
Expand Down
21 changes: 16 additions & 5 deletions stateengine/StateEngineItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,9 @@ def print_readable_dict(data):
else:
formatted_entries.append(item)
if formatted_entries:
self.__logger.info(f"- {key}: {', '.join(formatted_entries)}")
self.__logger.info("- {}: {}", key, ', '.join(formatted_entries))
else:
self.__logger.info(f"- {key}: {value}")
self.__logger.info("- {}: {}", key, value)
def list_issues(v):
_issuelist = StateEngineTools.flatten_list(v.get('issue'))
if isinstance(_issuelist, list) and len(_issuelist) > 1:
Expand All @@ -1077,9 +1077,21 @@ def list_issues(v):
self.__logger.info("- {}", e)
self.__logger.decrease_indent()
elif isinstance(_issuelist, list) and len(_issuelist) == 1:
self.__logger.info("has the following issue: {}", _issuelist[0])
if isinstance(_issuelist[0], dict):
self.__logger.info("has the following issues:")
self.__logger.increase_indent()
print_readable_dict(_issuelist[0])
self.__logger.decrease_indent()
else:
self.__logger.info("has the following issue: {}", _issuelist[0])
else:
self.__logger.info("has the following issue: {}", _issuelist)
if isinstance(_issuelist, dict):
self.__logger.info("has the following issues:")
self.__logger.increase_indent()
print_readable_dict(_issuelist)
self.__logger.decrease_indent()
else:
self.__logger.info("has the following issue: {}", _issuelist)
if "ignore" in v:
self.__logger.info("It will be ignored")

Expand Down Expand Up @@ -1996,7 +2008,6 @@ def return_item(self, item_id):
item = None
_, item_id = StateEngineTools.partition_strip(item_id, ":")
try:
# self.__logger.debug("Creating struct for id {}".format(item_id))
item = StateEngineStructs.create(self, item_id)
except Exception as e:
_issue = "Struct {} creation failed. Error: {}".format(item_id, e)
Expand Down
5 changes: 2 additions & 3 deletions stateengine/StateEngineTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ def find_attribute(smarthome, state, attribute, recursion_depth=0, use=None):
base_item = state.state_item
if use is None:
use = state.use.get()
print(f"got use {use}")
except Exception:
# if state is a standard item (e.g. evaluated by se_use, just take it as it is
base_item = state
Expand Down Expand Up @@ -304,8 +303,8 @@ def find_attribute(smarthome, state, attribute, recursion_depth=0, use=None):
# splitchar: where to split
# returns: Parts before and after split, whitespaces stripped
def partition_strip(value, splitchar):
if isinstance(value, list):
raise ValueError("You can not use list entries!")
if not isinstance(value, str):
raise ValueError("value has to be a string!")
elif value.startswith("se_") and splitchar == "_":
part1, __, part2 = value[3:].partition(splitchar)
return "se_" + part1.strip(), part2.strip()
Expand Down
12 changes: 7 additions & 5 deletions stateengine/StateEngineValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def cast_item(self, value):
_issue_dict = {str(value): _issue}
if _issue_dict not in self.__get_issues['cast_item']:
self.__get_issues['cast_item'].append(_issue_dict)
self._log_error("Can't cast {0} to item/struct! {1}".format(value, ex))
self._log_error(_issue)
return value

def __update_item_listorder(self, value, newvalue, item_id=None):
Expand Down Expand Up @@ -712,7 +712,7 @@ def __get_from_regex(self):
values = self.__regex
_issue = "Problem while creating regex '{0}': {1}.".format(values, ex)
_issue_dict = {values: _issue}
if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['regex']:
if _issue_dict not in self.__get_issues['regex']:
self.__get_issues['regex'].append(_issue_dict)
self._log_info(_issue)
return values
Expand Down Expand Up @@ -744,7 +744,9 @@ def __get_eval(self):
self._log_decrease_indent()
_name = StateEngineTools.get_eval_name(self.__eval)
_issue = "Problem evaluating '{0}': {1}.".format(_name, ex)
self.__get_issues['eval'].append({_name: ex})
_issue_dict = {_name: _issue}
if _issue_dict not in self.__get_issues['eval']:
self.__get_issues['eval'].append(_issue_dict)
self._log_warning(_issue)
self._log_increase_indent()
values = None
Expand Down Expand Up @@ -779,7 +781,7 @@ def __get_eval(self):
self._log_decrease_indent()
_issue = "Problem evaluating from list '{0}': {1}.".format(
StateEngineTools.get_eval_name(val), ex)
_issue_dict = {val: ex}
_issue_dict = {val: _issue}
if _issue_dict not in self.__get_issues['eval']:
self.__get_issues['eval'].append(_issue_dict)
self._log_warning(_issue)
Expand Down Expand Up @@ -827,7 +829,7 @@ def __get_eval(self):
self._log_warning(_issue)
self._log_increase_indent()
_issue_dict = {_name: _issue}
if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['eval']:
if _issue_dict not in self.__get_issues['eval']:
self.__get_issues['eval'].append(_issue_dict)
return None
return values
Expand Down

0 comments on commit 9a358f8

Please sign in to comment.