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 2020-08-31 19:07:40 -05:00 committed by GitHub
parent 8d68963854
commit a77e09b2c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 508 additions and 215 deletions

View file

@ -21,6 +21,7 @@ from homeassistant.exceptions import TemplateError
from homeassistant.helpers import condition
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import (
TrackTemplate,
async_track_state_change_event,
async_track_template_result,
)
@ -187,7 +188,10 @@ class BayesianBinarySensor(BinarySensorEntity):
)
@callback
def _async_template_result_changed(event, template, last_result, result):
def _async_template_result_changed(event, updates):
track_template_result = updates.pop()
template = track_template_result.template
result = track_template_result.result
entity = event and event.data.get("entity_id")
if isinstance(result, TemplateError):
@ -215,7 +219,9 @@ class BayesianBinarySensor(BinarySensorEntity):
for template in self.observations_by_template:
info = async_track_template_result(
self.hass, template, _async_template_result_changed
self.hass,
[TrackTemplate(template, None)],
_async_template_result_changed,
)
self._callbacks.append(info)