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,41 +397,45 @@ 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, {
"flow_id": flow_id, "type": change_type,
"flow": hass.config_entries.flow.async_get(flow_id), "flow_id": 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(): connection.send_message(
if flw["context"]["source"] in ( websocket_api.event_message(
config_entries.SOURCE_RECONFIGURE, msg["id"],
config_entries.SOURCE_USER, [
): {"type": None, "flow_id": flw["flow_id"], "flow": flw}
continue for flw in hass.config_entries.flow.async_progress()
connection.send_message( if flw["context"]["source"]
websocket_api.event_message( not in (
msg["id"], config_entries.SOURCE_RECONFIGURE,
{"type": "init", "flow_id": flw["flow_id"], "flow": flw}, 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,26 +969,30 @@ 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"],
"handler": "test",
"step_id": "account",
"context": flow_context[key],
},
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"handler": "test", "type": "added",
"step_id": "account", }
"context": flow_context[key], ],
}, "id": 1,
"flow_id": forms[key]["flow_id"],
"type": "init",
},
"id": subscription,
"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"], {
"type": "remove", "flow_id": forms[key]["flow_id"],
}, "type": "removed",
}
],
"id": 1, "id": 1,
"type": "event", "type": "event",
} }
@@ -1071,26 +1076,27 @@ 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"],
"handler": "test",
"step_id": "account",
"context": flow_context[key],
},
"flow_id": forms[key]["flow_id"], "flow_id": forms[key]["flow_id"],
"handler": "test", "type": None,
"step_id": "account", }
"context": flow_context[key], for key in ("hassio", "reauth")
}, ]
"flow_id": forms[key]["flow_id"], ),
"type": "init", "id": 1,
}, "type": "event",
"id": 1, }
"type": "event", ]
}
for key in ("hassio", "reauth")
]
)
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"], {
"type": "remove", "flow_id": forms[key]["flow_id"],
}, "type": "removed",
}
],
"id": 1, "id": 1,
"type": "event", "type": "event",
} }