mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Add websocket call for adding item to shopping-list (#18623)
This commit is contained in:
parent
c99204149c
commit
92978b2f26
@ -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))
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user