Align WS event message format with config_entries/subscribe

This commit is contained in:
Erik
2025-04-10 09:20:39 +02:00
parent c0ce2b6f25
commit 622f8b7b06
3 changed files with 73 additions and 61 deletions

View File

@@ -397,39 +397,43 @@ def config_entries_flow_subscribe(
@callback @callback
def async_on_flow_init_remove(change_type: str, flow_id: str) -> None: def async_on_flow_init_remove(change_type: str, flow_id: str) -> None:
"""Forward config entry state events to websocket.""" """Forward config entry state events to websocket."""
if change_type == "remove": if change_type == "removed":
connection.send_message( connection.send_message(
websocket_api.event_message( websocket_api.event_message(
msg["id"], msg["id"],
{"type": change_type, "flow_id": flow_id}, [{"type": change_type, "flow_id": flow_id}],
) )
) )
return return
# change_type == "init" # change_type == "added"
connection.send_message( connection.send_message(
websocket_api.event_message( websocket_api.event_message(
msg["id"], msg["id"],
[
{ {
"type": change_type, "type": change_type,
"flow_id": flow_id, "flow_id": flow_id,
"flow": hass.config_entries.flow.async_get(flow_id), "flow": hass.config_entries.flow.async_get(flow_id),
}, }
],
) )
) )
connection.subscriptions[msg["id"]] = hass.config_entries.flow.async_subscribe_flow( connection.subscriptions[msg["id"]] = hass.config_entries.flow.async_subscribe_flow(
async_on_flow_init_remove async_on_flow_init_remove
) )
for flw in hass.config_entries.flow.async_progress():
if flw["context"]["source"] in (
config_entries.SOURCE_RECONFIGURE,
config_entries.SOURCE_USER,
):
continue
connection.send_message( connection.send_message(
websocket_api.event_message( websocket_api.event_message(
msg["id"], msg["id"],
{"type": "init", "flow_id": flw["flow_id"], "flow": flw}, [
{"type": None, "flow_id": flw["flow_id"], "flow": flw}
for flw in hass.config_entries.flow.async_progress()
if flw["context"]["source"]
not in (
config_entries.SOURCE_RECONFIGURE,
config_entries.SOURCE_USER,
)
],
) )
) )
connection.send_result(msg["id"]) connection.send_result(msg["id"])

View File

@@ -1467,7 +1467,7 @@ class ConfigEntriesFlowManager(
): ):
# Notify listeners that a flow is created # Notify listeners that a flow is created
for subscription in self._flow_subscriptions: for subscription in self._flow_subscriptions:
subscription("init", flow.flow_id) subscription("added", flow.flow_id)
return result return result
@@ -1768,7 +1768,7 @@ class ConfigEntriesFlowManager(
): ):
return return
for listeners in self._flow_subscriptions: for listeners in self._flow_subscriptions:
listeners("remove", flow_id) listeners("removed", flow_id)
class ConfigEntryItems(UserDict[str, ConfigEntry]): class ConfigEntryItems(UserDict[str, ConfigEntry]):

View File

@@ -935,10 +935,11 @@ async def test_get_progress_subscribe(
assert self._get_reconfigure_entry() is entry assert self._get_reconfigure_entry() is entry
return await self.async_step_account() return await self.async_step_account()
await ws_client.send_json_auto_id({"type": "config_entries/flow/subscribe"}) await ws_client.send_json({"id": 1, "type": "config_entries/flow/subscribe"})
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response == {"id": ANY, "result": None, "success": True, "type": "result"} assert response == {"id": 1, "event": [], "type": "event"}
subscription = response["id"] response = await ws_client.receive_json()
assert response == {"id": 1, "result": None, "success": True, "type": "result"}
flow_context = { flow_context = {
"bluetooth": {"source": core_ce.SOURCE_BLUETOOTH}, "bluetooth": {"source": core_ce.SOURCE_BLUETOOTH},
@@ -968,7 +969,8 @@ async def test_get_progress_subscribe(
for key in ("hassio", "reauth"): for key in ("hassio", "reauth"):
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response == { assert response == {
"event": { "event": [
{
"flow": { "flow": {
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"handler": "test", "handler": "test",
@@ -976,18 +978,21 @@ async def test_get_progress_subscribe(
"context": flow_context[key], "context": flow_context[key],
}, },
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"type": "init", "type": "added",
}, }
"id": subscription, ],
"id": 1,
"type": "event", "type": "event",
} }
for key in ("hassio", "reauth"): for key in ("hassio", "reauth"):
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response == { assert response == {
"event": { "event": [
{
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"type": "remove", "type": "removed",
}, }
],
"id": 1, "id": 1,
"type": "event", "type": "event",
} }
@@ -1071,11 +1076,11 @@ async def test_get_progress_subscribe_in_progress(
# should be filtered out # should be filtered out
responses = [] responses = []
responses.append(await ws_client.receive_json()) responses.append(await ws_client.receive_json())
responses.append(await ws_client.receive_json()) assert responses == [
assert responses == unordered( {
"event": unordered(
[ [
{ {
"event": {
"flow": { "flow": {
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"handler": "test", "handler": "test",
@@ -1083,14 +1088,15 @@ async def test_get_progress_subscribe_in_progress(
"context": flow_context[key], "context": flow_context[key],
}, },
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"type": "init", "type": None,
},
"id": 1,
"type": "event",
} }
for key in ("hassio", "reauth") for key in ("hassio", "reauth")
] ]
) ),
"id": 1,
"type": "event",
}
]
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response == {"id": ANY, "result": None, "success": True, "type": "result"} assert response == {"id": ANY, "result": None, "success": True, "type": "result"}
@@ -1101,10 +1107,12 @@ async def test_get_progress_subscribe_in_progress(
for key in ("hassio", "reauth"): for key in ("hassio", "reauth"):
response = await ws_client.receive_json() response = await ws_client.receive_json()
assert response == { assert response == {
"event": { "event": [
{
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"type": "remove", "type": "removed",
}, }
],
"id": 1, "id": 1,
"type": "event", "type": "event",
} }