Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert shopping-list clear to WebSockets #18769

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions homeassistant/components/shopping_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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({
Expand All @@ -60,6 +61,11 @@
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):
Expand Down Expand Up @@ -127,6 +133,10 @@ def complete_item_service(call):
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

Expand Down Expand Up @@ -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']))
37 changes: 36 additions & 1 deletion tests/components/test_shopping_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', {})

Expand Down Expand Up @@ -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."""
Expand Down