diff --git a/custom_components/pyscript/__init__.py b/custom_components/pyscript/__init__.py index b386ba1..8401b45 100644 --- a/custom_components/pyscript/__init__.py +++ b/custom_components/pyscript/__init__.py @@ -218,6 +218,15 @@ async def state_changed(event): "old_value": old_val, "context": event.context, } + + if new_val is not None: + for attribute in event.data["new_state"].attributes: + new_vars[f"{var_name}.{attribute}"] = event.data["new_state"].attributes[attribute] + + if old_val is not None: + for attribute in event.data["old_state"].attributes: + new_vars[f"{var_name}.{attribute}.old"] = event.data["old_state"].attributes[attribute] + await State.update(new_vars, func_args) async def hass_started(event): diff --git a/custom_components/pyscript/state.py b/custom_components/pyscript/state.py index ea4c3a5..a55b2de 100644 --- a/custom_components/pyscript/state.py +++ b/custom_components/pyscript/state.py @@ -115,12 +115,12 @@ async def update(cls, new_vars, func_args): @classmethod def notify_var_get(cls, var_names, new_vars): """Return the most recent value of a state variable change.""" - notify_vars = {} + notify_vars = new_vars for var_name in var_names if var_names is not None else []: if var_name in cls.notify_var_last: notify_vars[var_name] = cls.notify_var_last[var_name] - elif var_name in new_vars: - notify_vars[var_name] = new_vars[var_name] + # elif var_name in new_vars: + # notify_vars[var_name] = new_vars[var_name] elif 1 <= var_name.count(".") <= 2 and not cls.exist(var_name): notify_vars[var_name] = None return notify_vars diff --git a/custom_components/pyscript/trigger.py b/custom_components/pyscript/trigger.py index bbc6689..1e3139c 100644 --- a/custom_components/pyscript/trigger.py +++ b/custom_components/pyscript/trigger.py @@ -812,6 +812,19 @@ async def trigger_watch(self): if self.task_unique is not None: task_unique_func = Function.task_unique_factory(action_ast_ctx) + + # + # for state notify, check that changes occurred in state_trig_ident variables + # + if notify_type == 'state': + trig_ident_changed = False + for var in self.state_trig_ident: + if new_vars.get(var) != new_vars.get(f"{var}.old"): + trig_ident_changed = True + + if not trig_ident_changed: + continue + # # check for @task_unique with kill_me=True #