Skip to content

Commit

Permalink
Add missing hass type hint in component tests (g) (#124203)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Aug 19, 2024
1 parent 1fdcbc3 commit 25d33e9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 36 deletions.
14 changes: 9 additions & 5 deletions tests/components/generic_hygrostat/test_humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
DOMAIN as HOMEASSISTANT_DOMAIN,
CoreState,
HomeAssistant,
ServiceCall,
State,
callback,
)
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.typing import StateType
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util

Expand Down Expand Up @@ -206,7 +208,7 @@ async def test_unique_id(
assert entry.unique_id == unique_id


def _setup_sensor(hass, humidity):
def _setup_sensor(hass: HomeAssistant, humidity: StateType) -> None:
"""Set up the test sensor."""
hass.states.async_set(ENT_SENSOR, humidity)

Expand Down Expand Up @@ -669,13 +671,13 @@ async def test_operation_mode_humidify(hass: HomeAssistant) -> None:
assert call.data["entity_id"] == ENT_SWITCH


async def _setup_switch(hass, is_on):
async def _setup_switch(hass: HomeAssistant, is_on: bool) -> list[ServiceCall]:
"""Set up the test switch."""
hass.states.async_set(ENT_SWITCH, STATE_ON if is_on else STATE_OFF)
calls = []

@callback
def log_call(call):
def log_call(call: ServiceCall) -> None:
"""Log service calls."""
calls.append(call)

Expand Down Expand Up @@ -1615,7 +1617,7 @@ async def test_restore_state_uncoherence_case(hass: HomeAssistant) -> None:
assert state.state == STATE_OFF


async def _setup_humidifier(hass):
async def _setup_humidifier(hass: HomeAssistant) -> None:
assert await async_setup_component(
hass,
DOMAIN,
Expand All @@ -1635,7 +1637,9 @@ async def _setup_humidifier(hass):
await hass.async_block_till_done()


def _mock_restore_cache(hass, humidity=40, state=STATE_OFF):
def _mock_restore_cache(
hass: HomeAssistant, humidity: int = 40, state: str = STATE_OFF
) -> None:
mock_restore_cache(
hass,
(
Expand Down
14 changes: 9 additions & 5 deletions tests/components/generic_thermostat/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
DOMAIN as HOMEASSISTANT_DOMAIN,
CoreState,
HomeAssistant,
ServiceCall,
State,
callback,
)
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.typing import StateType
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
Expand Down Expand Up @@ -208,7 +210,7 @@ async def test_unique_id(
assert entry.unique_id == unique_id


def _setup_sensor(hass, temp):
def _setup_sensor(hass: HomeAssistant, temp: StateType) -> None:
"""Set up the test sensor."""
hass.states.async_set(ENT_SENSOR, temp)

Expand Down Expand Up @@ -594,13 +596,13 @@ async def test_hvac_mode_heat(hass: HomeAssistant) -> None:
assert call.data["entity_id"] == ENT_SWITCH


def _setup_switch(hass, is_on):
def _setup_switch(hass: HomeAssistant, is_on: bool) -> list[ServiceCall]:
"""Set up the test switch."""
hass.states.async_set(ENT_SWITCH, STATE_ON if is_on else STATE_OFF)
calls = []

@callback
def log_call(call):
def log_call(call: ServiceCall) -> None:
"""Log service calls."""
calls.append(call)

Expand Down Expand Up @@ -1374,7 +1376,7 @@ async def test_restore_state_uncoherence_case(hass: HomeAssistant) -> None:
assert state.state == HVACMode.OFF


async def _setup_climate(hass):
async def _setup_climate(hass: HomeAssistant) -> None:
assert await async_setup_component(
hass,
DOMAIN,
Expand All @@ -1393,7 +1395,9 @@ async def _setup_climate(hass):
)


def _mock_restore_cache(hass, temperature=20, hvac_mode=HVACMode.OFF):
def _mock_restore_cache(
hass: HomeAssistant, temperature: int = 20, hvac_mode: HVACMode = HVACMode.OFF
) -> None:
mock_restore_cache(
hass,
(
Expand Down
2 changes: 1 addition & 1 deletion tests/components/google/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async def primary_calendar(
)


async def fire_alarm(hass, point_in_time):
async def fire_alarm(hass: HomeAssistant, point_in_time: datetime.datetime) -> None:
"""Fire an alarm and wait for callbacks to run."""
with freeze_time(point_in_time):
async_fire_time_changed(hass, point_in_time)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for the Google Generative AI Conversation integration conversation platform."""

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

from freezegun import freeze_time
Expand Down Expand Up @@ -215,7 +216,9 @@ async def test_function_call(
},
)

def tool_call(hass, tool_input, tool_context):
def tool_call(
hass: HomeAssistant, tool_input: llm.ToolInput, tool_context: llm.LLMContext
) -> dict[str, Any]:
mock_part.function_call = None
mock_part.text = "Hi there!"
return {"result": "Test response"}
Expand Down Expand Up @@ -314,7 +317,9 @@ async def test_function_call_without_parameters(
mock_part = MagicMock()
mock_part.function_call = FunctionCall(name="test_tool", args={})

def tool_call(hass, tool_input, tool_context):
def tool_call(
hass: HomeAssistant, tool_input: llm.ToolInput, tool_context: llm.LLMContext
) -> dict[str, Any]:
mock_part.function_call = None
mock_part.text = "Hi there!"
return {"result": "Test response"}
Expand Down Expand Up @@ -400,7 +405,9 @@ async def test_function_exception(
mock_part = MagicMock()
mock_part.function_call = FunctionCall(name="test_tool", args={"param1": 1})

def tool_call(hass, tool_input, tool_context):
def tool_call(
hass: HomeAssistant, tool_input: llm.ToolInput, tool_context: llm.LLMContext
) -> dict[str, Any]:
mock_part.function_call = None
mock_part.text = "Hi there!"
raise HomeAssistantError("Test tool exception")
Expand Down
3 changes: 2 additions & 1 deletion tests/components/google_pubsub/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass
from datetime import datetime
import os
from typing import Any
from unittest.mock import MagicMock, Mock, patch

import pytest
Expand Down Expand Up @@ -111,7 +112,7 @@ async def test_full_config(hass: HomeAssistant, mock_client) -> None:
)


async def _setup(hass, filter_config):
async def _setup(hass: HomeAssistant, filter_config: dict[str, Any]) -> None:
"""Shared set up for filtering tests."""
config = {
google_pubsub.DOMAIN: {
Expand Down
11 changes: 8 additions & 3 deletions tests/components/google_wifi/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import datetime, timedelta
from http import HTTPStatus
from typing import Any
from unittest.mock import Mock, patch

import requests_mock
Expand Down Expand Up @@ -78,7 +79,9 @@ async def test_setup_get(
assert_setup_component(6, "sensor")


def setup_api(hass, data, requests_mock):
def setup_api(
hass: HomeAssistant | None, data: str | None, requests_mock: requests_mock.Mocker
) -> tuple[google_wifi.GoogleWifiAPI, dict[str, Any]]:
"""Set up API with fake data."""
resource = f"http://localhost{google_wifi.ENDPOINT}"
now = datetime(1970, month=1, day=1)
Expand All @@ -101,7 +104,7 @@ def setup_api(hass, data, requests_mock):
return api, sensor_dict


def fake_delay(hass, ha_delay):
def fake_delay(hass: HomeAssistant, ha_delay: int) -> None:
"""Fake delay to prevent update throttle."""
hass_now = dt_util.utcnow()
shifted_time = hass_now + timedelta(seconds=ha_delay)
Expand Down Expand Up @@ -220,7 +223,9 @@ def test_update_when_unavailable(
assert sensor.state is None


def update_side_effect(hass, requests_mock):
def update_side_effect(
hass: HomeAssistant, requests_mock: requests_mock.Mocker
) -> None:
"""Mock representation of update function."""
api, sensor_dict = setup_api(hass, MOCK_DATA, requests_mock)
api.data = None
Expand Down
36 changes: 18 additions & 18 deletions tests/components/group/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@
SERVICE_SET,
)
from homeassistant.const import ATTR_ICON, ATTR_NAME, SERVICE_RELOAD
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.loader import bind_hass


@bind_hass
def reload(hass):
def reload(hass: HomeAssistant) -> None:
"""Reload the automation from config."""
hass.add_job(async_reload, hass)


@callback
@bind_hass
def async_reload(hass):
def async_reload(hass: HomeAssistant) -> None:
"""Reload the automation from config."""
hass.async_create_task(hass.services.async_call(DOMAIN, SERVICE_RELOAD))


@bind_hass
def set_group(
hass,
object_id,
name=None,
entity_ids=None,
icon=None,
add=None,
):
hass: HomeAssistant,
object_id: str,
name: str | None = None,
entity_ids: list[str] | None = None,
icon: str | None = None,
add: list[str] | None = None,
) -> None:
"""Create/Update a group."""
hass.add_job(
async_set_group,
Expand All @@ -54,13 +54,13 @@ def set_group(
@callback
@bind_hass
def async_set_group(
hass,
object_id,
name=None,
entity_ids=None,
icon=None,
add=None,
):
hass: HomeAssistant,
object_id: str,
name: str | None = None,
entity_ids: list[str] | None = None,
icon: str | None = None,
add: list[str] | None = None,
) -> None:
"""Create/Update a group."""
data = {
key: value
Expand All @@ -79,7 +79,7 @@ def async_set_group(

@callback
@bind_hass
def async_remove(hass, object_id):
def async_remove(hass: HomeAssistant, object_id: str) -> None:
"""Remove a user group."""
data = {ATTR_OBJECT_ID: object_id}
hass.async_create_task(hass.services.async_call(DOMAIN, SERVICE_REMOVE, data))

0 comments on commit 25d33e9

Please sign in to comment.