init home-assistant
This commit is contained in:
1
home-assistant/tests/components/script/__init__.py
Normal file
1
home-assistant/tests/components/script/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the script component."""
|
||||
8
home-assistant/tests/components/script/conftest.py
Normal file
8
home-assistant/tests/components/script/conftest.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""Conftest for script tests."""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
||||
"""Stub copying the blueprints to the config folder."""
|
||||
139
home-assistant/tests/components/script/test_blueprint.py
Normal file
139
home-assistant/tests/components/script/test_blueprint.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""Test script blueprints."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Iterator
|
||||
import contextlib
|
||||
import pathlib
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import script
|
||||
from homeassistant.components.blueprint import (
|
||||
BLUEPRINT_SCHEMA,
|
||||
Blueprint,
|
||||
DomainBlueprints,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import Context, HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr, template
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import yaml
|
||||
|
||||
from tests.common import MockConfigEntry, async_mock_service
|
||||
|
||||
BUILTIN_BLUEPRINT_FOLDER = pathlib.Path(script.__file__).parent / "blueprints"
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def patch_blueprint(blueprint_path: str, data_path: str) -> Iterator[None]:
|
||||
"""Patch blueprint loading from a different source."""
|
||||
orig_load = DomainBlueprints._load_blueprint
|
||||
|
||||
@callback
|
||||
def mock_load_blueprint(self, path: str) -> Blueprint:
|
||||
if path != blueprint_path:
|
||||
pytest.fail(f"Unexpected blueprint {path}")
|
||||
return orig_load(self, path)
|
||||
|
||||
return Blueprint(
|
||||
yaml.load_yaml(data_path),
|
||||
expected_domain=self.domain,
|
||||
path=path,
|
||||
schema=BLUEPRINT_SCHEMA,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.blueprint.models.DomainBlueprints._load_blueprint",
|
||||
mock_load_blueprint,
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
async def test_confirmable_notification(
|
||||
hass: HomeAssistant, device_registry: dr.DeviceRegistry
|
||||
) -> None:
|
||||
"""Test confirmable notification blueprint."""
|
||||
config_entry = MockConfigEntry(domain="fake_integration", data={})
|
||||
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
frodo = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "00:00:00:00:00:01")},
|
||||
)
|
||||
|
||||
with patch_blueprint(
|
||||
"confirmable_notification.yaml",
|
||||
BUILTIN_BLUEPRINT_FOLDER / "confirmable_notification.yaml",
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
script.DOMAIN,
|
||||
{
|
||||
"script": {
|
||||
"confirm": {
|
||||
"use_blueprint": {
|
||||
"path": "confirmable_notification.yaml",
|
||||
"input": {
|
||||
"notify_device": frodo.id,
|
||||
"title": "Lord of the things",
|
||||
"message": "Throw ring in mountain?",
|
||||
"confirm_action": [
|
||||
{
|
||||
"action": "homeassistant.turn_on",
|
||||
"target": {"entity_id": "mount.doom"},
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
turn_on_calls = async_mock_service(hass, "homeassistant", "turn_on")
|
||||
context = Context()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.mobile_app.device_action.async_call_action_from_config"
|
||||
) as mock_call_action:
|
||||
# Trigger script
|
||||
await hass.services.async_call(script.DOMAIN, "confirm", context=context)
|
||||
|
||||
# Give script the time to attach the trigger.
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
hass.bus.async_fire("mobile_app_notification_action", {"action": "ANYTHING_ELSE"})
|
||||
hass.bus.async_fire(
|
||||
"mobile_app_notification_action", {"action": "CONFIRM_" + Context().id}
|
||||
)
|
||||
hass.bus.async_fire(
|
||||
"mobile_app_notification_action", {"action": "CONFIRM_" + context.id}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_call_action.mock_calls) == 1
|
||||
_hass, config, variables, _context = mock_call_action.mock_calls[0][1]
|
||||
|
||||
rendered_config = template.render_complex(config, variables)
|
||||
|
||||
assert rendered_config == {
|
||||
"title": "Lord of the things",
|
||||
"message": "Throw ring in mountain?",
|
||||
"alias": "Send notification",
|
||||
"domain": "mobile_app",
|
||||
"type": "notify",
|
||||
"device_id": frodo.id,
|
||||
"data": {
|
||||
"actions": [
|
||||
{"action": "CONFIRM_" + _context.id, "title": "Confirm"},
|
||||
{"action": "DISMISS_" + _context.id, "title": "Dismiss"},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
assert len(turn_on_calls) == 1
|
||||
assert turn_on_calls[0].data == {
|
||||
"entity_id": ["mount.doom"],
|
||||
}
|
||||
1874
home-assistant/tests/components/script/test_init.py
Normal file
1874
home-assistant/tests/components/script/test_init.py
Normal file
File diff suppressed because it is too large
Load Diff
81
home-assistant/tests/components/script/test_recorder.py
Normal file
81
home-assistant/tests/components/script/test_recorder.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""The tests for script recorder."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import script
|
||||
from homeassistant.components.recorder import Recorder
|
||||
from homeassistant.components.recorder.history import get_significant_states
|
||||
from homeassistant.components.script import (
|
||||
ATTR_CUR,
|
||||
ATTR_LAST_ACTION,
|
||||
ATTR_LAST_TRIGGERED,
|
||||
ATTR_MAX,
|
||||
ATTR_MODE,
|
||||
)
|
||||
from homeassistant.const import ATTR_FRIENDLY_NAME
|
||||
from homeassistant.core import Context, HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import async_mock_service
|
||||
from tests.components.recorder.common import async_wait_recording_done
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass: HomeAssistant) -> list[ServiceCall]:
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
async def test_exclude_attributes(
|
||||
recorder_mock: Recorder, hass: HomeAssistant, calls: list[ServiceCall]
|
||||
) -> None:
|
||||
"""Test automation registered attributes to be excluded."""
|
||||
now = dt_util.utcnow()
|
||||
await hass.async_block_till_done()
|
||||
calls = []
|
||||
context = Context()
|
||||
|
||||
@callback
|
||||
def record_call(service):
|
||||
"""Add recorded event to set."""
|
||||
calls.append(service)
|
||||
|
||||
hass.services.async_register("test", "script", record_call)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"script",
|
||||
{
|
||||
"script": {
|
||||
"test": {
|
||||
"sequence": {
|
||||
"action": "test.script",
|
||||
"data_template": {"hello": "{{ greeting }}"},
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
script.DOMAIN, "test", {"greeting": "world"}, context=context
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await async_wait_recording_done(hass)
|
||||
assert len(calls) == 1
|
||||
|
||||
states = await hass.async_add_executor_job(
|
||||
get_significant_states, hass, now, None, hass.states.async_entity_ids()
|
||||
)
|
||||
assert len(states) >= 1
|
||||
for entity_states in states.values():
|
||||
for state in entity_states:
|
||||
assert ATTR_LAST_TRIGGERED not in state.attributes
|
||||
assert ATTR_MODE not in state.attributes
|
||||
assert ATTR_CUR not in state.attributes
|
||||
assert ATTR_LAST_ACTION not in state.attributes
|
||||
assert ATTR_MAX not in state.attributes
|
||||
assert ATTR_FRIENDLY_NAME in state.attributes
|
||||
Reference in New Issue
Block a user