Improve type hints in kaleidescape tests (#119040)

This commit is contained in:
epenet 2024-06-07 10:49:09 +02:00 committed by GitHub
parent c107d980fa
commit 7638380add
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 41 additions and 73 deletions

View File

@ -1,6 +1,6 @@
"""Fixtures for Kaleidescape integration.""" """Fixtures for Kaleidescape integration."""
from unittest.mock import AsyncMock, patch from unittest.mock import MagicMock, patch
from kaleidescape import Dispatcher from kaleidescape import Dispatcher
from kaleidescape.device import Automation, Movie, Power, System from kaleidescape.device import Automation, Movie, Power, System
@ -17,7 +17,7 @@ from tests.common import MockConfigEntry
@pytest.fixture(name="mock_device") @pytest.fixture(name="mock_device")
def fixture_mock_device() -> Generator[AsyncMock]: def fixture_mock_device() -> Generator[MagicMock]:
"""Return a mocked Kaleidescape device.""" """Return a mocked Kaleidescape device."""
with patch( with patch(
"homeassistant.components.kaleidescape.KaleidescapeDevice", autospec=True "homeassistant.components.kaleidescape.KaleidescapeDevice", autospec=True
@ -64,6 +64,7 @@ def fixture_mock_config_entry() -> MockConfigEntry:
@pytest.fixture(name="mock_integration") @pytest.fixture(name="mock_integration")
async def fixture_mock_integration( async def fixture_mock_integration(
hass: HomeAssistant, hass: HomeAssistant,
mock_device: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> MockConfigEntry: ) -> MockConfigEntry:
"""Return a mock ConfigEntry setup for Kaleidescape integration.""" """Return a mock ConfigEntry setup for Kaleidescape integration."""

View File

@ -1,7 +1,9 @@
"""Tests for Kaleidescape config flow.""" """Tests for Kaleidescape config flow."""
import dataclasses import dataclasses
from unittest.mock import AsyncMock from unittest.mock import MagicMock
import pytest
from homeassistant.components.kaleidescape.const import DOMAIN from homeassistant.components.kaleidescape.const import DOMAIN
from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER
@ -11,12 +13,9 @@ from homeassistant.data_entry_flow import FlowResultType
from . import MOCK_HOST, MOCK_SSDP_DISCOVERY_INFO from . import MOCK_HOST, MOCK_SSDP_DISCOVERY_INFO
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("mock_device")
async def test_user_config_flow_success( async def test_user_config_flow_success(hass: HomeAssistant) -> None:
hass: HomeAssistant, mock_device: AsyncMock
) -> None:
"""Test user config flow success.""" """Test user config flow success."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
@ -35,7 +34,7 @@ async def test_user_config_flow_success(
async def test_user_config_flow_bad_connect_errors( async def test_user_config_flow_bad_connect_errors(
hass: HomeAssistant, mock_device: AsyncMock hass: HomeAssistant, mock_device: MagicMock
) -> None: ) -> None:
"""Test errors when connection error occurs.""" """Test errors when connection error occurs."""
mock_device.connect.side_effect = ConnectionError mock_device.connect.side_effect = ConnectionError
@ -50,7 +49,7 @@ async def test_user_config_flow_bad_connect_errors(
async def test_user_config_flow_unsupported_device_errors( async def test_user_config_flow_unsupported_device_errors(
hass: HomeAssistant, mock_device: AsyncMock hass: HomeAssistant, mock_device: MagicMock
) -> None: ) -> None:
"""Test errors when connecting to unsupported device.""" """Test errors when connecting to unsupported device."""
mock_device.is_server_only = True mock_device.is_server_only = True
@ -64,9 +63,8 @@ async def test_user_config_flow_unsupported_device_errors(
assert result["errors"] == {"base": "unsupported"} assert result["errors"] == {"base": "unsupported"}
async def test_user_config_flow_device_exists_abort( @pytest.mark.usefixtures("mock_device", "mock_integration")
hass: HomeAssistant, mock_device: AsyncMock, mock_integration: MockConfigEntry async def test_user_config_flow_device_exists_abort(hass: HomeAssistant) -> None:
) -> None:
"""Test flow aborts when device already configured.""" """Test flow aborts when device already configured."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data={CONF_HOST: MOCK_HOST} DOMAIN, context={"source": SOURCE_USER}, data={CONF_HOST: MOCK_HOST}
@ -75,9 +73,8 @@ async def test_user_config_flow_device_exists_abort(
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
async def test_ssdp_config_flow_success( @pytest.mark.usefixtures("mock_device")
hass: HomeAssistant, mock_device: AsyncMock async def test_ssdp_config_flow_success(hass: HomeAssistant) -> None:
) -> None:
"""Test ssdp config flow success.""" """Test ssdp config flow success."""
discovery_info = dataclasses.replace(MOCK_SSDP_DISCOVERY_INFO) discovery_info = dataclasses.replace(MOCK_SSDP_DISCOVERY_INFO)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -97,7 +94,7 @@ async def test_ssdp_config_flow_success(
async def test_ssdp_config_flow_bad_connect_aborts( async def test_ssdp_config_flow_bad_connect_aborts(
hass: HomeAssistant, mock_device: AsyncMock hass: HomeAssistant, mock_device: MagicMock
) -> None: ) -> None:
"""Test abort when connection error occurs.""" """Test abort when connection error occurs."""
mock_device.connect.side_effect = ConnectionError mock_device.connect.side_effect = ConnectionError
@ -112,7 +109,7 @@ async def test_ssdp_config_flow_bad_connect_aborts(
async def test_ssdp_config_flow_unsupported_device_aborts( async def test_ssdp_config_flow_unsupported_device_aborts(
hass: HomeAssistant, mock_device: AsyncMock hass: HomeAssistant, mock_device: MagicMock
) -> None: ) -> None:
"""Test abort when connecting to unsupported device.""" """Test abort when connecting to unsupported device."""
mock_device.is_server_only = True mock_device.is_server_only = True

View File

@ -1,6 +1,8 @@
"""Tests for Kaleidescape config entry.""" """Tests for Kaleidescape config entry."""
from unittest.mock import AsyncMock from unittest.mock import MagicMock
import pytest
from homeassistant.components.kaleidescape.const import DOMAIN from homeassistant.components.kaleidescape.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
@ -14,7 +16,7 @@ from tests.common import MockConfigEntry
async def test_unload_config_entry( async def test_unload_config_entry(
hass: HomeAssistant, hass: HomeAssistant,
mock_device: AsyncMock, mock_device: MagicMock,
mock_integration: MockConfigEntry, mock_integration: MockConfigEntry,
) -> None: ) -> None:
"""Test config entry loading and unloading.""" """Test config entry loading and unloading."""
@ -32,7 +34,7 @@ async def test_unload_config_entry(
async def test_config_entry_not_ready( async def test_config_entry_not_ready(
hass: HomeAssistant, hass: HomeAssistant,
mock_device: AsyncMock, mock_device: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test config entry not ready.""" """Test config entry not ready."""
@ -45,12 +47,8 @@ async def test_config_entry_not_ready(
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_device( @pytest.mark.usefixtures("mock_device", "mock_integration")
hass: HomeAssistant, async def test_device(device_registry: dr.DeviceRegistry) -> None:
device_registry: dr.DeviceRegistry,
mock_device: AsyncMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test device.""" """Test device."""
device = device_registry.async_get_device( device = device_registry.async_get_device(
identifiers={("kaleidescape", MOCK_SERIAL)} identifiers={("kaleidescape", MOCK_SERIAL)}

View File

@ -4,6 +4,7 @@ from unittest.mock import MagicMock
from kaleidescape import const as kaleidescape_const from kaleidescape import const as kaleidescape_const
from kaleidescape.device import Movie from kaleidescape.device import Movie
import pytest
from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN
from homeassistant.const import ( from homeassistant.const import (
@ -25,17 +26,12 @@ from homeassistant.helpers import device_registry as dr
from . import MOCK_SERIAL from . import MOCK_SERIAL
from tests.common import MockConfigEntry
ENTITY_ID = f"media_player.kaleidescape_device_{MOCK_SERIAL}" ENTITY_ID = f"media_player.kaleidescape_device_{MOCK_SERIAL}"
FRIENDLY_NAME = f"Kaleidescape Device {MOCK_SERIAL}" FRIENDLY_NAME = f"Kaleidescape Device {MOCK_SERIAL}"
async def test_entity( @pytest.mark.usefixtures("mock_device", "mock_integration")
hass: HomeAssistant, async def test_entity(hass: HomeAssistant) -> None:
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test entity attributes.""" """Test entity attributes."""
entity = hass.states.get(ENTITY_ID) entity = hass.states.get(ENTITY_ID)
assert entity is not None assert entity is not None
@ -43,11 +39,8 @@ async def test_entity(
assert entity.attributes["friendly_name"] == FRIENDLY_NAME assert entity.attributes["friendly_name"] == FRIENDLY_NAME
async def test_update_state( @pytest.mark.usefixtures("mock_integration")
hass: HomeAssistant, async def test_update_state(hass: HomeAssistant, mock_device: MagicMock) -> None:
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Tests dispatched signals update player.""" """Tests dispatched signals update player."""
entity = hass.states.get(ENTITY_ID) entity = hass.states.get(ENTITY_ID)
assert entity is not None assert entity is not None
@ -105,11 +98,8 @@ async def test_update_state(
assert entity.state == STATE_PAUSED assert entity.state == STATE_PAUSED
async def test_services( @pytest.mark.usefixtures("mock_integration")
hass: HomeAssistant, async def test_services(hass: HomeAssistant, mock_device: MagicMock) -> None:
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test service calls.""" """Test service calls."""
await hass.services.async_call( await hass.services.async_call(
MEDIA_PLAYER_DOMAIN, MEDIA_PLAYER_DOMAIN,
@ -168,12 +158,8 @@ async def test_services(
assert mock_device.previous.call_count == 1 assert mock_device.previous.call_count == 1
async def test_device( @pytest.mark.usefixtures("mock_device", "mock_integration")
hass: HomeAssistant, async def test_device(device_registry: dr.DeviceRegistry) -> None:
device_registry: dr.DeviceRegistry,
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test device attributes.""" """Test device attributes."""
device = device_registry.async_get_device( device = device_registry.async_get_device(
identifiers={("kaleidescape", MOCK_SERIAL)} identifiers={("kaleidescape", MOCK_SERIAL)}

View File

@ -15,25 +15,17 @@ from homeassistant.exceptions import HomeAssistantError
from . import MOCK_SERIAL from . import MOCK_SERIAL
from tests.common import MockConfigEntry
ENTITY_ID = f"remote.kaleidescape_device_{MOCK_SERIAL}" ENTITY_ID = f"remote.kaleidescape_device_{MOCK_SERIAL}"
async def test_entity( @pytest.mark.usefixtures("mock_device", "mock_integration")
hass: HomeAssistant, async def test_entity(hass: HomeAssistant) -> None:
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test entity attributes.""" """Test entity attributes."""
assert hass.states.get(ENTITY_ID) assert hass.states.get(ENTITY_ID)
async def test_commands( @pytest.mark.usefixtures("mock_integration")
hass: HomeAssistant, async def test_commands(hass: HomeAssistant, mock_device: MagicMock) -> None:
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test service calls.""" """Test service calls."""
await hass.services.async_call( await hass.services.async_call(
REMOTE_DOMAIN, REMOTE_DOMAIN,
@ -140,11 +132,8 @@ async def test_commands(
assert mock_device.menu_toggle.call_count == 1 assert mock_device.menu_toggle.call_count == 1
async def test_unknown_command( @pytest.mark.usefixtures("mock_device", "mock_integration")
hass: HomeAssistant, async def test_unknown_command(hass: HomeAssistant) -> None:
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None:
"""Test service calls.""" """Test service calls."""
with pytest.raises(HomeAssistantError) as err: with pytest.raises(HomeAssistantError) as err:
await hass.services.async_call( await hass.services.async_call(

View File

@ -3,6 +3,7 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
from kaleidescape import const as kaleidescape_const from kaleidescape import const as kaleidescape_const
import pytest
from homeassistant.const import ATTR_FRIENDLY_NAME from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -10,17 +11,13 @@ from homeassistant.helpers import entity_registry as er
from . import MOCK_SERIAL from . import MOCK_SERIAL
from tests.common import MockConfigEntry
ENTITY_ID = f"sensor.kaleidescape_device_{MOCK_SERIAL}" ENTITY_ID = f"sensor.kaleidescape_device_{MOCK_SERIAL}"
FRIENDLY_NAME = f"Kaleidescape Device {MOCK_SERIAL}" FRIENDLY_NAME = f"Kaleidescape Device {MOCK_SERIAL}"
@pytest.mark.usefixtures("mock_integration")
async def test_sensors( async def test_sensors(
hass: HomeAssistant, hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_device: MagicMock
entity_registry: er.EntityRegistry,
mock_device: MagicMock,
mock_integration: MockConfigEntry,
) -> None: ) -> None:
"""Test sensors.""" """Test sensors."""
entity = hass.states.get(f"{ENTITY_ID}_media_location") entity = hass.states.get(f"{ENTITY_ID}_media_location")