Last active
May 11, 2025 18:55
-
-
Save jasonk/2b8612243b609a59380dabc438bed2f5 to your computer and use it in GitHub Desktop.
Import template functions from Home Assistant into pyscript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Inspired by [pyscript#251](https://github.com/custom-components/pyscript/issues/251) and [pyscript#568](https://github.com/custom-components/pyscript/issues/568). | |
This is a module that you can put in your [pyscript](https://github.com/custom-components/pyscript) modules directory and then import from any pyscript script and have access to all the template functions that Home Assistant provides. | |
Features: | |
* It automatically determines what functions Home Assistant is providing to templates, so when new functions get added by a Home Assistant update (like labels and areas did recently) they get included automatically. | |
* It even includes things added by extensions, like the template functions added by [spook](https://spook.boo/). | |
* It automatically figures out which functions need the `hass` instance passed to them and wraps the ones that do. | |
## Usage ## | |
```python | |
import template_functions as tf | |
for area in tf.areas(): | |
print(tf.area_name(area)) | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Import template functions from Home Assistant into pyscript.""" | |
import functools | |
import inspect | |
import builtins | |
import homeassistant.helpers.template | |
def get_hass_globals(): | |
"""Get all the global functions that are defined in Home Assistant templates""" | |
_env = homeassistant.helpers.template.TemplateEnvironment(hass) | |
res = {} | |
def needs_hass_wrapper(func): | |
"""Determine if a value is a function that needs to be bound to the hass object""" | |
if not inspect.isfunction(func): | |
return False | |
sig = inspect.signature(func) | |
for p in sig.parameters.values(): | |
if p.name == "hass": | |
return True | |
return False | |
# Make a list of builtins so we can skip installing any template functions | |
# that shadow builtins (mainly `list` and `dict`) | |
_builtins = dir(builtins) | |
# Get all the globals, and any filters that are not also globals | |
for src in (_env.globals, _env.filters): | |
for key, value in src.items(): | |
if key in _builtins or key in res: | |
continue | |
if needs_hass_wrapper(value): | |
res[key] = functools.partial(value, hass) | |
else: | |
res[key] = value | |
return res | |
globals().update(get_hass_globals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment