Make async_track_template_result track multiple templates (#39371)

* Make async_track_template_result track multiple templates

Combine template entity updates to only write ha
state once per template group update

* Make async_track_template_result use dataclasses for input/output

* black versions

* naming
This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-08-31 19:07:40 -05:00
1 parent 8d68963854
commit a77e09b2c2
8 files changed
+508 -215

No files matched your search

@@ -15,7 +15,7 @@ from homeassistant.exceptions import (
Unauthorized,
)
from homeassistant.helpers import config_validation as cv, entity
from homeassistant.helpers.event import async_track_template_result
from homeassistant.helpers.event import TrackTemplate, async_track_template_result
from homeassistant.helpers.service import async_get_all_descriptions
from homeassistant.loader import IntegrationNotFound, async_get_integration
@@ -255,19 +255,23 @@ def handle_render_template(hass, connection, msg):
variables = msg.get("variables")
@callback
def _template_listener(event, template, last_result, result):
def _template_listener(event, updates):
track_template_result = updates.pop()
result = track_template_result.result
if isinstance(result, TemplateError):
_LOGGER.error(
"TemplateError('%s') " "while processing template '%s'",
result,
template,
track_template_result.template,
)
result = None
connection.send_message(messages.event_message(msg["id"], {"result": result}))
info = async_track_template_result(hass, template, _template_listener, variables)
info = async_track_template_result(
hass, [TrackTemplate(template, variables)], _template_listener
)
connection.subscriptions[msg["id"]] = info.async_remove