Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace value_json in templates with filters #811

Closed
wants to merge 14 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions homeassistant/util/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
Template utility methods for rendering strings with HA data.
"""
# pylint: disable=too-few-public-methods
import os
import json
import logging
import jinja2
import importlib
import pkgutil
from jinja2.sandbox import ImmutableSandboxedEnvironment
from homeassistant.const import STATE_UNKNOWN
from homeassistant.exceptions import TemplateError
Expand Down Expand Up @@ -112,3 +115,27 @@ def is_safe_callable(self, obj):
ENV = TemplateEnvironment()
ENV.filters['round'] = forgiving_round
ENV.filters['multiply'] = multiply


def load_custom_filters(root):
# load custom filters into ENV
global ENV
try:
root_module = importlib.import_module(root)
prefix = root_module.__name__+'.'
except ImportError as err:
_LOGGER.exception(err)
return

for importer,modname,ispkg in pkgutil.iter_modules(root_module.__path__, prefix):
try:
module = importlib.import_module(modname)
except ImportError as err:
_LOGGER.exception(err)
pass
for fname,func in root_module.hass_custom_filters.items():
ENV.filters[fname] = func
_LOGGER.info('Registered filter %s' % fname)


load_custom_filters('homeassistant.util.filters')