-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clear shopping list button for Cookidoo (#133583)
* add clear button * set clear button to disabled per default * add actions exception
- Loading branch information
Showing
8 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Support for Cookidoo buttons.""" | ||
|
||
from collections.abc import Awaitable, Callable | ||
from dataclasses import dataclass | ||
|
||
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 | ||
|
||
PARALLEL_UPDATES = 0 | ||
|
||
|
||
@dataclass(frozen=True, kw_only=True) | ||
class CookidooButtonEntityDescription(ButtonEntityDescription): | ||
"""Describes cookidoo button entity.""" | ||
|
||
press_fn: Callable[[Cookidoo], Awaitable[None]] | ||
|
||
|
||
TODO_CLEAR = CookidooButtonEntityDescription( | ||
key="todo_clear", | ||
translation_key="todo_clear", | ||
press_fn=lambda client: client.clear_shopping_list(), | ||
entity_registry_enabled_default=False, | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: CookidooConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up Cookidoo button entities based on a config entry.""" | ||
coordinator = entry.runtime_data | ||
|
||
async_add_entities([CookidooButton(coordinator, TODO_CLEAR)]) | ||
|
||
|
||
class CookidooButton(CookidooBaseEntity, ButtonEntity): | ||
"""Defines an Cookidoo button.""" | ||
|
||
entity_description: CookidooButtonEntityDescription | ||
|
||
def __init__( | ||
self, | ||
coordinator: CookidooDataUpdateCoordinator, | ||
description: CookidooButtonEntityDescription, | ||
) -> None: | ||
"""Initialize cookidoo button.""" | ||
super().__init__(coordinator) | ||
self.entity_description = description | ||
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{description.key}" | ||
|
||
async def async_press(self) -> None: | ||
"""Press the button.""" | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# serializer version: 1 | ||
# name: test_all_entities[button.cookidoo_clear_shopping_list_and_additional_purchases-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'button', | ||
'entity_category': None, | ||
'entity_id': 'button.cookidoo_clear_shopping_list_and_additional_purchases', | ||
'has_entity_name': True, | ||
'hidden_by': None, | ||
'icon': None, | ||
'id': <ANY>, | ||
'labels': set({ | ||
}), | ||
'name': None, | ||
'options': dict({ | ||
}), | ||
'original_device_class': None, | ||
'original_icon': None, | ||
'original_name': 'Clear shopping list and additional purchases', | ||
'platform': 'cookidoo', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'todo_clear', | ||
'unique_id': '01JBVVVJ87F6G5V0QJX6HBC94T_todo_clear', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_all_entities[button.cookidoo_clear_shopping_list_and_additional_purchases-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'friendly_name': 'Cookidoo Clear shopping list and additional purchases', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'button.cookidoo_clear_shopping_list_and_additional_purchases', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'unknown', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"""Tests for the Cookidoo button platform.""" | ||
|
||
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 | ||
|
||
from tests.common import MockConfigEntry, snapshot_platform | ||
|
||
|
||
@pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
async def test_all_entities( | ||
hass: HomeAssistant, | ||
snapshot: SnapshotAssertion, | ||
mock_cookidoo_client: AsyncMock, | ||
cookidoo_config_entry: MockConfigEntry, | ||
entity_registry: er.EntityRegistry, | ||
) -> None: | ||
"""Test all entities.""" | ||
with patch("homeassistant.components.cookidoo.PLATFORMS", [Platform.BUTTON]): | ||
await setup_integration(hass, cookidoo_config_entry) | ||
|
||
assert cookidoo_config_entry.state is ConfigEntryState.LOADED | ||
|
||
await snapshot_platform( | ||
hass, entity_registry, snapshot, cookidoo_config_entry.entry_id | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
async def test_pressing_button( | ||
hass: HomeAssistant, | ||
mock_cookidoo_client: AsyncMock, | ||
cookidoo_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test pressing button.""" | ||
await setup_integration(hass, cookidoo_config_entry) | ||
|
||
await hass.services.async_call( | ||
BUTTON_DOMAIN, | ||
SERVICE_PRESS, | ||
{ | ||
ATTR_ENTITY_ID: "button.cookidoo_clear_shopping_list_and_additional_purchases", | ||
}, | ||
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters