2019-11-07 15:28:45 +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 15:28:45 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-09-04 00:25:51 +00:00
|
|
|
from homeassistant.components.automation import (
|
|
|
|
AutomationActionType,
|
|
|
|
AutomationTriggerInfo,
|
|
|
|
)
|
2021-06-10 17:11:38 +00:00
|
|
|
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
|
2020-08-17 16:54:56 +00:00
|
|
|
from homeassistant.components.homeassistant.triggers import (
|
|
|
|
numeric_state as numeric_state_trigger,
|
|
|
|
state as state_trigger,
|
|
|
|
)
|
2019-11-07 15:28:45 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-08 16:55:01 +00:00
|
|
|
CONF_ABOVE,
|
|
|
|
CONF_BELOW,
|
2019-11-07 15:28:45 +00:00
|
|
|
CONF_DEVICE_ID,
|
2019-12-08 16:55:01 +00:00
|
|
|
CONF_DOMAIN,
|
2019-11-07 15:28:45 +00:00
|
|
|
CONF_ENTITY_ID,
|
|
|
|
CONF_FOR,
|
2019-12-08 16:55:01 +00:00
|
|
|
CONF_PLATFORM,
|
|
|
|
CONF_TYPE,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2019-11-07 15:28:45 +00:00
|
|
|
)
|
2019-12-08 16:55:01 +00:00
|
|
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
|
2019-11-07 15:28:45 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_registry
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-12-08 16:55:01 +00:00
|
|
|
|
2019-11-07 15:28:45 +00:00
|
|
|
from . import DOMAIN, const
|
|
|
|
|
|
|
|
TRIGGER_TYPES = {
|
|
|
|
"current_temperature_changed",
|
|
|
|
"current_humidity_changed",
|
|
|
|
"hvac_mode_changed",
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:11:38 +00:00
|
|
|
HVAC_MODE_TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
|
2019-11-07 15:28:45 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
|
|
|
vol.Required(CONF_TYPE): "hvac_mode_changed",
|
2020-08-17 16:54:56 +00:00
|
|
|
vol.Required(state_trigger.CONF_TO): vol.In(const.HVAC_MODES),
|
2019-11-07 15:28:45 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
CURRENT_TRIGGER_SCHEMA = vol.All(
|
2021-06-10 17:11:38 +00:00
|
|
|
DEVICE_TRIGGER_BASE_SCHEMA.extend(
|
2019-11-07 15:28:45 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
|
|
|
vol.Required(CONF_TYPE): vol.In(
|
|
|
|
["current_temperature_changed", "current_humidity_changed"]
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_BELOW): vol.Any(vol.Coerce(float)),
|
|
|
|
vol.Optional(CONF_ABOVE): vol.Any(vol.Coerce(float)),
|
|
|
|
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
cv.has_at_least_one_key(CONF_BELOW, CONF_ABOVE),
|
|
|
|
)
|
|
|
|
|
|
|
|
TRIGGER_SCHEMA = vol.Any(HVAC_MODE_TRIGGER_SCHEMA, CURRENT_TRIGGER_SCHEMA)
|
|
|
|
|
|
|
|
|
2021-08-22 18:32:50 +00:00
|
|
|
async def async_get_triggers(
|
|
|
|
hass: HomeAssistant, device_id: str
|
2022-05-23 14:03:21 +00:00
|
|
|
) -> list[dict[str, str]]:
|
2019-11-07 15:28:45 +00:00
|
|
|
"""List device triggers for Climate devices."""
|
2022-05-17 11:40:19 +00:00
|
|
|
registry = entity_registry.async_get(hass)
|
2019-11-07 15:28:45 +00:00
|
|
|
triggers = []
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
state = hass.states.get(entry.entity_id)
|
|
|
|
|
|
|
|
# Add triggers for each entity that belongs to this integration
|
2021-03-30 11:54:27 +00:00
|
|
|
base_trigger = {
|
|
|
|
CONF_PLATFORM: "device",
|
|
|
|
CONF_DEVICE_ID: device_id,
|
|
|
|
CONF_DOMAIN: DOMAIN,
|
|
|
|
CONF_ENTITY_ID: entry.entity_id,
|
|
|
|
}
|
|
|
|
|
2019-11-07 15:28:45 +00:00
|
|
|
triggers.append(
|
|
|
|
{
|
2021-03-30 11:54:27 +00:00
|
|
|
**base_trigger,
|
2019-11-07 15:28:45 +00:00
|
|
|
CONF_TYPE: "hvac_mode_changed",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if state and const.ATTR_CURRENT_TEMPERATURE in state.attributes:
|
|
|
|
triggers.append(
|
|
|
|
{
|
2021-03-30 11:54:27 +00:00
|
|
|
**base_trigger,
|
2019-11-07 15:28:45 +00:00
|
|
|
CONF_TYPE: "current_temperature_changed",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if state and const.ATTR_CURRENT_HUMIDITY in state.attributes:
|
|
|
|
triggers.append(
|
|
|
|
{
|
2021-03-30 11:54:27 +00:00
|
|
|
**base_trigger,
|
2019-11-07 15:28:45 +00:00
|
|
|
CONF_TYPE: "current_humidity_changed",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return triggers
|
|
|
|
|
|
|
|
|
|
|
|
async def async_attach_trigger(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
action: AutomationActionType,
|
2021-09-04 00:25:51 +00:00
|
|
|
automation_info: AutomationTriggerInfo,
|
2019-11-07 15:28:45 +00:00
|
|
|
) -> CALLBACK_TYPE:
|
|
|
|
"""Attach a trigger."""
|
2021-10-17 18:19:56 +00:00
|
|
|
if (trigger_type := config[CONF_TYPE]) == "hvac_mode_changed":
|
2019-11-07 15:28:45 +00:00
|
|
|
state_config = {
|
2020-08-17 16:54:56 +00:00
|
|
|
state_trigger.CONF_PLATFORM: "state",
|
|
|
|
state_trigger.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
|
|
|
state_trigger.CONF_TO: config[state_trigger.CONF_TO],
|
|
|
|
state_trigger.CONF_FROM: [
|
2019-11-07 15:28:45 +00:00
|
|
|
mode
|
|
|
|
for mode in const.HVAC_MODES
|
2020-08-17 16:54:56 +00:00
|
|
|
if mode != config[state_trigger.CONF_TO]
|
2019-11-07 15:28:45 +00:00
|
|
|
],
|
|
|
|
}
|
|
|
|
if CONF_FOR in config:
|
|
|
|
state_config[CONF_FOR] = config[CONF_FOR]
|
2021-12-02 13:26:45 +00:00
|
|
|
state_config = await state_trigger.async_validate_trigger_config(
|
|
|
|
hass, state_config
|
|
|
|
)
|
2020-08-17 16:54:56 +00:00
|
|
|
return await state_trigger.async_attach_trigger(
|
2019-11-07 15:28:45 +00:00
|
|
|
hass, state_config, action, automation_info, platform_type="device"
|
|
|
|
)
|
|
|
|
|
|
|
|
numeric_state_config = {
|
2020-08-17 16:54:56 +00:00
|
|
|
numeric_state_trigger.CONF_PLATFORM: "numeric_state",
|
|
|
|
numeric_state_trigger.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
2019-11-07 15:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if trigger_type == "current_temperature_changed":
|
|
|
|
numeric_state_config[
|
2020-08-17 16:54:56 +00:00
|
|
|
numeric_state_trigger.CONF_VALUE_TEMPLATE
|
2019-11-07 15:28:45 +00:00
|
|
|
] = "{{ state.attributes.current_temperature }}"
|
|
|
|
else:
|
|
|
|
numeric_state_config[
|
2020-08-17 16:54:56 +00:00
|
|
|
numeric_state_trigger.CONF_VALUE_TEMPLATE
|
2019-11-07 15:28:45 +00:00
|
|
|
] = "{{ state.attributes.current_humidity }}"
|
|
|
|
|
|
|
|
if CONF_ABOVE in config:
|
|
|
|
numeric_state_config[CONF_ABOVE] = config[CONF_ABOVE]
|
|
|
|
if CONF_BELOW in config:
|
|
|
|
numeric_state_config[CONF_BELOW] = config[CONF_BELOW]
|
|
|
|
if CONF_FOR in config:
|
|
|
|
numeric_state_config[CONF_FOR] = config[CONF_FOR]
|
|
|
|
|
2021-12-02 17:45:40 +00:00
|
|
|
numeric_state_config = await numeric_state_trigger.async_validate_trigger_config(
|
|
|
|
hass, numeric_state_config
|
|
|
|
)
|
2020-08-17 16:54:56 +00:00
|
|
|
return await numeric_state_trigger.async_attach_trigger(
|
2019-11-07 15:28:45 +00:00
|
|
|
hass, numeric_state_config, action, automation_info, platform_type="device"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-22 18:32:50 +00:00
|
|
|
async def async_get_trigger_capabilities(
|
|
|
|
hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> dict[str, vol.Schema]:
|
2019-11-07 15:28:45 +00:00
|
|
|
"""List trigger capabilities."""
|
|
|
|
trigger_type = config[CONF_TYPE]
|
|
|
|
|
|
|
|
if trigger_type == "hvac_action_changed":
|
2021-08-22 18:32:50 +00:00
|
|
|
return {}
|
2019-11-07 15:28:45 +00:00
|
|
|
|
|
|
|
if trigger_type == "hvac_mode_changed":
|
|
|
|
return {
|
|
|
|
"extra_fields": vol.Schema(
|
|
|
|
{vol.Optional(CONF_FOR): cv.positive_time_period_dict}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if trigger_type == "current_temperature_changed":
|
|
|
|
unit_of_measurement = hass.config.units.temperature_unit
|
|
|
|
else:
|
2020-09-05 19:09:14 +00:00
|
|
|
unit_of_measurement = PERCENTAGE
|
2019-11-07 15:28:45 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
"extra_fields": vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_ABOVE, description={"suffix": unit_of_measurement}
|
|
|
|
): vol.Coerce(float),
|
|
|
|
vol.Optional(
|
|
|
|
CONF_BELOW, description={"suffix": unit_of_measurement}
|
|
|
|
): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|