Optimize template 2 (#3521)
* Enforce compiling templates * Refactor templates * Add template validator to Logbook service * Some more fixes * Lint * Allow easy skipping of rfxtrx tests * Fix template bug in AND & OR conditions * add entities extractor Conflicts: tests/helpers/test_template.py * fix unittest * Convert template to be async * Fix Farcy * Lint fix * Limit template updates to related entities * Make template automation async
This commit is contained in:
1 parent
6694b0470e
commit
00e298206e
52 files changed
+841
-562
No files matched your search
@@ -9,7 +9,7 @@ import subprocess
|
||||
|
||||
from homeassistant.components.rollershutter import RollershutterDevice
|
||||
from homeassistant.const import CONF_VALUE_TEMPLATE
|
||||
from homeassistant.helpers import template
|
||||
from homeassistant.helpers.template import Template
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -20,6 +20,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
devices = []
|
||||
|
||||
for dev_name, properties in rollershutters.items():
|
||||
value_template = properties.get(CONF_VALUE_TEMPLATE)
|
||||
|
||||
if value_template is not None:
|
||||
value_template = Template(value_template, hass)
|
||||
|
||||
devices.append(
|
||||
CommandRollershutter(
|
||||
hass,
|
||||
@@ -28,7 +33,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
properties.get('downcmd', 'true'),
|
||||
properties.get('stopcmd', 'true'),
|
||||
properties.get('statecmd', False),
|
||||
properties.get(CONF_VALUE_TEMPLATE, '{{ value }}')))
|
||||
value_template))
|
||||
add_devices_callback(devices)
|
||||
|
||||
|
||||
@@ -103,8 +108,8 @@ class CommandRollershutter(RollershutterDevice):
|
||||
if self._command_state:
|
||||
payload = str(self._query_state())
|
||||
if self._value_template:
|
||||
payload = template.render_with_possible_json_value(
|
||||
self._hass, self._value_template, payload)
|
||||
payload = self._value_template.render_with_possible_json_value(
|
||||
payload)
|
||||
self._state = int(payload)
|
||||
|
||||
def move_up(self, **kwargs):
|
||||
|
||||
@@ -13,7 +13,6 @@ from homeassistant.components.rollershutter import RollershutterDevice
|
||||
from homeassistant.const import CONF_NAME, CONF_VALUE_TEMPLATE
|
||||
from homeassistant.components.mqtt import (
|
||||
CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS)
|
||||
from homeassistant.helpers import template
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -39,6 +38,9 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
|
||||
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
"""Add MQTT Rollershutter."""
|
||||
value_template = config.get(CONF_VALUE_TEMPLATE)
|
||||
if value_template is not None:
|
||||
value_template.hass = hass
|
||||
add_devices_callback([MqttRollershutter(
|
||||
hass,
|
||||
config[CONF_NAME],
|
||||
@@ -48,7 +50,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
config[CONF_PAYLOAD_UP],
|
||||
config[CONF_PAYLOAD_DOWN],
|
||||
config[CONF_PAYLOAD_STOP],
|
||||
config.get(CONF_VALUE_TEMPLATE)
|
||||
value_template,
|
||||
)])
|
||||
|
||||
|
||||
@@ -76,8 +78,8 @@ class MqttRollershutter(RollershutterDevice):
|
||||
def message_received(topic, payload, qos):
|
||||
"""A new MQTT message has been received."""
|
||||
if value_template is not None:
|
||||
payload = template.render_with_possible_json_value(
|
||||
hass, value_template, payload)
|
||||
payload = value_template.render_with_possible_json_value(
|
||||
payload)
|
||||
if payload.isnumeric() and 0 <= int(payload) <= 100:
|
||||
self._state = int(payload)
|
||||
self.update_ha_state()
|
||||
|
||||
Reference in new issue
Block a user