From a9e9d7b112b3555484e7a948bc995ab748a6e28d Mon Sep 17 00:00:00 2001 From: R0nd Date: Tue, 19 Jul 2022 07:13:12 +0300 Subject: [PATCH] Pass context to shopping list events (#75377) --- .../components/shopping_list/__init__.py | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/shopping_list/__init__.py b/homeassistant/components/shopping_list/__init__.py index 5f6a13e8e13..2af54722739 100644 --- a/homeassistant/components/shopping_list/__init__.py +++ b/homeassistant/components/shopping_list/__init__.py @@ -335,7 +335,11 @@ class ClearCompletedItemsView(http.HomeAssistantView): @callback -def websocket_handle_items(hass, connection, msg): +def websocket_handle_items( + hass: HomeAssistant, + connection: websocket_api.connection.ActiveConnection, + msg: dict, +) -> None: """Handle get shopping_list items.""" connection.send_message( websocket_api.result_message(msg["id"], hass.data[DOMAIN].items) @@ -343,15 +347,25 @@ def websocket_handle_items(hass, connection, msg): @websocket_api.async_response -async def websocket_handle_add(hass, connection, msg): +async def websocket_handle_add( + hass: HomeAssistant, + connection: websocket_api.connection.ActiveConnection, + msg: dict, +) -> None: """Handle add item to shopping_list.""" item = await hass.data[DOMAIN].async_add(msg["name"]) - hass.bus.async_fire(EVENT, {"action": "add", "item": item}) + hass.bus.async_fire( + EVENT, {"action": "add", "item": item}, context=connection.context(msg) + ) connection.send_message(websocket_api.result_message(msg["id"], item)) @websocket_api.async_response -async def websocket_handle_update(hass, connection, msg): +async def websocket_handle_update( + hass: HomeAssistant, + connection: websocket_api.connection.ActiveConnection, + msg: dict, +) -> None: """Handle update shopping_list item.""" msg_id = msg.pop("id") item_id = msg.pop("item_id") @@ -360,7 +374,9 @@ async def websocket_handle_update(hass, connection, msg): try: item = await hass.data[DOMAIN].async_update(item_id, data) - hass.bus.async_fire(EVENT, {"action": "update", "item": item}) + hass.bus.async_fire( + EVENT, {"action": "update", "item": item}, context=connection.context(msg) + ) connection.send_message(websocket_api.result_message(msg_id, item)) except KeyError: connection.send_message( @@ -369,10 +385,14 @@ async def websocket_handle_update(hass, connection, msg): @websocket_api.async_response -async def websocket_handle_clear(hass, connection, msg): +async def websocket_handle_clear( + hass: HomeAssistant, + connection: websocket_api.connection.ActiveConnection, + msg: dict, +) -> None: """Handle clearing shopping_list items.""" await hass.data[DOMAIN].async_clear_completed() - hass.bus.async_fire(EVENT, {"action": "clear"}) + hass.bus.async_fire(EVENT, {"action": "clear"}, context=connection.context(msg)) connection.send_message(websocket_api.result_message(msg["id"])) @@ -382,12 +402,18 @@ async def websocket_handle_clear(hass, connection, msg): vol.Required("item_ids"): [str], } ) -def websocket_handle_reorder(hass, connection, msg): +def websocket_handle_reorder( + hass: HomeAssistant, + connection: websocket_api.connection.ActiveConnection, + msg: dict, +) -> None: """Handle reordering shopping_list items.""" msg_id = msg.pop("id") try: hass.data[DOMAIN].async_reorder(msg.pop("item_ids")) - hass.bus.async_fire(EVENT, {"action": "reorder"}) + hass.bus.async_fire( + EVENT, {"action": "reorder"}, context=connection.context(msg) + ) connection.send_result(msg_id) except KeyError: connection.send_error(