Skip to content

WIP: Alternate implementation to only trigger on changes #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions custom_components/pyscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ async def state_changed(event):
"old_value": old_val,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With f7169ac, "value" can be StateVar(event.data.get('new_state')), or None if new_state is not provided (eg, when state variable is deleted. Same for "old_value".

"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]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines shouldn't be necessary now, since the value and old_value contain the full states.

await State.update(new_vars, func_args)

async def hass_started(event):
Expand Down
6 changes: 3 additions & 3 deletions custom_components/pyscript/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good simplification. One small change: it's probably better to do

notify_vars = new_vars.copy()

so that new_vars isn't modified in-place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't know why this wasn't working the way it was. I made this change just to be SURE it was what I expected it to be, and it fixed the whole thing. I guess cls.notify had something in it I didn't expect. But I'm not sure how.

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
Copy link
Member

@craigbarratt craigbarratt Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is going to need some modifications. I still haven't fully though this through. The motivation is to avoid exceptions due to undefined variables (eg: undefined state variable or attribute) by setting all variables that are not found in cls.notify_var_last or new_vars to None, so they don't generate an exception when the expression is evaluated. The logic is bit trickier now: if, say, domain.entity.attr is in var_names, and domain.entity has been set to its StateVar, then there's no need to set domain.entity.attr to None since attr is now defined in StateVar.

Not sure if that makes sense. I'm happy to deal with this after merge.

return notify_vars
Expand Down
13 changes: 13 additions & 0 deletions custom_components/pyscript/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to check if var is domain.entity (if so, just compare value with old_value) or domain.entity.attr(if so, compare getattr(value, attr) with getattr(old_value, attr)).

trig_ident_changed = True

if not trig_ident_changed:
continue

#
# check for @task_unique with kill_me=True
#
Expand Down