diff --git a/homeassistant/components/shopping_list.py b/homeassistant/components/shopping_list.py index ac59c15572a..9922960da3f 100644 --- a/homeassistant/components/shopping_list.py +++ b/homeassistant/components/shopping_list.py @@ -1,9 +1,11 @@ """Component to manage a shoppling list.""" import asyncio import logging +import uuid import voluptuous as vol +from homeassistant.const import HTTP_NOT_FOUND, HTTP_BAD_REQUEST from homeassistant.core import callback from homeassistant.components import http from homeassistant.helpers import intent @@ -17,15 +19,20 @@ CONFIG_SCHEMA = vol.Schema({DOMAIN: {}}, extra=vol.ALLOW_EXTRA) EVENT = 'shopping_list_updated' INTENT_ADD_ITEM = 'HassShoppingListAddItem' INTENT_LAST_ITEMS = 'HassShoppingListLastItems' +ITEM_UPDATE_SCHEMA = vol.Schema({ + 'complete': bool, + 'name': str, +}) @asyncio.coroutine def async_setup(hass, config): """Initialize the shopping list.""" - hass.data[DOMAIN] = [] + hass.data[DOMAIN] = ShoppingData([]) intent.async_register(hass, AddItemIntent()) intent.async_register(hass, ListTopItemsIntent()) hass.http.register_view(ShoppingListView) + hass.http.register_view(UpdateShoppingListItemView) hass.components.conversation.async_register(INTENT_ADD_ITEM, [ 'Add {item} to my shopping list', ]) @@ -37,6 +44,37 @@ def async_setup(hass, config): return True +class ShoppingData: + """Class to hold shopping list data.""" + + def __init__(self, items): + """Initialize the shopping list.""" + self.items = items + + def add(self, name): + """Add a shopping list item.""" + self.items.append({ + 'name': name, + 'id': uuid.uuid4().hex, + 'complete': False + }) + + def update(self, item_id, info): + """Update a shopping list item.""" + item = next((itm for itm in self.items if itm['id'] == item_id), None) + + if item is None: + raise KeyError + + info = ITEM_UPDATE_SCHEMA(info) + item.update(info) + return item + + def clear_completed(self): + """Clear completed items.""" + self.items = [itm for itm in self.items if not itm['complete']] + + class AddItemIntent(intent.IntentHandler): """Handle AddItem intents.""" @@ -50,7 +88,7 @@ class AddItemIntent(intent.IntentHandler): """Handle the intent.""" slots = self.async_validate_slots(intent_obj.slots) item = slots['item']['value'] - intent_obj.hass.data[DOMAIN].append(item) + intent_obj.hass.data[DOMAIN].add(item) response = intent_obj.create_response() response.async_set_speech( @@ -70,16 +108,17 @@ class ListTopItemsIntent(intent.IntentHandler): @asyncio.coroutine def async_handle(self, intent_obj): """Handle the intent.""" - items = intent_obj.hass.data[DOMAIN][-5:] + items = intent_obj.hass.data[DOMAIN].items[-5:] response = intent_obj.create_response() - if len(items) == 0: + if not items: response.async_set_speech( "There are no items on your shopping list") else: response.async_set_speech( "These are the top {} items on your shopping list: {}".format( - min(len(items), 5), ', '.join(reversed(items)))) + min(len(items), 5), + ', '.join(itm['name'] for itm in reversed(items)))) return response @@ -92,4 +131,25 @@ class ShoppingListView(http.HomeAssistantView): @callback def get(self, request): """Retrieve if API is running.""" - return self.json(request.app['hass'].data[DOMAIN]) + return self.json(request.app['hass'].data[DOMAIN].items) + + +class UpdateShoppingListItemView(http.HomeAssistantView): + """View to retrieve shopping list content.""" + + url = '/api/shopping_list/{item_id}' + name = "api:shopping_list:id" + + @callback + def post(self, request, item_id): + """Retrieve if API is running.""" + data = yield from request.json() + + try: + item = request.app['hass'].data[DOMAIN].update(item_id, data) + request.app['hass'].bus.async_fire(EVENT) + return self.json(item) + except KeyError: + return self.json_message('Item not found', HTTP_NOT_FOUND) + except vol.Invalid: + return self.json_message('Item not found', HTTP_BAD_REQUEST) diff --git a/tests/components/test_shopping_list.py b/tests/components/test_shopping_list.py index c25ce593c54..e4a99ad39d4 100644 --- a/tests/components/test_shopping_list.py +++ b/tests/components/test_shopping_list.py @@ -42,7 +42,7 @@ def test_recent_items_intent(hass): @asyncio.coroutine -def test_api(hass, test_client): +def test_api_get_all(hass, test_client): """Test the API.""" yield from async_setup_component(hass, 'shopping_list', {}) @@ -58,4 +58,90 @@ def test_api(hass, test_client): assert resp.status == 200 data = yield from resp.json() - assert data == ['beer', 'wine'] + assert len(data) == 2 + assert data[0]['name'] == 'beer' + assert not data[0]['complete'] + assert data[1]['name'] == 'wine' + assert not data[1]['complete'] + + +@asyncio.coroutine +def test_api_update(hass, test_client): + """Test the API.""" + yield from async_setup_component(hass, 'shopping_list', {}) + + yield from intent.async_handle( + hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}} + ) + yield from 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 = yield from test_client(hass.http.app) + resp = yield from client.post( + '/api/shopping_list/{}'.format(beer_id), json={ + 'name': 'soda' + }) + + assert resp.status == 200 + data = yield from resp.json() + assert data == { + 'id': beer_id, + 'name': 'soda', + 'complete': False + } + + resp = yield from client.post( + '/api/shopping_list/{}'.format(wine_id), json={ + 'complete': True + }) + + assert resp.status == 200 + data = yield from resp.json() + assert data == { + 'id': wine_id, + 'name': 'wine', + 'complete': True + } + + beer, wine = hass.data['shopping_list'].items + assert beer == { + 'id': beer_id, + 'name': 'soda', + 'complete': False + } + assert wine == { + 'id': wine_id, + 'name': 'wine', + 'complete': True + } + + +@asyncio.coroutine +def test_api_update_fails(hass, test_client): + """Test the API.""" + yield from async_setup_component(hass, 'shopping_list', {}) + + yield from intent.async_handle( + hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}} + ) + + client = yield from test_client(hass.http.app) + resp = yield from client.post( + '/api/shopping_list/non_existing', json={ + 'name': 'soda' + }) + + assert resp.status == 404 + + beer_id = hass.data['shopping_list'].items[0]['id'] + client = yield from test_client(hass.http.app) + resp = yield from client.post( + '/api/shopping_list/{}'.format(beer_id), json={ + 'name': 123, + }) + + assert resp.status == 400