Use TargetKey and the action enums in vacuum (#194142)

This commit is contained in:
epenet
2026-11-24 18:44:59 +00:00
committed by GitHub
parent 4849849062
commit 7cb9ff85e5
3 changed files with 44 additions and 46 deletions
@@ -1,14 +1,16 @@
"""Provides device automations for Vacuum."""
from typing import Any
import probatio
from homeassistant.components.device_automation import async_validate_entity_schema
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DEVICE_ID,
CONF_DOMAIN,
CONF_ENTITY_ID,
CONF_TYPE,
TargetKey,
)
from homeassistant.core import Context, HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_registry as er
@@ -65,7 +67,7 @@ async def async_call_action_from_config(
context: Context | None,
) -> None:
"""Execute a device action."""
service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]}
service_data: dict[str, Any] = {TargetKey.ENTITY_ID: config[CONF_ENTITY_ID]}
if config[CONF_TYPE] == "clean":
service = VacuumService.START
@@ -5,11 +5,11 @@ from collections.abc import Iterable
import logging
from typing import Any
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import TargetKey
from homeassistant.core import Context, HomeAssistant, State
from . import DOMAIN, VacuumActivity
from .const import VacuumEntityStateAttribute, VacuumService
from .const import VacuumEntityStateAttribute, VacuumService, VacuumServiceArgument
_LOGGER = logging.getLogger(__name__)
@@ -46,7 +46,7 @@ async def _async_reproduce_state(
) == state.attributes.get(VacuumEntityStateAttribute.FAN_SPEED):
return
service_data = {ATTR_ENTITY_ID: state.entity_id}
service_data: dict[str, Any] = {TargetKey.ENTITY_ID: state.entity_id}
if cur_state.state != state.state:
# Wrong state
@@ -67,7 +67,7 @@ async def _async_reproduce_state(
VacuumEntityStateAttribute.FAN_SPEED
) != state.attributes.get(VacuumEntityStateAttribute.FAN_SPEED):
# Wrong fan speed
service_data["fan_speed"] = state.attributes[
service_data[VacuumServiceArgument.FAN_SPEED] = state.attributes[
VacuumEntityStateAttribute.FAN_SPEED
]
await hass.services.async_call(
+36 -40
View File
@@ -6,34 +6,20 @@ components. Instead call the service directly.
from typing import Any
from homeassistant.components.vacuum import (
ATTR_FAN_SPEED,
ATTR_PARAMS,
DOMAIN,
SERVICE_CLEAN_AREA,
SERVICE_CLEAN_SPOT,
SERVICE_LOCATE,
SERVICE_PAUSE,
SERVICE_RETURN_TO_BASE,
SERVICE_SEND_COMMAND,
SERVICE_SET_FAN_SPEED,
SERVICE_START,
SERVICE_STOP,
)
from homeassistant.components.vacuum import DOMAIN, VacuumService, VacuumServiceArgument
from homeassistant.const import (
ATTR_COMMAND,
ATTR_ENTITY_ID,
ENTITY_MATCH_ALL,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
TargetKey,
)
from homeassistant.core import HomeAssistant
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
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
@@ -41,20 +27,20 @@ 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
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
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
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data, blocking=True)
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)
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, VacuumService.LOCATE, data, blocking=True)
async def async_clean_area(
@@ -65,51 +51,59 @@ async def async_clean_area(
"""Tell all or specified vacuum to perform an area clean."""
data = {"cleaning_area_id": cleaning_area_id}
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
await hass.services.async_call(DOMAIN, SERVICE_CLEAN_AREA, data, blocking=True)
data[TargetKey.ENTITY_ID] = entity_id
await hass.services.async_call(
DOMAIN, VacuumService.CLEAN_AREA, data, blocking=True
)
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)
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(
DOMAIN, VacuumService.CLEAN_SPOT, data, blocking=True
)
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)
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(
DOMAIN, VacuumService.RETURN_TO_BASE, data, blocking=True
)
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)
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, VacuumService.START, data, blocking=True)
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)
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, VacuumService.PAUSE, data, blocking=True)
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)
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else None
await hass.services.async_call(DOMAIN, VacuumService.STOP, data, blocking=True)
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)
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else {}
data[VacuumServiceArgument.FAN_SPEED] = fan_speed
await hass.services.async_call(
DOMAIN, VacuumService.SET_FAN_SPEED, data, blocking=True
)
async def async_send_command(
@@ -119,8 +113,10 @@ async def async_send_command(
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
data = {TargetKey.ENTITY_ID: entity_id} if entity_id else {}
data[VacuumServiceArgument.COMMAND] = command
if params is not None:
data[ATTR_PARAMS] = params
await hass.services.async_call(DOMAIN, SERVICE_SEND_COMMAND, data, blocking=True)
data[VacuumServiceArgument.PARAMS] = params
await hass.services.async_call(
DOMAIN, VacuumService.SEND_COMMAND, data, blocking=True
)