Improve type hints in lcn tests (#123648)
parent
31dcc6f685
commit
b0d1d7bdb2
|
@ -1,5 +1,6 @@
|
||||||
"""Test configuration and mocks for LCN component."""
|
"""Test configuration and mocks for LCN component."""
|
||||||
|
|
||||||
|
from collections.abc import AsyncGenerator
|
||||||
import json
|
import json
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
@ -10,8 +11,9 @@ from pypck.module import GroupConnection, ModuleConnection
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.lcn.const import DOMAIN
|
from homeassistant.components.lcn.const import DOMAIN
|
||||||
from homeassistant.components.lcn.helpers import generate_unique_id
|
from homeassistant.components.lcn.helpers import AddressType, generate_unique_id
|
||||||
from homeassistant.const import CONF_HOST
|
from homeassistant.const import CONF_HOST
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
@ -42,13 +44,13 @@ class MockGroupConnection(GroupConnection):
|
||||||
class MockPchkConnectionManager(PchkConnectionManager):
|
class MockPchkConnectionManager(PchkConnectionManager):
|
||||||
"""Fake connection handler."""
|
"""Fake connection handler."""
|
||||||
|
|
||||||
async def async_connect(self, timeout=30):
|
async def async_connect(self, timeout: int = 30) -> None:
|
||||||
"""Mock establishing a connection to PCHK."""
|
"""Mock establishing a connection to PCHK."""
|
||||||
self.authentication_completed_future.set_result(True)
|
self.authentication_completed_future.set_result(True)
|
||||||
self.license_error_future.set_result(True)
|
self.license_error_future.set_result(True)
|
||||||
self.segment_scan_completed_event.set()
|
self.segment_scan_completed_event.set()
|
||||||
|
|
||||||
async def async_close(self):
|
async def async_close(self) -> None:
|
||||||
"""Mock closing a connection to PCHK."""
|
"""Mock closing a connection to PCHK."""
|
||||||
|
|
||||||
@patch.object(pypck.connection, "ModuleConnection", MockModuleConnection)
|
@patch.object(pypck.connection, "ModuleConnection", MockModuleConnection)
|
||||||
|
@ -60,7 +62,7 @@ class MockPchkConnectionManager(PchkConnectionManager):
|
||||||
send_command = AsyncMock()
|
send_command = AsyncMock()
|
||||||
|
|
||||||
|
|
||||||
def create_config_entry(name):
|
def create_config_entry(name: str) -> MockConfigEntry:
|
||||||
"""Set up config entries with configuration data."""
|
"""Set up config entries with configuration data."""
|
||||||
fixture_filename = f"lcn/config_entry_{name}.json"
|
fixture_filename = f"lcn/config_entry_{name}.json"
|
||||||
entry_data = json.loads(load_fixture(fixture_filename))
|
entry_data = json.loads(load_fixture(fixture_filename))
|
||||||
|
@ -78,19 +80,21 @@ def create_config_entry(name):
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="entry")
|
@pytest.fixture(name="entry")
|
||||||
def create_config_entry_pchk():
|
def create_config_entry_pchk() -> MockConfigEntry:
|
||||||
"""Return one specific config entry."""
|
"""Return one specific config entry."""
|
||||||
return create_config_entry("pchk")
|
return create_config_entry("pchk")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="entry2")
|
@pytest.fixture(name="entry2")
|
||||||
def create_config_entry_myhome():
|
def create_config_entry_myhome() -> MockConfigEntry:
|
||||||
"""Return one specific config entry."""
|
"""Return one specific config entry."""
|
||||||
return create_config_entry("myhome")
|
return create_config_entry("myhome")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="lcn_connection")
|
@pytest.fixture(name="lcn_connection")
|
||||||
async def init_integration(hass, entry):
|
async def init_integration(
|
||||||
|
hass: HomeAssistant, entry: MockConfigEntry
|
||||||
|
) -> AsyncGenerator[MockPchkConnectionManager]:
|
||||||
"""Set up the LCN integration in Home Assistant."""
|
"""Set up the LCN integration in Home Assistant."""
|
||||||
lcn_connection = None
|
lcn_connection = None
|
||||||
|
|
||||||
|
@ -109,7 +113,7 @@ async def init_integration(hass, entry):
|
||||||
yield lcn_connection
|
yield lcn_connection
|
||||||
|
|
||||||
|
|
||||||
async def setup_component(hass):
|
async def setup_component(hass: HomeAssistant) -> None:
|
||||||
"""Set up the LCN component."""
|
"""Set up the LCN component."""
|
||||||
fixture_filename = "lcn/config.json"
|
fixture_filename = "lcn/config.json"
|
||||||
config_data = json.loads(load_fixture(fixture_filename))
|
config_data = json.loads(load_fixture(fixture_filename))
|
||||||
|
@ -118,7 +122,9 @@ async def setup_component(hass):
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
||||||
def get_device(hass, entry, address):
|
def get_device(
|
||||||
|
hass: HomeAssistant, entry: MockConfigEntry, address: AddressType
|
||||||
|
) -> dr.DeviceEntry:
|
||||||
"""Get LCN device for specified address."""
|
"""Get LCN device for specified address."""
|
||||||
device_registry = dr.async_get(hass)
|
device_registry = dr.async_get(hass)
|
||||||
identifiers = {(DOMAIN, generate_unique_id(entry.entry_id, address))}
|
identifiers = {(DOMAIN, generate_unique_id(entry.entry_id, address))}
|
||||||
|
|
Loading…
Reference in New Issue