Add device triggers to cover (#28063)
* Add device triggers to cover * Use numeric_state trigger instead of template trigger * Tweak translations
This commit is contained in:
1 parent
bda3aadbcf
commit
8ae43d2de3
4 files changed
+935
-14
No files matched your search
@@ -4,7 +4,7 @@ import logging
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import exceptions
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import CALLBACK_TYPE, callback
|
||||
from homeassistant.const import (
|
||||
CONF_VALUE_TEMPLATE,
|
||||
CONF_PLATFORM,
|
||||
@@ -17,7 +17,8 @@ from homeassistant.helpers.event import async_track_state_change, async_track_sa
|
||||
from homeassistant.helpers import condition, config_validation as cv, template
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
|
||||
# mypy: no-check-untyped-defs
|
||||
|
||||
TRIGGER_SCHEMA = vol.All(
|
||||
vol.Schema(
|
||||
@@ -42,7 +43,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def async_attach_trigger(
|
||||
hass, config, action, automation_info, *, platform_type="numeric_state"
|
||||
):
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Listen for state changes based on configuration."""
|
||||
entity_id = config.get(CONF_ENTITY_ID)
|
||||
below = config.get(CONF_BELOW)
|
||||
@@ -52,7 +53,7 @@ async def async_attach_trigger(
|
||||
value_template = config.get(CONF_VALUE_TEMPLATE)
|
||||
unsub_track_same = {}
|
||||
entities_triggered = set()
|
||||
period = {}
|
||||
period: dict = {}
|
||||
|
||||
if value_template is not None:
|
||||
value_template.hass = hass
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
"""Provides device automations for Cover."""
|
||||
from typing import List
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_SUPPORTED_FEATURES,
|
||||
CONF_ABOVE,
|
||||
CONF_BELOW,
|
||||
CONF_DOMAIN,
|
||||
CONF_TYPE,
|
||||
CONF_PLATFORM,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_ENTITY_ID,
|
||||
STATE_CLOSED,
|
||||
STATE_CLOSING,
|
||||
STATE_OPEN,
|
||||
STATE_OPENING,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, CALLBACK_TYPE
|
||||
from homeassistant.helpers import config_validation as cv, entity_registry
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.components.automation import (
|
||||
state as state_automation,
|
||||
numeric_state as numeric_state_automation,
|
||||
AutomationActionType,
|
||||
)
|
||||
from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA
|
||||
from . import (
|
||||
DOMAIN,
|
||||
SUPPORT_CLOSE,
|
||||
SUPPORT_OPEN,
|
||||
SUPPORT_SET_POSITION,
|
||||
SUPPORT_SET_TILT_POSITION,
|
||||
)
|
||||
|
||||
POSITION_TRIGGER_TYPES = {"position", "tilt_position"}
|
||||
STATE_TRIGGER_TYPES = {"opened", "closed", "opening", "closing"}
|
||||
|
||||
POSITION_TRIGGER_SCHEMA = vol.All(
|
||||
TRIGGER_BASE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TYPE): vol.In(POSITION_TRIGGER_TYPES),
|
||||
vol.Optional(CONF_ABOVE): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=0, max=100)
|
||||
),
|
||||
vol.Optional(CONF_BELOW): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=0, max=100)
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.has_at_least_one_key(CONF_BELOW, CONF_ABOVE),
|
||||
)
|
||||
|
||||
STATE_TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TYPE): vol.In(STATE_TRIGGER_TYPES),
|
||||
}
|
||||
)
|
||||
|
||||
TRIGGER_SCHEMA = vol.Any(POSITION_TRIGGER_SCHEMA, STATE_TRIGGER_SCHEMA)
|
||||
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant, device_id: str) -> List[dict]:
|
||||
"""List device triggers for Cover devices."""
|
||||
registry = await entity_registry.async_get_registry(hass)
|
||||
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)
|
||||
if not state or ATTR_SUPPORTED_FEATURES not in state.attributes:
|
||||
continue
|
||||
|
||||
supported_features = state.attributes[ATTR_SUPPORTED_FEATURES]
|
||||
supports_open_close = supported_features & (SUPPORT_OPEN | SUPPORT_CLOSE)
|
||||
|
||||
# Add triggers for each entity that belongs to this integration
|
||||
if supports_open_close:
|
||||
triggers.append(
|
||||
{
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "opened",
|
||||
}
|
||||
)
|
||||
triggers.append(
|
||||
{
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "closed",
|
||||
}
|
||||
)
|
||||
triggers.append(
|
||||
{
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "opening",
|
||||
}
|
||||
)
|
||||
triggers.append(
|
||||
{
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "closing",
|
||||
}
|
||||
)
|
||||
if supported_features & SUPPORT_SET_POSITION:
|
||||
triggers.append(
|
||||
{
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "position",
|
||||
}
|
||||
)
|
||||
if supported_features & SUPPORT_SET_TILT_POSITION:
|
||||
triggers.append(
|
||||
{
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "tilt_position",
|
||||
}
|
||||
)
|
||||
|
||||
return triggers
|
||||
|
||||
|
||||
async def async_get_trigger_capabilities(hass: HomeAssistant, config: dict) -> dict:
|
||||
"""List trigger capabilities."""
|
||||
if config[CONF_TYPE] not in ["position", "tilt_position"]:
|
||||
return {}
|
||||
|
||||
return {
|
||||
"extra_fields": vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_ABOVE, default=0): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=0, max=100)
|
||||
),
|
||||
vol.Optional(CONF_BELOW, default=100): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=0, max=100)
|
||||
),
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
async def async_attach_trigger(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
action: AutomationActionType,
|
||||
automation_info: dict,
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Attach a trigger."""
|
||||
config = TRIGGER_SCHEMA(config)
|
||||
|
||||
if config[CONF_TYPE] in STATE_TRIGGER_TYPES:
|
||||
if config[CONF_TYPE] == "opened":
|
||||
to_state = STATE_OPEN
|
||||
elif config[CONF_TYPE] == "closed":
|
||||
to_state = STATE_CLOSED
|
||||
elif config[CONF_TYPE] == "opening":
|
||||
to_state = STATE_OPENING
|
||||
elif config[CONF_TYPE] == "closing":
|
||||
to_state = STATE_CLOSING
|
||||
|
||||
state_config = {
|
||||
state_automation.CONF_PLATFORM: "state",
|
||||
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||
state_automation.CONF_TO: to_state,
|
||||
}
|
||||
state_config = state_automation.TRIGGER_SCHEMA(state_config)
|
||||
return await state_automation.async_attach_trigger(
|
||||
hass, state_config, action, automation_info, platform_type="device"
|
||||
)
|
||||
|
||||
if config[CONF_TYPE] == "position":
|
||||
position = "current_position"
|
||||
if config[CONF_TYPE] == "tilt_position":
|
||||
position = "current_tilt_position"
|
||||
min_pos = config.get(CONF_ABOVE, -1)
|
||||
max_pos = config.get(CONF_BELOW, 101)
|
||||
value_template = f"{{{{ state.attributes.{position} }}}}"
|
||||
|
||||
numeric_state_config = {
|
||||
numeric_state_automation.CONF_PLATFORM: "numeric_state",
|
||||
numeric_state_automation.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||
numeric_state_automation.CONF_BELOW: max_pos,
|
||||
numeric_state_automation.CONF_ABOVE: min_pos,
|
||||
numeric_state_automation.CONF_VALUE_TEMPLATE: value_template,
|
||||
}
|
||||
numeric_state_config = numeric_state_automation.TRIGGER_SCHEMA(numeric_state_config)
|
||||
return await numeric_state_automation.async_attach_trigger(
|
||||
hass, numeric_state_config, action, automation_info, platform_type="device"
|
||||
)
|
||||
@@ -1,12 +1,20 @@
|
||||
{
|
||||
"device_automation": {
|
||||
"condition_type": {
|
||||
"is_open": "{entity_name} is open",
|
||||
"is_closed": "{entity_name} is closed",
|
||||
"is_opening": "{entity_name} is opening",
|
||||
"is_closing": "{entity_name} is closing",
|
||||
"is_position": "{entity_name} position is",
|
||||
"is_tilt_position": "{entity_name} tilt position is"
|
||||
}
|
||||
"device_automation": {
|
||||
"condition_type": {
|
||||
"is_open": "{entity_name} is open",
|
||||
"is_closed": "{entity_name} is closed",
|
||||
"is_opening": "{entity_name} is opening",
|
||||
"is_closing": "{entity_name} is closing",
|
||||
"is_position": "Current {entity_name} position is",
|
||||
"is_tilt_position": "Current {entity_name} tilt position is"
|
||||
},
|
||||
"trigger_type": {
|
||||
"opened": "{entity_name} opened",
|
||||
"closed": "{entity_name} closed",
|
||||
"opening": "{entity_name} opening",
|
||||
"closing": "{entity_name} closing",
|
||||
"position": "{entity_name} position changes",
|
||||
"tilt_position": "{entity_name} tilt position changes"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user