From 92978b2f267b58ec1225c63c0eef8a163837e313 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Fri, 23 Nov 2018 01:56:18 -0600 Subject: [PATCH] Add websocket call for adding item to shopping-list (#18623) --- homeassistant/components/shopping_list.py | 20 ++++++++++++ tests/components/test_shopping_list.py | 38 +++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/shopping_list.py b/homeassistant/components/shopping_list.py index 45650ece621..650d23fe1df 100644 --- a/homeassistant/components/shopping_list.py +++ b/homeassistant/components/shopping_list.py @@ -38,12 +38,19 @@ SERVICE_ITEM_SCHEMA = vol.Schema({ }) WS_TYPE_SHOPPING_LIST_ITEMS = 'shopping_list/items' +WS_TYPE_SHOPPING_LIST_ADD_ITEM = 'shopping_list/items/add' SCHEMA_WEBSOCKET_ITEMS = \ websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({ vol.Required('type'): WS_TYPE_SHOPPING_LIST_ITEMS }) +SCHEMA_WEBSOCKET_ADD_ITEM = \ + websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({ + vol.Required('type'): WS_TYPE_SHOPPING_LIST_ADD_ITEM, + vol.Required('name'): str + }) + @asyncio.coroutine def async_setup(hass, config): @@ -103,6 +110,10 @@ def async_setup(hass, config): WS_TYPE_SHOPPING_LIST_ITEMS, websocket_handle_items, SCHEMA_WEBSOCKET_ITEMS) + hass.components.websocket_api.async_register_command( + WS_TYPE_SHOPPING_LIST_ADD_ITEM, + websocket_handle_add, + SCHEMA_WEBSOCKET_ADD_ITEM) return True @@ -276,3 +287,12 @@ def websocket_handle_items(hass, connection, msg): """Handle get shopping_list items.""" connection.send_message(websocket_api.result_message( msg['id'], hass.data[DOMAIN].items)) + + +@callback +def websocket_handle_add(hass, connection, msg): + """Handle add item to shopping_list.""" + item = hass.data[DOMAIN].async_add(msg['name']) + hass.bus.async_fire(EVENT) + connection.send_message(websocket_api.result_message( + msg['id'], item)) diff --git a/tests/components/test_shopping_list.py b/tests/components/test_shopping_list.py index e64b9a5ae26..44714138eb3 100644 --- a/tests/components/test_shopping_list.py +++ b/tests/components/test_shopping_list.py @@ -228,7 +228,7 @@ def test_api_clear_completed(hass, aiohttp_client): @asyncio.coroutine -def test_api_create(hass, aiohttp_client): +def test_deprecated_api_create(hass, aiohttp_client): """Test the API.""" yield from async_setup_component(hass, 'shopping_list', {}) @@ -249,7 +249,7 @@ def test_api_create(hass, aiohttp_client): @asyncio.coroutine -def test_api_create_fail(hass, aiohttp_client): +def test_deprecated_api_create_fail(hass, aiohttp_client): """Test the API.""" yield from async_setup_component(hass, 'shopping_list', {}) @@ -260,3 +260,37 @@ def test_api_create_fail(hass, aiohttp_client): assert resp.status == 400 assert len(hass.data['shopping_list'].items) == 0 + + +async def test_ws_add_item(hass, hass_ws_client): + """Test adding shopping_list item websocket command.""" + await async_setup_component(hass, 'shopping_list', {}) + client = await hass_ws_client(hass) + await client.send_json({ + 'id': 5, + 'type': 'shopping_list/items/add', + 'name': 'soda', + }) + msg = await client.receive_json() + assert msg['success'] is True + data = msg['result'] + assert data['name'] == 'soda' + assert data['complete'] is False + items = hass.data['shopping_list'].items + assert len(items) == 1 + assert items[0]['name'] == 'soda' + assert items[0]['complete'] is False + + +async def test_ws_add_item_fail(hass, hass_ws_client): + """Test adding shopping_list item failure websocket command.""" + await async_setup_component(hass, 'shopping_list', {}) + client = await hass_ws_client(hass) + await client.send_json({ + 'id': 5, + 'type': 'shopping_list/items/add', + 'name': 123, + }) + msg = await client.receive_json() + assert msg['success'] is False + assert len(hass.data['shopping_list'].items) == 0