mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Add service to clear completed shoppinglist items (#55032)
This commit is contained in:
parent
3432efddaa
commit
a23f4dac62
@ -12,7 +12,15 @@ from homeassistant.core import callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.util.json import load_json, save_json
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
SERVICE_ADD_ITEM,
|
||||
SERVICE_CLEAR_COMPLETED_ITEMS,
|
||||
SERVICE_COMPLETE_ALL,
|
||||
SERVICE_COMPLETE_ITEM,
|
||||
SERVICE_INCOMPLETE_ALL,
|
||||
SERVICE_INCOMPLETE_ITEM,
|
||||
)
|
||||
|
||||
ATTR_COMPLETE = "complete"
|
||||
|
||||
@ -22,11 +30,6 @@ EVENT = "shopping_list_updated"
|
||||
ITEM_UPDATE_SCHEMA = vol.Schema({ATTR_COMPLETE: bool, ATTR_NAME: str})
|
||||
PERSISTENCE = ".shopping_list.json"
|
||||
|
||||
SERVICE_ADD_ITEM = "add_item"
|
||||
SERVICE_COMPLETE_ITEM = "complete_item"
|
||||
SERVICE_INCOMPLETE_ITEM = "incomplete_item"
|
||||
SERVICE_COMPLETE_ALL = "complete_all"
|
||||
SERVICE_INCOMPLETE_ALL = "incomplete_all"
|
||||
SERVICE_ITEM_SCHEMA = vol.Schema({vol.Required(ATTR_NAME): vol.Any(None, cv.string)})
|
||||
SERVICE_LIST_SCHEMA = vol.Schema({})
|
||||
|
||||
@ -116,6 +119,10 @@ async def async_setup_entry(hass, config_entry):
|
||||
"""Mark all items in the list as incomplete."""
|
||||
await data.async_update_list({"complete": False})
|
||||
|
||||
async def clear_completed_items_service(call):
|
||||
"""Clear all completed items from the list."""
|
||||
await data.async_clear_completed()
|
||||
|
||||
data = hass.data[DOMAIN] = ShoppingData(hass)
|
||||
await data.async_load()
|
||||
|
||||
@ -143,6 +150,12 @@ async def async_setup_entry(hass, config_entry):
|
||||
incomplete_all_service,
|
||||
schema=SERVICE_LIST_SCHEMA,
|
||||
)
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_CLEAR_COMPLETED_ITEMS,
|
||||
clear_completed_items_service,
|
||||
schema=SERVICE_LIST_SCHEMA,
|
||||
)
|
||||
|
||||
hass.http.register_view(ShoppingListView)
|
||||
hass.http.register_view(CreateShoppingListItemView)
|
||||
|
@ -1,2 +1,9 @@
|
||||
"""All constants related to the shopping list component."""
|
||||
DOMAIN = "shopping_list"
|
||||
|
||||
SERVICE_ADD_ITEM = "add_item"
|
||||
SERVICE_COMPLETE_ITEM = "complete_item"
|
||||
SERVICE_INCOMPLETE_ITEM = "incomplete_item"
|
||||
SERVICE_COMPLETE_ALL = "complete_all"
|
||||
SERVICE_INCOMPLETE_ALL = "incomplete_all"
|
||||
SERVICE_CLEAR_COMPLETED_ITEMS = "clear_completed_items"
|
||||
|
@ -40,3 +40,7 @@ complete_all:
|
||||
incomplete_all:
|
||||
name: Incomplete all
|
||||
description: Marks all items as incomplete in the shopping list.
|
||||
|
||||
clear_completed_items:
|
||||
name: Clear completed items
|
||||
description: Clear completed items from the shopping list.
|
||||
|
@ -1,12 +1,17 @@
|
||||
"""Test shopping list component."""
|
||||
|
||||
from homeassistant.components.shopping_list.const import DOMAIN
|
||||
from homeassistant.components.shopping_list.const import (
|
||||
DOMAIN,
|
||||
SERVICE_ADD_ITEM,
|
||||
SERVICE_CLEAR_COMPLETED_ITEMS,
|
||||
SERVICE_COMPLETE_ITEM,
|
||||
)
|
||||
from homeassistant.components.websocket_api.const import (
|
||||
ERR_INVALID_FORMAT,
|
||||
ERR_NOT_FOUND,
|
||||
TYPE_RESULT,
|
||||
)
|
||||
from homeassistant.const import HTTP_NOT_FOUND
|
||||
from homeassistant.const import ATTR_NAME, HTTP_NOT_FOUND
|
||||
from homeassistant.helpers import intent
|
||||
|
||||
|
||||
@ -53,6 +58,29 @@ async def test_update_list(hass, sl_setup):
|
||||
assert cheese["complete"] is False
|
||||
|
||||
|
||||
async def test_clear_completed_items(hass, sl_setup):
|
||||
"""Test clear completed list items."""
|
||||
await intent.async_handle(
|
||||
hass,
|
||||
"test",
|
||||
"HassShoppingListAddItem",
|
||||
{"item": {"value": "beer"}},
|
||||
)
|
||||
|
||||
await intent.async_handle(
|
||||
hass, "test", "HassShoppingListAddItem", {"item": {"value": "cheese"}}
|
||||
)
|
||||
|
||||
assert len(hass.data[DOMAIN].items) == 2
|
||||
|
||||
# Update a single attribute, other attributes shouldn't change
|
||||
await hass.data[DOMAIN].async_update_list({"complete": True})
|
||||
|
||||
await hass.data[DOMAIN].async_clear_completed()
|
||||
|
||||
assert len(hass.data[DOMAIN].items) == 0
|
||||
|
||||
|
||||
async def test_recent_items_intent(hass, sl_setup):
|
||||
"""Test recent items."""
|
||||
|
||||
@ -471,3 +499,46 @@ async def test_ws_reorder_items_failure(hass, hass_ws_client, sl_setup):
|
||||
msg = await client.receive_json()
|
||||
assert msg["success"] is False
|
||||
assert msg["error"]["code"] == ERR_INVALID_FORMAT
|
||||
|
||||
|
||||
async def test_add_item_service(hass, sl_setup):
|
||||
"""Test adding shopping_list item service."""
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_ADD_ITEM,
|
||||
{ATTR_NAME: "beer"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.data[DOMAIN].items) == 1
|
||||
|
||||
|
||||
async def test_clear_completed_items_service(hass, sl_setup):
|
||||
"""Test clearing completed shopping_list items service."""
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_ADD_ITEM,
|
||||
{ATTR_NAME: "beer"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[DOMAIN].items) == 1
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_COMPLETE_ITEM,
|
||||
{ATTR_NAME: "beer"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[DOMAIN].items) == 1
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_CLEAR_COMPLETED_ITEMS,
|
||||
{},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[DOMAIN].items) == 0
|
||||
|
@ -20,3 +20,12 @@ async def test_recent_items_intent(hass, sl_setup):
|
||||
response.speech["plain"]["speech"]
|
||||
== "These are the top 3 items on your shopping list: soda, wine, beer"
|
||||
)
|
||||
|
||||
|
||||
async def test_recent_items_intent_no_items(hass, sl_setup):
|
||||
"""Test recent items."""
|
||||
response = await intent.async_handle(hass, "test", "HassShoppingListLastItems")
|
||||
|
||||
assert (
|
||||
response.speech["plain"]["speech"] == "There are no items on your shopping list"
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user