mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 05:37:44 +00:00
Cache condition in helpers.Script (#3797)
* Cache condition in helpers.Script The caching is a simple in-memory, per-instance dictionary. We use __str__ to format cache keys. This naive implementation has some disadvantages (e.g., we won't be able to cache two conditions that contain references to distinct-but-equivalent object instances and we don't have any control over the size of the condition cache), but for most simple use-cases the approach should be good enough. Resolves #3629 * Fix docstring style
This commit is contained in:
parent
a2503e4d13
commit
711526e574
@ -51,6 +51,7 @@ class Script():
|
|||||||
in self.sequence)
|
in self.sequence)
|
||||||
self._async_unsub_delay_listener = None
|
self._async_unsub_delay_listener = None
|
||||||
self._template_cache = {}
|
self._template_cache = {}
|
||||||
|
self._config_cache = {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_running(self) -> bool:
|
def is_running(self) -> bool:
|
||||||
@ -153,9 +154,14 @@ class Script():
|
|||||||
|
|
||||||
def _async_check_condition(self, action, variables):
|
def _async_check_condition(self, action, variables):
|
||||||
"""Test if condition is matching."""
|
"""Test if condition is matching."""
|
||||||
|
config_cache_key = frozenset((k, str(v)) for k, v in action.items())
|
||||||
|
config = self._config_cache.get(config_cache_key)
|
||||||
|
if not config:
|
||||||
|
config = condition.async_from_config(action, False)
|
||||||
|
self._config_cache[config_cache_key] = config
|
||||||
|
|
||||||
self.last_action = action.get(CONF_ALIAS, action[CONF_CONDITION])
|
self.last_action = action.get(CONF_ALIAS, action[CONF_CONDITION])
|
||||||
check = condition.async_from_config(action, False)(
|
check = config(self.hass, variables)
|
||||||
self.hass, variables)
|
|
||||||
self._log("Test condition {}: {}".format(self.last_action, check))
|
self._log("Test condition {}: {}".format(self.last_action, check))
|
||||||
return check
|
return check
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""The tests for the Script component."""
|
"""The tests for the Script component."""
|
||||||
# pylint: disable=too-many-public-methods,protected-access
|
# pylint: disable=too-many-public-methods,protected-access
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from unittest import mock
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
# Otherwise can't test just this file (import order issue)
|
# Otherwise can't test just this file (import order issue)
|
||||||
@ -279,3 +280,62 @@ class TestScriptHelper(unittest.TestCase):
|
|||||||
script_obj.run()
|
script_obj.run()
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
assert len(events) == 3
|
assert len(events) == 3
|
||||||
|
|
||||||
|
@mock.patch('homeassistant.helpers.script.condition.async_from_config')
|
||||||
|
def test_condition_created_once(self, async_from_config):
|
||||||
|
"""Test that the conditions do not get created multiple times."""
|
||||||
|
event = 'test_event'
|
||||||
|
events = []
|
||||||
|
|
||||||
|
def record_event(event):
|
||||||
|
"""Add recorded event to set."""
|
||||||
|
events.append(event)
|
||||||
|
|
||||||
|
self.hass.bus.listen(event, record_event)
|
||||||
|
|
||||||
|
self.hass.states.set('test.entity', 'hello')
|
||||||
|
|
||||||
|
script_obj = script.Script(self.hass, cv.SCRIPT_SCHEMA([
|
||||||
|
{'event': event},
|
||||||
|
{
|
||||||
|
'condition': 'template',
|
||||||
|
'value_template': '{{ states.test.entity.state == "hello" }}',
|
||||||
|
},
|
||||||
|
{'event': event},
|
||||||
|
]))
|
||||||
|
|
||||||
|
script_obj.run()
|
||||||
|
script_obj.run()
|
||||||
|
self.hass.block_till_done()
|
||||||
|
assert async_from_config.call_count == 1
|
||||||
|
assert len(script_obj._config_cache) == 1
|
||||||
|
|
||||||
|
def test_all_conditions_cached(self):
|
||||||
|
"""Test that multiple conditions get cached."""
|
||||||
|
event = 'test_event'
|
||||||
|
events = []
|
||||||
|
|
||||||
|
def record_event(event):
|
||||||
|
"""Add recorded event to set."""
|
||||||
|
events.append(event)
|
||||||
|
|
||||||
|
self.hass.bus.listen(event, record_event)
|
||||||
|
|
||||||
|
self.hass.states.set('test.entity', 'hello')
|
||||||
|
|
||||||
|
script_obj = script.Script(self.hass, cv.SCRIPT_SCHEMA([
|
||||||
|
{'event': event},
|
||||||
|
{
|
||||||
|
'condition': 'template',
|
||||||
|
'value_template': '{{ states.test.entity.state == "hello" }}',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'condition': 'template',
|
||||||
|
'value_template': '{{ states.test.entity.state != "hello" }}',
|
||||||
|
},
|
||||||
|
{'event': event},
|
||||||
|
]))
|
||||||
|
|
||||||
|
script_obj.run()
|
||||||
|
self.hass.block_till_done()
|
||||||
|
assert len(script_obj._config_cache) == 2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user