mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add websocket trigger/condition commands (#39109)
This commit is contained in:
@@ -8,7 +8,7 @@ from homeassistant.components.websocket_api.auth import (
|
||||
TYPE_AUTH_REQUIRED,
|
||||
)
|
||||
from homeassistant.components.websocket_api.const import URL
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import entity
|
||||
from homeassistant.loader import async_get_integration
|
||||
@@ -654,3 +654,81 @@ async def test_entity_source_admin(hass, websocket_client, hass_admin_user):
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
assert not msg["success"]
|
||||
assert msg["error"]["code"] == const.ERR_UNAUTHORIZED
|
||||
|
||||
|
||||
async def test_subscribe_trigger(hass, websocket_client):
|
||||
"""Test subscribing to a trigger."""
|
||||
init_count = sum(hass.bus.async_listeners().values())
|
||||
|
||||
await websocket_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "subscribe_trigger",
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"variables": {"hello": "world"},
|
||||
}
|
||||
)
|
||||
|
||||
msg = await websocket_client.receive_json()
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
assert msg["success"]
|
||||
|
||||
# Verify we have a new listener
|
||||
assert sum(hass.bus.async_listeners().values()) == init_count + 1
|
||||
|
||||
context = Context()
|
||||
|
||||
hass.bus.async_fire("ignore_event")
|
||||
hass.bus.async_fire("test_event", {"hello": "world"}, context=context)
|
||||
hass.bus.async_fire("ignore_event")
|
||||
|
||||
with timeout(3):
|
||||
msg = await websocket_client.receive_json()
|
||||
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == "event"
|
||||
assert msg["event"]["context"]["id"] == context.id
|
||||
assert msg["event"]["variables"]["trigger"]["platform"] == "event"
|
||||
|
||||
event = msg["event"]["variables"]["trigger"]["event"]
|
||||
|
||||
assert event["event_type"] == "test_event"
|
||||
assert event["data"] == {"hello": "world"}
|
||||
assert event["origin"] == "LOCAL"
|
||||
|
||||
await websocket_client.send_json(
|
||||
{"id": 6, "type": "unsubscribe_events", "subscription": 5}
|
||||
)
|
||||
|
||||
msg = await websocket_client.receive_json()
|
||||
assert msg["id"] == 6
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
assert msg["success"]
|
||||
|
||||
# Check our listener got unsubscribed
|
||||
assert sum(hass.bus.async_listeners().values()) == init_count
|
||||
|
||||
|
||||
async def test_test_condition(hass, websocket_client):
|
||||
"""Test testing a condition."""
|
||||
hass.states.async_set("hello.world", "paulus")
|
||||
|
||||
await websocket_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "test_condition",
|
||||
"condition": {
|
||||
"condition": "state",
|
||||
"entity_id": "hello.world",
|
||||
"state": "paulus",
|
||||
},
|
||||
"variables": {"hello": "world"},
|
||||
}
|
||||
)
|
||||
|
||||
msg = await websocket_client.receive_json()
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == const.TYPE_RESULT
|
||||
assert msg["success"]
|
||||
assert msg["result"]["result"] is True
|
||||
|
||||
Reference in New Issue
Block a user