Skip to content
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

Strange behaviour of @event_trigger #439

Closed
Michael-CGN opened this issue Feb 23, 2023 · 3 comments
Closed

Strange behaviour of @event_trigger #439

Michael-CGN opened this issue Feb 23, 2023 · 3 comments

Comments

@Michael-CGN
Copy link

Hi,

I just found some strange behaviour when using the event_trigger.

Having somehting like this:

@event_trigger("some_event", "action == 'some_action' and state=='finished'") def dummy(**kwargs): log.debug(f"{kwargs}")

a and triggering the event with event.fire("some_event") leads to the correct result - the function dummy isn't called and thus no log output.

Triggering the event with event.fire("some_event", action="some_action",state="finished") also works correctly and leads to a log output like this:
{'trigger_type': 'event', 'event_type': 'some_event', 'context': <homeassistant.core.Context object at 0x7f7ced65ab40>, 'action': 'some_action', 'state': 'finished'}

BUT, each subsequent event without any arguements (event.fire("some_event")) now also triggers the function and shows an output like this:
{'trigger_type': 'event', 'event_type': 'some_event', 'context': <homeassistant.core.Context object at 0x7f7ce9657e00>}

It seems the the str_expr of @event_trigger is not evaluated anymore. Giving some random arguments to the event like this
event.fire("some_event", x="s")
also triggers the function.

But providing at least one of the arguments to be checked in the str_expr like here
event.fire("some_event", action="s")
prevents the function from being called.

Thats pretty strange. I would expect the str_expr to return false if the arguments are not provided in the event instead of ingoring this and evaluating to true.

@Michael-CGN Michael-CGN changed the title Strane behaviour of @event_trigger Strange behaviour of @event_trigger Feb 23, 2023
@craigbarratt
Copy link
Member

Thanks for the clear explanation - it's definitely a bug. Each event trigger creates a context for evaluating the expression, and the local symbol table is simply updated (not replaced) each time with the event parameters. Fix is to replace the local symbol table each time the expression is evaluated.

Fixed with 8a250d8.

@Michael-CGN
Copy link
Author

Thanks for the quick response and the bugfix. And a really BIG thank you for this amazing tool. Without pyscript home assistant would be useless for me :-)

@craigbarratt
Copy link
Member

This is fixed in 1.4.0. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants