Add device automation action (#26455)
* Add support for device actions, with light as example. * Add translation; return list
This commit is contained in:
1 parent
23fdc04554
commit
b1c2a5fa08
8 files changed
+274
-2
No files matched your search
@@ -31,6 +31,53 @@ def _same_lists(a, b):
|
||||
return True
|
||||
|
||||
|
||||
async def test_websocket_get_actions(hass, hass_ws_client, device_reg, entity_reg):
|
||||
"""Test we get the expected conditions from a light through websocket."""
|
||||
await async_setup_component(hass, "device_automation", {})
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id)
|
||||
expected_actions = [
|
||||
{
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"type": "turn_off",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "light.test_5678",
|
||||
},
|
||||
{
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"type": "turn_on",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "light.test_5678",
|
||||
},
|
||||
{
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"type": "toggle",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "light.test_5678",
|
||||
},
|
||||
]
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json(
|
||||
{"id": 1, "type": "device_automation/action/list", "device_id": device_entry.id}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg["id"] == 1
|
||||
assert msg["type"] == TYPE_RESULT
|
||||
assert msg["success"]
|
||||
actions = msg["result"]
|
||||
assert _same_lists(actions, expected_actions)
|
||||
|
||||
|
||||
async def test_websocket_get_conditions(hass, hass_ws_client, device_reg, entity_reg):
|
||||
"""Test we get the expected conditions from a light through websocket."""
|
||||
await async_setup_component(hass, "device_automation", {})
|
||||
|
||||
@@ -46,6 +46,44 @@ def _same_lists(a, b):
|
||||
return True
|
||||
|
||||
|
||||
async def test_get_actions(hass, device_reg, entity_reg):
|
||||
"""Test we get the expected actions from a light."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id)
|
||||
expected_actions = [
|
||||
{
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"type": "turn_off",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "light.test_5678",
|
||||
},
|
||||
{
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"type": "turn_on",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "light.test_5678",
|
||||
},
|
||||
{
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"type": "toggle",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "light.test_5678",
|
||||
},
|
||||
]
|
||||
actions = await async_get_device_automations(
|
||||
hass, "async_get_actions", device_entry.id
|
||||
)
|
||||
assert _same_lists(actions, expected_actions)
|
||||
|
||||
|
||||
async def test_get_conditions(hass, device_reg, entity_reg):
|
||||
"""Test we get the expected conditions from a light."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
@@ -263,3 +301,78 @@ async def test_if_state(hass, calls):
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["some"] == "is_off event - test_event2"
|
||||
|
||||
|
||||
async def test_action(hass, calls):
|
||||
"""Test for turn_on and turn_off actions."""
|
||||
platform = getattr(hass.components, "test.light")
|
||||
|
||||
platform.init()
|
||||
assert await async_setup_component(
|
||||
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
)
|
||||
|
||||
dev1, dev2, dev3 = platform.DEVICES
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"action": {
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"entity_id": dev1.entity_id,
|
||||
"type": "turn_off",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event2"},
|
||||
"action": {
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"entity_id": dev1.entity_id,
|
||||
"type": "turn_on",
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event3"},
|
||||
"action": {
|
||||
"device": None,
|
||||
"domain": "light",
|
||||
"entity_id": dev1.entity_id,
|
||||
"type": "toggle",
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(dev1.entity_id).state == STATE_ON
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(dev1.entity_id).state == STATE_OFF
|
||||
|
||||
hass.bus.async_fire("test_event1")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(dev1.entity_id).state == STATE_OFF
|
||||
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(dev1.entity_id).state == STATE_ON
|
||||
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(dev1.entity_id).state == STATE_ON
|
||||
|
||||
hass.bus.async_fire("test_event3")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(dev1.entity_id).state == STATE_OFF
|
||||
|
||||
hass.bus.async_fire("test_event3")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(dev1.entity_id).state == STATE_ON
|
||||
Reference in new issue
Block a user