2019-11-07 11:24:58 +00:00
|
|
|
"""Provides device automations for Vacuum."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
2019-12-08 17:17:01 +00:00
|
|
|
|
2019-11-07 11:24:58 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_DEVICE_ID,
|
2019-12-08 17:17:01 +00:00
|
|
|
CONF_DOMAIN,
|
2019-11-07 11:24:58 +00:00
|
|
|
CONF_ENTITY_ID,
|
2019-12-08 17:17:01 +00:00
|
|
|
CONF_TYPE,
|
2019-11-07 11:24:58 +00:00
|
|
|
)
|
2019-12-08 17:17:01 +00:00
|
|
|
from homeassistant.core import Context, HomeAssistant
|
2019-11-07 11:24:58 +00:00
|
|
|
from homeassistant.helpers import entity_registry
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-05-23 14:01:40 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
2019-12-08 17:17:01 +00:00
|
|
|
|
|
|
|
from . import DOMAIN, SERVICE_RETURN_TO_BASE, SERVICE_START
|
2019-11-07 11:24:58 +00:00
|
|
|
|
|
|
|
ACTION_TYPES = {"clean", "dock"}
|
|
|
|
|
|
|
|
ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_TYPE): vol.In(ACTION_TYPES),
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-20 04:13:25 +00:00
|
|
|
async def async_get_actions(
|
|
|
|
hass: HomeAssistant, device_id: str
|
|
|
|
) -> list[dict[str, str]]:
|
2019-11-07 11:24:58 +00:00
|
|
|
"""List device actions for Vacuum devices."""
|
2022-05-17 11:40:19 +00:00
|
|
|
registry = entity_registry.async_get(hass)
|
2019-11-07 11:24:58 +00:00
|
|
|
actions = []
|
|
|
|
|
|
|
|
# Get all the integrations entities for this device
|
|
|
|
for entry in entity_registry.async_entries_for_device(registry, device_id):
|
|
|
|
if entry.domain != DOMAIN:
|
|
|
|
continue
|
|
|
|
|
2021-05-31 07:47:15 +00:00
|
|
|
base_action = {
|
|
|
|
CONF_DEVICE_ID: device_id,
|
|
|
|
CONF_DOMAIN: DOMAIN,
|
|
|
|
CONF_ENTITY_ID: entry.entity_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
actions.append({**base_action, CONF_TYPE: "clean"})
|
|
|
|
actions.append({**base_action, CONF_TYPE: "dock"})
|
2019-11-07 11:24:58 +00:00
|
|
|
|
|
|
|
return actions
|
|
|
|
|
|
|
|
|
|
|
|
async def async_call_action_from_config(
|
2022-05-23 14:01:40 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
variables: TemplateVarsType,
|
|
|
|
context: Context | None,
|
2019-11-07 11:24:58 +00:00
|
|
|
) -> None:
|
|
|
|
"""Execute a device action."""
|
|
|
|
config = ACTION_SCHEMA(config)
|
|
|
|
|
|
|
|
service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]}
|
|
|
|
|
|
|
|
if config[CONF_TYPE] == "clean":
|
|
|
|
service = SERVICE_START
|
|
|
|
elif config[CONF_TYPE] == "dock":
|
|
|
|
service = SERVICE_RETURN_TO_BASE
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, service, service_data, blocking=True, context=context
|
|
|
|
)
|