Skip to content

Commit

Permalink
stateengine plugin: minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
onkelandy committed Jul 25, 2024
1 parent 168e63f commit 4a6309f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 21 deletions.
1 change: 0 additions & 1 deletion stateengine/StateEngineActions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def update(self, attribute, value):
value = ":".join(map(str.strip, value.split(":")))
if value[:1] == '[' and value[-1:] == ']':
value = StateEngineTools.convert_str_to_list(value, False)
self._log_debug("Value is now {}", value)
if func == "se_delay":
# set delay
if name not in self.__actions:
Expand Down
4 changes: 4 additions & 0 deletions stateengine/StateEngineConditionSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def name(self):
def id(self):
return self.__id

@property
def path(self):
return self.__id

# List of conditions that are part of this condition set
@property
def conditions(self):
Expand Down
7 changes: 5 additions & 2 deletions stateengine/StateEngineEval.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def get_relative_itemvalue(self, subitem_id):
item, issue = self._abitem.return_item(subitem_id)
returnvalue = item.property.value
returnvalue = StateEngineTools.convert_str_to_list(returnvalue)
self._log_debug("Return item value '{0}' for item {1}", returnvalue, subitem_id)
issue = f" Issue: {issue}" if issue not in [[], None, [None]] else ""
self._log_debug("Return item value '{0}' for item {1}.{2}",
returnvalue, subitem_id, issue)
except Exception as ex:
self._log_warning("Problem evaluating value of '{0}': {1}", subitem_id, ex)
finally:
Expand All @@ -183,7 +185,7 @@ def get_relative_itemproperty(self, subitem_id, prop):
self._eval_lock.acquire()
self._log_debug("Executing method 'get_relative_itemproperty({0}, {1})'", subitem_id, prop)
try:
item, issue = self._abitem.return_item(subitem_id)
item, _ = self._abitem.return_item(subitem_id)
except Exception as ex:
self._log_warning("Problem evaluating property of {0} - relative item might not exist. Error: {1}",
subitem_id, ex)
Expand Down Expand Up @@ -223,6 +225,7 @@ def get_attribute_value(self, item, attrib):
def get_attributevalue(self, item, attrib):
self._eval_lock.acquire()
self._log_debug("Executing method 'get_attributevalue({0}, {1})'", item, attrib)
issue = None
if ":" in item:
var_type, item = StateEngineTools.partition_strip(item, ":")
if var_type == "var":
Expand Down
31 changes: 19 additions & 12 deletions stateengine/StateEngineItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class SeItem:
def id(self):
return self.__id

@property
def path(self):
return self.__id

@property
def variables(self):
return self.__variables
Expand Down Expand Up @@ -114,7 +118,7 @@ def laststate(self):
@property
def laststate_releasedby(self):
_returnvalue = None if self.__laststate_item_id is None \
else self.__release_info.get(self.__laststate_item_id.property.value)
else self.__release_info.get(self.__laststate_item_id.property.value)
return _returnvalue

@property
Expand Down Expand Up @@ -290,7 +294,8 @@ def __init__(self, smarthome, item, se_plugin):
self.__previousstate_conditionset_internal_name = "" if self.__previousstate_conditionset_item_name is None else \
self.__previousstate_conditionset_item_name.property.value
self.__config_issues.update(_issue)
filtered_dict = {key: value for key, value in self.__config_issues.items() if value.get('issue') not in [[], [None], None]}
filtered_dict = {key: value for key, value in self.__config_issues.items() if
value.get('issue') not in [[], [None], None]}
self.__config_issues = filtered_dict

