Add missing hass type hint in component tests (v) (#124281)

This commit is contained in:
epenet 2024-08-20 12:54:15 +02:00 committed by GitHub
parent d901cb04b8
commit e6eedc0717
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 31 deletions

View File

@ -4,6 +4,8 @@ All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly. components. Instead call the service directly.
""" """
from typing import Any
from homeassistant.components.vacuum import ( from homeassistant.components.vacuum import (
ATTR_FAN_SPEED, ATTR_FAN_SPEED,
ATTR_PARAMS, ATTR_PARAMS,
@ -26,136 +28,149 @@ 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
@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.""" """Turn all or specified vacuum on."""
hass.add_job(async_turn_on, hass, entity_id) 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.""" """Turn all or specified vacuum on."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
@bind_hass @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.""" """Turn all or specified vacuum off."""
hass.add_job(async_turn_off, hass, entity_id) 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.""" """Turn all or specified vacuum off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
@bind_hass @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.""" """Toggle all or specified vacuum."""
hass.add_job(async_toggle, hass, entity_id) 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.""" """Toggle all or specified vacuum."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data, blocking=True)
@bind_hass @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.""" """Locate all or specified vacuum."""
hass.add_job(async_locate, hass, entity_id) 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.""" """Locate all or specified vacuum."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_LOCATE, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_LOCATE, data, blocking=True)
@bind_hass @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.""" """Tell all or specified vacuum to perform a spot clean-up."""
hass.add_job(async_clean_spot, hass, entity_id) 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.""" """Tell all or specified vacuum to perform a spot clean-up."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_CLEAN_SPOT, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_CLEAN_SPOT, data, blocking=True)
@bind_hass @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.""" """Tell all or specified vacuum to return to base."""
hass.add_job(async_return_to_base, hass, entity_id) 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.""" """Tell all or specified vacuum to return to base."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_RETURN_TO_BASE, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_RETURN_TO_BASE, data, blocking=True)
@bind_hass @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.""" """Tell all or specified vacuum to start or pause the current task."""
hass.add_job(async_start_pause, hass, entity_id) 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.""" """Tell all or specified vacuum to start or pause the current task."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_START_PAUSE, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_START_PAUSE, data, blocking=True)
@bind_hass @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.""" """Tell all or specified vacuum to start or resume the current task."""
hass.add_job(async_start, hass, entity_id) 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.""" """Tell all or specified vacuum to start or resume the current task."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_START, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_START, data, blocking=True)
@bind_hass @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.""" """Tell all or the specified vacuum to pause the current task."""
hass.add_job(async_pause, hass, entity_id) 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.""" """Tell all or the specified vacuum to pause the current task."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_PAUSE, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_PAUSE, data, blocking=True)
@bind_hass @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.""" """Stop all or specified vacuum."""
hass.add_job(async_stop, hass, entity_id) 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.""" """Stop all or specified vacuum."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_STOP, data, blocking=True) await hass.services.async_call(DOMAIN, SERVICE_STOP, data, blocking=True)
@bind_hass @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.""" """Set fan speed for all or specified vacuum."""
hass.add_job(async_set_fan_speed, hass, fan_speed, entity_id) 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.""" """Set fan speed for all or specified vacuum."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
data[ATTR_FAN_SPEED] = fan_speed 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 @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.""" """Send command to all or specified vacuum."""
hass.add_job(async_send_command, hass, command, params, entity_id) 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.""" """Send command to all or specified vacuum."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
data[ATTR_COMMAND] = command data[ATTR_COMMAND] = command

View File

@ -332,7 +332,9 @@ async def test_supported_features(hass: HomeAssistant) -> None:
assert valve.supported_features is 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.""" """Call any service on entity."""
params = {ATTR_ENTITY_ID: ent.entity_id} params = {ATTR_ENTITY_ID: ent.entity_id}
if position is not None: if position is not None:
@ -345,21 +347,21 @@ def set_valve_position(ent, position) -> None:
ent._values["current_valve_position"] = position 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 if the valve 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: ValveEntity) -> bool:
"""Return if the valve is closed based on the statemachine.""" """Return if the valve 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: ValveEntity) -> bool:
"""Return if the valve is closed based on the statemachine.""" """Return if the valve 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: ValveEntity) -> bool:
"""Return if the valve is closed based on the statemachine.""" """Return if the valve 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)

View File

@ -15,7 +15,7 @@ TEST_MODELS = ["t2k", "colortouch"]
def mock_venstar_devices(f): def mock_venstar_devices(f):
"""Decorate function to mock a Venstar Colortouch and T2000 thermostat API.""" """Decorate function to mock a Venstar Colortouch and T2000 thermostat API."""
async def wrapper(hass): async def wrapper(hass: HomeAssistant) -> None:
# Mock thermostats are: # Mock thermostats are:
# Venstar T2000, FW 4.38 # Venstar T2000, FW 4.38
# Venstar "colortouch" T7850, FW 5.1 # Venstar "colortouch" T7850, FW 5.1
@ -37,7 +37,7 @@ def mock_venstar_devices(f):
f"http://venstar-{model}.localdomain/query/alerts", f"http://venstar-{model}.localdomain/query/alerts",
text=load_fixture(f"venstar/{model}_alerts.json"), text=load_fixture(f"venstar/{model}_alerts.json"),
) )
return await f(hass) await f(hass)
return wrapper return wrapper