From 975363b66037686179d35c1303a1b4c117bff5f6 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:35:31 +0200 Subject: [PATCH] Add missing hass type hint in component tests (c) (#124067) --- tests/components/cast/test_media_player.py | 21 ++++- tests/components/clicksend_tts/test_notify.py | 2 +- tests/components/climate/common.py | 84 ++++++++++++------- tests/components/cloud/test_client.py | 4 +- tests/components/coinbase/common.py | 13 ++- .../components/config/test_device_registry.py | 13 ++- tests/components/counter/common.py | 8 +- tests/components/cover/test_init.py | 13 +-- 8 files changed, 111 insertions(+), 47 deletions(-) diff --git a/tests/components/cast/test_media_player.py b/tests/components/cast/test_media_player.py index 1d99adb4723..513f32b1ad6 100644 --- a/tests/components/cast/test_media_player.py +++ b/tests/components/cast/test_media_player.py @@ -3,7 +3,9 @@ from __future__ import annotations import asyncio +from collections.abc import Callable import json +from typing import Any from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch from uuid import UUID @@ -112,7 +114,9 @@ def get_fake_zconf(host="192.168.178.42", port=8009): return zconf -async def async_setup_cast(hass, config=None): +async def async_setup_cast( + hass: HomeAssistant, config: dict[str, Any] | None = None +) -> MagicMock: """Set up the cast platform.""" if config is None: config = {} @@ -128,7 +132,20 @@ async def async_setup_cast(hass, config=None): return add_entities -async def async_setup_cast_internal_discovery(hass, config=None): +async def async_setup_cast_internal_discovery( + hass: HomeAssistant, config: dict[str, Any] | None = None +) -> tuple[ + Callable[ + [ + pychromecast.discovery.HostServiceInfo + | pychromecast.discovery.MDNSServiceInfo, + ChromecastInfo, + ], + None, + ], + Callable[[str, ChromecastInfo], None], + MagicMock, +]: """Set up the cast platform and the discovery.""" browser = MagicMock(devices={}, zc={}) diff --git a/tests/components/clicksend_tts/test_notify.py b/tests/components/clicksend_tts/test_notify.py index e73f0576d9e..892d7541354 100644 --- a/tests/components/clicksend_tts/test_notify.py +++ b/tests/components/clicksend_tts/test_notify.py @@ -46,7 +46,7 @@ def mock_clicksend_tts_notify(): yield ns -async def setup_notify(hass): +async def setup_notify(hass: HomeAssistant) -> None: """Test setup.""" with assert_setup_component(1, notify.DOMAIN) as config: assert await async_setup_component(hass, notify.DOMAIN, CONFIG) diff --git a/tests/components/climate/common.py b/tests/components/climate/common.py index c890d3a7bb5..d6aedd23671 100644 --- a/tests/components/climate/common.py +++ b/tests/components/climate/common.py @@ -23,6 +23,7 @@ from homeassistant.components.climate import ( SERVICE_SET_SWING_MODE, SERVICE_SET_TEMPERATURE, ) +from homeassistant.components.climate.const import HVACMode from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_TEMPERATURE, @@ -30,10 +31,13 @@ from homeassistant.const import ( SERVICE_TURN_OFF, SERVICE_TURN_ON, ) +from homeassistant.core import HomeAssistant from homeassistant.loader import bind_hass -async def async_set_preset_mode(hass, preset_mode, entity_id=ENTITY_MATCH_ALL): +async def async_set_preset_mode( + hass: HomeAssistant, preset_mode: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new preset mode.""" data = {ATTR_PRESET_MODE: preset_mode} @@ -44,7 +48,9 @@ async def async_set_preset_mode(hass, preset_mode, entity_id=ENTITY_MATCH_ALL): @bind_hass -def set_preset_mode(hass, preset_mode, entity_id=ENTITY_MATCH_ALL): +def set_preset_mode( + hass: HomeAssistant, preset_mode: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new preset mode.""" data = {ATTR_PRESET_MODE: preset_mode} @@ -54,7 +60,9 @@ def set_preset_mode(hass, preset_mode, entity_id=ENTITY_MATCH_ALL): hass.services.call(DOMAIN, SERVICE_SET_PRESET_MODE, data) -async def async_set_aux_heat(hass, aux_heat, entity_id=ENTITY_MATCH_ALL): +async def async_set_aux_heat( + hass: HomeAssistant, aux_heat: bool, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Turn all or specified climate devices auxiliary heater on.""" data = {ATTR_AUX_HEAT: aux_heat} @@ -65,7 +73,9 @@ async def async_set_aux_heat(hass, aux_heat, entity_id=ENTITY_MATCH_ALL): @bind_hass -def set_aux_heat(hass, aux_heat, entity_id=ENTITY_MATCH_ALL): +def set_aux_heat( + hass: HomeAssistant, aux_heat: bool, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Turn all or specified climate devices auxiliary heater on.""" data = {ATTR_AUX_HEAT: aux_heat} @@ -76,13 +86,13 @@ def set_aux_heat(hass, aux_heat, entity_id=ENTITY_MATCH_ALL): async def async_set_temperature( - hass, - temperature=None, - entity_id=ENTITY_MATCH_ALL, - target_temp_high=None, - target_temp_low=None, - hvac_mode=None, -): + hass: HomeAssistant, + temperature: float | None = None, + entity_id: str = ENTITY_MATCH_ALL, + target_temp_high: float | None = None, + target_temp_low: float | None = None, + hvac_mode: HVACMode | None = None, +) -> None: """Set new target temperature.""" kwargs = { key: value @@ -103,13 +113,13 @@ async def async_set_temperature( @bind_hass def set_temperature( - hass, - temperature=None, - entity_id=ENTITY_MATCH_ALL, - target_temp_high=None, - target_temp_low=None, - hvac_mode=None, -): + hass: HomeAssistant, + temperature: float | None = None, + entity_id: str = ENTITY_MATCH_ALL, + target_temp_high: float | None = None, + target_temp_low: float | None = None, + hvac_mode: HVACMode | None = None, +) -> None: """Set new target temperature.""" kwargs = { key: value @@ -126,7 +136,9 @@ def set_temperature( hass.services.call(DOMAIN, SERVICE_SET_TEMPERATURE, kwargs) -async def async_set_humidity(hass, humidity, entity_id=ENTITY_MATCH_ALL): +async def async_set_humidity( + hass: HomeAssistant, humidity: int, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new target humidity.""" data = {ATTR_HUMIDITY: humidity} @@ -137,7 +149,9 @@ async def async_set_humidity(hass, humidity, entity_id=ENTITY_MATCH_ALL): @bind_hass -def set_humidity(hass, humidity, entity_id=ENTITY_MATCH_ALL): +def set_humidity( + hass: HomeAssistant, humidity: int, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new target humidity.""" data = {ATTR_HUMIDITY: humidity} @@ -147,7 +161,9 @@ def set_humidity(hass, humidity, entity_id=ENTITY_MATCH_ALL): hass.services.call(DOMAIN, SERVICE_SET_HUMIDITY, data) -async def async_set_fan_mode(hass, fan, entity_id=ENTITY_MATCH_ALL): +async def async_set_fan_mode( + hass: HomeAssistant, fan: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set all or specified climate devices fan mode on.""" data = {ATTR_FAN_MODE: fan} @@ -158,7 +174,9 @@ async def async_set_fan_mode(hass, fan, entity_id=ENTITY_MATCH_ALL): @bind_hass -def set_fan_mode(hass, fan, entity_id=ENTITY_MATCH_ALL): +def set_fan_mode( + hass: HomeAssistant, fan: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set all or specified climate devices fan mode on.""" data = {ATTR_FAN_MODE: fan} @@ -168,7 +186,9 @@ def set_fan_mode(hass, fan, entity_id=ENTITY_MATCH_ALL): hass.services.call(DOMAIN, SERVICE_SET_FAN_MODE, data) -async def async_set_hvac_mode(hass, hvac_mode, entity_id=ENTITY_MATCH_ALL): +async def async_set_hvac_mode( + hass: HomeAssistant, hvac_mode: HVACMode, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new target operation mode.""" data = {ATTR_HVAC_MODE: hvac_mode} @@ -179,7 +199,9 @@ async def async_set_hvac_mode(hass, hvac_mode, entity_id=ENTITY_MATCH_ALL): @bind_hass -def set_operation_mode(hass, hvac_mode, entity_id=ENTITY_MATCH_ALL): +def set_operation_mode( + hass: HomeAssistant, hvac_mode: HVACMode, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new target operation mode.""" data = {ATTR_HVAC_MODE: hvac_mode} @@ -189,7 +211,9 @@ def set_operation_mode(hass, hvac_mode, entity_id=ENTITY_MATCH_ALL): hass.services.call(DOMAIN, SERVICE_SET_HVAC_MODE, data) -async def async_set_swing_mode(hass, swing_mode, entity_id=ENTITY_MATCH_ALL): +async def async_set_swing_mode( + hass: HomeAssistant, swing_mode: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new target swing mode.""" data = {ATTR_SWING_MODE: swing_mode} @@ -200,7 +224,9 @@ async def async_set_swing_mode(hass, swing_mode, entity_id=ENTITY_MATCH_ALL): @bind_hass -def set_swing_mode(hass, swing_mode, entity_id=ENTITY_MATCH_ALL): +def set_swing_mode( + hass: HomeAssistant, swing_mode: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set new target swing mode.""" data = {ATTR_SWING_MODE: swing_mode} @@ -210,7 +236,7 @@ def set_swing_mode(hass, swing_mode, entity_id=ENTITY_MATCH_ALL): hass.services.call(DOMAIN, SERVICE_SET_SWING_MODE, data) -async def async_turn_on(hass, entity_id=ENTITY_MATCH_ALL): +async def async_turn_on(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Turn on device.""" data = {} @@ -220,7 +246,9 @@ async def async_turn_on(hass, entity_id=ENTITY_MATCH_ALL): await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True) -async def async_turn_off(hass, entity_id=ENTITY_MATCH_ALL): +async def async_turn_off( + hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Turn off device.""" data = {} diff --git a/tests/components/cloud/test_client.py b/tests/components/cloud/test_client.py index 005efd990fb..7af163cc49d 100644 --- a/tests/components/cloud/test_client.py +++ b/tests/components/cloud/test_client.py @@ -208,7 +208,9 @@ async def test_webhook_msg( received = [] - async def handler(hass, webhook_id, request): + async def handler( + hass: HomeAssistant, webhook_id: str, request: web.Request + ) -> web.Response: """Handle a webhook.""" received.append(request) return web.json_response({"from": "handler"}) diff --git a/tests/components/coinbase/common.py b/tests/components/coinbase/common.py index 1a141c88bc3..0a2475ac218 100644 --- a/tests/components/coinbase/common.py +++ b/tests/components/coinbase/common.py @@ -6,6 +6,7 @@ from homeassistant.components.coinbase.const import ( DOMAIN, ) from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_API_VERSION +from homeassistant.core import HomeAssistant from .const import ( GOOD_CURRENCY_2, @@ -115,7 +116,11 @@ def mock_get_portfolios(): } -async def init_mock_coinbase(hass, currencies=None, rates=None): +async def init_mock_coinbase( + hass: HomeAssistant, + currencies: list[str] | None = None, + rates: list[str] | None = None, +) -> MockConfigEntry: """Init Coinbase integration for testing.""" config_entry = MockConfigEntry( domain=DOMAIN, @@ -136,7 +141,11 @@ async def init_mock_coinbase(hass, currencies=None, rates=None): return config_entry -async def init_mock_coinbase_v3(hass, currencies=None, rates=None): +async def init_mock_coinbase_v3( + hass: HomeAssistant, + currencies: list[str] | None = None, + rates: list[str] | None = None, +) -> MockConfigEntry: """Init Coinbase integration for testing.""" config_entry = MockConfigEntry( domain=DOMAIN, diff --git a/tests/components/config/test_device_registry.py b/tests/components/config/test_device_registry.py index aab898f5fd6..c840ce2bed2 100644 --- a/tests/components/config/test_device_registry.py +++ b/tests/components/config/test_device_registry.py @@ -7,6 +7,7 @@ import pytest from pytest_unordered import unordered from homeassistant.components.config import device_registry +from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component @@ -274,7 +275,9 @@ async def test_remove_config_entry_from_device( can_remove = False - async def async_remove_config_entry_device(hass, config_entry, device_entry): + async def async_remove_config_entry_device( + hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry + ) -> bool: return can_remove mock_integration( @@ -356,7 +359,9 @@ async def test_remove_config_entry_from_device_fails( assert await async_setup_component(hass, "config", {}) ws_client = await hass_ws_client(hass) - async def async_remove_config_entry_device(hass, config_entry, device_entry): + async def async_remove_config_entry_device( + hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry + ) -> bool: return True mock_integration( @@ -473,7 +478,9 @@ async def test_remove_config_entry_from_device_if_integration_remove( can_remove = False - async def async_remove_config_entry_device(hass, config_entry, device_entry): + async def async_remove_config_entry_device( + hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry + ) -> bool: if can_remove: device_registry.async_update_device( device_entry.id, remove_config_entry_id=config_entry.entry_id diff --git a/tests/components/counter/common.py b/tests/components/counter/common.py index b5156c1a432..e5d9316cd22 100644 --- a/tests/components/counter/common.py +++ b/tests/components/counter/common.py @@ -11,13 +11,13 @@ from homeassistant.components.counter import ( SERVICE_RESET, ) from homeassistant.const import ATTR_ENTITY_ID -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from homeassistant.loader import bind_hass @callback @bind_hass -def async_increment(hass, entity_id): +def async_increment(hass: HomeAssistant, entity_id: str) -> None: """Increment a counter.""" hass.async_create_task( hass.services.async_call(DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id}) @@ -26,7 +26,7 @@ def async_increment(hass, entity_id): @callback @bind_hass -def async_decrement(hass, entity_id): +def async_decrement(hass: HomeAssistant, entity_id: str) -> None: """Decrement a counter.""" hass.async_create_task( hass.services.async_call(DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id}) @@ -35,7 +35,7 @@ def async_decrement(hass, entity_id): @callback @bind_hass -def async_reset(hass, entity_id): +def async_reset(hass: HomeAssistant, entity_id: str) -> None: """Reset a counter.""" hass.async_create_task( hass.services.async_call(DOMAIN, SERVICE_RESET, {ATTR_ENTITY_ID: entity_id}) diff --git a/tests/components/cover/test_init.py b/tests/components/cover/test_init.py index 37740260c2f..d1d84ffad6c 100644 --- a/tests/components/cover/test_init.py +++ b/tests/components/cover/test_init.py @@ -14,7 +14,8 @@ from homeassistant.const import ( STATE_OPEN, STATE_OPENING, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, ServiceResponse +from homeassistant.helpers.entity import Entity from homeassistant.setup import async_setup_component from .common import MockCover @@ -119,7 +120,7 @@ async def test_services( assert is_closing(hass, ent5) -def call_service(hass, service, ent): +def call_service(hass: HomeAssistant, service: str, ent: Entity) -> ServiceResponse: """Call any service on entity.""" return hass.services.async_call( cover.DOMAIN, service, {ATTR_ENTITY_ID: ent.entity_id}, blocking=True @@ -136,22 +137,22 @@ def set_state(ent, state) -> None: ent._values["state"] = state -def is_open(hass, ent): +def is_open(hass: HomeAssistant, ent: Entity) -> bool: """Return if the cover is closed based on the statemachine.""" return hass.states.is_state(ent.entity_id, STATE_OPEN) -def is_opening(hass, ent): +def is_opening(hass: HomeAssistant, ent: Entity) -> bool: """Return if the cover is closed based on the statemachine.""" return hass.states.is_state(ent.entity_id, STATE_OPENING) -def is_closed(hass, ent): +def is_closed(hass: HomeAssistant, ent: Entity) -> bool: """Return if the cover is closed based on the statemachine.""" return hass.states.is_state(ent.entity_id, STATE_CLOSED) -def is_closing(hass, ent): +def is_closing(hass: HomeAssistant, ent: Entity) -> bool: """Return if the cover is closed based on the statemachine.""" return hass.states.is_state(ent.entity_id, STATE_CLOSING)