self.__states = []
Expand Down Expand Up @@ -1203,7 +1208,7 @@ def __initialize_state(self, item_state, _statecount):
_issue = _state.update_order(_statecount)
if _issue:
self.__config_issues.update({item_state.property.path:
{'issue': _issue, 'attribute': 'se_stateorder'}})
{'issue': _issue, 'attribute': 'se_stateorder'}})
self.__logger.error("Issue with state {0} while setting order: {1}",
item_state.property.path, _issue)
self.__states.append(_state)
Expand All @@ -1216,13 +1221,13 @@ def __initialize_state(self, item_state, _statecount):
return _statecount + 1
except ValueError as ex:
self.update_issues('state', {item_state.property.path: {'issue': ex, 'issueorigin':
[{'conditionset': 'None', 'condition': 'ValueError'}]}})
[{'conditionset': 'None', 'condition': 'ValueError'}]}})
self.__logger.error("Ignoring state {0} because ValueError: {1}",
item_state.property.path, ex)
return _statecount
except Exception as ex:
self.update_issues('state', {item_state.property.path: {'issue': ex, 'issueorigin':
[{'conditionset': 'None', 'condition': 'GeneralError'}]}})
[{'conditionset': 'None', 'condition': 'GeneralError'}]}})
self.__logger.error("Ignoring state {0} because: {1}",
item_state.property.path, ex)
return _statecount
Expand All @@ -1241,7 +1246,8 @@ def update_state(self, item, caller=None, source=None, dest=None):
return
self.__queue.put(["stateevaluation", item, caller, source, dest])
if not self.update_lock.locked():
self.__logger.debug("Run queue to update state. Item: {}, caller: {}, source: {}", item.property.path, caller, source)
self.__logger.debug("Run queue to update state. Item: {}, caller: {}, source: {}", item.property.path,
caller, source)
self.run_queue()

# check if state can be entered after setting state-specific variables
Expand Down Expand Up @@ -1609,7 +1615,7 @@ def process_returnvalue(value):
_returnvalue_issue_list.append(_returnvalue_issue)
elif _valueindex < _stateindex:
_returnvalue_issue = "State {} defined by {} in se_released_by " \
"attribute of state {} must be lower priority "\
"attribute of state {} must be lower priority " \
"than actual state.".format(match, _returnvalue[i], state.id)
self.__logger.warning("{} Removing it.", _returnvalue_issue)
if _returnvalue_issue not in _returnvalue_issue_list:
Expand All @@ -1624,7 +1630,7 @@ def process_returnvalue(value):

_returnvalue_issue = _returnvalue_issue_list
if not matches:
_returnvalue_issue = "No states match regex {} defined in "\
_returnvalue_issue = "No states match regex {} defined in " \
"se_released_by attribute of state {}.".format(value, state.id)
self.__logger.warning("{} Removing it.", _returnvalue_issue)
elif _returntype[i] == 'eval':
Expand Down Expand Up @@ -1661,7 +1667,7 @@ def process_returnvalue(value):
v_list.append(v)
_converted_typelist.append(_returntype[i])
else:
_returnvalue_issue = "Found invalid definition in se_released_by attribute "\
_returnvalue_issue = "Found invalid definition in se_released_by attribute " \
"of state {}, original {}.".format(state.id, v, original_value)
self.__logger.warning("{} Removing it.", _returnvalue_issue)
_converted_evaluatedlist.append(v_list)
Expand Down Expand Up @@ -1769,7 +1775,8 @@ def write_to_log(self):
state.write_to_log()
self.initstate = None

filtered_dict = {key: value for key, value in self.__config_issues.items() if value.get('issue') not in [[], [None], None]}
filtered_dict = {key: value for key, value in self.__config_issues.items() if
value.get('issue') not in [[], [None], None]}
self.__config_issues = filtered_dict

# endregion
Expand Down Expand Up @@ -1938,8 +1945,8 @@ def return_item(self, item_id):
_issue = "item_id is None"
return None, [_issue]
if not isinstance(item_id, str):
_issue = "'{0}' is not defined as string.".format(item_id)
self.__logger.info("{0} Check your item config!", _issue, item_id)
_issue = "'{0}' is not defined as string, cannot find item.".format(item_id)
self.__logger.warning("{0} Check your item config!", _issue, item_id)
return None, [_issue]
item_id = item_id.strip()
if item_id.startswith("struct:"):
Expand Down
11 changes: 10 additions & 1 deletion stateengine/StateEngineState.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ class SeState(StateEngineTools.SeItemChild):
def id(self):
return self.__id

