Find related items scripts/automations (#31293)
* Find related items scripts/automations * Update manifest
This commit is contained in:
1 parent
881437c085
commit
424e15c7a7
11 files changed
+586
-49
No files matched your search
@@ -5,6 +5,7 @@ from unittest.mock import Mock, patch
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.components.automation import DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_NAME,
|
||||
@@ -922,3 +923,80 @@ async def test_automation_restore_last_triggered_with_initial_state(hass):
|
||||
assert state
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes["last_triggered"] == time
|
||||
|
||||
|
||||
async def test_extraction_functions(hass):
|
||||
"""Test extraction functions."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: [
|
||||
{
|
||||
"alias": "test1",
|
||||
"trigger": {"platform": "state", "entity_id": "sensor.trigger_1"},
|
||||
"action": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": "light.in_both"},
|
||||
},
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": "light.in_first"},
|
||||
},
|
||||
{
|
||||
"domain": "light",
|
||||
"device_id": "device-in-both",
|
||||
"entity_id": "light.bla",
|
||||
"type": "turn_on",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"alias": "test2",
|
||||
"trigger": {"platform": "state", "entity_id": "sensor.trigger_2"},
|
||||
"action": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": "light.in_both"},
|
||||
},
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "sensor.condition",
|
||||
"state": "100",
|
||||
},
|
||||
{"scene": "scene.hello"},
|
||||
{
|
||||
"domain": "light",
|
||||
"device_id": "device-in-both",
|
||||
"entity_id": "light.bla",
|
||||
"type": "turn_on",
|
||||
},
|
||||
{
|
||||
"domain": "light",
|
||||
"device_id": "device-in-last",
|
||||
"entity_id": "light.bla",
|
||||
"type": "turn_on",
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
assert set(automation.automations_with_entity(hass, "light.in_both")) == {
|
||||
"automation.test1",
|
||||
"automation.test2",
|
||||
}
|
||||
assert set(automation.entities_in_automation(hass, "automation.test1")) == {
|
||||
"light.in_both",
|
||||
"light.in_first",
|
||||
}
|
||||
assert set(automation.automations_with_device(hass, "device-in-both")) == {
|
||||
"automation.test1",
|
||||
"automation.test2",
|
||||
}
|
||||
assert set(automation.devices_in_automation(hass, "automation.test2")) == {
|
||||
"device-in-both",
|
||||
"device-in-last",
|
||||
}
|
||||
@@ -358,9 +358,8 @@ async def test_turning_no_scripts_off(hass):
|
||||
|
||||
async def test_async_get_descriptions_script(hass):
|
||||
"""Test async_set_service_schema for the script integration."""
|
||||
script = hass.components.script
|
||||
script_config = {
|
||||
script.DOMAIN: {
|
||||
DOMAIN: {
|
||||
"test1": {"sequence": [{"service": "homeassistant.restart"}]},
|
||||
"test2": {
|
||||
"description": "test2",
|
||||
@@ -375,18 +374,75 @@ async def test_async_get_descriptions_script(hass):
|
||||
}
|
||||
}
|
||||
|
||||
await async_setup_component(hass, script.DOMAIN, script_config)
|
||||
await async_setup_component(hass, DOMAIN, script_config)
|
||||
descriptions = await hass.helpers.service.async_get_all_descriptions()
|
||||
|
||||
assert descriptions[script.DOMAIN]["test1"]["description"] == ""
|
||||
assert not descriptions[script.DOMAIN]["test1"]["fields"]
|
||||
assert descriptions[DOMAIN]["test1"]["description"] == ""
|
||||
assert not descriptions[DOMAIN]["test1"]["fields"]
|
||||
|
||||
assert descriptions[script.DOMAIN]["test2"]["description"] == "test2"
|
||||
assert descriptions[DOMAIN]["test2"]["description"] == "test2"
|
||||
assert (
|
||||
descriptions[script.DOMAIN]["test2"]["fields"]["param"]["description"]
|
||||
descriptions[DOMAIN]["test2"]["fields"]["param"]["description"]
|
||||
== "param_description"
|
||||
)
|
||||
assert (
|
||||
descriptions[script.DOMAIN]["test2"]["fields"]["param"]["example"]
|
||||
== "param_example"
|
||||
descriptions[DOMAIN]["test2"]["fields"]["param"]["example"] == "param_example"
|
||||
)
|
||||
|
||||
|
||||
async def test_extraction_functions(hass):
|
||||
"""Test extraction functions."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
"test1": {
|
||||
"sequence": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": "light.in_both"},
|
||||
},
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": "light.in_first"},
|
||||
},
|
||||
{"domain": "light", "device_id": "device-in-both"},
|
||||
]
|
||||
},
|
||||
"test2": {
|
||||
"sequence": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": "light.in_both"},
|
||||
},
|
||||
{
|
||||
"condition": "state",
|
||||
"entity_id": "sensor.condition",
|
||||
"state": "100",
|
||||
},
|
||||
{"scene": "scene.hello"},
|
||||
{"domain": "light", "device_id": "device-in-both"},
|
||||
{"domain": "light", "device_id": "device-in-last"},
|
||||
],
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert set(script.scripts_with_entity(hass, "light.in_both")) == {
|
||||
"script.test1",
|
||||
"script.test2",
|
||||
}
|
||||
assert set(script.entities_in_script(hass, "script.test1")) == {
|
||||
"light.in_both",
|
||||
"light.in_first",
|
||||
}
|
||||
assert set(script.scripts_with_device(hass, "device-in-both")) == {
|
||||
"script.test1",
|
||||
"script.test2",
|
||||
}
|
||||
assert set(script.devices_in_script(hass, "script.test2")) == {
|
||||
"device-in-both",
|
||||
"device-in-last",
|
||||
}
|
||||
@@ -131,6 +131,62 @@ async def test_search(hass):
|
||||
},
|
||||
)
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"script",
|
||||
{
|
||||
"script": {
|
||||
"wled": {
|
||||
"sequence": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": wled_segment_1_entity.entity_id},
|
||||
},
|
||||
]
|
||||
},
|
||||
"hue": {
|
||||
"sequence": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": hue_segment_1_entity.entity_id},
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"automation",
|
||||
{
|
||||
"automation": [
|
||||
{
|
||||
"alias": "wled_entity",
|
||||
"trigger": {"platform": "state", "entity_id": "sensor.trigger_1"},
|
||||
"action": [
|
||||
{
|
||||
"service": "test.script",
|
||||
"data": {"entity_id": wled_segment_1_entity.entity_id},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"alias": "wled_device",
|
||||
"trigger": {"platform": "state", "entity_id": "sensor.trigger_1"},
|
||||
"action": [
|
||||
{
|
||||
"domain": "light",
|
||||
"device_id": wled_device.id,
|
||||
"entity_id": wled_segment_1_entity.entity_id,
|
||||
"type": "turn_on",
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# Explore the graph from every node and make sure we find the same results
|
||||
expected = {
|
||||
"config_entry": {wled_config_entry.entry_id},
|
||||
@@ -139,6 +195,8 @@ async def test_search(hass):
|
||||
"entity": {wled_segment_1_entity.entity_id, wled_segment_2_entity.entity_id},
|
||||
"scene": {"scene.scene_wled_seg_1", "scene.scene_wled_hue"},
|
||||
"group": {"group.wled", "group.wled_hue"},
|
||||
"script": {"script.wled"},
|
||||
"automation": {"automation.wled_entity", "automation.wled_device"},
|
||||
}
|
||||
|
||||
for search_type, search_id in (
|
||||
@@ -149,6 +207,9 @@ async def test_search(hass):
|
||||
("entity", wled_segment_2_entity.entity_id),
|
||||
("scene", "scene.scene_wled_seg_1"),
|
||||
("group", "group.wled"),
|
||||
("script", "script.wled"),
|
||||
("automation", "automation.wled_entity"),
|
||||
("automation", "automation.wled_device"),
|
||||
):
|
||||
searcher = search.Searcher(hass, device_reg, entity_reg)
|
||||
results = searcher.async_search(search_type, search_id)
|
||||
@@ -176,6 +237,8 @@ async def test_search(hass):
|
||||
"scene.scene_wled_hue",
|
||||
},
|
||||
"group": {"group.wled", "group.hue", "group.wled_hue"},
|
||||
"script": {"script.wled", "script.hue"},
|
||||
"automation": {"automation.wled_entity", "automation.wled_device"},
|
||||
}
|
||||
for search_type, search_id in (
|
||||
("scene", "scene.scene_wled_hue"),
|
||||
|
||||
Reference in new issue
Block a user