Tweak Elgato tests (#87629)

This commit is contained in:
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 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 @pytest.fixture
def mock_config_entry() -> MockConfigEntry: def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry.""" """Return the default mocked config entry."""
@ -48,40 +60,44 @@ def mock_onboarding() -> Generator[None, MagicMock, None]:
@pytest.fixture @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.""" """Return a mocked Elgato client."""
with patch( with patch(
"homeassistant.components.elgato.config_flow.Elgato", autospec=True "homeassistant.components.elgato.config_flow.Elgato", autospec=True
) as elgato_mock: ) as elgato_mock:
elgato = elgato_mock.return_value 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 yield elgato
@pytest.fixture @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.""" """Return a mocked Elgato client."""
variant = {"state": "temperature", "settings": "temperature"}
if hasattr(request, "param") and request.param:
variant = request.param
with patch( with patch(
"homeassistant.components.elgato.coordinator.Elgato", autospec=True "homeassistant.components.elgato.coordinator.Elgato", autospec=True
) as elgato_mock: ) as elgato_mock:
elgato = elgato_mock.return_value 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( 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( 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 yield elgato
@pytest.fixture @pytest.fixture
async def init_integration( async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_elgato: MagicMock hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_elgato: MagicMock,
) -> MockConfigEntry: ) -> MockConfigEntry:
"""Set up the Elgato integration for testing.""" """Set up the Elgato integration for testing."""
mock_config_entry.add_to_hass(hass) 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 import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from tests.common import MockConfigEntry
@pytest.mark.freeze_time("2021-11-13 11:48:00") @pytest.mark.freeze_time("2021-11-13 11:48:00")
async def test_button_identify( @pytest.mark.usefixtures("init_integration")
hass: HomeAssistant, async def test_button_identify(hass: HomeAssistant, mock_elgato: MagicMock) -> None:
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None:
"""Test the Elgato identify button.""" """Test the Elgato identify button."""
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
entity_registry = er.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" assert state.state == "2021-11-13T11:48:00+00:00"
@pytest.mark.usefixtures("init_integration")
async def test_button_identify_error( async def test_button_identify_error(
hass: HomeAssistant, hass: HomeAssistant, mock_elgato: MagicMock
init_integration: MockConfigEntry,
mock_elgato: MagicMock,
) -> None: ) -> None:
"""Test an error occurs with the Elgato identify button.""" """Test an error occurs with the Elgato identify button."""
mock_elgato.identify.side_effect = ElgatoError mock_elgato.identify.side_effect = ElgatoError
@ -82,6 +76,5 @@ async def test_button_identify_error(
{ATTR_ENTITY_ID: "button.frenck_identify"}, {ATTR_ENTITY_ID: "button.frenck_identify"},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
assert len(mock_elgato.identify.mock_calls) == 1 assert len(mock_elgato.identify.mock_calls) == 1

View file

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

View file

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

View file

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