Add missing hass type hint in component tests (r) (#124231)

This commit is contained in:
epenet 2024-08-19 21:38:41 +02:00 committed by GitHub
parent 20f7af25e9
commit b4afca3e7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 122 additions and 31 deletions

View File

@ -18,7 +18,11 @@ from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockUser, mock_platform from tests.common import MockUser, mock_platform
from tests.typing import ClientSessionGenerator, WebSocketGenerator from tests.typing import (
ClientSessionGenerator,
MockHAClientWebSocket,
WebSocketGenerator,
)
DEFAULT_ISSUES = [ DEFAULT_ISSUES = [
{ {
@ -34,7 +38,11 @@ DEFAULT_ISSUES = [
] ]
async def create_issues(hass, ws_client, issues=None): async def create_issues(
hass: HomeAssistant,
ws_client: MockHAClientWebSocket,
issues: list[dict[str, Any]] | None = None,
) -> list[dict[str, Any]]:
"""Create issues.""" """Create issues."""
def api_issue(issue): def api_issue(issue):
@ -119,7 +127,11 @@ async def mock_repairs_integration(hass: HomeAssistant) -> None:
"""Mock a repairs integration.""" """Mock a repairs integration."""
hass.config.components.add("fake_integration") hass.config.components.add("fake_integration")
def async_create_fix_flow(hass, issue_id, data): def async_create_fix_flow(
hass: HomeAssistant,
issue_id: str,
data: dict[str, str | int | float | None] | None,
) -> RepairsFlow:
assert issue_id in EXPECTED_DATA assert issue_id in EXPECTED_DATA
assert data == EXPECTED_DATA[issue_id] assert data == EXPECTED_DATA[issue_id]

View File

@ -29,7 +29,9 @@ def com_port():
return port return port
async def start_options_flow(hass, entry): async def start_options_flow(
hass: HomeAssistant, entry: MockConfigEntry
) -> config_entries.ConfigFlowResult:
"""Start the options flow with the entry under test.""" """Start the options flow with the entry under test."""
entry.add_to_hass(hass) entry.add_to_hass(hass)

View File

@ -47,7 +47,7 @@ async def test_device_test_data(rfxtrx, device: DeviceTestData) -> None:
} }
async def setup_entry(hass, devices): async def setup_entry(hass: HomeAssistant, devices: dict[str, Any]) -> None:
"""Construct a config setup.""" """Construct a config setup."""
entry_data = create_rfx_test_cfg(devices=devices) entry_data = create_rfx_test_cfg(devices=devices)
mock_entry = MockConfigEntry(domain="rfxtrx", unique_id=DOMAIN, data=entry_data) mock_entry = MockConfigEntry(domain="rfxtrx", unique_id=DOMAIN, data=entry_data)
@ -79,7 +79,10 @@ def _get_expected_actions(data):
], ],
) )
async def test_get_actions( async def test_get_actions(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, device, expected hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
device: DeviceTestData,
expected,
) -> None: ) -> None:
"""Test we get the expected actions from a rfxtrx.""" """Test we get the expected actions from a rfxtrx."""
await setup_entry(hass, {device.code: {}}) await setup_entry(hass, {device.code: {}})
@ -136,7 +139,7 @@ async def test_action(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
rfxtrx: RFXtrx.Connect, rfxtrx: RFXtrx.Connect,
device, device: DeviceTestData,
config, config,
expected, expected,
) -> None: ) -> None:

View File

@ -46,7 +46,7 @@ EVENT_FIREALARM_1 = EventTestData(
) )
async def setup_entry(hass, devices): async def setup_entry(hass: HomeAssistant, devices: dict[str, Any]) -> None:
"""Construct a config setup.""" """Construct a config setup."""
entry_data = create_rfx_test_cfg(devices=devices) entry_data = create_rfx_test_cfg(devices=devices)
mock_entry = MockConfigEntry(domain="rfxtrx", unique_id=DOMAIN, data=entry_data) mock_entry = MockConfigEntry(domain="rfxtrx", unique_id=DOMAIN, data=entry_data)

View File

