core/tests/components/elgato/test_button.py

78 lines
2.2 KiB
Python
Raw Normal View History

"""Tests for the Elgato Light button platform."""
2022-01-14 16:16:59 +00:00
from unittest.mock import MagicMock
from elgato import ElgatoError
import pytest
2023-02-19 19:14:18 +00:00
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
2023-02-19 19:14:18 +00:00
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
2022-06-15 10:12:07 +00:00
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
pytestmark = [
pytest.mark.parametrize("device_fixtures", ["key-light-mini"]),
pytest.mark.usefixtures("device_fixtures", "init_integration"),
pytest.mark.freeze_time("2021-11-13 11:48:00"),
]
2023-02-19 19:14:18 +00:00
@pytest.mark.parametrize(
("entity_id", "method"),
[
("button.frenck_identify", "identify"),
("button.frenck_restart", "restart"),
],
)
async def test_buttons(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_elgato: MagicMock,
2023-02-19 19:14:18 +00:00
snapshot: SnapshotAssertion,
entity_id: str,
method: str,
) -> None:
"""Test the Elgato identify button."""
2023-02-19 19:14:18 +00:00
assert (state := hass.states.get(entity_id))
assert state == snapshot
2023-02-19 19:14:18 +00:00
assert (entry := entity_registry.async_get(entity_id))
assert entry == snapshot
assert entry.device_id
2023-02-19 19:14:18 +00:00
assert (device_entry := device_registry.async_get(entry.device_id))
assert device_entry == snapshot
2022-01-14 16:16:59 +00:00
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
2023-02-19 19:14:18 +00:00
{ATTR_ENTITY_ID: entity_id},
2022-01-14 16:16:59 +00:00
blocking=True,
)
2023-02-19 19:14:18 +00:00
mocked_method = getattr(mock_elgato, method)
assert len(mocked_method.mock_calls) == 1
mocked_method.assert_called_with()
2023-02-19 19:14:18 +00:00
state = hass.states.get(entity_id)
assert state
assert state.state == "2021-11-13T11:48:00+00:00"
2023-02-19 19:14:18 +00:00
mocked_method.side_effect = ElgatoError
2022-06-15 10:12:07 +00:00
with pytest.raises(
2023-02-19 19:14:18 +00:00
HomeAssistantError,
match="An error occurred while communicating with the Elgato Light",
2022-06-15 10:12:07 +00:00
):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
2023-02-19 19:14:18 +00:00
{ATTR_ENTITY_ID: entity_id},
2022-06-15 10:12:07 +00:00
blocking=True,
)
2022-01-14 16:16:59 +00:00
2023-02-19 19:14:18 +00:00
assert len(mocked_method.mock_calls) == 2