Modernize Elgato tests (#64060)

This commit is contained in:
Franck Nijhof 2022-01-14 17:16:59 +01:00 committed by GitHub
parent b17860a7dd
commit b1848cd2f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 343 additions and 367 deletions

View file

@ -1,5 +1,5 @@
"""Tests for the Elgato Light button platform."""
from unittest.mock import patch
from unittest.mock import MagicMock
from elgato import ElgatoError
import pytest
@ -10,17 +10,16 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import EntityCategory
from tests.components.elgato import init_integration
from tests.test_util.aiohttp import AiohttpClientMocker
from tests.common import MockConfigEntry
@pytest.mark.freeze_time("2021-11-13 11:48:00")
async def test_button_identify(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None:
"""Test the Elgato identify button."""
await init_integration(hass, aioclient_mock)
entity_registry = er.async_get(hass)
state = hass.states.get("button.identify")
@ -33,18 +32,15 @@ async def test_button_identify(
assert entry.unique_id == "CN11A1A00001_identify"
assert entry.entity_category == EntityCategory.CONFIG
with patch(
"homeassistant.components.elgato.light.Elgato.identify"
) as mock_identify:
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.identify"},
blocking=True,
)
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.identify"},
blocking=True,
)
assert len(mock_identify.mock_calls) == 1
mock_identify.assert_called_with()
assert len(mock_elgato.identify.mock_calls) == 1
mock_elgato.identify.assert_called_with()
state = hass.states.get("button.identify")
assert state
@ -52,22 +48,20 @@ async def test_button_identify(
async def test_button_identify_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test an error occurs with the Elgato identify button."""
await init_integration(hass, aioclient_mock)
with patch(
"homeassistant.components.elgato.light.Elgato.identify",
side_effect=ElgatoError,
) as mock_identify:
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.identify"},
blocking=True,
)
mock_elgato.identify.side_effect = ElgatoError
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.identify"},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_identify.mock_calls) == 1
assert len(mock_elgato.identify.mock_calls) == 1
assert "An error occurred while identifying the Elgato Light" in caplog.text