Add missing hass type hint in component tests (c) (#124067)

This commit is contained in:
epenet 2024-08-18 15:35:31 +02:00 committed by GitHub
parent c8797298ea
commit 975363b660
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 111 additions and 47 deletions

View file

@ -3,7 +3,9 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import Callable
import json import json
from typing import Any
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch
from uuid import UUID from uuid import UUID
@ -112,7 +114,9 @@ def get_fake_zconf(host="192.168.178.42", port=8009):
return zconf 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.""" """Set up the cast platform."""
if config is None: if config is None:
config = {} config = {}
@ -128,7 +132,20 @@ async def async_setup_cast(hass, config=None):
return add_entities 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.""" """Set up the cast platform and the discovery."""
browser = MagicMock(devices={}, zc={}) browser = MagicMock(devices={}, zc={})

View file

@ -46,7 +46,7 @@ def mock_clicksend_tts_notify():
yield ns yield ns
async def setup_notify(hass): async def setup_notify(hass: HomeAssistant) -> None:
"""Test setup.""" """Test setup."""
with assert_setup_component(1, notify.DOMAIN) as config: with assert_setup_component(1, notify.DOMAIN) as config:
assert await async_setup_component(hass, notify.DOMAIN, CONFIG) assert await async_setup_component(hass, notify.DOMAIN, CONFIG)

View file

@ -23,6 +23,7 @@ from homeassistant.components.climate import (
SERVICE_SET_SWING_MODE, SERVICE_SET_SWING_MODE,
SERVICE_SET_TEMPERATURE, SERVICE_SET_TEMPERATURE,
) )
from homeassistant.components.climate.const import HVACMode
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
@ -30,10 +31,13 @@ from homeassistant.const import (
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
) )
from homeassistant.core import HomeAssistant
from homeassistant.loader import bind_hass 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.""" """Set new preset mode."""
data = {ATTR_PRESET_MODE: 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 @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.""" """Set new preset mode."""
data = {ATTR_PRESET_MODE: 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) 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.""" """Turn all or specified climate devices auxiliary heater on."""
data = {ATTR_AUX_HEAT: aux_heat} 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 @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.""" """Turn all or specified climate devices auxiliary heater on."""
data = {ATTR_AUX_HEAT: aux_heat} 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( async def async_set_temperature(
hass, hass: HomeAssistant,
temperature=None, temperature: float | None = None,
entity_id=ENTITY_MATCH_ALL, entity_id: str = ENTITY_MATCH_ALL,
target_temp_high=None, target_temp_high: float | None = None,
target_temp_low=None, target_temp_low: float | None = None,
hvac_mode=None, hvac_mode: HVACMode | None = None,
): ) -> None:
"""Set new target temperature.""" """Set new target temperature."""
kwargs = { kwargs = {
key: value key: value
@ -103,13 +113,13 @@ async def async_set_temperature(
@bind_hass @bind_hass
def set_temperature( def set_temperature(
hass, hass: HomeAssistant,
temperature=None, temperature: float | None = None,
entity_id=ENTITY_MATCH_ALL, entity_id: str = ENTITY_MATCH_ALL,
target_temp_high=None, target_temp_high: float | None = None,
target_temp_low=None, target_temp_low: float | None = None,
hvac_mode=None, hvac_mode: HVACMode | None = None,
): ) -> None:
"""Set new target temperature.""" """Set new target temperature."""
kwargs = { kwargs = {
key: value key: value
@ -126,7 +136,9 @@ def set_temperature(
hass.services.call(DOMAIN, SERVICE_SET_TEMPERATURE, kwargs) 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.""" """Set new target humidity."""
data = {ATTR_HUMIDITY: humidity} data = {ATTR_HUMIDITY: humidity}
@ -137,7 +149,9 @@ async def async_set_humidity(hass, humidity, entity_id=ENTITY_MATCH_ALL):
@bind_hass @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.""" """Set new target humidity."""
data = {ATTR_HUMIDITY: 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) 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.""" """Set all or specified climate devices fan mode on."""
data = {ATTR_FAN_MODE: fan} data = {ATTR_FAN_MODE: fan}
@ -158,7 +174,9 @@ async def async_set_fan_mode(hass, fan, entity_id=ENTITY_MATCH_ALL):
@bind_hass @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.""" """Set all or specified climate devices fan mode on."""
data = {ATTR_FAN_MODE: fan} 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) 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.""" """Set new target operation mode."""
data = {ATTR_HVAC_MODE: hvac_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 @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.""" """Set new target operation mode."""
data = {ATTR_HVAC_MODE: hvac_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) 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.""" """Set new target swing mode."""
data = {ATTR_SWING_MODE: 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 @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.""" """Set new target swing mode."""
data = {ATTR_SWING_MODE: 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) 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.""" """Turn on device."""
data = {} 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) 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.""" """Turn off device."""
data = {} data = {}

View file

@ -208,7 +208,9 @@ async def test_webhook_msg(
received = [] received = []
async def handler(hass, webhook_id, request): async def handler(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> web.Response:
"""Handle a webhook.""" """Handle a webhook."""
received.append(request) received.append(request)
return web.json_response({"from": "handler"}) return web.json_response({"from": "handler"})

View file

@ -6,6 +6,7 @@ from homeassistant.components.coinbase.const import (
DOMAIN, DOMAIN,
) )
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_API_VERSION from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_API_VERSION
from homeassistant.core import HomeAssistant
from .const import ( from .const import (
GOOD_CURRENCY_2, 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.""" """Init Coinbase integration for testing."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -136,7 +141,11 @@ async def init_mock_coinbase(hass, currencies=None, rates=None):
return config_entry 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.""" """Init Coinbase integration for testing."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,

View file

@ -7,6 +7,7 @@ import pytest
from pytest_unordered import unordered from pytest_unordered import unordered
from homeassistant.components.config import device_registry from homeassistant.components.config import device_registry
from homeassistant.config_entries import ConfigEntry
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
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -274,7 +275,9 @@ async def test_remove_config_entry_from_device(
can_remove = False 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 return can_remove
mock_integration( mock_integration(
@ -356,7 +359,9 @@ async def test_remove_config_entry_from_device_fails(
assert await async_setup_component(hass, "config", {}) assert await async_setup_component(hass, "config", {})
ws_client = await hass_ws_client(hass) 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 return True
mock_integration( mock_integration(
@ -473,7 +478,9 @@ async def test_remove_config_entry_from_device_if_integration_remove(
can_remove = False 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: if can_remove:
device_registry.async_update_device( device_registry.async_update_device(
device_entry.id, remove_config_entry_id=config_entry.entry_id device_entry.id, remove_config_entry_id=config_entry.entry_id

View file

@ -11,13 +11,13 @@ from homeassistant.components.counter import (
SERVICE_RESET, SERVICE_RESET,
) )
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import callback from homeassistant.core import HomeAssistant, callback
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
@callback @callback
@bind_hass @bind_hass
def async_increment(hass, entity_id): def async_increment(hass: HomeAssistant, entity_id: str) -> None:
"""Increment a counter.""" """Increment a counter."""
hass.async_create_task( hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id}) hass.services.async_call(DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id})
@ -26,7 +26,7 @@ def async_increment(hass, entity_id):
@callback @callback
@bind_hass @bind_hass
def async_decrement(hass, entity_id): def async_decrement(hass: HomeAssistant, entity_id: str) -> None:
"""Decrement a counter.""" """Decrement a counter."""
hass.async_create_task( hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id}) hass.services.async_call(DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id})
@ -35,7 +35,7 @@ def async_decrement(hass, entity_id):
@callback @callback
@bind_hass @bind_hass
def async_reset(hass, entity_id): def async_reset(hass: HomeAssistant, entity_id: str) -> None:
"""Reset a counter.""" """Reset a counter."""
hass.async_create_task( hass.async_create_task(
hass.services.async_call(DOMAIN, SERVICE_RESET, {ATTR_ENTITY_ID: entity_id}) hass.services.async_call(DOMAIN, SERVICE_RESET, {ATTR_ENTITY_ID: entity_id})

View file

@ -14,7 +14,8 @@ from homeassistant.const import (
STATE_OPEN, STATE_OPEN,
STATE_OPENING, 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 homeassistant.setup import async_setup_component
from .common import MockCover from .common import MockCover
@ -119,7 +120,7 @@ async def test_services(
assert is_closing(hass, ent5) 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.""" """Call any service on entity."""
return hass.services.async_call( return hass.services.async_call(
cover.DOMAIN, service, {ATTR_ENTITY_ID: ent.entity_id}, blocking=True 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 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 if the cover is closed based on the statemachine."""
return hass.states.is_state(ent.entity_id, STATE_OPEN) 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 if the cover is closed based on the statemachine."""
return hass.states.is_state(ent.entity_id, STATE_OPENING) 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 if the cover is closed based on the statemachine."""
return hass.states.is_state(ent.entity_id, STATE_CLOSED) 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 if the cover is closed based on the statemachine."""
return hass.states.is_state(ent.entity_id, STATE_CLOSING) return hass.states.is_state(ent.entity_id, STATE_CLOSING)