Skip to content

Commit

Permalink
Add missing hass type hint in component tests (i) (#124216)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Aug 19, 2024
1 parent 057f311 commit f0af33b
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 33 deletions.
2 changes: 1 addition & 1 deletion tests/components/iaqualink/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from tests.common import async_fire_time_changed


async def _ffwd_next_update_interval(hass):
async def _ffwd_next_update_interval(hass: HomeAssistant) -> None:
now = dt_util.utcnow()
async_fire_time_changed(hass, now + UPDATE_INTERVAL)
await hass.async_block_till_done()
Expand Down
6 changes: 3 additions & 3 deletions tests/components/image_processing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

from homeassistant.components.image_processing import DOMAIN, SERVICE_SCAN
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.loader import bind_hass


@bind_hass
def scan(hass, entity_id=ENTITY_MATCH_ALL):
def scan(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None:
"""Force process of all cameras or given entity."""
hass.add_job(async_scan, hass, entity_id)


@callback
@bind_hass
def async_scan(hass, entity_id=ENTITY_MATCH_ALL):
def async_scan(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None:
"""Force process of all cameras or given entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_create_task(hass.services.async_call(DOMAIN, SERVICE_SCAN, data))
14 changes: 8 additions & 6 deletions tests/components/image_processing/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ def aiohttp_unused_port_factory(
return unused_tcp_port_factory


def get_url(hass):
def get_url(hass: HomeAssistant) -> str:
"""Return camera url."""
state = hass.states.get("camera.demo_camera")
return f"{hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}"


async def setup_image_processing(hass, aiohttp_unused_port_factory):
async def setup_image_processing(
hass: HomeAssistant, aiohttp_unused_port_factory: Callable[[], int]
) -> None:
"""Set up things to be run when tests are started."""
await async_setup_component(
hass,
Expand All @@ -55,7 +57,7 @@ async def setup_image_processing(hass, aiohttp_unused_port_factory):
await hass.async_block_till_done()


async def setup_image_processing_face(hass):
async def setup_image_processing_face(hass: HomeAssistant) -> None:
"""Set up things to be run when tests are started."""
config = {ip.DOMAIN: {"platform": "demo"}, "camera": {"platform": "demo"}}

Expand Down Expand Up @@ -93,7 +95,7 @@ async def test_setup_component_with_service(hass: HomeAssistant) -> None:
async def test_get_image_from_camera(
mock_camera_read,
hass: HomeAssistant,
aiohttp_unused_port_factory,
aiohttp_unused_port_factory: Callable[[], int],
) -> None:
"""Grab an image from camera entity."""
await setup_image_processing(hass, aiohttp_unused_port_factory)
Expand All @@ -116,7 +118,7 @@ async def test_get_image_from_camera(
async def test_get_image_without_exists_camera(
mock_image,
hass: HomeAssistant,
aiohttp_unused_port_factory,
aiohttp_unused_port_factory: Callable[[], int],
) -> None:
"""Try to get image without exists camera."""
await setup_image_processing(hass, aiohttp_unused_port_factory)
Expand Down Expand Up @@ -191,7 +193,7 @@ async def test_face_event_call_no_confidence(
@pytest.mark.usefixtures("enable_custom_integrations")
async def test_update_missing_camera(
hass: HomeAssistant,
aiohttp_unused_port_factory,
aiohttp_unused_port_factory: Callable[[], int],
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test when entity does not set camera."""
Expand Down
4 changes: 3 additions & 1 deletion tests/components/influxdb/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ async def test_invalid_config(
assert not await async_setup_component(hass, influxdb.DOMAIN, config)


async def _setup(hass, mock_influx_client, config_ext, get_write_api):
async def _setup(
hass: HomeAssistant, mock_influx_client, config_ext, get_write_api
) -> None:
"""Prepare client for next test and return event handler method."""
config = {
"influxdb": {
Expand Down
6 changes: 4 additions & 2 deletions tests/components/influxdb/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from homeassistant.components.influxdb.sensor import PLATFORM_SCHEMA
from homeassistant.const import STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.entity_platform import PLATFORM_NOT_READY_BASE_WAIT_TIME
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
Expand Down Expand Up @@ -190,7 +190,9 @@ def get_return_value(query):
return query_api


async def _setup(hass, config_ext, queries, expected_sensors):
async def _setup(
hass: HomeAssistant, config_ext, queries, expected_sensors
) -> list[State]:
"""Create client and test expected sensors."""
config = {
DOMAIN: config_ext,
Expand Down
12 changes: 9 additions & 3 deletions tests/components/input_datetime/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ async def _storage(items=None, config=None):
return _storage


async def async_set_date_and_time(hass, entity_id, dt_value):
async def async_set_date_and_time(
hass: HomeAssistant, entity_id: str, dt_value: datetime.datetime
) -> None:
"""Set date and / or time of input_datetime."""
await hass.services.async_call(
DOMAIN,
Expand All @@ -93,7 +95,9 @@ async def async_set_date_and_time(hass, entity_id, dt_value):
)


async def async_set_datetime(hass, entity_id, dt_value):
async def async_set_datetime(
hass: HomeAssistant, entity_id: str, dt_value: datetime.datetime
) -> None:
"""Set date and / or time of input_datetime."""
await hass.services.async_call(
DOMAIN,
Expand All @@ -103,7 +107,9 @@ async def async_set_datetime(hass, entity_id, dt_value):
)


async def async_set_timestamp(hass, entity_id, timestamp):
async def async_set_timestamp(
hass: HomeAssistant, entity_id: str, timestamp: float
) -> None:
"""Set date and / or time of input_datetime."""
await hass.services.async_call(
DOMAIN,
Expand Down
6 changes: 3 additions & 3 deletions tests/components/input_number/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def _storage(items=None, config=None):
return _storage


async def set_value(hass, entity_id, value):
async def set_value(hass: HomeAssistant, entity_id: str, value: str) -> None:
"""Set input_number to value.
This is a legacy helper method. Do not use it for new tests.
Expand All @@ -78,7 +78,7 @@ async def set_value(hass, entity_id, value):
)


async def increment(hass, entity_id):
async def increment(hass: HomeAssistant, entity_id: str) -> None:
"""Increment value of entity.
This is a legacy helper method. Do not use it for new tests.
Expand All @@ -88,7 +88,7 @@ async def increment(hass, entity_id):
)


async def decrement(hass, entity_id):
async def decrement(hass: HomeAssistant, entity_id: str) -> None:
"""Decrement value of entity.
This is a legacy helper method. Do not use it for new tests.
Expand Down
2 changes: 1 addition & 1 deletion tests/components/input_text/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async def _storage(items=None, config=None):
return _storage


async def async_set_value(hass, entity_id, value):
async def async_set_value(hass: HomeAssistant, entity_id: str, value: str) -> None:
"""Set input_text to value."""
await hass.services.async_call(
DOMAIN,
Expand Down
7 changes: 5 additions & 2 deletions tests/components/insteon/test_api_aldb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the Insteon All-Link Database APIs."""

import json
from typing import Any
from unittest.mock import patch

from pyinsteon import pub
Expand All @@ -23,7 +24,7 @@
from .mock_devices import MockDevices

from tests.common import load_fixture
from tests.typing import WebSocketGenerator
from tests.typing import MockHAClientWebSocket, WebSocketGenerator


@pytest.fixture(name="aldb_data", scope="module")
Expand All @@ -32,7 +33,9 @@ def aldb_data_fixture():
return json.loads(load_fixture("insteon/aldb_data.json"))


async def _setup(hass, hass_ws_client, aldb_data):
async def _setup(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, aldb_data: dict[str, Any]
) -> tuple[MockHAClientWebSocket, MockDevices]:
"""Set up tests."""
ws_client = await hass_ws_client(hass)
devices = MockDevices()
Expand Down
10 changes: 8 additions & 2 deletions tests/components/insteon/test_api_properties.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the Insteon properties APIs."""

import json
from typing import Any
from unittest.mock import AsyncMock, patch

from pyinsteon.config import MOMENTARY_DELAY, RELAY_MODE, TOGGLE_BUTTON
Expand All @@ -26,7 +27,7 @@
from .mock_devices import MockDevices

from tests.common import load_fixture
from tests.typing import WebSocketGenerator
from tests.typing import MockHAClientWebSocket, WebSocketGenerator


@pytest.fixture(name="kpl_properties_data", scope="module")
Expand All @@ -41,7 +42,12 @@ def iolinc_properties_data_fixture():
return json.loads(load_fixture("insteon/iolinc_properties.json"))


async def _setup(hass, hass_ws_client, address, properties_data):
async def _setup(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
address: str,
properties_data: dict[str, Any],
) -> tuple[MockHAClientWebSocket, MockDevices]:
"""Set up tests."""
ws_client = await hass_ws_client(hass)
devices = MockDevices()
Expand Down
15 changes: 11 additions & 4 deletions tests/components/insteon/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Test the config flow for the Insteon integration."""

from unittest.mock import patch
from collections.abc import Callable
from typing import Any
from unittest.mock import AsyncMock, patch

import pytest
from voluptuous_serialize import convert
Expand All @@ -14,7 +16,7 @@
STEP_PLM_MANUALLY,
)
from homeassistant.components.insteon.const import CONF_HUB_VERSION, DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.config_entries import ConfigEntryState, ConfigFlowResult
from homeassistant.const import CONF_DEVICE, CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
Expand Down Expand Up @@ -60,7 +62,7 @@ async def mock_failed_connection(*args, **kwargs):
raise ConnectionError("Connection failed")


async def _init_form(hass, modem_type):
async def _init_form(hass: HomeAssistant, modem_type: str) -> ConfigFlowResult:
"""Run the user form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
Expand All @@ -73,7 +75,12 @@ async def _init_form(hass, modem_type):
)


async def _device_form(hass, flow_id, connection, user_input):
async def _device_form(
hass: HomeAssistant,
flow_id: str,
connection: Callable[..., Any],
user_input: dict[str, Any] | None,
) -> tuple[ConfigFlowResult, AsyncMock]:
"""Test the PLM, Hub v1 or Hub v2 form."""
with (
patch(
Expand Down
12 changes: 7 additions & 5 deletions tests/components/izone/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for iZone."""

from collections.abc import Callable
from typing import Any
from unittest.mock import Mock, patch

import pytest
Expand All @@ -12,23 +14,23 @@


@pytest.fixture
def mock_disco():
def mock_disco() -> Mock:
"""Mock discovery service."""
disco = Mock()
disco.pi_disco = Mock()
disco.pi_disco.controllers = {}
return disco


def _mock_start_discovery(hass, mock_disco):
def do_disovered(*args):
def _mock_start_discovery(hass: HomeAssistant, mock_disco: Mock) -> Callable[..., Mock]:
def do_disovered(*args: Any) -> Mock:
async_dispatcher_send(hass, DISPATCH_CONTROLLER_DISCOVERED, True)
return mock_disco

return do_disovered


async def test_not_found(hass: HomeAssistant, mock_disco) -> None:
async def test_not_found(hass: HomeAssistant, mock_disco: Mock) -> None:
"""Test not finding iZone controller."""

with (
Expand Down Expand Up @@ -56,7 +58,7 @@ async def test_not_found(hass: HomeAssistant, mock_disco) -> None:
stop_disco.assert_called_once()


async def test_found(hass: HomeAssistant, mock_disco) -> None:
async def test_found(hass: HomeAssistant, mock_disco: Mock) -> None:
"""Test not finding iZone controller."""
mock_disco.pi_disco.controllers["blah"] = object()

Expand Down

0 comments on commit f0af33b

Please sign in to comment.