Improve type hints in enphase_envoy tests (#120676)

This commit is contained in:
epenet 2024-06-27 20:24:22 +02:00 committed by GitHub
parent c419d226d5
commit 629dab238f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 17 deletions

View File

@ -21,6 +21,7 @@ from pyenphase.models.meters import (
EnvoyPhaseMode, EnvoyPhaseMode,
) )
import pytest import pytest
from typing_extensions import AsyncGenerator
from homeassistant.components.enphase_envoy import DOMAIN from homeassistant.components.enphase_envoy import DOMAIN
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
@ -31,7 +32,9 @@ from tests.common import MockConfigEntry
@pytest.fixture(name="config_entry") @pytest.fixture(name="config_entry")
def config_entry_fixture(hass: HomeAssistant, config, serial_number): def config_entry_fixture(
hass: HomeAssistant, config: dict[str, str], serial_number: str
) -> MockConfigEntry:
"""Define a config entry fixture.""" """Define a config entry fixture."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -45,7 +48,7 @@ def config_entry_fixture(hass: HomeAssistant, config, serial_number):
@pytest.fixture(name="config") @pytest.fixture(name="config")
def config_fixture(): def config_fixture() -> dict[str, str]:
"""Define a config entry data fixture.""" """Define a config entry data fixture."""
return { return {
CONF_HOST: "1.1.1.1", CONF_HOST: "1.1.1.1",
@ -57,11 +60,11 @@ def config_fixture():
@pytest.fixture(name="mock_envoy") @pytest.fixture(name="mock_envoy")
def mock_envoy_fixture( def mock_envoy_fixture(
serial_number, serial_number: str,
mock_authenticate, mock_authenticate: AsyncMock,
mock_setup, mock_setup: AsyncMock,
mock_auth, mock_auth: EnvoyTokenAuth,
): ) -> Mock:
"""Define a mocked Envoy fixture.""" """Define a mocked Envoy fixture."""
mock_envoy = Mock(spec=Envoy) mock_envoy = Mock(spec=Envoy)
mock_envoy.serial_number = serial_number mock_envoy.serial_number = serial_number
@ -352,9 +355,9 @@ def mock_envoy_fixture(
@pytest.fixture(name="setup_enphase_envoy") @pytest.fixture(name="setup_enphase_envoy")
async def setup_enphase_envoy_fixture( async def setup_enphase_envoy_fixture(
hass: HomeAssistant, hass: HomeAssistant,
config, config: dict[str, str],
mock_envoy, mock_envoy: Mock,
): ) -> AsyncGenerator[None]:
"""Define a fixture to set up Enphase Envoy.""" """Define a fixture to set up Enphase Envoy."""
with ( with (
patch( patch(
@ -372,13 +375,13 @@ async def setup_enphase_envoy_fixture(
@pytest.fixture(name="mock_authenticate") @pytest.fixture(name="mock_authenticate")
def mock_authenticate(): def mock_authenticate() -> AsyncMock:
"""Define a mocked Envoy.authenticate fixture.""" """Define a mocked Envoy.authenticate fixture."""
return AsyncMock() return AsyncMock()
@pytest.fixture(name="mock_auth") @pytest.fixture(name="mock_auth")
def mock_auth(serial_number): def mock_auth(serial_number: str) -> EnvoyTokenAuth:
"""Define a mocked EnvoyAuth fixture.""" """Define a mocked EnvoyAuth fixture."""
token = jwt.encode( token = jwt.encode(
payload={"name": "envoy", "exp": 1907837780}, key="secret", algorithm="HS256" payload={"name": "envoy", "exp": 1907837780}, key="secret", algorithm="HS256"
@ -387,12 +390,12 @@ def mock_auth(serial_number):
@pytest.fixture(name="mock_setup") @pytest.fixture(name="mock_setup")
def mock_setup(): def mock_setup() -> AsyncMock:
"""Define a mocked Envoy.setup fixture.""" """Define a mocked Envoy.setup fixture."""
return AsyncMock() return AsyncMock()
@pytest.fixture(name="serial_number") @pytest.fixture(name="serial_number")
def serial_number_fixture(): def serial_number_fixture() -> str:
"""Define a serial number fixture.""" """Define a serial number fixture."""
return "1234" return "1234"

View File

@ -1,9 +1,10 @@
"""Test Enphase Envoy sensors.""" """Test Enphase Envoy sensors."""
from unittest.mock import patch from unittest.mock import Mock, patch
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from typing_extensions import AsyncGenerator
from homeassistant.components.enphase_envoy import DOMAIN from homeassistant.components.enphase_envoy import DOMAIN
from homeassistant.components.enphase_envoy.const import Platform from homeassistant.components.enphase_envoy.const import Platform
@ -15,7 +16,9 @@ from tests.common import MockConfigEntry
@pytest.fixture(name="setup_enphase_envoy_sensor") @pytest.fixture(name="setup_enphase_envoy_sensor")
async def setup_enphase_envoy_sensor_fixture(hass, config, mock_envoy): async def setup_enphase_envoy_sensor_fixture(
hass: HomeAssistant, config: dict[str, str], mock_envoy: Mock
) -> AsyncGenerator[None]:
"""Define a fixture to set up Enphase Envoy with sensor platform only.""" """Define a fixture to set up Enphase Envoy with sensor platform only."""
with ( with (
patch( patch(
@ -41,7 +44,7 @@ async def test_sensor(
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
setup_enphase_envoy_sensor, setup_enphase_envoy_sensor: None,
) -> None: ) -> None:
"""Test enphase_envoy sensor entities.""" """Test enphase_envoy sensor entities."""
# compare registered entities against snapshot of prior run # compare registered entities against snapshot of prior run