From a306475065d8f794762ade133799554e823c5864 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Thu, 29 Nov 2018 03:06:18 -0600 Subject: [PATCH] Convert shopping-list clear to WebSockets (#18769) --- homeassistant/components/shopping_list.py | 18 +++++++++++ tests/components/test_shopping_list.py | 37 ++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/shopping_list.py b/homeassistant/components/shopping_list.py index ad4680982b4..2ebd80c3de0 100644 --- a/homeassistant/components/shopping_list.py +++ b/homeassistant/components/shopping_list.py @@ -40,6 +40,7 @@ SERVICE_ITEM_SCHEMA = vol.Schema({ WS_TYPE_SHOPPING_LIST_ITEMS = 'shopping_list/items' WS_TYPE_SHOPPING_LIST_ADD_ITEM = 'shopping_list/items/add' WS_TYPE_SHOPPING_LIST_UPDATE_ITEM = 'shopping_list/items/update' +WS_TYPE_SHOPPING_LIST_CLEAR_ITEMS = 'shopping_list/items/clear' SCHEMA_WEBSOCKET_ITEMS = \ websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({ @@ -60,6 +61,11 @@ SCHEMA_WEBSOCKET_UPDATE_ITEM = \ vol.Optional('complete'): bool }) +SCHEMA_WEBSOCKET_CLEAR_ITEMS = \ + websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({ + vol.Required('type'): WS_TYPE_SHOPPING_LIST_CLEAR_ITEMS + }) + @asyncio.coroutine def async_setup(hass, config): @@ -127,6 +133,10 @@ def async_setup(hass, config): WS_TYPE_SHOPPING_LIST_UPDATE_ITEM, websocket_handle_update, SCHEMA_WEBSOCKET_UPDATE_ITEM) + hass.components.websocket_api.async_register_command( + WS_TYPE_SHOPPING_LIST_CLEAR_ITEMS, + websocket_handle_clear, + SCHEMA_WEBSOCKET_CLEAR_ITEMS) return True @@ -327,3 +337,11 @@ async def websocket_handle_update(hass, connection, msg): except KeyError: connection.send_message(websocket_api.error_message( msg_id, 'item_not_found', 'Item not found')) + + +@callback +def websocket_handle_clear(hass, connection, msg): + """Handle clearing shopping_list items.""" + hass.data[DOMAIN].async_clear_completed() + hass.bus.async_fire(EVENT) + connection.send_message(websocket_api.result_message(msg['id'])) diff --git a/tests/components/test_shopping_list.py b/tests/components/test_shopping_list.py index 1e89287bcc1..f4095b77316 100644 --- a/tests/components/test_shopping_list.py +++ b/tests/components/test_shopping_list.py @@ -275,7 +275,7 @@ async def test_ws_update_item_fail(hass, hass_ws_client): @asyncio.coroutine -def test_api_clear_completed(hass, hass_client): +def test_deprecated_api_clear_completed(hass, hass_client): """Test the API.""" yield from async_setup_component(hass, 'shopping_list', {}) @@ -311,6 +311,41 @@ def test_api_clear_completed(hass, hass_client): } +async def test_ws_clear_items(hass, hass_ws_client): + """Test clearing shopping_list items websocket command.""" + await async_setup_component(hass, 'shopping_list', {}) + await intent.async_handle( + hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}} + ) + await intent.async_handle( + hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'wine'}} + ) + beer_id = hass.data['shopping_list'].items[0]['id'] + wine_id = hass.data['shopping_list'].items[1]['id'] + client = await hass_ws_client(hass) + await client.send_json({ + 'id': 5, + 'type': 'shopping_list/items/update', + 'item_id': beer_id, + 'complete': True + }) + msg = await client.receive_json() + assert msg['success'] is True + await client.send_json({ + 'id': 6, + 'type': 'shopping_list/items/clear' + }) + msg = await client.receive_json() + assert msg['success'] is True + items = hass.data['shopping_list'].items + assert len(items) == 1 + assert items[0] == { + 'id': wine_id, + 'name': 'wine', + 'complete': False + } + + @asyncio.coroutine def test_deprecated_api_create(hass, hass_client): """Test the API."""