Tweak Elgato tests (#87629)

pull/87366/head^2
Franck Nijhof 2023-02-07 19:14:13 +01:00 committed by GitHub
parent 3e09950dd6
commit c25bff9389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 64 additions and 60 deletions

View File

@ -13,6 +13,18 @@ from tests.common import MockConfigEntry, load_fixture
from tests.components.light.conftest import mock_light_profiles # noqa: F401
@pytest.fixture
def device_fixtures() -> str:
"""Return the device fixtures for a specific device."""
return "key-light"
@pytest.fixture
def state_variant() -> str:
"""Return the state variant to load for a device."""
return "state"
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
@ -48,40 +60,44 @@ def mock_onboarding() -> Generator[None, MagicMock, None]:
@pytest.fixture
def mock_elgato_config_flow() -> Generator[None, MagicMock, None]:
def mock_elgato_config_flow(device_fixtures: str) -> Generator[None, MagicMock, None]:
"""Return a mocked Elgato client."""
with patch(
"homeassistant.components.elgato.config_flow.Elgato", autospec=True
) as elgato_mock:
elgato = elgato_mock.return_value
elgato.info.return_value = Info.parse_raw(load_fixture("info.json", DOMAIN))
elgato.info.return_value = Info.parse_raw(
load_fixture(f"{device_fixtures}/info.json", DOMAIN)
)
yield elgato
@pytest.fixture
def mock_elgato(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
def mock_elgato(
device_fixtures: str, state_variant: str
) -> Generator[None, MagicMock, None]:
"""Return a mocked Elgato client."""
variant = {"state": "temperature", "settings": "temperature"}
if hasattr(request, "param") and request.param:
variant = request.param
with patch(
"homeassistant.components.elgato.coordinator.Elgato", autospec=True
) as elgato_mock:
elgato = elgato_mock.return_value
elgato.info.return_value = Info.parse_raw(load_fixture("info.json", DOMAIN))
elgato.info.return_value = Info.parse_raw(
load_fixture(f"{device_fixtures}/info.json", DOMAIN)
)
elgato.state.return_value = State.parse_raw(
load_fixture(f"state-{variant['state']}.json", DOMAIN)
load_fixture(f"{device_fixtures}/{state_variant}.json", DOMAIN)
)
elgato.settings.return_value = Settings.parse_raw(
load_fixture(f"settings-{variant['settings']}.json", DOMAIN)
load_fixture(f"{device_fixtures}/settings.json", DOMAIN)
)
yield elgato
@pytest.fixture
async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_elgato: MagicMock
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_elgato: MagicMock,
) -> MockConfigEntry:
"""Set up the Elgato integration for testing."""
mock_config_entry.add_to_hass(hass)

View File

@ -0,0 +1,9 @@
{
"productName": "Elgato Key Light",
"hardwareBoardType": 53,
"firmwareBuildNumber": 192,
"firmwareVersion": "1.0.3",
"serialNumber": "CN11A1A00001",
"displayName": "Frenck",
"features": ["lights"]
}

View File

@ -0,0 +1,5 @@
{
"on": 1,
"brightness": 21,
"temperature": 297
}

View File

@ -12,15 +12,10 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import EntityCategory
from tests.common import MockConfigEntry
@pytest.mark.freeze_time("2021-11-13 11:48:00")
async def test_button_identify(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None:
@pytest.mark.usefixtures("init_integration")
async def test_button_identify(hass: HomeAssistant, mock_elgato: MagicMock) -> None:
"""Test the Elgato identify button."""
device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)
@ -65,10 +60,9 @@ async def test_button_identify(
assert state.state == "2021-11-13T11:48:00+00:00"
@pytest.mark.usefixtures("init_integration")
async def test_button_identify_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
hass: HomeAssistant, mock_elgato: MagicMock
) -> None:
"""Test an error occurs with the Elgato identify button."""
mock_elgato.identify.side_effect = ElgatoError
@ -82,6 +76,5 @@ async def test_button_identify_error(
{ATTR_ENTITY_ID: "button.frenck_identify"},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_elgato.identify.mock_calls) == 1

View File

@ -2,6 +2,7 @@
from unittest.mock import AsyncMock, MagicMock
from elgato import ElgatoConnectionError
import pytest
from homeassistant.components import zeroconf
from homeassistant.components.elgato.const import DOMAIN
@ -134,10 +135,9 @@ async def test_zeroconf_connection_error(
assert result.get("type") == FlowResultType.ABORT
@pytest.mark.usefixtures("mock_elgato_config_flow")
async def test_user_device_exists_abort(
hass: HomeAssistant,
mock_elgato_config_flow: MagicMock,
mock_config_entry: MockConfigEntry,
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
mock_config_entry.add_to_hass(hass)
@ -151,10 +151,9 @@ async def test_user_device_exists_abort(
assert result.get("reason") == "already_configured"
@pytest.mark.usefixtures("mock_elgato_config_flow")
async def test_zeroconf_device_exists_abort(
hass: HomeAssistant,
mock_elgato_config_flow: MagicMock,
mock_config_entry: MockConfigEntry,
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
mock_config_entry.add_to_hass(hass)

View File

@ -11,7 +11,7 @@ async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
init_integration: MockConfigEntry,
):
) -> None:
"""Test diagnostics."""
assert await get_diagnostics_for_config_entry(
hass, hass_client, init_integration

View File

@ -27,14 +27,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry
async def test_light_state_temperature(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None:
@pytest.mark.usefixtures("init_integration", "mock_elgato")
async def test_light_state_temperature(hass: HomeAssistant) -> None:
"""Test the creation and values of the Elgato Lights in temperature mode."""
device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)
@ -71,14 +66,9 @@ async def test_light_state_temperature(
assert device_entry.hw_version == "53"
@pytest.mark.parametrize(
"mock_elgato", [{"settings": "color", "state": "color"}], indirect=True
)
async def test_light_state_color(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None:
@pytest.mark.parametrize("device_fixtures", ["light-strip"])
@pytest.mark.usefixtures("device_fixtures", "init_integration", "mock_elgato")
async def test_light_state_color(hass: HomeAssistant) -> None:
"""Test the creation and values of the Elgato Lights in temperature mode."""
entity_registry = er.async_get(hass)
@ -103,11 +93,11 @@ async def test_light_state_color(
@pytest.mark.parametrize(
"mock_elgato", [{"settings": "color", "state": "temperature"}], indirect=True
("device_fixtures", "state_variant"), [("light-strip", "state-color-temperature")]
)
@pytest.mark.usefixtures("state_variant", "device_fixtures", "init_integration")
async def test_light_change_state_temperature(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None:
"""Test the change of state of a Elgato Key Light device."""
@ -174,11 +164,9 @@ async def test_light_change_state_temperature(
@pytest.mark.parametrize("service", [SERVICE_TURN_ON, SERVICE_TURN_OFF])
@pytest.mark.usefixtures("init_integration")
async def test_light_unavailable(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
service: str,
hass: HomeAssistant, mock_elgato: MagicMock, service: str
) -> None:
"""Test error/unavailable handling of an Elgato Light."""
mock_elgato.state.side_effect = ElgatoError
@ -191,18 +179,14 @@ async def test_light_unavailable(
{ATTR_ENTITY_ID: "light.frenck"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.frenck")
assert state
assert state.state == STATE_UNAVAILABLE
async def test_light_identify(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None:
@pytest.mark.usefixtures("init_integration")
async def test_light_identify(hass: HomeAssistant, mock_elgato: MagicMock) -> None:
"""Test identifying an Elgato Light."""
await hass.services.async_call(
DOMAIN,
@ -217,10 +201,9 @@ async def test_light_identify(
mock_elgato.identify.assert_called_with()
@pytest.mark.usefixtures("init_integration")
async def test_light_identify_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
hass: HomeAssistant, mock_elgato: MagicMock
) -> None:
"""Test error occurred during identifying an Elgato Light."""
mock_elgato.identify.side_effect = ElgatoError
@ -235,6 +218,5 @@ async def test_light_identify_error(
},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_elgato.identify.mock_calls) == 1