mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add variable support to template rendering
This commit is contained in:
parent
0dcc69d800
commit
7acef84aad
@ -5,37 +5,17 @@ homeassistant.util.template
|
|||||||
Template utility methods for rendering strings with HA data.
|
Template utility methods for rendering strings with HA data.
|
||||||
"""
|
"""
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
from jinja2.sandbox import SandboxedEnvironment
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||||
|
|
||||||
ENV = SandboxedEnvironment()
|
|
||||||
|
|
||||||
|
|
||||||
def forgiving_round(value, precision=0):
|
def render(hass, template, variables=None, **kwargs):
|
||||||
""" Rounding method that accepts strings. """
|
|
||||||
try:
|
|
||||||
return int(float(value)) if precision == 0 else round(float(value),
|
|
||||||
precision)
|
|
||||||
except ValueError:
|
|
||||||
# If value can't be converted to float
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
def multiply(value, amount):
|
|
||||||
""" Converts to float and multiplies value. """
|
|
||||||
try:
|
|
||||||
return float(value) * amount
|
|
||||||
except ValueError:
|
|
||||||
# If value can't be converted to float
|
|
||||||
return value
|
|
||||||
|
|
||||||
ENV.filters['round'] = forgiving_round
|
|
||||||
ENV.filters['multiply'] = multiply
|
|
||||||
|
|
||||||
|
|
||||||
def render(hass, template):
|
|
||||||
""" Render given template. """
|
""" Render given template. """
|
||||||
return ENV.from_string(template).render(
|
if variables is not None:
|
||||||
states=AllStates(hass))
|
kwargs.update(variables)
|
||||||
|
|
||||||
|
return ENV.from_string(template, {
|
||||||
|
'states': AllStates(hass)
|
||||||
|
}).render(kwargs)
|
||||||
|
|
||||||
|
|
||||||
class AllStates(object):
|
class AllStates(object):
|
||||||
@ -66,3 +46,26 @@ class DomainStates(object):
|
|||||||
(state for state in self._hass.states.all()
|
(state for state in self._hass.states.all()
|
||||||
if state.domain == self._domain),
|
if state.domain == self._domain),
|
||||||
key=lambda state: state.entity_id))
|
key=lambda state: state.entity_id))
|
||||||
|
|
||||||
|
|
||||||
|
def forgiving_round(value, precision=0):
|
||||||
|
""" Rounding method that accepts strings. """
|
||||||
|
try:
|
||||||
|
return int(float(value)) if precision == 0 else round(float(value),
|
||||||
|
precision)
|
||||||
|
except ValueError:
|
||||||
|
# If value can't be converted to float
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def multiply(value, amount):
|
||||||
|
""" Converts to float and multiplies value. """
|
||||||
|
try:
|
||||||
|
return float(value) * amount
|
||||||
|
except ValueError:
|
||||||
|
# If value can't be converted to float
|
||||||
|
return value
|
||||||
|
|
||||||
|
ENV = ImmutableSandboxedEnvironment()
|
||||||
|
ENV.filters['round'] = forgiving_round
|
||||||
|
ENV.filters['multiply'] = multiply
|
||||||
|
@ -64,3 +64,11 @@ class TestUtilTemplate(unittest.TestCase):
|
|||||||
template.render(
|
template.render(
|
||||||
self.hass,
|
self.hass,
|
||||||
'{{ states.sensor.temperature.state | multiply(10) | round }}'))
|
'{{ states.sensor.temperature.state | multiply(10) | round }}'))
|
||||||
|
|
||||||
|
def test_passing_vars_as_keywords(self):
|
||||||
|
self.assertEqual(
|
||||||
|
'127', template.render(self.hass, '{{ hello }}', hello=127))
|
||||||
|
|
||||||
|
def test_passing_vars_as_vars(self):
|
||||||
|
self.assertEqual(
|
||||||
|
'127', template.render(self.hass, '{{ hello }}', {'hello': 127}))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user