2020-10-16 10:51:12 +00:00
|
|
|
"""Provides device automations for Water Heater."""
|
2021-03-18 14:08:35 +00:00
|
|
|
from __future__ import annotations
|
2020-10-16 10:51:12 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_DEVICE_ID,
|
|
|
|
CONF_DOMAIN,
|
|
|
|
CONF_ENTITY_ID,
|
|
|
|
CONF_TYPE,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
)
|
|
|
|
from homeassistant.core import Context, HomeAssistant
|
|
|
|
from homeassistant.helpers import entity_registry
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
from . import DOMAIN
|
|
|
|
|
|
|
|
ACTION_TYPES = {"turn_on", "turn_off"}
|
|
|
|
|
|
|
|
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-03-18 14:08:35 +00:00
|
|
|
async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
2020-10-16 10:51:12 +00:00
|
|
|
"""List device actions for Water Heater devices."""
|
|
|
|
registry = await entity_registry.async_get_registry(hass)
|
|
|
|
actions = []
|
|
|
|
|
|
|
|
for entry in entity_registry.async_entries_for_device(registry, device_id):
|
|
|
|
if entry.domain != DOMAIN:
|
|
|
|
continue
|
|
|
|
|
|
|
|
actions.append(
|
|
|
|
{
|
|
|
|
CONF_DEVICE_ID: device_id,
|
|
|
|
CONF_DOMAIN: DOMAIN,
|
|
|
|
CONF_ENTITY_ID: entry.entity_id,
|
|
|
|
CONF_TYPE: "turn_on",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
actions.append(
|
|
|
|
{
|
|
|
|
CONF_DEVICE_ID: device_id,
|
|
|
|
CONF_DOMAIN: DOMAIN,
|
|
|
|
CONF_ENTITY_ID: entry.entity_id,
|
|
|
|
CONF_TYPE: "turn_off",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return actions
|
|
|
|
|
|
|
|
|
|
|
|
async def async_call_action_from_config(
|
2021-03-18 14:08:35 +00:00
|
|
|
hass: HomeAssistant, config: dict, variables: dict, context: Context | None
|
2020-10-16 10:51:12 +00:00
|
|
|
) -> None:
|
|
|
|
"""Execute a device action."""
|
|
|
|
service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]}
|
|
|
|
|
|
|
|
if config[CONF_TYPE] == "turn_on":
|
|
|
|
service = SERVICE_TURN_ON
|
|
|
|
elif config[CONF_TYPE] == "turn_off":
|
|
|
|
service = SERVICE_TURN_OFF
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN, service, service_data, blocking=True, context=context
|
|
|
|
)
|