|
| 1 | +"""Tests for the Cookidoo button platform.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, patch |
| 4 | + |
| 5 | +from cookidoo_api import CookidooRequestException |
| 6 | +import pytest |
| 7 | +from syrupy import SnapshotAssertion |
| 8 | + |
| 9 | +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS |
| 10 | +from homeassistant.config_entries import ConfigEntryState |
| 11 | +from homeassistant.const import ATTR_ENTITY_ID, Platform |
| 12 | +from homeassistant.core import HomeAssistant |
| 13 | +from homeassistant.exceptions import HomeAssistantError |
| 14 | +from homeassistant.helpers import entity_registry as er |
| 15 | + |
| 16 | +from . import setup_integration |
| 17 | + |
| 18 | +from tests.common import MockConfigEntry, snapshot_platform |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.usefixtures("entity_registry_enabled_by_default") |
| 22 | +async def test_all_entities( |
| 23 | + hass: HomeAssistant, |
| 24 | + snapshot: SnapshotAssertion, |
| 25 | + mock_cookidoo_client: AsyncMock, |
| 26 | + cookidoo_config_entry: MockConfigEntry, |
| 27 | + entity_registry: er.EntityRegistry, |
| 28 | +) -> None: |
| 29 | + """Test all entities.""" |
| 30 | + with patch("homeassistant.components.cookidoo.PLATFORMS", [Platform.BUTTON]): |
| 31 | + await setup_integration(hass, cookidoo_config_entry) |
| 32 | + |
| 33 | + assert cookidoo_config_entry.state is ConfigEntryState.LOADED |
| 34 | + |
| 35 | + await snapshot_platform( |
| 36 | + hass, entity_registry, snapshot, cookidoo_config_entry.entry_id |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.usefixtures("entity_registry_enabled_by_default") |
| 41 | +async def test_pressing_button( |
| 42 | + hass: HomeAssistant, |
| 43 | + mock_cookidoo_client: AsyncMock, |
| 44 | + cookidoo_config_entry: MockConfigEntry, |
| 45 | +) -> None: |
| 46 | + """Test pressing button.""" |
| 47 | + await setup_integration(hass, cookidoo_config_entry) |
| 48 | + |
| 49 | + await hass.services.async_call( |
| 50 | + BUTTON_DOMAIN, |
| 51 | + SERVICE_PRESS, |
| 52 | + { |
| 53 | + ATTR_ENTITY_ID: "button.cookidoo_clear_shopping_list_and_additional_purchases", |
| 54 | + }, |
| 55 | + blocking=True, |
| 56 | + ) |
| 57 | + mock_cookidoo_client.clear_shopping_list.assert_called_once() |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.usefixtures("entity_registry_enabled_by_default") |
| 61 | +async def test_pressing_button_exception( |
| 62 | + hass: HomeAssistant, |
| 63 | + mock_cookidoo_client: AsyncMock, |
| 64 | + cookidoo_config_entry: MockConfigEntry, |
| 65 | +) -> None: |
| 66 | + """Test pressing button with exception.""" |
| 67 | + |
| 68 | + await setup_integration(hass, cookidoo_config_entry) |
| 69 | + |
| 70 | + assert cookidoo_config_entry.state is ConfigEntryState.LOADED |
| 71 | + |
| 72 | + mock_cookidoo_client.clear_shopping_list.side_effect = CookidooRequestException |
| 73 | + with pytest.raises( |
| 74 | + HomeAssistantError, |
| 75 | + match="Failed to clear all items from the Cookidoo shopping list", |
| 76 | + ): |
| 77 | + await hass.services.async_call( |
| 78 | + BUTTON_DOMAIN, |
| 79 | + SERVICE_PRESS, |
| 80 | + { |
| 81 | + ATTR_ENTITY_ID: "button.cookidoo_clear_shopping_list_and_additional_purchases", |
| 82 | + }, |
| 83 | + blocking=True, |
| 84 | + ) |
0 commit comments