From e6eedc071742b8135ff5e5741eacfc180707fb69 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 20 Aug 2024 12:54:15 +0200 Subject: [PATCH] Add missing hass type hint in component tests (v) (#124281) --- tests/components/vacuum/common.py | 73 +++++++++++++++++++---------- tests/components/valve/test_init.py | 12 +++-- tests/components/venstar/util.py | 4 +- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/tests/components/vacuum/common.py b/tests/components/vacuum/common.py index 0e46ebf5e44..6228c1d2f74 100644 --- a/tests/components/vacuum/common.py +++ b/tests/components/vacuum/common.py @@ -4,6 +4,8 @@ All containing methods are legacy helpers that should not be used by new components. Instead call the service directly. """ +from typing import Any + from homeassistant.components.vacuum import ( ATTR_FAN_SPEED, ATTR_PARAMS, @@ -26,136 +28,149 @@ from homeassistant.const import ( SERVICE_TURN_OFF, SERVICE_TURN_ON, ) +from homeassistant.core import HomeAssistant from homeassistant.loader import bind_hass @bind_hass -def turn_on(hass, entity_id=ENTITY_MATCH_ALL): +def turn_on(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Turn all or specified vacuum on.""" hass.add_job(async_turn_on, hass, entity_id) -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 all or specified vacuum on.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True) @bind_hass -def turn_off(hass, entity_id=ENTITY_MATCH_ALL): +def turn_off(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Turn all or specified vacuum off.""" hass.add_job(async_turn_off, hass, entity_id) -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 all or specified vacuum off.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True) @bind_hass -def toggle(hass, entity_id=ENTITY_MATCH_ALL): +def toggle(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Toggle all or specified vacuum.""" hass.add_job(async_toggle, hass, entity_id) -async def async_toggle(hass, entity_id=ENTITY_MATCH_ALL): +async def async_toggle(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Toggle all or specified vacuum.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data, blocking=True) @bind_hass -def locate(hass, entity_id=ENTITY_MATCH_ALL): +def locate(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Locate all or specified vacuum.""" hass.add_job(async_locate, hass, entity_id) -async def async_locate(hass, entity_id=ENTITY_MATCH_ALL): +async def async_locate(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Locate all or specified vacuum.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_LOCATE, data, blocking=True) @bind_hass -def clean_spot(hass, entity_id=ENTITY_MATCH_ALL): +def clean_spot(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Tell all or specified vacuum to perform a spot clean-up.""" hass.add_job(async_clean_spot, hass, entity_id) -async def async_clean_spot(hass, entity_id=ENTITY_MATCH_ALL): +async def async_clean_spot( + hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Tell all or specified vacuum to perform a spot clean-up.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_CLEAN_SPOT, data, blocking=True) @bind_hass -def return_to_base(hass, entity_id=ENTITY_MATCH_ALL): +def return_to_base(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Tell all or specified vacuum to return to base.""" hass.add_job(async_return_to_base, hass, entity_id) -async def async_return_to_base(hass, entity_id=ENTITY_MATCH_ALL): +async def async_return_to_base( + hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Tell all or specified vacuum to return to base.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_RETURN_TO_BASE, data, blocking=True) @bind_hass -def start_pause(hass, entity_id=ENTITY_MATCH_ALL): +def start_pause(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Tell all or specified vacuum to start or pause the current task.""" hass.add_job(async_start_pause, hass, entity_id) -async def async_start_pause(hass, entity_id=ENTITY_MATCH_ALL): +async def async_start_pause( + hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Tell all or specified vacuum to start or pause the current task.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_START_PAUSE, data, blocking=True) @bind_hass -def start(hass, entity_id=ENTITY_MATCH_ALL): +def start(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Tell all or specified vacuum to start or resume the current task.""" hass.add_job(async_start, hass, entity_id) -async def async_start(hass, entity_id=ENTITY_MATCH_ALL): +async def async_start(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Tell all or specified vacuum to start or resume the current task.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_START, data, blocking=True) @bind_hass -def pause(hass, entity_id=ENTITY_MATCH_ALL): +def pause(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Tell all or the specified vacuum to pause the current task.""" hass.add_job(async_pause, hass, entity_id) -async def async_pause(hass, entity_id=ENTITY_MATCH_ALL): +async def async_pause(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Tell all or the specified vacuum to pause the current task.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_PAUSE, data, blocking=True) @bind_hass -def stop(hass, entity_id=ENTITY_MATCH_ALL): +def stop(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Stop all or specified vacuum.""" hass.add_job(async_stop, hass, entity_id) -async def async_stop(hass, entity_id=ENTITY_MATCH_ALL): +async def async_stop(hass: HomeAssistant, entity_id: str = ENTITY_MATCH_ALL) -> None: """Stop all or specified vacuum.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else None await hass.services.async_call(DOMAIN, SERVICE_STOP, data, blocking=True) @bind_hass -def set_fan_speed(hass, fan_speed, entity_id=ENTITY_MATCH_ALL): +def set_fan_speed( + hass: HomeAssistant, fan_speed: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set fan speed for all or specified vacuum.""" hass.add_job(async_set_fan_speed, hass, fan_speed, entity_id) -async def async_set_fan_speed(hass, fan_speed, entity_id=ENTITY_MATCH_ALL): +async def async_set_fan_speed( + hass: HomeAssistant, fan_speed: str, entity_id: str = ENTITY_MATCH_ALL +) -> None: """Set fan speed for all or specified vacuum.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data[ATTR_FAN_SPEED] = fan_speed @@ -163,12 +178,22 @@ async def async_set_fan_speed(hass, fan_speed, entity_id=ENTITY_MATCH_ALL): @bind_hass -def send_command(hass, command, params=None, entity_id=ENTITY_MATCH_ALL): +def send_command( + hass: HomeAssistant, + command: str, + params: dict[str, Any] | list[Any] | None = None, + entity_id: str = ENTITY_MATCH_ALL, +) -> None: """Send command to all or specified vacuum.""" hass.add_job(async_send_command, hass, command, params, entity_id) -async def async_send_command(hass, command, params=None, entity_id=ENTITY_MATCH_ALL): +async def async_send_command( + hass: HomeAssistant, + command: str, + params: dict[str, Any] | list[Any] | None = None, + entity_id: str = ENTITY_MATCH_ALL, +) -> None: """Send command to all or specified vacuum.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data[ATTR_COMMAND] = command diff --git a/tests/components/valve/test_init.py b/tests/components/valve/test_init.py index e4519bcef08..378ddb2a94b 100644 --- a/tests/components/valve/test_init.py +++ b/tests/components/valve/test_init.py @@ -332,7 +332,9 @@ async def test_supported_features(hass: HomeAssistant) -> None: assert valve.supported_features is None -def call_service(hass, service, ent, position=None): +def call_service( + hass: HomeAssistant, service: str, ent: ValveEntity, position: int | None = None +): """Call any service on entity.""" params = {ATTR_ENTITY_ID: ent.entity_id} if position is not None: @@ -345,21 +347,21 @@ def set_valve_position(ent, position) -> None: ent._values["current_valve_position"] = position -def is_open(hass, ent): +def is_open(hass: HomeAssistant, ent: ValveEntity) -> bool: """Return if the valve 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: ValveEntity) -> bool: """Return if the valve 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: ValveEntity) -> bool: """Return if the valve 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: ValveEntity) -> bool: """Return if the valve is closed based on the statemachine.""" return hass.states.is_state(ent.entity_id, STATE_CLOSING) diff --git a/tests/components/venstar/util.py b/tests/components/venstar/util.py index 369d3332135..f1e85e9019e 100644 --- a/tests/components/venstar/util.py +++ b/tests/components/venstar/util.py @@ -15,7 +15,7 @@ TEST_MODELS = ["t2k", "colortouch"] def mock_venstar_devices(f): """Decorate function to mock a Venstar Colortouch and T2000 thermostat API.""" - async def wrapper(hass): + async def wrapper(hass: HomeAssistant) -> None: # Mock thermostats are: # Venstar T2000, FW 4.38 # Venstar "colortouch" T7850, FW 5.1 @@ -37,7 +37,7 @@ def mock_venstar_devices(f): f"http://venstar-{model}.localdomain/query/alerts", text=load_fixture(f"venstar/{model}_alerts.json"), ) - return await f(hass) + await f(hass) return wrapper