Improve type hints in insteon tests (#121504)

This commit is contained in:
epenet 2024-07-08 11:34:50 +02:00 committed by GitHub
parent 1aa9588dcb
commit 3059bf0536
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,8 @@
"""Test the Insteon Scenes APIs.""" """Test the Insteon Scenes APIs."""
import json from collections.abc import Generator
import os import os
from typing import Any
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from pyinsteon.constants import ResponseStatus from pyinsteon.constants import ResponseStatus
@ -11,21 +12,22 @@ import pytest
from homeassistant.components.insteon.api import async_load_api, scenes from homeassistant.components.insteon.api import async_load_api, scenes
from homeassistant.components.insteon.const import ID, TYPE from homeassistant.components.insteon.const import ID, TYPE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.util.json import JsonArrayType
from .mock_devices import MockDevices from .mock_devices import MockDevices
from tests.common import load_fixture from tests.common import load_json_array_fixture
from tests.typing import WebSocketGenerator from tests.typing import MockHAClientWebSocket, WebSocketGenerator
@pytest.fixture(name="scene_data", scope="module") @pytest.fixture(name="scene_data", scope="module")
def aldb_data_fixture(): def aldb_data_fixture() -> JsonArrayType:
"""Load the controller state fixture data.""" """Load the controller state fixture data."""
return json.loads(load_fixture("insteon/scene_data.json")) return load_json_array_fixture("insteon/scene_data.json")
@pytest.fixture(name="remove_json") @pytest.fixture(name="remove_json")
def remove_insteon_devices_json(hass): def remove_insteon_devices_json(hass: HomeAssistant) -> Generator[None]:
"""Fixture to remove insteon_devices.json at the end of the test.""" """Fixture to remove insteon_devices.json at the end of the test."""
yield yield
file = os.path.join(hass.config.config_dir, "insteon_devices.json") file = os.path.join(hass.config.config_dir, "insteon_devices.json")
@ -33,7 +35,7 @@ def remove_insteon_devices_json(hass):
os.remove(file) os.remove(file)
def _scene_to_array(scene): def _scene_to_array(scene: dict[str, Any]) -> list[dict[str, Any]]:
"""Convert a scene object to a dictionary.""" """Convert a scene object to a dictionary."""
scene_list = [] scene_list = []
for device, links in scene["devices"].items(): for device, links in scene["devices"].items():
@ -47,7 +49,9 @@ def _scene_to_array(scene):
return scene_list return scene_list
async def _setup(hass, hass_ws_client, scene_data): async def _setup(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data: JsonArrayType
) -> tuple[MockHAClientWebSocket, MockDevices]:
"""Set up tests.""" """Set up tests."""
ws_client = await hass_ws_client(hass) ws_client = await hass_ws_client(hass)
devices = MockDevices() devices = MockDevices()
@ -63,7 +67,7 @@ async def _setup(hass, hass_ws_client, scene_data):
# This tests needs to be adjusted to remove lingering tasks # This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True]) @pytest.mark.parametrize("expected_lingering_tasks", [True])
async def test_get_scenes( async def test_get_scenes(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data: JsonArrayType
) -> None: ) -> None:
"""Test getting all Insteon scenes.""" """Test getting all Insteon scenes."""
ws_client, devices = await _setup(hass, hass_ws_client, scene_data) ws_client, devices = await _setup(hass, hass_ws_client, scene_data)
@ -79,7 +83,7 @@ async def test_get_scenes(
# This tests needs to be adjusted to remove lingering tasks # This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True]) @pytest.mark.parametrize("expected_lingering_tasks", [True])
async def test_get_scene( async def test_get_scene(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data: JsonArrayType
) -> None: ) -> None:
"""Test getting an Insteon scene.""" """Test getting an Insteon scene."""
ws_client, devices = await _setup(hass, hass_ws_client, scene_data) ws_client, devices = await _setup(hass, hass_ws_client, scene_data)
@ -93,8 +97,11 @@ async def test_get_scene(
# This tests needs to be adjusted to remove lingering tasks # This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True]) @pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.usefixtures("remove_json")
async def test_save_scene( async def test_save_scene(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data, remove_json hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
scene_data: JsonArrayType,
) -> None: ) -> None:
"""Test saving an Insteon scene.""" """Test saving an Insteon scene."""
ws_client, devices = await _setup(hass, hass_ws_client, scene_data) ws_client, devices = await _setup(hass, hass_ws_client, scene_data)
@ -125,8 +132,11 @@ async def test_save_scene(
# This tests needs to be adjusted to remove lingering tasks # This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True]) @pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.usefixtures("remove_json")
async def test_save_new_scene( async def test_save_new_scene(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data, remove_json hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
scene_data: JsonArrayType,
) -> None: ) -> None:
"""Test saving a new Insteon scene.""" """Test saving a new Insteon scene."""
ws_client, devices = await _setup(hass, hass_ws_client, scene_data) ws_client, devices = await _setup(hass, hass_ws_client, scene_data)
@ -157,8 +167,11 @@ async def test_save_new_scene(
# This tests needs to be adjusted to remove lingering tasks # This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True]) @pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.usefixtures("remove_json")
async def test_save_scene_error( async def test_save_scene_error(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data, remove_json hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
scene_data: JsonArrayType,
) -> None: ) -> None:
"""Test saving an Insteon scene with error.""" """Test saving an Insteon scene with error."""
ws_client, devices = await _setup(hass, hass_ws_client, scene_data) ws_client, devices = await _setup(hass, hass_ws_client, scene_data)
@ -189,8 +202,11 @@ async def test_save_scene_error(
# This tests needs to be adjusted to remove lingering tasks # This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True]) @pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.usefixtures("remove_json")
async def test_delete_scene( async def test_delete_scene(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, scene_data, remove_json hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
scene_data: JsonArrayType,
) -> None: ) -> None:
"""Test delete an Insteon scene.""" """Test delete an Insteon scene."""
ws_client, devices = await _setup(hass, hass_ws_client, scene_data) ws_client, devices = await _setup(hass, hass_ws_client, scene_data)