2019-11-07 12:44:59 +00:00
|
|
|
"""Provides device automations for Climate."""
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
2019-12-08 16:55:01 +00:00
|
|
|
|
2019-11-07 12:44:59 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_DEVICE_ID,
|
2019-12-08 16:55:01 +00:00
|
|
|
CONF_DOMAIN,
|
2019-11-07 12:44:59 +00:00
|
|
|
CONF_ENTITY_ID,
|
2019-12-08 16:55:01 +00:00
|
|
|
CONF_TYPE,
|
2019-11-07 12:44:59 +00:00
|
|
|
)
|
2022-01-04 18:51:19 +00:00
|
|
|
from homeassistant.core import Context, HomeAssistant
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2019-11-07 12:44:59 +00:00
|
|
|
from homeassistant.helpers import entity_registry
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-06-11 09:13:55 +00:00
|
|
|
from homeassistant.helpers.entity import get_capability, get_supported_features
|
2022-05-23 14:03:21 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
2019-12-08 16:55:01 +00:00
|
|
|
|
2019-11-07 12:44:59 +00:00
|
|
|
from . import DOMAIN, const
|
|
|
|
|
|
|
|
ACTION_TYPES = {"set_hvac_mode", "set_preset_mode"}
|
|
|
|
|
|
|
|
SET_HVAC_MODE_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_TYPE): "set_hvac_mode",
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN),
|
|
|
|
vol.Required(const.ATTR_HVAC_MODE): vol.In(const.HVAC_MODES),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
SET_PRESET_MODE_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_TYPE): "set_preset_mode",
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN),
|
|
|
|
vol.Required(const.ATTR_PRESET_MODE): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
ACTION_SCHEMA = vol.Any(SET_HVAC_MODE_SCHEMA, SET_PRESET_MODE_SCHEMA)
|
|
|
|
|
|
|
|
|
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 12:44:59 +00:00
|
|
|
"""List device actions for Climate devices."""
|
2022-05-17 11:40:19 +00:00
|
|
|
registry = entity_registry.async_get(hass)
|
2019-11-07 12:44:59 +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-06-11 09:13:55 +00:00
|
|
|
supported_features = get_supported_features(hass, entry.entity_id)
|
2019-11-07 12:44:59 +00:00
|
|
|
|
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: "set_hvac_mode"})
|
2021-06-11 09:13:55 +00:00
|
|
|
if supported_features & const.SUPPORT_PRESET_MODE:
|
2021-05-31 07:47:15 +00:00
|
|
|
actions.append({**base_action, CONF_TYPE: "set_preset_mode"})
|
2019-11-07 12:44:59 +00:00
|
|
|
|
|
|
|
return actions
|
|
|
|
|
|
|
|
|
|
|
|
async def async_call_action_from_config(
|
2022-05-23 14:03:21 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
variables: TemplateVarsType,
|
|
|
|
context: Context | None,
|
2019-11-07 12:44:59 +00:00
|
|
|
) -> None:
|
|
|
|
"""Execute a device action."""
|
|
|
|
service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]}
|
|
|
|
|
|
|
|
if config[CONF_TYPE] == "set_hvac_mode":
|
|
|
|
service = const.SERVICE_SET_HVAC_MODE
|
|
|
|
service_data[const.ATTR_HVAC_MODE] = config[const.ATTR_HVAC_MODE]
|
|
|
|
elif config[CONF_TYPE] == "set_preset_mode":
|
|
|
|
service = const.SERVICE_SET_PRESET_MODE
|
|
|
|
service_data[const.ATTR_PRESET_MODE] = config[const.ATTR_PRESET_MODE]
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, service, service_data, blocking=True, context=context
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-05-23 14:03:21 +00:00
|
|
|
async def async_get_action_capabilities(
|
|
|
|
hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> dict[str, vol.Schema]:
|
2019-11-07 12:44:59 +00:00
|
|
|
"""List action capabilities."""
|
|
|
|
action_type = config[CONF_TYPE]
|
|
|
|
|
|
|
|
fields = {}
|
|
|
|
|
|
|
|
if action_type == "set_hvac_mode":
|
2021-06-11 09:13:55 +00:00
|
|
|
try:
|
|
|
|
hvac_modes = (
|
|
|
|
get_capability(hass, config[ATTR_ENTITY_ID], const.ATTR_HVAC_MODES)
|
|
|
|
or []
|
|
|
|
)
|
|
|
|
except HomeAssistantError:
|
|
|
|
hvac_modes = []
|
2019-11-07 12:44:59 +00:00
|
|
|
fields[vol.Required(const.ATTR_HVAC_MODE)] = vol.In(hvac_modes)
|
|
|
|
elif action_type == "set_preset_mode":
|
2021-06-11 09:13:55 +00:00
|
|
|
try:
|
|
|
|
preset_modes = (
|
|
|
|
get_capability(hass, config[ATTR_ENTITY_ID], const.ATTR_PRESET_MODES)
|
|
|
|
or []
|
|
|
|
)
|
|
|
|
except HomeAssistantError:
|
2019-11-07 12:44:59 +00:00
|
|
|
preset_modes = []
|
|
|
|
fields[vol.Required(const.ATTR_PRESET_MODE)] = vol.In(preset_modes)
|
|
|
|
|
|
|
|
return {"extra_fields": vol.Schema(fields)}
|