Convert shopping-list clear to WebSockets (#18769)

This commit is contained in:
Ian Richardson 2018-11-29 03:06:18 -06:00 committed by Paulus Schoutsen
parent aadf72d445
commit a306475065
2 changed files with 54 additions and 1 deletions

View File

@ -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']))

View File

@ -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."""