Skip to content

Commit

Permalink
Shopping list: add item HTTP API (#10674)
Browse files Browse the repository at this point in the history
* Shopping list: add item HTTP API

* Fix order of decorators
  • Loading branch information
balloob authored Nov 21, 2017
1 parent f0fe8cb commit 6e27e73
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
12 changes: 6 additions & 6 deletions homeassistant/components/cloud/http_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class CloudLoginView(HomeAssistantView):
url = '/api/cloud/login'
name = 'api:cloud:login'

@asyncio.coroutine
@_handle_cloud_errors
@RequestDataValidator(vol.Schema({
vol.Required('email'): str,
vol.Required('password'): str,
}))
@asyncio.coroutine
def post(self, request, data):
"""Handle login request."""
hass = request.app['hass']
Expand All @@ -92,8 +92,8 @@ class CloudLogoutView(HomeAssistantView):
url = '/api/cloud/logout'
name = 'api:cloud:logout'

@asyncio.coroutine
@_handle_cloud_errors
@asyncio.coroutine
def post(self, request):
"""Handle logout request."""
hass = request.app['hass']
Expand Down Expand Up @@ -129,12 +129,12 @@ class CloudRegisterView(HomeAssistantView):
url = '/api/cloud/register'
name = 'api:cloud:register'

@asyncio.coroutine
@_handle_cloud_errors
@RequestDataValidator(vol.Schema({
vol.Required('email'): str,
vol.Required('password'): vol.All(str, vol.Length(min=6)),
}))
@asyncio.coroutine
def post(self, request, data):
"""Handle registration request."""
hass = request.app['hass']
Expand All @@ -153,12 +153,12 @@ class CloudConfirmRegisterView(HomeAssistantView):
url = '/api/cloud/confirm_register'
name = 'api:cloud:confirm_register'

@asyncio.coroutine
@_handle_cloud_errors
@RequestDataValidator(vol.Schema({
vol.Required('confirmation_code'): str,
vol.Required('email'): str,
}))
@asyncio.coroutine
def post(self, request, data):
"""Handle registration confirmation request."""
hass = request.app['hass']
Expand All @@ -178,11 +178,11 @@ class CloudForgotPasswordView(HomeAssistantView):
url = '/api/cloud/forgot_password'
name = 'api:cloud:forgot_password'

@asyncio.coroutine
@_handle_cloud_errors
@RequestDataValidator(vol.Schema({
vol.Required('email'): str,
}))
@asyncio.coroutine
def post(self, request, data):
"""Handle forgot password request."""
hass = request.app['hass']
Expand All @@ -201,13 +201,13 @@ class CloudConfirmForgotPasswordView(HomeAssistantView):
url = '/api/cloud/confirm_forgot_password'
name = 'api:cloud:confirm_forgot_password'

@asyncio.coroutine
@_handle_cloud_errors
@RequestDataValidator(vol.Schema({
vol.Required('confirmation_code'): str,
vol.Required('email'): str,
vol.Required('new_password'): vol.All(str, vol.Length(min=6))
}))
@asyncio.coroutine
def post(self, request, data):
"""Handle forgot password confirm request."""
hass = request.app['hass']
Expand Down
31 changes: 25 additions & 6 deletions homeassistant/components/shopping_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def async_setup(hass, config):
intent.async_register(hass, ListTopItemsIntent())

hass.http.register_view(ShoppingListView)
hass.http.register_view(CreateShoppingListItemView)
hass.http.register_view(UpdateShoppingListItemView)
hass.http.register_view(ClearCompletedItemsView)

Expand Down Expand Up @@ -65,12 +66,14 @@ def __init__(self, hass):
@callback
def async_add(self, name):
"""Add a shopping list item."""
self.items.append({
item = {
'name': name,
'id': uuid.uuid4().hex,
'complete': False
})
}
self.items.append(item)
self.hass.async_add_job(self.save)
return item

@callback
def async_update(self, item_id, info):
Expand Down Expand Up @@ -102,8 +105,7 @@ def load():
with open(path) as file:
return json.loads(file.read())

items = yield from self.hass.async_add_job(load)
self.items = items
self.items = yield from self.hass.async_add_job(load)

def save(self):
"""Save the items."""
Expand Down Expand Up @@ -166,7 +168,7 @@ class ShoppingListView(http.HomeAssistantView):

@callback
def get(self, request):
"""Retrieve if API is running."""
"""Retrieve shopping list items."""
return self.json(request.app['hass'].data[DOMAIN].items)


Expand All @@ -178,7 +180,7 @@ class UpdateShoppingListItemView(http.HomeAssistantView):

@callback
def post(self, request, item_id):
"""Retrieve if API is running."""
"""Update a shopping list item."""
data = yield from request.json()

try:
Expand All @@ -191,6 +193,23 @@ def post(self, request, item_id):
return self.json_message('Item not found', HTTP_BAD_REQUEST)


class CreateShoppingListItemView(http.HomeAssistantView):
"""View to retrieve shopping list content."""

url = '/api/shopping_list/item'
name = "api:shopping_list:item"

@http.RequestDataValidator(vol.Schema({
vol.Required('name'): str,
}))
@asyncio.coroutine
def post(self, request, data):
"""Create a new shopping list item."""
item = request.app['hass'].data[DOMAIN].async_add(data['name'])
request.app['hass'].bus.async_fire(EVENT)
return self.json(item)


class ClearCompletedItemsView(http.HomeAssistantView):
"""View to retrieve shopping list content."""

Expand Down
41 changes: 39 additions & 2 deletions tests/components/test_shopping_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@


@pytest.fixture(autouse=True)
def mock_shopping_list_save():
def mock_shopping_list_io():
"""Stub out the persistence."""
with patch('homeassistant.components.shopping_list.ShoppingData.save'):
with patch('homeassistant.components.shopping_list.ShoppingData.save'), \
patch('homeassistant.components.shopping_list.'
'ShoppingData.async_load'):
yield


Expand Down Expand Up @@ -192,3 +194,38 @@ def test_api_clear_completed(hass, test_client):
'name': 'wine',
'complete': False
}


@asyncio.coroutine
def test_api_create(hass, test_client):
"""Test the API."""
yield from async_setup_component(hass, 'shopping_list', {})

client = yield from test_client(hass.http.app)
resp = yield from client.post('/api/shopping_list/item', json={
'name': 'soda'
})

assert resp.status == 200
data = yield from resp.json()
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


@asyncio.coroutine
def test_api_create_fail(hass, test_client):
"""Test the API."""
yield from async_setup_component(hass, 'shopping_list', {})

client = yield from test_client(hass.http.app)
resp = yield from client.post('/api/shopping_list/item', json={
'name': 1234
})

assert resp.status == 400
assert len(hass.data['shopping_list'].items) == 0

0 comments on commit 6e27e73

Please sign in to comment.