@property
def path(self):
return self.__id

@property
def use(self):
return self.__use

@property
def state_item(self):
return self.__item
Expand Down Expand Up @@ -171,7 +179,7 @@ def __init__(self, abitem, item_state):
self._log_decrease_indent()

def __repr__(self):
return "SeState item: {}, id {}.".format(self.__item, self.__id)
return "SeState item: {}, id {}".format(self.__item, self.__id)

# Check conditions if state can be entered
# returns: True = At least one enter condition set is fulfilled, False = No enter condition set is fulfilled
Expand Down Expand Up @@ -374,6 +382,7 @@ def update_name(self, item_state, recursion_depth=0):
elif self.__text.is_empty() and recursion_depth == 0:
self.__text.set("value:" + self.__name)
self.__name = self.text
self._log_develop("Updated name of state {} to {}.", item_state, self.__name)
return self.__name

def __fill_list(self, item_states, recursion_depth, se_use=None):
Expand Down
9 changes: 7 additions & 2 deletions stateengine/StateEngineStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def conf(self):
def id(self):
return self.struct_path

@property
def path(self):
return self.struct_path

def return_children(self):
for child in self._conf.keys():
yield child
Expand Down Expand Up @@ -103,7 +107,7 @@ def get(self):
raise NotImplementedError("Class {} doesn't implement get()".format(self.__class__.__name__))


# Class representing struct child
# Class representing struct
class SeStructMain(SeStruct):
# Initialize the action
# abitem: parent SeItem instance
Expand Down Expand Up @@ -200,11 +204,12 @@ def __init__(self, abitem, struct_path, global_struct):
#self._log_debug("Struct path {} for {}", self.struct_path, __class__.__name__)

def __repr__(self):
return "SeStructParent {}".format(self.struct_path, self._conf)
return "SeStructParent {}".format(self.struct_path)

def get(self):
try:
parent_name = self.struct_path.split(".")[-2]
self.struct_path = self.struct_path.rsplit('.', 1)[0]
_temp_dict = self.dict_get(self._global_struct.get(self._struct) or {}, parent_name,
self._global_struct.get(self._struct) or {})
_temp_dict = collections.OrderedDict(
Expand Down
4 changes: 2 additions & 2 deletions stateengine/StateEngineValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def write_to_logger(self):
if self.__eval is not None:
self._log_debug("{0} from eval: {1}", self.__name, self.__eval)
_original_listorder = self.__listorder.copy()
self._log_debug("Currently eval results in {}", self.__get_eval())
self._log_debug("Currently eval results in {}. ", self.__get_eval())
self.__listorder = _original_listorder
if self.__varname is not None:
if isinstance(self.__varname, list):
Expand Down Expand Up @@ -722,7 +722,7 @@ def __get_eval(self):
val = val.replace("\n", "")
except Exception:
pass
self._log_debug("Checking eval from list: {0}.", val)
self._log_debug("Checking eval {0} from list {1}.", val, self.__eval)
self._log_increase_indent()
if isinstance(val, str):
if "stateengine_eval" in val or "se_eval" in val:
Expand Down
2 changes: 1 addition & 1 deletion stateengine/StateEngineWebif.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, abitem):
self.__conditionset_count = 0

def __repr__(self):
return "WebInterface item: {}, id {}.".format(self.__states, self.__name) if REQUIRED_PACKAGE_IMPORTED else "None"
return "WebInterface item: {}, id {}".format(self.__states, self.__name) if REQUIRED_PACKAGE_IMPORTED else "None"

def _actionlabel(self, state, label_type, conditionset, previousconditionset, previousstate_conditionset):
# Check if conditions for action are met or not
Expand Down

0 comments on commit 4a6309f

Please sign in to comment.