mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use real devices in device automation tests (#102736)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
aa36229519
commit
789a00043a
@ -13,7 +13,7 @@ from homeassistant.components.device_automation import (
|
||||
toggle_entity,
|
||||
)
|
||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.const import CONF_PLATFORM, STATE_OFF, STATE_ON
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
@ -908,7 +908,11 @@ async def test_automation_with_non_existing_integration(
|
||||
|
||||
|
||||
async def test_automation_with_device_action(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, fake_integration
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
fake_integration,
|
||||
) -> None:
|
||||
"""Test automation with a device action."""
|
||||
|
||||
@ -916,6 +920,16 @@ async def test_automation_with_device_action(
|
||||
module = module_cache["fake_integration.device_action"]
|
||||
module.async_call_action_from_config = AsyncMock()
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
"fake_integration", "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@ -924,9 +938,9 @@ async def test_automation_with_device_action(
|
||||
"alias": "hello",
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"action": {
|
||||
"device_id": "",
|
||||
"device_id": device_entry.id,
|
||||
"domain": "fake_integration",
|
||||
"entity_id": "blah.blah",
|
||||
"entity_id": entity_entry.id,
|
||||
"type": "turn_on",
|
||||
},
|
||||
}
|
||||
@ -999,7 +1013,11 @@ async def test_automation_with_integration_without_device_action(
|
||||
|
||||
|
||||
async def test_automation_with_device_condition(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, fake_integration
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
fake_integration,
|
||||
) -> None:
|
||||
"""Test automation with a device condition."""
|
||||
|
||||
@ -1007,6 +1025,16 @@ async def test_automation_with_device_condition(
|
||||
module = module_cache["fake_integration.device_condition"]
|
||||
module.async_condition_from_config = Mock()
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
"fake_integration", "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@ -1016,9 +1044,9 @@ async def test_automation_with_device_condition(
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"condition": {
|
||||
"condition": "device",
|
||||
"device_id": "none",
|
||||
"device_id": device_entry.id,
|
||||
"domain": "fake_integration",
|
||||
"entity_id": "blah.blah",
|
||||
"entity_id": entity_entry.id,
|
||||
"type": "is_on",
|
||||
},
|
||||
"action": {"service": "test.automation", "entity_id": "hello.world"},
|
||||
@ -1098,7 +1126,11 @@ async def test_automation_with_integration_without_device_condition(
|
||||
|
||||
|
||||
async def test_automation_with_device_trigger(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, fake_integration
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
fake_integration,
|
||||
) -> None:
|
||||
"""Test automation with a device trigger."""
|
||||
|
||||
@ -1106,6 +1138,16 @@ async def test_automation_with_device_trigger(
|
||||
module = module_cache["fake_integration.device_trigger"]
|
||||
module.async_attach_trigger = AsyncMock()
|
||||
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry = entity_registry.async_get_or_create(
|
||||
"fake_integration", "test", "5678", device_id=device_entry.id
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@ -1114,9 +1156,9 @@ async def test_automation_with_device_trigger(
|
||||
"alias": "hello",
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"device_id": "none",
|
||||
"device_id": device_entry.id,
|
||||
"domain": "fake_integration",
|
||||
"entity_id": "blah.blah",
|
||||
"entity_id": entity_entry.id,
|
||||
"type": "turned_off",
|
||||
},
|
||||
"action": {"service": "test.automation", "entity_id": "hello.world"},
|
||||
@ -1199,9 +1241,20 @@ async def test_automation_with_integration_without_device_trigger(
|
||||
|
||||
|
||||
async def test_automation_with_bad_action(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test automation with bad device action."""
|
||||
config_entry = MockConfigEntry(domain="fake_integration", data={})
|
||||
config_entry.state = config_entries.ConfigEntryState.LOADED
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@ -1209,7 +1262,7 @@ async def test_automation_with_bad_action(
|
||||
automation.DOMAIN: {
|
||||
"alias": "hello",
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"action": {"device_id": "", "domain": "light"},
|
||||
"action": {"device_id": device_entry.id, "domain": "light"},
|
||||
}
|
||||
},
|
||||
)
|
||||
@ -1218,9 +1271,20 @@ async def test_automation_with_bad_action(
|
||||
|
||||
|
||||
async def test_automation_with_bad_condition_action(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test automation with bad device action."""
|
||||
config_entry = MockConfigEntry(domain="fake_integration", data={})
|
||||
config_entry.state = config_entries.ConfigEntryState.LOADED
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@ -1228,7 +1292,11 @@ async def test_automation_with_bad_condition_action(
|
||||
automation.DOMAIN: {
|
||||
"alias": "hello",
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"action": {"condition": "device", "device_id": "", "domain": "light"},
|
||||
"action": {
|
||||
"condition": "device",
|
||||
"device_id": device_entry.id,
|
||||
"domain": "light",
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
@ -1283,16 +1351,29 @@ def calls(hass):
|
||||
|
||||
|
||||
async def test_automation_with_sub_condition(
|
||||
hass: HomeAssistant, calls, enable_custom_integrations: None
|
||||
hass: HomeAssistant,
|
||||
calls,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test automation with device condition under and/or conditions."""
|
||||
DOMAIN = "light"
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
|
||||
platform.init()
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
ent1, ent2, ent3 = platform.ENTITIES
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_entry1 = entity_registry.async_get_or_create(
|
||||
"fake_integration", "test", "0001", device_id=device_entry.id
|
||||
)
|
||||
entity_entry2 = entity_registry.async_get_or_create(
|
||||
"fake_integration", "test", "0002", device_id=device_entry.id
|
||||
)
|
||||
|
||||
hass.states.async_set(entity_entry1.entity_id, STATE_ON)
|
||||
hass.states.async_set(entity_entry2.entity_id, STATE_OFF)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
@ -1308,15 +1389,15 @@ async def test_automation_with_sub_condition(
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent1.entity_id,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry1.id,
|
||||
"type": "is_on",
|
||||
},
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent2.entity_id,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry2.id,
|
||||
"type": "is_on",
|
||||
},
|
||||
],
|
||||
@ -1339,15 +1420,15 @@ async def test_automation_with_sub_condition(
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent1.entity_id,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry1.id,
|
||||
"type": "is_on",
|
||||
},
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": ent2.entity_id,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": entity_entry2.id,
|
||||
"type": "is_on",
|
||||
},
|
||||
],
|
||||
@ -1365,8 +1446,8 @@ async def test_automation_with_sub_condition(
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(ent1.entity_id).state == STATE_ON
|
||||
assert hass.states.get(ent2.entity_id).state == STATE_OFF
|
||||
assert hass.states.get(entity_entry1.entity_id).state == STATE_ON
|
||||
assert hass.states.get(entity_entry2.entity_id).state == STATE_OFF
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.bus.async_fire("test_event1")
|
||||
@ -1374,18 +1455,18 @@ async def test_automation_with_sub_condition(
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "or event - test_event1"
|
||||
|
||||
hass.states.async_set(ent1.entity_id, STATE_OFF)
|
||||
hass.states.async_set(entity_entry1.entity_id, STATE_OFF)
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set(ent2.entity_id, STATE_ON)
|
||||
hass.states.async_set(entity_entry2.entity_id, STATE_ON)
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["some"] == "or event - test_event1"
|
||||
|
||||
hass.states.async_set(ent1.entity_id, STATE_ON)
|
||||
hass.states.async_set(entity_entry1.entity_id, STATE_ON)
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 4
|
||||
|
Loading…
x
Reference in New Issue
Block a user