mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add device triggers to vacuum (#28548)
* Add device triggers to vacuum * Update strings
This commit is contained in:
parent
c3f07347a1
commit
6999a712ef
@ -27,8 +27,8 @@ TRIGGER_SCHEMA = vol.All(
|
|||||||
vol.Required(CONF_PLATFORM): "state",
|
vol.Required(CONF_PLATFORM): "state",
|
||||||
vol.Required(CONF_ENTITY_ID): cv.entity_ids,
|
vol.Required(CONF_ENTITY_ID): cv.entity_ids,
|
||||||
# These are str on purpose. Want to catch YAML conversions
|
# These are str on purpose. Want to catch YAML conversions
|
||||||
vol.Optional(CONF_FROM): str,
|
vol.Optional(CONF_FROM): vol.Any(str, [str]),
|
||||||
vol.Optional(CONF_TO): str,
|
vol.Optional(CONF_TO): vol.Any(str, [str]),
|
||||||
vol.Optional(CONF_FOR): vol.Any(
|
vol.Optional(CONF_FOR): vol.Any(
|
||||||
vol.All(cv.time_period, cv.positive_timedelta),
|
vol.All(cv.time_period, cv.positive_timedelta),
|
||||||
cv.template,
|
cv.template,
|
||||||
|
@ -71,6 +71,8 @@ STATE_DOCKED = "docked"
|
|||||||
STATE_RETURNING = "returning"
|
STATE_RETURNING = "returning"
|
||||||
STATE_ERROR = "error"
|
STATE_ERROR = "error"
|
||||||
|
|
||||||
|
STATES = [STATE_CLEANING, STATE_DOCKED, STATE_RETURNING, STATE_ERROR]
|
||||||
|
|
||||||
DEFAULT_NAME = "Vacuum cleaner robot"
|
DEFAULT_NAME = "Vacuum cleaner robot"
|
||||||
|
|
||||||
SUPPORT_TURN_ON = 1
|
SUPPORT_TURN_ON = 1
|
||||||
|
86
homeassistant/components/vacuum/device_trigger.py
Normal file
86
homeassistant/components/vacuum/device_trigger.py
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
"""Provides device automations for Vacuum."""
|
||||||
|
from typing import List
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONF_DOMAIN,
|
||||||
|
CONF_TYPE,
|
||||||
|
CONF_PLATFORM,
|
||||||
|
CONF_DEVICE_ID,
|
||||||
|
CONF_ENTITY_ID,
|
||||||
|
)
|
||||||
|
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, AutomationActionType
|
||||||
|
from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA
|
||||||
|
from . import DOMAIN, STATE_CLEANING, STATE_DOCKED, STATES
|
||||||
|
|
||||||
|
TRIGGER_TYPES = {"cleaning", "docked"}
|
||||||
|
|
||||||
|
TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||||
|
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_get_triggers(hass: HomeAssistant, device_id: str) -> List[dict]:
|
||||||
|
"""List device triggers for Vacuum 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
|
||||||
|
|
||||||
|
triggers.append(
|
||||||
|
{
|
||||||
|
CONF_PLATFORM: "device",
|
||||||
|
CONF_DEVICE_ID: device_id,
|
||||||
|
CONF_DOMAIN: DOMAIN,
|
||||||
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
|
CONF_TYPE: "cleaning",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
triggers.append(
|
||||||
|
{
|
||||||
|
CONF_PLATFORM: "device",
|
||||||
|
CONF_DEVICE_ID: device_id,
|
||||||
|
CONF_DOMAIN: DOMAIN,
|
||||||
|
CONF_ENTITY_ID: entry.entity_id,
|
||||||
|
CONF_TYPE: "docked",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return triggers
|
||||||
|
|
||||||
|
|
||||||
|
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] == "cleaning":
|
||||||
|
from_state = [state for state in STATES if state != STATE_CLEANING]
|
||||||
|
to_state = STATE_CLEANING
|
||||||
|
else:
|
||||||
|
from_state = [state for state in STATES if state != STATE_DOCKED]
|
||||||
|
to_state = STATE_DOCKED
|
||||||
|
|
||||||
|
state_config = {
|
||||||
|
state.CONF_PLATFORM: "state",
|
||||||
|
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||||
|
state.CONF_FROM: from_state,
|
||||||
|
state.CONF_TO: to_state,
|
||||||
|
}
|
||||||
|
state_config = state.TRIGGER_SCHEMA(state_config)
|
||||||
|
return await state.async_attach_trigger(
|
||||||
|
hass, state_config, action, automation_info, platform_type="device"
|
||||||
|
)
|
@ -1,5 +1,8 @@
|
|||||||
{
|
{
|
||||||
"device_automation": {
|
"device_automation": {
|
||||||
|
"trigger_type": {
|
||||||
|
"cleaning": "{entity_name} started cleaning",
|
||||||
|
"docked": "{entity_name} docked"
|
||||||
"action_type": {
|
"action_type": {
|
||||||
"clean": "Let {entity_name} clean",
|
"clean": "Let {entity_name} clean",
|
||||||
"dock": "Let {entity_name} return to the dock"
|
"dock": "Let {entity_name} return to the dock"
|
||||||
|
131
tests/components/vacuum/test_device_trigger.py
Normal file
131
tests/components/vacuum/test_device_trigger.py
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
"""The tests for Vacuum device triggers."""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.vacuum import DOMAIN, STATE_DOCKED, STATE_CLEANING
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
import homeassistant.components.automation as automation
|
||||||
|
from homeassistant.helpers import device_registry
|
||||||
|
|
||||||
|
from tests.common import (
|
||||||
|
MockConfigEntry,
|
||||||
|
assert_lists_same,
|
||||||
|
async_mock_service,
|
||||||
|
mock_device_registry,
|
||||||
|
mock_registry,
|
||||||
|
async_get_device_automations,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def device_reg(hass):
|
||||||
|
"""Return an empty, loaded, registry."""
|
||||||
|
return mock_device_registry(hass)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def entity_reg(hass):
|
||||||
|
"""Return an empty, loaded, registry."""
|
||||||
|
return mock_registry(hass)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def calls(hass):
|
||||||
|
"""Track calls to a mock serivce."""
|
||||||
|
return async_mock_service(hass, "test", "automation")
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_triggers(hass, device_reg, entity_reg):
|
||||||
|
"""Test we get the expected triggers from a vacuum."""
|
||||||
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
device_entry = device_reg.async_get_or_create(
|
||||||
|
config_entry_id=config_entry.entry_id,
|
||||||
|
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||||
|
)
|
||||||
|
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
|
||||||
|
expected_triggers = [
|
||||||
|
{
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"type": "cleaning",
|
||||||
|
"device_id": device_entry.id,
|
||||||
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"type": "docked",
|
||||||
|
"device_id": device_entry.id,
|
||||||
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||||
|
assert_lists_same(triggers, expected_triggers)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_state_change(hass, calls):
|
||||||
|
"""Test for turn_on and turn_off triggers firing."""
|
||||||
|
hass.states.async_set("vacuum.entity", STATE_DOCKED)
|
||||||
|
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: [
|
||||||
|
{
|
||||||
|
"trigger": {
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "",
|
||||||
|
"entity_id": "vacuum.entity",
|
||||||
|
"type": "cleaning",
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"service": "test.automation",
|
||||||
|
"data_template": {
|
||||||
|
"some": (
|
||||||
|
"cleaning - {{ trigger.platform}} - "
|
||||||
|
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
|
||||||
|
"{{ trigger.to_state.state}}"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"trigger": {
|
||||||
|
"platform": "device",
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "",
|
||||||
|
"entity_id": "vacuum.entity",
|
||||||
|
"type": "docked",
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"service": "test.automation",
|
||||||
|
"data_template": {
|
||||||
|
"some": (
|
||||||
|
"docked - {{ trigger.platform}} - "
|
||||||
|
"{{ trigger.entity_id}} - {{ trigger.from_state.state}} - "
|
||||||
|
"{{ trigger.to_state.state}}"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fake that the entity is cleaning
|
||||||
|
hass.states.async_set("vacuum.entity", STATE_CLEANING)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 1
|
||||||
|
assert calls[0].data["some"] == "cleaning - device - {} - docked - cleaning".format(
|
||||||
|
"vacuum.entity"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fake that the entity is docked
|
||||||
|
hass.states.async_set("vacuum.entity", STATE_DOCKED)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 2
|
||||||
|
assert calls[1].data["some"] == "docked - device - {} - cleaning - docked".format(
|
||||||
|
"vacuum.entity"
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user