PyScript Modules for Repeatable Automations
The Home Assistant world is abuzz with the idea of Blueprints: repeatable automation logic with different inputs.
While this feature is incredibly useful in Home Automation, most programming languages have “functions”, which is essentially the same idea (minus all of the UI bits that also make Blueprints a great addition). Since PyScript is based on Python, building repeatable automation logic is already included.
Let’s start with a simple “motion activated light” automation in PyScript:
|
|
If you aren’t familiar with the features being used above, checkout the PyScript Documentation and get acquainted.
Just like with a Home Assistant Blueprint, the first step is determining what the inputs will be. In this case, we have a motion_sensor
, a light_entity
, and a delay_seconds
. So, we’ll adjust our code to take these parameters by replacing each use of an entity_id with the variable that holds that information. We’ll do the same for the number of seconds to delay after motion stops.
|
|
Now, we need to wrap this in a function of its own.
|
|
Making this a pyscript module is easy. Create a directory called modules
in your pyscript
directory if you don’t already have one. Then create a new file in the modules
directory to hold our new module. Let’s call it lighting.py
. Just enter the above code into this file.
Finally, in any pyscript
file that you would like to create a “motion activated lights” automation, you can use this module, like so:
|
|
But perhaps you’d also like to be able to create an automation like this using only YAML. We’re going to use a pyscript
“app” for that.
First, we’ll create a file at /pyscript/apps/motion_activated_lights.py
. The following code goes in that file. It will go through the YAML configuration for this app, and use our module to create the automations. We’ll use a global variable called TRIGGERS
to store the triggers in so that Pyscript will register them.
|
|
Now, with our app ready to go, we only need to add the YAML configuration.
|
|
In just a few, repeatable steps we’ve turned a single automation into an automation that is usable in other pyscripts as well as usable with only YAML to configure it.