Allow non-admins to subscribe to conditions over the websocket (#175123)

This commit is contained in:
Petar Petrov
2026-09-04 18:50:13 +02:00
committed by GitHub
parent 5eaeb20bb3
commit 0828053500
2 changed files with 56 additions and 1 deletions
@@ -1114,7 +1114,6 @@ async def handle_test_condition(
vol.Required("condition"): cv.CONDITION_SCHEMA,
}
)
@decorators.require_admin
@decorators.async_response
async def handle_subscribe_condition(
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
@@ -2945,6 +2945,32 @@ async def test_test_condition(
assert msg["result"]["result"] is False
async def test_test_condition_requires_admin(
hass: HomeAssistant,
websocket_client: MockHAClientWebSocket,
hass_admin_user: MockUser,
) -> None:
"""Test testing a condition requires admin."""
hass_admin_user.groups = []
hass.states.async_set("hello.world", "paulus")
await websocket_client.send_json_auto_id(
{
"type": "test_condition",
"condition": {
"condition": "state",
"entity_id": "hello.world",
"state": "paulus",
},
}
)
msg = await websocket_client.receive_json()
assert msg["type"] == const.TYPE_RESULT
assert not msg["success"]
assert msg["error"]["code"] == const.ERR_UNAUTHORIZED
@pytest.mark.parametrize(
("value_template", "expected_template_errors"),
[
@@ -3114,6 +3140,36 @@ async def test_subscribe_condition(
}
async def test_subscribe_condition_non_admin(
hass: HomeAssistant,
websocket_client: MockHAClientWebSocket,
hass_admin_user: MockUser,
) -> None:
"""Test subscribing to a condition does not require admin."""
hass_admin_user.groups = []
hass.states.async_set("hello.world", "paulus")
await websocket_client.send_json_auto_id(
{
"type": "subscribe_condition",
"condition": {
"condition": "state",
"entity_id": "hello.world",
"state": "paulus",
},
}
)
msg = await websocket_client.receive_json()
assert msg["type"] == const.TYPE_RESULT
assert msg["success"]
subscription_id = msg["id"]
msg = await websocket_client.receive_json()
assert msg == {"id": subscription_id, "type": "event", "event": {"result": True}}
@pytest.mark.parametrize(
("value_template", "expected_event"),
[