|
| 1 | +Pyscript: Python Scripting for Home Assistant |
| 2 | +============================================= |
| 3 | + |
| 4 | +|GitHub Release| |License| |hacs| |Project Maintenance| |
| 5 | + |
| 6 | +Overview |
| 7 | +-------- |
| 8 | + |
| 9 | +This HACS custom integration allows you to write Python functions and |
| 10 | +scripts that can implement a wide range of automation, logic and |
| 11 | +triggers. State variables are bound to Python variables and services are |
| 12 | +callable as Python functions, so it’s easy and concise to implement |
| 13 | +logic. |
| 14 | + |
| 15 | +Functions you write can be configured to be called as a service or run |
| 16 | +upon time, state-change or event triggers. Functions can also call any |
| 17 | +service, fire events and set state variables. Functions can sleep or |
| 18 | +wait for additional changes in state variables or events, without |
| 19 | +slowing or affecting other operations. You can think of these functions |
| 20 | +as small programs that run in parallel, independently of each other, and |
| 21 | +they could be active for extended periods of time. |
| 22 | + |
| 23 | +State, event and time triggers are specified by Python function |
| 24 | +decorators (the “@” lines immediately before each function definition). |
| 25 | +A state trigger can be any Python expression using state variables - the |
| 26 | +trigger is evaluated only when a state variable it references changes, |
| 27 | +and the trigger occurs when the expression is true or non-zero. A time |
| 28 | +trigger could be a single event (eg: date and time), a repetitive event |
| 29 | +(eg: at a particular time each day or weekday, daily relative to sunrise |
| 30 | +or sunset or any regular time period within an optional range) or using |
| 31 | +cron syntax (where events occur periodically based on a concise |
| 32 | +specification of ranges of minutes, hours, days of week, days of month |
| 33 | +and months). An event trigger specifies the event type, and an optional |
| 34 | +Python trigger test based on the event data that runs the Python |
| 35 | +function if true. |
| 36 | + |
| 37 | +Pyscript implements a Python interpreter using the ast parser output, in |
| 38 | +a fully async manner. That allows several of the “magic” features to be |
| 39 | +implemented in a seamless Pythonic manner, such as binding of variables |
| 40 | +to states and functions to services. Pyscript supports imports, although |
| 41 | +by default the valid import list is restricted for security reasons |
| 42 | +(there is a configuration option ``allow_all_imports`` to allow all |
| 43 | +imports). Pyscript supports all language features except generators and |
| 44 | +``yield``. Pyscript provides a handful of additional built-in functions |
| 45 | +that connect to HASS features, like logging, accessing state variables |
| 46 | +as strings (if you need to compute their names dynamically), sleeping |
| 47 | +and waiting for triggers. |
| 48 | + |
| 49 | +Pyscript also provides a kernel that interfaces with the Jupyter |
| 50 | +front-ends (eg, notebook, console and lab). That allows you to develop |
| 51 | +and test pyscript code interactively. Plus you can interact with much of |
| 52 | +HASS by looking at state variables, calling services etc, in a similar |
| 53 | +way to `HASS |
| 54 | +CLI <https://github.com/home-assistant-ecosystem/home-assistant-cli>`__, |
| 55 | +although the CLI provides status on many other parts of HASS. |
| 56 | + |
| 57 | +For more information about the Jupyter kernel, see the |
| 58 | +`README <https://github.com/craigbarratt/hass-pyscript-jupyter/blob/master/README.md>`__. |
| 59 | +There is also a `Jupyter notebook |
| 60 | +tutorial <https://nbviewer.jupyter.org/github/craigbarratt/hass-pyscript-jupyter/blob/master/pyscript_tutorial.ipynb>`__, |
| 61 | +which can be downloaded and run interactively in Jupyter notebook |
| 62 | +connected to your live HASS with pyscript. |
| 63 | + |
| 64 | +Pyscript provides functionality that complements the existing |
| 65 | +automations, templates and triggers. Pyscript is most similar to |
| 66 | +`AppDaemon <https://appdaemon.readthedocs.io/en/latest/>`__, and some |
| 67 | +similarities and differences are discussed in this `Wiki |
| 68 | +page <https://github.com/custom-components/pyscript/wiki/Comparing-Pyscript-to-AppDaemon>`__. |
| 69 | +Pyscript with Jupyter makes it extremely easy to learn, use and debug. |
| 70 | +Pyscripts presents a simplified and more integrated binding for Python |
| 71 | +scripting than `Python |
| 72 | +Scripts <https://www.home-assistant.io/integrations/python_script>`__, |
| 73 | +which requires a lot more expertise and scaffolding using direct access |
| 74 | +to Home Assistant internals. |
| 75 | + |
| 76 | +Contents |
| 77 | +~~~~~~~~ |
| 78 | + |
| 79 | +.. toctree:: |
| 80 | + :maxdepth: 3 |
| 81 | + |
| 82 | + installation |
| 83 | + configuration |
| 84 | + tutorial |
| 85 | + reference |
| 86 | + contributing |
| 87 | + useful_links |
| 88 | + |
| 89 | +.. |GitHub Release| image:: https://img.shields.io/github/release/custom-components/pyscript.svg?style=for-the-badge |
| 90 | + :target: https://github.com/custom-components/pyscript/releases |
| 91 | +.. |License| image:: https://img.shields.io/github/license/custom-components/pyscript.svg?style=for-the-badge |
| 92 | + :target: https://github.com/custom-components/pyscript/blob/master/LICENSE |
| 93 | +.. |hacs| image:: https://img.shields.io/badge/HACS-Default-orange.svg?style=for-the-badge |
| 94 | + :target: https://github.com/custom-components/hacs |
| 95 | +.. |Project Maintenance| image:: https://img.shields.io/badge/maintainer-%40craigbarratt-blue.svg?style=for-the-badge |
| 96 | + :target: https://github.com/craigbarratt |
0 commit comments