Feature Request: Reusable Functions
See original GitHub issueI tend to reuse the same logic for various rooms, the only difference is the entities that get watched and acted on. This is also VERY useful for sharing “apps” that are configurable.
However, I can’t seem to find a nice way to make code reusable in pyscript.
For instance, this was the closest I could come, but it still doesn’t work:
def follow_me(leader=None,followers=[]):
log.error('creating trigger for {}'.format(leader))
@state_trigger("True or {}".format(leader))
def follow_me_trigger():
log.error('follow me triggered for {}'.format(leader))
value = state.get(leader)
if value == 'on':
for item in followers:
homeassistant.turn_on(entity_id=item)
else:
for item in followers:
homeassistant.turn_off(entity_id=item)
return follow_me_trigger
follow_me('input_boolean.test_1', ['input_boolean.test_2'])
follow_me('input_boolean.test_3', ['input_boolean.test_4'])
Is there a recommended way to do this already? If not, would you consider adding such a feature?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
[Feature] Reusable Functions #5186 - appsmithorg/appsmith
Currently, we can create and reuse API calls between various widgets. The ability to create generic functions bound to a page or app...
Read more >Write More Code & Create Reusable Functions with Our ...
This new feature enables users to write reusable JavaScript variables and functions as JS Objects. These JS Objects can be used anywhere ...
Read more >Functions — reusable blocks of code - Learn web development
Another essential concept in coding is functions, which allow you to store a piece of code that does a single task inside a...
Read more >Chapter 4. Code Reuse: Functions and Modules
Reusing code is key to building a maintainable system. And when it comes to reusing code in Python, it all starts and ends...
Read more >Reusable Functions In Postman - YouTube
A quick hack to reusing functions in Postman and keeping your code DRY. ... Send HTTP requests from scripts Postman. Valentin Despa.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just pushed a commit 8834dc1 that should allow trigger closures to work. Closures were also broken, so I fixed those with an earlier commit.
The way it works is very similar to what you wrote. However, the function trigger is destroyed if the function itself no longer has any reference to it in any scope. So in your example, you will want to assign the return value of
follow_me
to a variable:If you re-assign, or delete,
f1
orf2
then that trigger function will be destroyed. That’s consistent with the current behavior, which only worked in the global context - if you redefine a function in the global context, the old one is destroyed.You could put all the closure functions into a list if that is easier - all that matters is there is some reference to the function for it to survive, eg:
or
Caveat - the cumulative changes were pretty significant, and it’s only minimally tested. But please try the master version and see if it works for your use case.
Ah - great point. There isn’t a good way to do this currently. I do like your use of a closure, and as you note that doesn’t work currently. Let me think about how to best support this.