@ -3,12 +3,14 @@
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components.ring import DOMAIN from homeassistant.components.ring import DOMAIN
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def setup_platform(hass, platform): async def setup_platform(hass: HomeAssistant, platform: Platform) -> None:
"""Set up the ring platform and prerequisites.""" """Set up the ring platform and prerequisites."""
MockConfigEntry(domain=DOMAIN, data={"username": "foo", "token": {}}).add_to_hass( MockConfigEntry(domain=DOMAIN, data={"username": "foo", "token": {}}).add_to_hass(
hass hass

View File

@ -1,5 +1,7 @@
"""Tests for the Risco alarm control panel device.""" """Tests for the Risco alarm control panel device."""
from collections.abc import Callable
from typing import Any
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
import pytest import pytest
@ -180,8 +182,13 @@ async def test_cloud_setup(
async def _check_cloud_state( async def _check_cloud_state(
hass, partitions, property, state, entity_id, partition_id hass: HomeAssistant,
): partitions: dict[int, Any],
property: str,
state: str,
entity_id: str,
partition_id: int,
) -> None:
with patch.object(partitions[partition_id], property, return_value=True): with patch.object(partitions[partition_id], property, return_value=True):
await async_update_entity(hass, entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -256,7 +263,9 @@ async def test_cloud_states(
) )
async def _call_alarm_service(hass, service, entity_id, **kwargs): async def _call_alarm_service(
hass: HomeAssistant, service: str, entity_id: str, **kwargs: Any
) -> None:
data = {"entity_id": entity_id, **kwargs} data = {"entity_id": entity_id, **kwargs}
await hass.services.async_call( await hass.services.async_call(
@ -265,16 +274,27 @@ async def _call_alarm_service(hass, service, entity_id, **kwargs):
async def _test_cloud_service_call( async def _test_cloud_service_call(
hass, service, method, entity_id, partition_id, *args, **kwargs hass: HomeAssistant,
): service: str,
method: str,
entity_id: str,
partition_id: int,
*args: Any,
**kwargs: Any,
) -> None:
with patch(f"homeassistant.components.risco.RiscoCloud.{method}") as set_mock: with patch(f"homeassistant.components.risco.RiscoCloud.{method}") as set_mock:
await _call_alarm_service(hass, service, entity_id, **kwargs) await _call_alarm_service(hass, service, entity_id, **kwargs)
set_mock.assert_awaited_once_with(partition_id, *args) set_mock.assert_awaited_once_with(partition_id, *args)
async def _test_cloud_no_service_call( async def _test_cloud_no_service_call(
hass, service, method, entity_id, partition_id, **kwargs hass: HomeAssistant,
): service: str,
method: str,
entity_id: str,
partition_id: int,
**kwargs: Any,
) -> None:
with patch(f"homeassistant.components.risco.RiscoCloud.{method}") as set_mock: with patch(f"homeassistant.components.risco.RiscoCloud.{method}") as set_mock:
await _call_alarm_service(hass, service, entity_id, **kwargs) await _call_alarm_service(hass, service, entity_id, **kwargs)
set_mock.assert_not_awaited() set_mock.assert_not_awaited()
@ -531,8 +551,14 @@ async def test_local_setup(
async def _check_local_state( async def _check_local_state(
hass, partitions, property, state, entity_id, partition_id, callback hass: HomeAssistant,
): partitions: dict[int, Any],
property: str,
state: str,
entity_id: str,
partition_id: int,
callback: Callable,
) -> None:
with patch.object(partitions[partition_id], property, return_value=True): with patch.object(partitions[partition_id], property, return_value=True):
await callback(partition_id, partitions[partition_id]) await callback(partition_id, partitions[partition_id])
@ -629,16 +655,27 @@ async def test_local_states(
async def _test_local_service_call( async def _test_local_service_call(
hass, service, method, entity_id, partition, *args, **kwargs hass: HomeAssistant,
): service: str,
method: str,
entity_id: str,
partition: int,
*args: Any,
**kwargs: Any,
) -> None:
with patch.object(partition, method, AsyncMock()) as set_mock: with patch.object(partition, method, AsyncMock()) as set_mock:
await _call_alarm_service(hass, service, entity_id, **kwargs) await _call_alarm_service(hass, service, entity_id, **kwargs)
set_mock.assert_awaited_once_with(*args) set_mock.assert_awaited_once_with(*args)
async def _test_local_no_service_call( async def _test_local_no_service_call(
hass, service, method, entity_id, partition, **kwargs hass: HomeAssistant,
): service: str,
method: str,
entity_id: str,
partition: int,
**kwargs: Any,
) -> None:
with patch.object(partition, method, AsyncMock()) as set_mock: with patch.object(partition, method, AsyncMock()) as set_mock:
await _call_alarm_service(hass, service, entity_id, **kwargs) await _call_alarm_service(hass, service, entity_id, **kwargs)
set_mock.assert_not_awaited() set_mock.assert_not_awaited()

View File

@ -1,6 +1,8 @@
"""Tests for the Risco binary sensors.""" """Tests for the Risco binary sensors."""
from unittest.mock import PropertyMock, patch from collections.abc import Callable
from typing import Any
from unittest.mock import MagicMock, PropertyMock, patch
import pytest import pytest
@ -59,7 +61,13 @@ async def test_cloud_setup(
assert device.manufacturer == "Risco" assert device.manufacturer == "Risco"
async def _check_cloud_state(hass, zones, triggered, entity_id, zone_id): async def _check_cloud_state(
hass: HomeAssistant,
zones: dict[int, Any],
triggered: bool,
entity_id: str,
zone_id: int,
) -> None:
with patch.object( with patch.object(
zones[zone_id], zones[zone_id],
"triggered", "triggered",
@ -130,8 +138,14 @@ async def test_local_setup(
async def _check_local_state( async def _check_local_state(
hass, zones, entity_property, value, entity_id, zone_id, callback hass: HomeAssistant,
): zones: dict[int, Any],
entity_property: str,
value: bool,
entity_id: str,
zone_id: int,
callback: Callable,
) -> None:
with patch.object( with patch.object(
zones[zone_id], zones[zone_id],
entity_property, entity_property,
@ -218,7 +232,13 @@ async def test_armed_local_states(
) )
async def _check_system_state(hass, system, entity_property, value, callback): async def _check_system_state(
hass: HomeAssistant,
system: MagicMock,
entity_property: str,
value: bool,
callback: Callable,
) -> None:
with patch.object( with patch.object(
system, system,
entity_property, entity_property,

View File

@ -136,7 +136,7 @@ async def test_error_on_login(
assert not entity_registry.async_is_registered(entity_id) assert not entity_registry.async_is_registered(entity_id)
def _check_state(hass, category, entity_id): def _check_state(hass: HomeAssistant, category: str, entity_id: str) -> None:
event_index = CATEGORIES_TO_EVENTS[category] event_index = CATEGORIES_TO_EVENTS[category]
event = TEST_EVENTS[event_index] event = TEST_EVENTS[event_index]
state = hass.states.get(entity_id) state = hass.states.get(entity_id)

View File

@ -1,5 +1,7 @@
"""Tests for the Risco binary sensors.""" """Tests for the Risco binary sensors."""
from collections.abc import Callable
from typing import Any
from unittest.mock import PropertyMock, patch from unittest.mock import PropertyMock, patch
import pytest import pytest
@ -40,7 +42,13 @@ async def test_cloud_setup(
assert entity_registry.async_is_registered(SECOND_ENTITY_ID) assert entity_registry.async_is_registered(SECOND_ENTITY_ID)
async def _check_cloud_state(hass, zones, bypassed, entity_id, zone_id): async def _check_cloud_state(
hass: HomeAssistant,
zones: dict[int, Any],
bypassed: bool,
entity_id: str,
zone_id: int,
) -> None:
with patch.object( with patch.object(
zones[zone_id], zones[zone_id],
"bypassed", "bypassed",
@ -117,7 +125,14 @@ async def test_local_setup(
assert entity_registry.async_is_registered(SECOND_ENTITY_ID) assert entity_registry.async_is_registered(SECOND_ENTITY_ID)
async def _check_local_state(hass, zones, bypassed, entity_id, zone_id, callback): async def _check_local_state(
hass: HomeAssistant,
zones: dict[int, Any],
bypassed: bool,
entity_id: str,
zone_id: int,
callback: Callable,
) -> None:
with patch.object( with patch.object(
zones[zone_id], zones[zone_id],
"bypassed", "bypassed",

View File

@ -24,7 +24,7 @@ ENTITY_ID = "binary_sensor.rpi_power_status"
MODULE = "homeassistant.components.rpi_power.binary_sensor.new_under_voltage" MODULE = "homeassistant.components.rpi_power.binary_sensor.new_under_voltage"
async def _async_setup_component(hass, detected): async def _async_setup_component(hass: HomeAssistant, detected: bool) -> MagicMock:
mocked_under_voltage = MagicMock() mocked_under_voltage = MagicMock()
type(mocked_under_voltage).get = MagicMock(return_value=detected) type(mocked_under_voltage).get = MagicMock(return_value=detected)
entry = MockConfigEntry(domain=DOMAIN) entry = MockConfigEntry(domain=DOMAIN)