Improve type hints in onewire tests (#134993)

This commit is contained in:
epenet 2025-01-08 09:33:04 +01:00 committed by GitHub
parent e99aaed7fa
commit f8618e65f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 35 additions and 28 deletions

View File

@ -7,7 +7,7 @@ from pyownet.protocol import ConnError
import pytest import pytest
from homeassistant.components.onewire.const import DOMAIN from homeassistant.components.onewire.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER, ConfigEntry from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -32,7 +32,7 @@ def get_device_id(request: pytest.FixtureRequest) -> str:
@pytest.fixture(name="config_entry") @pytest.fixture(name="config_entry")
def get_config_entry(hass: HomeAssistant) -> ConfigEntry: def get_config_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Create and register mock config entry.""" """Create and register mock config entry."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -54,14 +54,14 @@ def get_config_entry(hass: HomeAssistant) -> ConfigEntry:
@pytest.fixture(name="owproxy") @pytest.fixture(name="owproxy")
def get_owproxy() -> MagicMock: def get_owproxy() -> Generator[MagicMock]:
"""Mock owproxy.""" """Mock owproxy."""
with patch("homeassistant.components.onewire.onewirehub.protocol.proxy") as owproxy: with patch("homeassistant.components.onewire.onewirehub.protocol.proxy") as owproxy:
yield owproxy yield owproxy
@pytest.fixture(name="owproxy_with_connerror") @pytest.fixture(name="owproxy_with_connerror")
def get_owproxy_with_connerror() -> MagicMock: def get_owproxy_with_connerror() -> Generator[MagicMock]:
"""Mock owproxy.""" """Mock owproxy."""
with patch( with patch(
"homeassistant.components.onewire.onewirehub.protocol.proxy", "homeassistant.components.onewire.onewirehub.protocol.proxy",

View File

@ -6,13 +6,14 @@ from unittest.mock import MagicMock, patch
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import setup_owproxy_mock_devices from . import setup_owproxy_mock_devices
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def override_platforms() -> Generator[None]: def override_platforms() -> Generator[None]:
@ -23,7 +24,7 @@ def override_platforms() -> Generator[None]:
async def test_binary_sensors( async def test_binary_sensors(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
owproxy: MagicMock, owproxy: MagicMock,
device_id: str, device_id: str,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,

View File

@ -11,7 +11,7 @@ from homeassistant.components.onewire.const import (
INPUT_ENTRY_DEVICE_SELECTION, INPUT_ENTRY_DEVICE_SELECTION,
MANUFACTURER_MAXIM, MANUFACTURER_MAXIM,
) )
from homeassistant.config_entries import SOURCE_USER, ConfigEntry from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
@ -24,7 +24,8 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@pytest.fixture @pytest.fixture
async def filled_device_registry( async def filled_device_registry(
hass: HomeAssistant, config_entry: ConfigEntry, device_registry: dr.DeviceRegistry config_entry: MockConfigEntry,
device_registry: dr.DeviceRegistry,
) -> dr.DeviceRegistry: ) -> dr.DeviceRegistry:
"""Fill device registry with mock devices.""" """Fill device registry with mock devices."""
for key in ("28.111111111111", "28.222222222222", "28.222222222223"): for key in ("28.111111111111", "28.222222222222", "28.222222222223"):
@ -81,7 +82,7 @@ async def test_user_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> No
async def test_user_duplicate( async def test_user_duplicate(
hass: HomeAssistant, config_entry: ConfigEntry, mock_setup_entry: AsyncMock hass: HomeAssistant, config_entry: MockConfigEntry, mock_setup_entry: AsyncMock
) -> None: ) -> None:
"""Test user duplicate flow.""" """Test user duplicate flow."""
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
@ -178,7 +179,7 @@ async def test_reconfigure_duplicate(
@pytest.mark.usefixtures("filled_device_registry") @pytest.mark.usefixtures("filled_device_registry")
async def test_user_options_clear( async def test_user_options_clear(
hass: HomeAssistant, config_entry: ConfigEntry hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test clearing the options.""" """Test clearing the options."""
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
@ -202,7 +203,7 @@ async def test_user_options_clear(
@pytest.mark.usefixtures("filled_device_registry") @pytest.mark.usefixtures("filled_device_registry")
async def test_user_options_empty_selection( async def test_user_options_empty_selection(
hass: HomeAssistant, config_entry: ConfigEntry hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test leaving the selection of devices empty.""" """Test leaving the selection of devices empty."""
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
@ -227,7 +228,7 @@ async def test_user_options_empty_selection(
@pytest.mark.usefixtures("filled_device_registry") @pytest.mark.usefixtures("filled_device_registry")
async def test_user_options_set_single( async def test_user_options_set_single(
hass: HomeAssistant, config_entry: ConfigEntry hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test configuring a single device.""" """Test configuring a single device."""
# Clear config options to certify functionality when starting from scratch # Clear config options to certify functionality when starting from scratch
@ -265,7 +266,7 @@ async def test_user_options_set_single(
async def test_user_options_set_multiple( async def test_user_options_set_multiple(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
filled_device_registry: dr.DeviceRegistry, filled_device_registry: dr.DeviceRegistry,
) -> None: ) -> None:
"""Test configuring multiple consecutive devices in a row.""" """Test configuring multiple consecutive devices in a row."""
@ -328,7 +329,7 @@ async def test_user_options_set_multiple(
async def test_user_options_no_devices( async def test_user_options_no_devices(
hass: HomeAssistant, config_entry: ConfigEntry hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test that options does not change when no devices are available.""" """Test that options does not change when no devices are available."""
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)

View File

@ -6,12 +6,12 @@ from unittest.mock import MagicMock, patch
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import setup_owproxy_mock_devices from . import setup_owproxy_mock_devices
from tests.common import MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
@ -40,7 +40,7 @@ DEVICE_DETAILS = {
@pytest.mark.parametrize("device_id", ["EF.111111111113"], indirect=True) @pytest.mark.parametrize("device_id", ["EF.111111111113"], indirect=True)
async def test_entry_diagnostics( async def test_entry_diagnostics(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
owproxy: MagicMock, owproxy: MagicMock,
device_id: str, device_id: str,

View File

@ -7,7 +7,7 @@ from pyownet import protocol
import pytest import pytest
from homeassistant.components.onewire.const import DOMAIN from homeassistant.components.onewire.const import DOMAIN
from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
@ -15,11 +15,14 @@ from homeassistant.setup import async_setup_component
from . import setup_owproxy_mock_devices from . import setup_owproxy_mock_devices
from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator from tests.typing import WebSocketGenerator
@pytest.mark.usefixtures("owproxy_with_connerror") @pytest.mark.usefixtures("owproxy_with_connerror")
async def test_connect_failure(hass: HomeAssistant, config_entry: ConfigEntry) -> None: async def test_connect_failure(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test connection failure raises ConfigEntryNotReady.""" """Test connection failure raises ConfigEntryNotReady."""
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -29,7 +32,7 @@ async def test_connect_failure(hass: HomeAssistant, config_entry: ConfigEntry) -
async def test_listing_failure( async def test_listing_failure(
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock hass: HomeAssistant, config_entry: MockConfigEntry, owproxy: MagicMock
) -> None: ) -> None:
"""Test listing failure raises ConfigEntryNotReady.""" """Test listing failure raises ConfigEntryNotReady."""
owproxy.return_value.dir.side_effect = protocol.OwnetError() owproxy.return_value.dir.side_effect = protocol.OwnetError()
@ -42,7 +45,7 @@ async def test_listing_failure(
@pytest.mark.usefixtures("owproxy") @pytest.mark.usefixtures("owproxy")
async def test_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None: async def test_unload_entry(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Test being able to unload an entry.""" """Test being able to unload an entry."""
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -57,7 +60,7 @@ async def test_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> N
async def test_update_options( async def test_update_options(
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock hass: HomeAssistant, config_entry: MockConfigEntry, owproxy: MagicMock
) -> None: ) -> None:
"""Test update options triggers reload.""" """Test update options triggers reload."""
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
@ -81,7 +84,7 @@ async def test_update_options(
async def test_registry_cleanup( async def test_registry_cleanup(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
owproxy: MagicMock, owproxy: MagicMock,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
) -> None: ) -> None:

View File

@ -9,7 +9,6 @@ from pyownet.protocol import OwnetError
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
@ -17,6 +16,8 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import setup_owproxy_mock_devices from . import setup_owproxy_mock_devices
from .const import ATTR_INJECT_READS, MOCK_OWPROXY_DEVICES from .const import ATTR_INJECT_READS, MOCK_OWPROXY_DEVICES
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def override_platforms() -> Generator[None]: def override_platforms() -> Generator[None]:
@ -27,7 +28,7 @@ def override_platforms() -> Generator[None]:
async def test_sensors( async def test_sensors(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
owproxy: MagicMock, owproxy: MagicMock,
device_id: str, device_id: str,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -66,7 +67,7 @@ async def test_sensors(
@pytest.mark.parametrize("device_id", ["12.111111111111"]) @pytest.mark.parametrize("device_id", ["12.111111111111"])
async def test_tai8570_sensors( async def test_tai8570_sensors(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
owproxy: MagicMock, owproxy: MagicMock,
device_id: str, device_id: str,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,

View File

@ -7,7 +7,6 @@ import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
SERVICE_TOGGLE, SERVICE_TOGGLE,
@ -20,6 +19,8 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import setup_owproxy_mock_devices from . import setup_owproxy_mock_devices
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def override_platforms() -> Generator[None]: def override_platforms() -> Generator[None]:
@ -30,7 +31,7 @@ def override_platforms() -> Generator[None]:
async def test_switches( async def test_switches(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
owproxy: MagicMock, owproxy: MagicMock,
device_id: str, device_id: str,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
@ -70,7 +71,7 @@ async def test_switches(
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_switch_toggle( async def test_switch_toggle(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: MockConfigEntry,
owproxy: MagicMock, owproxy: MagicMock,
device_id: str, device_id: str,
) -> None: ) -> None: