mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 09:47:52 +00:00
Improve type hints in guardian tests (#121175)
This commit is contained in:
parent
67a4c2c884
commit
3c69301365
@ -1,16 +1,18 @@
|
||||
"""Define fixtures for Elexa Guardian tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
import json
|
||||
from collections.abc import AsyncGenerator, Generator
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.guardian import CONF_UID, DOMAIN
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util.json import JsonObjectType
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
from tests.common import MockConfigEntry, load_json_object_fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -23,7 +25,9 @@ def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def config_entry_fixture(hass, config, unique_id):
|
||||
def config_entry_fixture(
|
||||
hass: HomeAssistant, config: dict[str, Any], unique_id: str
|
||||
) -> MockConfigEntry:
|
||||
"""Define a config entry fixture."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
@ -35,7 +39,7 @@ def config_entry_fixture(hass, config, unique_id):
|
||||
|
||||
|
||||
@pytest.fixture(name="config")
|
||||
def config_fixture(hass):
|
||||
def config_fixture() -> dict[str, Any]:
|
||||
"""Define a config entry data fixture."""
|
||||
return {
|
||||
CONF_IP_ADDRESS: "192.168.1.100",
|
||||
@ -44,68 +48,68 @@ def config_fixture(hass):
|
||||
|
||||
|
||||
@pytest.fixture(name="data_sensor_pair_dump", scope="package")
|
||||
def data_sensor_pair_dump_fixture():
|
||||
def data_sensor_pair_dump_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful sensor_pair_dump response."""
|
||||
return json.loads(load_fixture("sensor_pair_dump_data.json", "guardian"))
|
||||
return load_json_object_fixture("sensor_pair_dump_data.json", "guardian")
|
||||
|
||||
|
||||
@pytest.fixture(name="data_sensor_pair_sensor", scope="package")
|
||||
def data_sensor_pair_sensor_fixture():
|
||||
def data_sensor_pair_sensor_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful sensor_pair_sensor response."""
|
||||
return json.loads(load_fixture("sensor_pair_sensor_data.json", "guardian"))
|
||||
return load_json_object_fixture("sensor_pair_sensor_data.json", "guardian")
|
||||
|
||||
|
||||
@pytest.fixture(name="data_sensor_paired_sensor_status", scope="package")
|
||||
def data_sensor_paired_sensor_status_fixture():
|
||||
def data_sensor_paired_sensor_status_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful sensor_paired_sensor_status response."""
|
||||
return json.loads(load_fixture("sensor_paired_sensor_status_data.json", "guardian"))
|
||||
return load_json_object_fixture("sensor_paired_sensor_status_data.json", "guardian")
|
||||
|
||||
|
||||
@pytest.fixture(name="data_system_diagnostics", scope="package")
|
||||
def data_system_diagnostics_fixture():
|
||||
def data_system_diagnostics_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful system_diagnostics response."""
|
||||
return json.loads(load_fixture("system_diagnostics_data.json", "guardian"))
|
||||
return load_json_object_fixture("system_diagnostics_data.json", "guardian")
|
||||
|
||||
|
||||
@pytest.fixture(name="data_system_onboard_sensor_status", scope="package")
|
||||
def data_system_onboard_sensor_status_fixture():
|
||||
def data_system_onboard_sensor_status_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful system_onboard_sensor_status response."""
|
||||
return json.loads(
|
||||
load_fixture("system_onboard_sensor_status_data.json", "guardian")
|
||||
return load_json_object_fixture(
|
||||
"system_onboard_sensor_status_data.json", "guardian"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="data_system_ping", scope="package")
|
||||
def data_system_ping_fixture():
|
||||
def data_system_ping_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful system_ping response."""
|
||||
return json.loads(load_fixture("system_ping_data.json", "guardian"))
|
||||
return load_json_object_fixture("system_ping_data.json", "guardian")
|
||||
|
||||
|
||||
@pytest.fixture(name="data_valve_status", scope="package")
|
||||
def data_valve_status_fixture():
|
||||
def data_valve_status_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful valve_status response."""
|
||||
return json.loads(load_fixture("valve_status_data.json", "guardian"))
|
||||
return load_json_object_fixture("valve_status_data.json", "guardian")
|
||||
|
||||
|
||||
@pytest.fixture(name="data_wifi_status", scope="package")
|
||||
def data_wifi_status_fixture():
|
||||
def data_wifi_status_fixture() -> JsonObjectType:
|
||||
"""Define data from a successful wifi_status response."""
|
||||
return json.loads(load_fixture("wifi_status_data.json", "guardian"))
|
||||
return load_json_object_fixture("wifi_status_data.json", "guardian")
|
||||
|
||||
|
||||
@pytest.fixture(name="setup_guardian")
|
||||
async def setup_guardian_fixture(
|
||||
hass,
|
||||
config,
|
||||
data_sensor_pair_dump,
|
||||
data_sensor_pair_sensor,
|
||||
data_sensor_paired_sensor_status,
|
||||
data_system_diagnostics,
|
||||
data_system_onboard_sensor_status,
|
||||
data_system_ping,
|
||||
data_valve_status,
|
||||
data_wifi_status,
|
||||
):
|
||||
hass: HomeAssistant,
|
||||
config: dict[str, Any],
|
||||
data_sensor_pair_dump: JsonObjectType,
|
||||
data_sensor_pair_sensor: JsonObjectType,
|
||||
data_sensor_paired_sensor_status: JsonObjectType,
|
||||
data_system_diagnostics: JsonObjectType,
|
||||
data_system_onboard_sensor_status: JsonObjectType,
|
||||
data_system_ping: JsonObjectType,
|
||||
data_valve_status: JsonObjectType,
|
||||
data_wifi_status: JsonObjectType,
|
||||
) -> AsyncGenerator[None]:
|
||||
"""Define a fixture to set up Guardian."""
|
||||
with (
|
||||
patch("aioguardian.client.Client.connect"),
|
||||
@ -155,6 +159,6 @@ async def setup_guardian_fixture(
|
||||
|
||||
|
||||
@pytest.fixture(name="unique_id")
|
||||
def unique_id_fixture(hass):
|
||||
def unique_id_fixture() -> str:
|
||||
"""Define a config entry unique ID fixture."""
|
||||
return "guardian_3456"
|
||||
|
@ -1,6 +1,7 @@
|
||||
"""Define tests for the Elexa Guardian config flow."""
|
||||
|
||||
from ipaddress import ip_address
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from aioguardian.errors import GuardianError
|
||||
@ -22,9 +23,8 @@ from tests.common import MockConfigEntry
|
||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||
|
||||
|
||||
async def test_duplicate_error(
|
||||
hass: HomeAssistant, config, config_entry, setup_guardian
|
||||
) -> None:
|
||||
@pytest.mark.usefixtures("config_entry", "setup_guardian")
|
||||
async def test_duplicate_error(hass: HomeAssistant, config: dict[str, Any]) -> None:
|
||||
"""Test that errors are shown when duplicate entries are added."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
@ -33,7 +33,7 @@ async def test_duplicate_error(
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_connect_error(hass: HomeAssistant, config) -> None:
|
||||
async def test_connect_error(hass: HomeAssistant, config: dict[str, Any]) -> None:
|
||||
"""Test that the config entry errors out if the device cannot connect."""
|
||||
with patch(
|
||||
"aioguardian.client.Client.connect",
|
||||
@ -58,7 +58,8 @@ async def test_get_pin_from_uid() -> None:
|
||||
assert pin == "3456"
|
||||
|
||||
|
||||
async def test_step_user(hass: HomeAssistant, config, setup_guardian) -> None:
|
||||
@pytest.mark.usefixtures("setup_guardian")
|
||||
async def test_step_user(hass: HomeAssistant, config: dict[str, Any]) -> None:
|
||||
"""Test the user step."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
@ -78,7 +79,8 @@ async def test_step_user(hass: HomeAssistant, config, setup_guardian) -> None:
|
||||
}
|
||||
|
||||
|
||||
async def test_step_zeroconf(hass: HomeAssistant, setup_guardian) -> None:
|
||||
@pytest.mark.usefixtures("setup_guardian")
|
||||
async def test_step_zeroconf(hass: HomeAssistant) -> None:
|
||||
"""Test the zeroconf step."""
|
||||
zeroconf_data = zeroconf.ZeroconfServiceInfo(
|
||||
ip_address=ip_address("192.168.1.100"),
|
||||
@ -133,7 +135,8 @@ async def test_step_zeroconf_already_in_progress(hass: HomeAssistant) -> None:
|
||||
assert result["reason"] == "already_in_progress"
|
||||
|
||||
|
||||
async def test_step_dhcp(hass: HomeAssistant, setup_guardian) -> None:
|
||||
@pytest.mark.usefixtures("setup_guardian")
|
||||
async def test_step_dhcp(hass: HomeAssistant) -> None:
|
||||
"""Test the dhcp step."""
|
||||
dhcp_data = dhcp.DhcpServiceInfo(
|
||||
ip="192.168.1.100",
|
||||
|
@ -4,15 +4,16 @@ from homeassistant.components.diagnostics import REDACTED
|
||||
from homeassistant.components.guardian import DOMAIN, GuardianData
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
config_entry,
|
||||
config_entry: MockConfigEntry,
|
||||
hass_client: ClientSessionGenerator,
|
||||
setup_guardian,
|
||||
setup_guardian: None, # relies on config_entry fixture
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
data: GuardianData = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
Loading…
x
Reference in New Issue
Block a user