Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Always reload all scripts? (Issue with custom decorators) #428

Answered by craigbarratt
j-steve asked this question in Q&A
Discussion options

Is there a YAML config command that will force Pyscript to always reload all scripts, not just the script(s) that were changed?

Context
I've written some custom trigger decorators which wrap around the built-in triggers, like so:

# file: custom_triggers.py
def zha_trigger(device_ieee, command=None):
 
 def trigger_builder(func):
 
 event_command_suffix = '' if not command else f' and command = "{command}"'
 
 @event_trigger('zha_event', f'device_ieee=="{device_ieee}" {event_command_suffix}')
 def event_trigger(**kwargs):
 func(**kwargs)
 
 return event_trigger
 
 return trigger_builder

I can use them like this:

# file: garage.py
@zha_trigger('04:23:a1:b4:e2:23', 'toggle')
def garage_button_press(**kwargs):
 pass

These work perfectly! But the problem is that the triggers themselves are bound within custom_triggers.py rather than the calling class file. This means that if I make a code change to garage_button_press in the example above, it will reload garage.py but not custom_triggers.py, and as a result, duplicate trigger bindings will exist (one before the code change, one after).

I'm not clear why the bindings end up living in custom_triggers.py, but that wouldn't be a problem if I could force all scripts to reload everytime one of them changes.

Alternatively, perhaps there's a more elegant way I could create my trigger wrapper which would cause the binding to live within garage.py instead?

You must be logged in to vote

Ok, should be fixed with cb980a9.

Replies: 5 comments

Comment options

I hit the same issue myself. The only solution I found was to call "pyscript.reload" from the UI with global_ctx="*". I attempted to call this at the top of my relevant pyscript files in various forms, but it always seemed to end up in a continuous loop.

If anyone has a better solution, I'd LOVE to hear it. I wonder if this could be considered a bug (bindings not reloading) and an issue should be opened.

You must be logged in to vote
0 replies
Comment options

Sorry I didn't see this earlier. This is definitely a bug - the trigger should be bound to the garage's context, not where the decorator is defined. The internals that implement this are pretty tricky - let me see how easy it is to fix it.

Feel free to open as issue, or I can do it once I figure out how to fix it 😀.

In the meantime, you could touch custom_triggers.py to force it to reload.

You must be logged in to vote
0 replies
Comment options

I opened issue #457.

You must be logged in to vote
0 replies
Comment options

Ok, should be fixed with cb980a9.

You must be logged in to vote
0 replies
Answer selected by j-steve
Comment options

Amazing!! Thanks Craig, this will unlock some cool stuff.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /