This HACS custom integration allows you to write Python functions and scripts that can implement a wide range of automation, logic and triggers. State variables are bound to Python variables and services are callable as Python functions, so it's easy and concise to implement logic.
Functions you write can be configured to be called as a service or run upon time, state-change or event triggers. Functions can also call any service, fire events and set state variables. Functions can sleep or wait for additional changes in state variables or events, without slowing or affecting other operations. You can think of these functions as small programs that run in parallel, independently of each other, and they could be active for extended periods of time.
Pyscript also provides a kernel that interfaces with the Jupyter front-ends (eg, notebook, console, lab and VSCode). That allows you to develop and test pyscript code interactively. Plus you can interact with much of HASS by looking at state variables, calling services etc.
Here is the pyscript documentation.
For more information about the Jupyter kernel, see the README. There is also a Jupyter notebook tutorial, which can be downloaded and run interactively in Jupyter notebook connected to your live HASS with pyscript.
Under HACS -> Integrations, select "+", search for pyscript
and install it.
From the latest release download the zip file hass-custom-pyscript.zip
cd YOUR_HASS_CONFIG_DIRECTORY # same place as configuration.yaml
mkdir -p custom_components/pyscript
cd custom_components/pyscript
unzip hass-custom-pyscript.zip
Alternatively, you can install the current GitHub master version by cloning and copying:
mkdir SOME_LOCAL_WORKSPACE
cd SOME_LOCAL_WORKSPACE
git clone https://github.com/custom-components/pyscript.git
mkdir -p YOUR_HASS_CONFIG_DIRECTORY/custom_components
cp -pr pyscript/custom_components/pyscript YOUR_HASS_CONFIG_DIRECTORY/custom_components
Installing the Pyscript Jupyter kernel is optional. The steps to install and use it are in this README.
- Go to the Integrations menu in the Home Assistant Configuration UI and add
Pyscript Python scripting
from there, or addpyscript:
to<config>/configuration.yaml
; pyscript has one optional configuration parameter that allows any python package to be imported if set, eg:pyscript: allow_all_imports: true
- Add files with a suffix of
.py
in the folder<config>/pyscript
. - Restart HASS.
- Whenever you change a script file, make a
reload
service call topyscript
. - Watch the HASS log for
pyscript
errors and logger output from your scripts.
Contributions are welcome! You are encouraged to submit PRs, bug reports, feature requests or add to the Wiki with examples and tutorials. It would be fun to hear about unique and clever applications you develop. Please see this README for setting up a development environment and running tests.
- Documentation stable: latest release
- Documentation latest: current master in Github
- Issues
- Wiki
- GitHub repository (please add a star if you like pyscript!)
- Jupyter notebook tutorial
Copyright (c) 2020 Craig Barratt. May be freely used and copied according to the terms of the Apache 2.0 License.