Store automation and script traces (#56894)
* Store automation and script traces * Pylint * Deduplicate code * Fix issues when no stored traces are available * Store serialized data for restored traces * Update WS API * Update test * Restore context * Improve tests * Add new test files * Rename restore_traces to async_restore_traces * Refactor trace.websocket_api * Defer loading stored traces * Lint * Revert refactoring which is no longer needed * Correct order when restoring traces * Apply suggestion from code review * Improve test coverage * Apply suggestions from code review
This commit is contained in:
parent
29c062fcc4
commit
961ee717ef
11 changed files with 1256 additions and 191 deletions
|
@ -1,7 +1,6 @@
|
|||
"""The tests for the Script component."""
|
||||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
@ -29,113 +28,62 @@ from homeassistant.exceptions import ServiceNotFound
|
|||
from homeassistant.helpers import template
|
||||
from homeassistant.helpers.event import async_track_state_change
|
||||
from homeassistant.helpers.service import async_get_all_descriptions
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import async_mock_service, get_test_home_assistant, mock_restore_cache
|
||||
from tests.common import async_mock_service, mock_restore_cache
|
||||
from tests.components.logbook.test_init import MockLazyEventPartialState
|
||||
|
||||
ENTITY_ID = "script.test"
|
||||
|
||||
|
||||
@bind_hass
|
||||
def turn_on(hass, entity_id, variables=None, context=None):
|
||||
"""Turn script on.
|
||||
async def test_passing_variables(hass):
|
||||
"""Test different ways of passing in variables."""
|
||||
mock_restore_cache(hass, ())
|
||||
calls = []
|
||||
context = Context()
|
||||
|
||||
This is a legacy helper method. Do not use it for new tests.
|
||||
"""
|
||||
_, object_id = split_entity_id(entity_id)
|
||||
@callback
|
||||
def record_call(service):
|
||||
"""Add recorded event to set."""
|
||||
calls.append(service)
|
||||
|
||||
hass.services.call(DOMAIN, object_id, variables, context=context)
|
||||
hass.services.async_register("test", "script", record_call)
|
||||
|
||||
|
||||
@bind_hass
|
||||
def turn_off(hass, entity_id):
|
||||
"""Turn script on.
|
||||
|
||||
This is a legacy helper method. Do not use it for new tests.
|
||||
"""
|
||||
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
|
||||
|
||||
|
||||
@bind_hass
|
||||
def toggle(hass, entity_id):
|
||||
"""Toggle the script.
|
||||
|
||||
This is a legacy helper method. Do not use it for new tests.
|
||||
"""
|
||||
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
|
||||
|
||||
|
||||
@bind_hass
|
||||
def reload(hass):
|
||||
"""Reload script component.
|
||||
|
||||
This is a legacy helper method. Do not use it for new tests.
|
||||
"""
|
||||
hass.services.call(DOMAIN, SERVICE_RELOAD)
|
||||
|
||||
|
||||
class TestScriptComponent(unittest.TestCase):
|
||||
"""Test the Script component."""
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
self.addCleanup(self.tear_down_cleanup)
|
||||
|
||||
def tear_down_cleanup(self):
|
||||
"""Stop down everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_passing_variables(self):
|
||||
"""Test different ways of passing in variables."""
|
||||
mock_restore_cache(self.hass, ())
|
||||
calls = []
|
||||
context = Context()
|
||||
|
||||
@callback
|
||||
def record_call(service):
|
||||
"""Add recorded event to set."""
|
||||
calls.append(service)
|
||||
|
||||
self.hass.services.register("test", "script", record_call)
|
||||
|
||||
assert setup_component(
|
||||
self.hass,
|
||||
"script",
|
||||
{
|
||||
"script": {
|
||||
"test": {
|
||||
"sequence": {
|
||||
"service": "test.script",
|
||||
"data_template": {"hello": "{{ greeting }}"},
|
||||
}
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"script",
|
||||
{
|
||||
"script": {
|
||||
"test": {
|
||||
"sequence": {
|
||||
"service": "test.script",
|
||||
"data_template": {"hello": "{{ greeting }}"},
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
turn_on(self.hass, ENTITY_ID, {"greeting": "world"}, context=context)
|
||||
await hass.services.async_call(
|
||||
DOMAIN, "test", {"greeting": "world"}, context=context
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context is context
|
||||
assert calls[0].data["hello"] == "world"
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context is context
|
||||
assert calls[0].data["hello"] == "world"
|
||||
|
||||
self.hass.services.call(
|
||||
"script", "test", {"greeting": "universe"}, context=context
|
||||
)
|
||||
await hass.services.async_call(
|
||||
"script", "test", {"greeting": "universe"}, context=context
|
||||
)
|
||||
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 2
|
||||
assert calls[1].context is context
|
||||
assert calls[1].data["hello"] == "universe"
|
||||
assert len(calls) == 2
|
||||
assert calls[1].context is context
|
||||
assert calls[1].data["hello"] == "universe"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("toggle", [False, True])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue