Use real devices in automation and script tests (#102785)

This commit is contained in:
Erik Montnemery 2023-10-25 16:09:39 +02:00 committed by GitHub
parent e734a4bc53
commit b83ada8c19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 29 deletions

View file

@ -6,6 +6,7 @@ from unittest.mock import Mock, patch
import pytest
from homeassistant import config_entries
import homeassistant.components.automation as automation
from homeassistant.components.automation import (
ATTR_SOURCE,
@ -36,6 +37,7 @@ from homeassistant.core import (
callback,
)
from homeassistant.exceptions import HomeAssistantError, Unauthorized
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.script import (
SCRIPT_MODE_CHOICES,
SCRIPT_MODE_PARALLEL,
@ -49,6 +51,7 @@ from homeassistant.util import yaml
import homeassistant.util.dt as dt_util
from tests.common import (
MockConfigEntry,
MockUser,
assert_setup_component,
async_capture_events,
@ -1589,8 +1592,31 @@ async def test_extraction_functions_unavailable_automation(hass: HomeAssistant)
assert automation.entities_in_automation(hass, entity_id) == []
async def test_extraction_functions(hass: HomeAssistant) -> None:
async def test_extraction_functions(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test extraction functions."""
config_entry = MockConfigEntry(domain="fake_integration", data={})
config_entry.state = config_entries.ConfigEntryState.LOADED
config_entry.add_to_hass(hass)
condition_device = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "00:00:00:00:00:01")},
)
device_in_both = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "00:00:00:00:00:02")},
)
device_in_last = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "00:00:00:00:00:03")},
)
trigger_device_2 = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "00:00:00:00:00:04")},
)
await async_setup_component(hass, "homeassistant", {})
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
assert await async_setup_component(
@ -1652,7 +1678,7 @@ async def test_extraction_functions(hass: HomeAssistant) -> None:
},
{
"domain": "light",
"device_id": "device-in-both",
"device_id": device_in_both.id,
"entity_id": "light.bla",
"type": "turn_on",
},
@ -1670,7 +1696,7 @@ async def test_extraction_functions(hass: HomeAssistant) -> None:
"domain": "light",
"type": "turned_on",
"entity_id": "light.trigger_2",
"device_id": "trigger-device-2",
"device_id": trigger_device_2.id,
},
{
"platform": "tag",
@ -1702,7 +1728,7 @@ async def test_extraction_functions(hass: HomeAssistant) -> None:
],
"condition": {
"condition": "device",
"device_id": "condition-device",
"device_id": condition_device.id,
"domain": "light",
"type": "is_on",
"entity_id": "light.bla",
@ -1720,13 +1746,13 @@ async def test_extraction_functions(hass: HomeAssistant) -> None:
{"scene": "scene.hello"},
{
"domain": "light",
"device_id": "device-in-both",
"device_id": device_in_both.id,
"entity_id": "light.bla",
"type": "turn_on",
},
{
"domain": "light",
"device_id": "device-in-last",
"device_id": device_in_last.id,
"entity_id": "light.bla",
"type": "turn_on",
},
@ -1755,7 +1781,7 @@ async def test_extraction_functions(hass: HomeAssistant) -> None:
],
"condition": {
"condition": "device",
"device_id": "condition-device",
"device_id": condition_device.id,
"domain": "light",
"type": "is_on",
"entity_id": "light.bla",
@ -1799,15 +1825,15 @@ async def test_extraction_functions(hass: HomeAssistant) -> None:
"light.in_both",
"light.in_first",
}
assert set(automation.automations_with_device(hass, "device-in-both")) == {
assert set(automation.automations_with_device(hass, device_in_both.id)) == {
"automation.test1",
"automation.test2",
}
assert set(automation.devices_in_automation(hass, "automation.test2")) == {
"trigger-device-2",
"condition-device",
"device-in-both",
"device-in-last",
trigger_device_2.id,
condition_device.id,
device_in_both.id,
device_in_last.id,
"device-trigger-event",
"device-trigger-tag1",
"device-trigger-tag2",