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
def async_on_flow_init_remove(change_type: str, flow_id: str) -> None:
"""Forward config entry state events to websocket."""
if change_type == "remove":
if change_type == "removed":
connection.send_message(
websocket_api.event_message(
msg["id"],
{"type": change_type, "flow_id": flow_id},
[{"type": change_type, "flow_id": flow_id}],
)
)
return
# change_type == "init"
# change_type == "added"
connection.send_message(
websocket_api.event_message(
msg["id"],
[
{
"type": change_type,
"flow_id": flow_id,
"flow": hass.config_entries.flow.async_get(flow_id),
},
}
],
)
)
connection.subscriptions[msg["id"]] = hass.config_entries.flow.async_subscribe_flow(
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(
websocket_api.event_message(
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"])

View File

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

View File

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