Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.9 KiB
Python
Raw Permalink Normal View History

"""Tests for the switchbot button platform."""
from collections.abc import Callable
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from . import ART_FRAME_INFO
from tests.common import MockConfigEntry
from tests.components.bluetooth import inject_bluetooth_service_info
@pytest.mark.parametrize(
("service", "mock_method", "entity_id"),
[
(SERVICE_PRESS, "next_image", "button.test_name_next_image"),
(SERVICE_PRESS, "prev_image", "button.test_name_previous_image"),
],
)
async def test_art_frame_button_press(
hass: HomeAssistant,
mock_entry_encrypted_factory: Callable[[str], MockConfigEntry],
service: str,
mock_method: str,
entity_id: str,
) -> None:
"""Test pressing the button on the art frame device."""
inject_bluetooth_service_info(hass, ART_FRAME_INFO)
entry = mock_entry_encrypted_factory("art_frame")
entry.add_to_hass(hass)
mock_basic_info = AsyncMock(
return_value=b"\x016\x07\x01\x00\x00\x04\x00\xde\x18\xa5\x00\x00\x00\x00\x00\x00"
)
mocked_instance = AsyncMock(return_value=True)
with patch.multiple(
"homeassistant.components.switchbot.button.switchbot.SwitchbotArtFrame",
_get_basic_info=mock_basic_info,
**{mock_method: mocked_instance},
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
entity_ids = [
entity.entity_id for entity in hass.states.async_all(BUTTON_DOMAIN)
]
assert entity_ids, "No button entities found"
await hass.services.async_call(
BUTTON_DOMAIN,
service,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mocked_instance.assert_awaited_once()