feat: add support for local expressions in ADExpressionContext and update smart condition handling

This commit is contained in:
2026-06-03 21:52:58 +02:00
parent 943361453a
commit 56e3f6e9e0
3 changed files with 64 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import pyparsing as pp
from expressionparser import BaseExpressionContext
from expressionparser import BaseExpressionContext, ExpressionParser
class ADExpressionContext(BaseExpressionContext):
def __init__(self,appdaemon_api, sensor_existence_validation = True):
@@ -13,6 +13,7 @@ class ADExpressionContext(BaseExpressionContext):
self.sensors_default_value = dict()
self.sensor_states = dict()
self.sensor_existence_validation = sensor_existence_validation
self.local_expressions = {}
self.declare_default_constants()
@@ -131,7 +132,22 @@ class ADExpressionContext(BaseExpressionContext):
def get_entities_to_listen(self):
return self.entities_to_listen
def get_constant_value(self,name): return self.variables[name]
def get_constant_value(self, name):
if name in self.local_expressions:
return self.local_expressions[name].eval()
return self.variables[name]
def declare_local_expression(self, name: str, expression_string: str):
assert name not in self.variables, f"'{name}' is already declared"
assert name.replace('_', '').isalnum() and name.isascii(), f"Invalid local expression name: {name}"
# Register as a constant placeholder so the variable parser recognises it
self.variables[name] = None
self.constants.append(name)
self.parser = None # reset variable parser cache
# Parse the expression; set_context registers its sensor dependencies
# Store only the parsed tree so evaluation skips the top-level reset()
full_parser = ExpressionParser(expression_string, self)
self.local_expressions[name] = full_parser.parsed_data
def declare_constant(self,name : str,value):
def is_a_sensor(string):