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

Add clear shopping list button for Cookidoo #133583

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
add actions exception
  • Loading branch information
miaucl committed Dec 25, 2024
commit ed9c8b37db4a086f3a00a6ed43b9a0dbe4863d6a
12 changes: 10 additions & 2 deletions homeassistant/components/cookidoo/button.py
miaucl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from collections.abc import Awaitable, Callable
from dataclasses import dataclass

from cookidoo_api import Cookidoo
from cookidoo_api import Cookidoo, CookidooException

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import CookidooConfigEntry, CookidooDataUpdateCoordinator
from .entity import CookidooBaseEntity

Expand Down Expand Up @@ -58,5 +60,11 @@ def __init__(

async def async_press(self) -> None:
"""Press the button."""
await self.entity_description.press_fn(self.coordinator.cookidoo)
try:
await self.entity_description.press_fn(self.coordinator.cookidoo)
except CookidooException as e:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="button_clear_todo_failed",
) from e
await self.coordinator.async_refresh()
3 changes: 3 additions & 0 deletions homeassistant/components/cookidoo/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
}
},
"exceptions": {
"button_clear_todo_failed": {
"message": "Failed to clear all items from the Cookidoo shopping list"
},
"todo_save_item_failed": {
"message": "Failed to save {name} to Cookidoo shopping list"
},
Expand Down
29 changes: 29 additions & 0 deletions tests/components/cookidoo/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

from unittest.mock import AsyncMock, patch

from cookidoo_api import CookidooRequestException
import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from . import setup_integration
Expand Down Expand Up @@ -53,3 +55,30 @@ async def test_pressing_button(
blocking=True,
)
mock_cookidoo_client.clear_shopping_list.assert_called_once()


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_pressing_button_exception(
hass: HomeAssistant,
mock_cookidoo_client: AsyncMock,
cookidoo_config_entry: MockConfigEntry,
) -> None:
"""Test pressing button with exception."""

await setup_integration(hass, cookidoo_config_entry)

assert cookidoo_config_entry.state is ConfigEntryState.LOADED

mock_cookidoo_client.clear_shopping_list.side_effect = CookidooRequestException
with pytest.raises(
HomeAssistantError,
match="Failed to clear all items from the Cookidoo shopping list",
):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{
ATTR_ENTITY_ID: "button.cookidoo_clear_shopping_list_and_additional_purchases",
},
blocking=True,
)