From 622f8b7b0655cc5e5c9f278bb4d1226ec14c6709 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 10 Apr 2025 09:20:39 +0200 Subject: [PATCH] Align WS event message format with config_entries/subscribe --- .../components/config/config_entries.py | 42 +++++---- homeassistant/config_entries.py | 4 +- .../components/config/test_config_entries.py | 88 ++++++++++--------- 3 files changed, 73 insertions(+), 61 deletions(-) diff --git a/homeassistant/components/config/config_entries.py b/homeassistant/components/config/config_entries.py index 0d3f11efa40..6e2d4a5da49 100644 --- a/homeassistant/components/config/config_entries.py +++ b/homeassistant/components/config/config_entries.py @@ -397,41 +397,45 @@ 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), - }, + [ + { + "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}, - ) + connection.send_message( + websocket_api.event_message( + msg["id"], + [ + {"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"]) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index ebedbd807c5..30bd075ed95 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -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]): diff --git a/tests/components/config/test_config_entries.py b/tests/components/config/test_config_entries.py index f2c49adb74b..6784866ea4b 100644 --- a/tests/components/config/test_config_entries.py +++ b/tests/components/config/test_config_entries.py @@ -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,26 +969,30 @@ async def test_get_progress_subscribe( for key in ("hassio", "reauth"): response = await ws_client.receive_json() assert response == { - "event": { - "flow": { + "event": [ + { + "flow": { + "flow_id": forms[key]["flow_id"], + "handler": "test", + "step_id": "account", + "context": flow_context[key], + }, "flow_id": forms[key]["flow_id"], - "handler": "test", - "step_id": "account", - "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": { - "flow_id": forms[key]["flow_id"], - "type": "remove", - }, + "event": [ + { + "flow_id": forms[key]["flow_id"], + "type": "removed", + } + ], "id": 1, "type": "event", } @@ -1071,26 +1076,27 @@ 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( - [ - { - "event": { - "flow": { + assert responses == [ + { + "event": unordered( + [ + { + "flow": { + "flow_id": forms[key]["flow_id"], + "handler": "test", + "step_id": "account", + "context": flow_context[key], + }, "flow_id": forms[key]["flow_id"], - "handler": "test", - "step_id": "account", - "context": flow_context[key], - }, - "flow_id": forms[key]["flow_id"], - "type": "init", - }, - "id": 1, - "type": "event", - } - for key in ("hassio", "reauth") - ] - ) + "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": { - "flow_id": forms[key]["flow_id"], - "type": "remove", - }, + "event": [ + { + "flow_id": forms[key]["flow_id"], + "type": "removed", + } + ], "id": 1, "type": "event", }