Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 49 additions & 24 deletions tests/components/vacuum/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
components. Instead call the service directly.
"""

from typing import Any

from homeassistant.components.vacuum import (
ATTR_FAN_SPEED,
ATTR_PARAMS,
Expand All @@ -26,149 +28,172 @@
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
await hass.services.async_call(DOMAIN, SERVICE_SET_FAN_SPEED, data, blocking=True)


@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
Expand Down
12 changes: 7 additions & 5 deletions tests/components/valve/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
4 changes: 2 additions & 2 deletions tests/components/venstar/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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
Expand All @@ -37,7 +37,7 @@ async def wrapper(hass):
f"http://venstar-{model}.localdomain/query/alerts",
text=load_fixture(f"venstar/{model}_alerts.json"),
)
return await f(hass)
await f(hass)

return wrapper

Expand Down