Add device automation support to binary_sensor entities (#26643)
* Add device automation support to binary_sensor entities * turn_on -> turned_on * Correct spelling of present * Improve tests * Fix strings * Fix stale comment
This commit is contained in:
parent
14647f5391
commit
e5f6f33340
4 changed files with 875 additions and 0 deletions
423
homeassistant/components/binary_sensor/device_automation.py
Normal file
423
homeassistant/components/binary_sensor/device_automation.py
Normal file
|
@ -0,0 +1,423 @@
|
|||
"""Provides device automations for lights."""
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.components.automation.state as state
|
||||
from homeassistant.components.device_automation.const import (
|
||||
CONF_IS_OFF,
|
||||
CONF_IS_ON,
|
||||
CONF_TURNED_OFF,
|
||||
CONF_TURNED_ON,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
CONF_CONDITION,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_DOMAIN,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_PLATFORM,
|
||||
CONF_TYPE,
|
||||
)
|
||||
from homeassistant.core import split_entity_id
|
||||
from homeassistant.helpers.entity_registry import async_entries_for_device
|
||||
from homeassistant.helpers import condition, config_validation as cv
|
||||
|
||||
from . import (
|
||||
DOMAIN,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
DEVICE_CLASS_COLD,
|
||||
DEVICE_CLASS_CONNECTIVITY,
|
||||
DEVICE_CLASS_DOOR,
|
||||
DEVICE_CLASS_GARAGE_DOOR,
|
||||
DEVICE_CLASS_GAS,
|
||||
DEVICE_CLASS_HEAT,
|
||||
DEVICE_CLASS_LIGHT,
|
||||
DEVICE_CLASS_LOCK,
|
||||
DEVICE_CLASS_MOISTURE,
|
||||
DEVICE_CLASS_MOTION,
|
||||
DEVICE_CLASS_MOVING,
|
||||
DEVICE_CLASS_OCCUPANCY,
|
||||
DEVICE_CLASS_OPENING,
|
||||
DEVICE_CLASS_PLUG,
|
||||
DEVICE_CLASS_POWER,
|
||||
DEVICE_CLASS_PRESENCE,
|
||||
DEVICE_CLASS_PROBLEM,
|
||||
DEVICE_CLASS_SAFETY,
|
||||
DEVICE_CLASS_SMOKE,
|
||||
DEVICE_CLASS_SOUND,
|
||||
DEVICE_CLASS_VIBRATION,
|
||||
DEVICE_CLASS_WINDOW,
|
||||
)
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
DEVICE_CLASS_NONE = "none"
|
||||
|
||||
CONF_IS_BAT_LOW = "is_bat_low"
|
||||
CONF_IS_NOT_BAT_LOW = "is_not_bat_low"
|
||||
CONF_IS_COLD = "is_cold"
|
||||
CONF_IS_NOT_COLD = "is_not_cold"
|
||||
CONF_IS_CONNECTED = "is_connected"
|
||||
CONF_IS_NOT_CONNECTED = "is_not_connected"
|
||||
CONF_IS_GAS = "is_gas"
|
||||
CONF_IS_NO_GAS = "is_no_gas"
|
||||
CONF_IS_HOT = "is_hot"
|
||||
CONF_IS_NOT_HOT = "is_not_hot"
|
||||
CONF_IS_LIGHT = "is_light"
|
||||
CONF_IS_NO_LIGHT = "is_no_light"
|
||||
CONF_IS_LOCKED = "is_locked"
|
||||
CONF_IS_NOT_LOCKED = "is_not_locked"
|
||||
CONF_IS_MOIST = "is_moist"
|
||||
CONF_IS_NOT_MOIST = "is_not_moist"
|
||||
CONF_IS_MOTION = "is_motion"
|
||||
CONF_IS_NO_MOTION = "is_no_motion"
|
||||
CONF_IS_MOVING = "is_moving"
|
||||
CONF_IS_NOT_MOVING = "is_not_moving"
|
||||
CONF_IS_OCCUPIED = "is_occupied"
|
||||
CONF_IS_NOT_OCCUPIED = "is_not_occupied"
|
||||
CONF_IS_PLUGGED_IN = "is_plugged_in"
|
||||
CONF_IS_NOT_PLUGGED_IN = "is_not_plugged_in"
|
||||
CONF_IS_POWERED = "is_powered"
|
||||
CONF_IS_NOT_POWERED = "is_not_powered"
|
||||
CONF_IS_PRESENT = "is_present"
|
||||
CONF_IS_NOT_PRESENT = "is_not_present"
|
||||
CONF_IS_PROBLEM = "is_problem"
|
||||
CONF_IS_NO_PROBLEM = "is_no_problem"
|
||||
CONF_IS_UNSAFE = "is_unsafe"
|
||||
CONF_IS_NOT_UNSAFE = "is_not_unsafe"
|
||||
CONF_IS_SMOKE = "is_smoke"
|
||||
CONF_IS_NO_SMOKE = "is_no_smoke"
|
||||
CONF_IS_SOUND = "is_sound"
|
||||
CONF_IS_NO_SOUND = "is_no_sound"
|
||||
CONF_IS_VIBRATION = "is_vibration"
|
||||
CONF_IS_NO_VIBRATION = "is_no_vibration"
|
||||
CONF_IS_OPEN = "is_open"
|
||||
CONF_IS_NOT_OPEN = "is_not_open"
|
||||
|
||||
CONF_BAT_LOW = "bat_low"
|
||||
CONF_NOT_BAT_LOW = "not_bat_low"
|
||||
CONF_COLD = "cold"
|
||||
CONF_NOT_COLD = "not_cold"
|
||||
CONF_CONNECTED = "connected"
|
||||
CONF_NOT_CONNECTED = "not_connected"
|
||||
CONF_GAS = "gas"
|
||||
CONF_NO_GAS = "no_gas"
|
||||
CONF_HOT = "hot"
|
||||
CONF_NOT_HOT = "not_hot"
|
||||
CONF_LIGHT = "light"
|
||||
CONF_NO_LIGHT = "no_light"
|
||||
CONF_LOCKED = "locked"
|
||||
CONF_NOT_LOCKED = "not_locked"
|
||||
CONF_MOIST = "moist"
|
||||
CONF_NOT_MOIST = "not_moist"
|
||||
CONF_MOTION = "motion"
|
||||
CONF_NO_MOTION = "no_motion"
|
||||
CONF_MOVING = "moving"
|
||||
CONF_NOT_MOVING = "not_moving"
|
||||
CONF_OCCUPIED = "occupied"
|
||||
CONF_NOT_OCCUPIED = "not_occupied"
|
||||
CONF_PLUGGED_IN = "plugged_in"
|
||||
CONF_NOT_PLUGGED_IN = "not_plugged_in"
|
||||
CONF_POWERED = "powered"
|
||||
CONF_NOT_POWERED = "not_powered"
|
||||
CONF_PRESENT = "present"
|
||||
CONF_NOT_PRESENT = "not_present"
|
||||
CONF_PROBLEM = "problem"
|
||||
CONF_NO_PROBLEM = "no_problem"
|
||||
CONF_UNSAFE = "unsafe"
|
||||
CONF_NOT_UNSAFE = "not_unsafe"
|
||||
CONF_SMOKE = "smoke"
|
||||
CONF_NO_SMOKE = "no_smoke"
|
||||
CONF_SOUND = "sound"
|
||||
CONF_NO_SOUND = "no_sound"
|
||||
CONF_VIBRATION = "vibration"
|
||||
CONF_NO_VIBRATION = "no_vibration"
|
||||
CONF_OPEN = "open"
|
||||
CONF_NOT_OPEN = "not_open"
|
||||
|
||||
IS_ON = [
|
||||
CONF_IS_BAT_LOW,
|
||||
CONF_IS_COLD,
|
||||
CONF_IS_CONNECTED,
|
||||
CONF_IS_GAS,
|
||||
CONF_IS_HOT,
|
||||
CONF_IS_LIGHT,
|
||||
CONF_IS_LOCKED,
|
||||
CONF_IS_MOIST,
|
||||
CONF_IS_MOTION,
|
||||
CONF_IS_MOVING,
|
||||
CONF_IS_OCCUPIED,
|
||||
CONF_IS_OPEN,
|
||||
CONF_IS_PLUGGED_IN,
|
||||
CONF_IS_POWERED,
|
||||
CONF_IS_PRESENT,
|
||||
CONF_IS_PROBLEM,
|
||||
CONF_IS_SMOKE,
|
||||
CONF_IS_SOUND,
|
||||
CONF_IS_UNSAFE,
|
||||
CONF_IS_VIBRATION,
|
||||
CONF_IS_ON,
|
||||
]
|
||||
|
||||
IS_OFF = [
|
||||
CONF_IS_NOT_BAT_LOW,
|
||||
CONF_IS_NOT_COLD,
|
||||
CONF_IS_NOT_CONNECTED,
|
||||
CONF_IS_NOT_HOT,
|
||||
CONF_IS_NOT_LOCKED,
|
||||
CONF_IS_NOT_MOIST,
|
||||
CONF_IS_NOT_MOVING,
|
||||
CONF_IS_NOT_OCCUPIED,
|
||||
CONF_IS_NOT_OPEN,
|
||||
CONF_IS_NOT_PLUGGED_IN,
|
||||
CONF_IS_NOT_POWERED,
|
||||
CONF_IS_NOT_PRESENT,
|
||||
CONF_IS_NOT_UNSAFE,
|
||||
CONF_IS_NO_GAS,
|
||||
CONF_IS_NO_LIGHT,
|
||||
CONF_IS_NO_MOTION,
|
||||
CONF_IS_NO_PROBLEM,
|
||||
CONF_IS_NO_SMOKE,
|
||||
CONF_IS_NO_SOUND,
|
||||
CONF_IS_NO_VIBRATION,
|
||||
CONF_IS_OFF,
|
||||
]
|
||||
|
||||
TURNED_ON = [
|
||||
CONF_BAT_LOW,
|
||||
CONF_COLD,
|
||||
CONF_CONNECTED,
|
||||
CONF_GAS,
|
||||
CONF_HOT,
|
||||
CONF_LIGHT,
|
||||
CONF_LOCKED,
|
||||
CONF_MOIST,
|
||||
CONF_MOTION,
|
||||
CONF_MOVING,
|
||||
CONF_OCCUPIED,
|
||||
CONF_OPEN,
|
||||
CONF_PLUGGED_IN,
|
||||
CONF_POWERED,
|
||||
CONF_PRESENT,
|
||||
CONF_PROBLEM,
|
||||
CONF_SMOKE,
|
||||
CONF_SOUND,
|
||||
CONF_UNSAFE,
|
||||
CONF_VIBRATION,
|
||||
CONF_TURNED_ON,
|
||||
]
|
||||
|
||||
TURNED_OFF = [
|
||||
CONF_NOT_BAT_LOW,
|
||||
CONF_NOT_COLD,
|
||||
CONF_NOT_CONNECTED,
|
||||
CONF_NOT_HOT,
|
||||
CONF_NOT_LOCKED,
|
||||
CONF_NOT_MOIST,
|
||||
CONF_NOT_MOVING,
|
||||
CONF_NOT_OCCUPIED,
|
||||
CONF_NOT_OPEN,
|
||||
CONF_NOT_PLUGGED_IN,
|
||||
CONF_NOT_POWERED,
|
||||
CONF_NOT_PRESENT,
|
||||
CONF_NOT_UNSAFE,
|
||||
CONF_NO_GAS,
|
||||
CONF_NO_LIGHT,
|
||||
CONF_NO_MOTION,
|
||||
CONF_NO_PROBLEM,
|
||||
CONF_NO_SMOKE,
|
||||
CONF_NO_SOUND,
|
||||
CONF_NO_VIBRATION,
|
||||
CONF_TURNED_OFF,
|
||||
]
|
||||
|
||||
ENTITY_CONDITIONS = {
|
||||
DEVICE_CLASS_BATTERY: [
|
||||
{CONF_TYPE: CONF_IS_BAT_LOW},
|
||||
{CONF_TYPE: CONF_IS_NOT_BAT_LOW},
|
||||
],
|
||||
DEVICE_CLASS_COLD: [{CONF_TYPE: CONF_IS_COLD}, {CONF_TYPE: CONF_IS_NOT_COLD}],
|
||||
DEVICE_CLASS_CONNECTIVITY: [
|
||||
{CONF_TYPE: CONF_IS_CONNECTED},
|
||||
{CONF_TYPE: CONF_IS_NOT_CONNECTED},
|
||||
],
|
||||
DEVICE_CLASS_DOOR: [{CONF_TYPE: CONF_IS_OPEN}, {CONF_TYPE: CONF_IS_NOT_OPEN}],
|
||||
DEVICE_CLASS_GARAGE_DOOR: [
|
||||
{CONF_TYPE: CONF_IS_OPEN},
|
||||
{CONF_TYPE: CONF_IS_NOT_OPEN},
|
||||
],
|
||||
DEVICE_CLASS_GAS: [{CONF_TYPE: CONF_IS_GAS}, {CONF_TYPE: CONF_IS_NO_GAS}],
|
||||
DEVICE_CLASS_HEAT: [{CONF_TYPE: CONF_IS_HOT}, {CONF_TYPE: CONF_IS_NOT_HOT}],
|
||||
DEVICE_CLASS_LIGHT: [{CONF_TYPE: CONF_IS_LIGHT}, {CONF_TYPE: CONF_IS_NO_LIGHT}],
|
||||
DEVICE_CLASS_LOCK: [{CONF_TYPE: CONF_IS_LOCKED}, {CONF_TYPE: CONF_IS_NOT_LOCKED}],
|
||||
DEVICE_CLASS_MOISTURE: [{CONF_TYPE: CONF_IS_MOIST}, {CONF_TYPE: CONF_IS_NOT_MOIST}],
|
||||
DEVICE_CLASS_MOTION: [{CONF_TYPE: CONF_IS_MOTION}, {CONF_TYPE: CONF_IS_NO_MOTION}],
|
||||
DEVICE_CLASS_MOVING: [{CONF_TYPE: CONF_IS_MOVING}, {CONF_TYPE: CONF_IS_NOT_MOVING}],
|
||||
DEVICE_CLASS_OCCUPANCY: [
|
||||
{CONF_TYPE: CONF_IS_OCCUPIED},
|
||||
{CONF_TYPE: CONF_IS_NOT_OCCUPIED},
|
||||
],
|
||||
DEVICE_CLASS_OPENING: [{CONF_TYPE: CONF_IS_OPEN}, {CONF_TYPE: CONF_IS_NOT_OPEN}],
|
||||
DEVICE_CLASS_PLUG: [
|
||||
{CONF_TYPE: CONF_IS_PLUGGED_IN},
|
||||
{CONF_TYPE: CONF_IS_NOT_PLUGGED_IN},
|
||||
],
|
||||
DEVICE_CLASS_POWER: [
|
||||
{CONF_TYPE: CONF_IS_POWERED},
|
||||
{CONF_TYPE: CONF_IS_NOT_POWERED},
|
||||
],
|
||||
DEVICE_CLASS_PRESENCE: [
|
||||
{CONF_TYPE: CONF_IS_PRESENT},
|
||||
{CONF_TYPE: CONF_IS_NOT_PRESENT},
|
||||
],
|
||||
DEVICE_CLASS_PROBLEM: [
|
||||
{CONF_TYPE: CONF_IS_PROBLEM},
|
||||
{CONF_TYPE: CONF_IS_NO_PROBLEM},
|
||||
],
|
||||
DEVICE_CLASS_SAFETY: [{CONF_TYPE: CONF_IS_UNSAFE}, {CONF_TYPE: CONF_IS_NOT_UNSAFE}],
|
||||
DEVICE_CLASS_SMOKE: [{CONF_TYPE: CONF_IS_SMOKE}, {CONF_TYPE: CONF_IS_NO_SMOKE}],
|
||||
DEVICE_CLASS_SOUND: [{CONF_TYPE: CONF_IS_SOUND}, {CONF_TYPE: CONF_IS_NO_SOUND}],
|
||||
DEVICE_CLASS_VIBRATION: [
|
||||
{CONF_TYPE: CONF_IS_VIBRATION},
|
||||
{CONF_TYPE: CONF_IS_NO_VIBRATION},
|
||||
],
|
||||
DEVICE_CLASS_WINDOW: [{CONF_TYPE: CONF_IS_OPEN}, {CONF_TYPE: CONF_IS_NOT_OPEN}],
|
||||
DEVICE_CLASS_NONE: [{CONF_TYPE: CONF_IS_ON}, {CONF_TYPE: CONF_IS_OFF}],
|
||||
}
|
||||
|
||||
ENTITY_TRIGGERS = {
|
||||
DEVICE_CLASS_BATTERY: [{CONF_TYPE: CONF_BAT_LOW}, {CONF_TYPE: CONF_NOT_BAT_LOW}],
|
||||
DEVICE_CLASS_COLD: [{CONF_TYPE: CONF_COLD}, {CONF_TYPE: CONF_NOT_COLD}],
|
||||
DEVICE_CLASS_CONNECTIVITY: [
|
||||
{CONF_TYPE: CONF_CONNECTED},
|
||||
{CONF_TYPE: CONF_NOT_CONNECTED},
|
||||
],
|
||||
DEVICE_CLASS_DOOR: [{CONF_TYPE: CONF_OPEN}, {CONF_TYPE: CONF_NOT_OPEN}],
|
||||
DEVICE_CLASS_GARAGE_DOOR: [{CONF_TYPE: CONF_OPEN}, {CONF_TYPE: CONF_NOT_OPEN}],
|
||||
DEVICE_CLASS_GAS: [{CONF_TYPE: CONF_GAS}, {CONF_TYPE: CONF_NO_GAS}],
|
||||
DEVICE_CLASS_HEAT: [{CONF_TYPE: CONF_HOT}, {CONF_TYPE: CONF_NOT_HOT}],
|
||||
DEVICE_CLASS_LIGHT: [{CONF_TYPE: CONF_LIGHT}, {CONF_TYPE: CONF_NO_LIGHT}],
|
||||
DEVICE_CLASS_LOCK: [{CONF_TYPE: CONF_LOCKED}, {CONF_TYPE: CONF_NOT_LOCKED}],
|
||||
DEVICE_CLASS_MOISTURE: [{CONF_TYPE: CONF_MOIST}, {CONF_TYPE: CONF_NOT_MOIST}],
|
||||
DEVICE_CLASS_MOTION: [{CONF_TYPE: CONF_MOTION}, {CONF_TYPE: CONF_NO_MOTION}],
|
||||
DEVICE_CLASS_MOVING: [{CONF_TYPE: CONF_MOVING}, {CONF_TYPE: CONF_NOT_MOVING}],
|
||||
DEVICE_CLASS_OCCUPANCY: [
|
||||
{CONF_TYPE: CONF_OCCUPIED},
|
||||
{CONF_TYPE: CONF_NOT_OCCUPIED},
|
||||
],
|
||||
DEVICE_CLASS_OPENING: [{CONF_TYPE: CONF_OPEN}, {CONF_TYPE: CONF_NOT_OPEN}],
|
||||
DEVICE_CLASS_PLUG: [{CONF_TYPE: CONF_PLUGGED_IN}, {CONF_TYPE: CONF_NOT_PLUGGED_IN}],
|
||||
DEVICE_CLASS_POWER: [{CONF_TYPE: CONF_POWERED}, {CONF_TYPE: CONF_NOT_POWERED}],
|
||||
DEVICE_CLASS_PRESENCE: [{CONF_TYPE: CONF_PRESENT}, {CONF_TYPE: CONF_NOT_PRESENT}],
|
||||
DEVICE_CLASS_PROBLEM: [{CONF_TYPE: CONF_PROBLEM}, {CONF_TYPE: CONF_NO_PROBLEM}],
|
||||
DEVICE_CLASS_SAFETY: [{CONF_TYPE: CONF_UNSAFE}, {CONF_TYPE: CONF_NOT_UNSAFE}],
|
||||
DEVICE_CLASS_SMOKE: [{CONF_TYPE: CONF_SMOKE}, {CONF_TYPE: CONF_NO_SMOKE}],
|
||||
DEVICE_CLASS_SOUND: [{CONF_TYPE: CONF_SOUND}, {CONF_TYPE: CONF_NO_SOUND}],
|
||||
DEVICE_CLASS_VIBRATION: [
|
||||
{CONF_TYPE: CONF_VIBRATION},
|
||||
{CONF_TYPE: CONF_NO_VIBRATION},
|
||||
],
|
||||
DEVICE_CLASS_WINDOW: [{CONF_TYPE: CONF_OPEN}, {CONF_TYPE: CONF_NOT_OPEN}],
|
||||
DEVICE_CLASS_NONE: [{CONF_TYPE: CONF_TURNED_ON}, {CONF_TYPE: CONF_TURNED_OFF}],
|
||||
}
|
||||
|
||||
CONDITION_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_CONDITION): "device",
|
||||
vol.Required(CONF_DEVICE_ID): str,
|
||||
vol.Required(CONF_DOMAIN): DOMAIN,
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TYPE): vol.In(IS_OFF + IS_ON),
|
||||
}
|
||||
)
|
||||
|
||||
TRIGGER_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_PLATFORM): "device",
|
||||
vol.Required(CONF_DEVICE_ID): str,
|
||||
vol.Required(CONF_DOMAIN): DOMAIN,
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TYPE): vol.In(TURNED_OFF + TURNED_ON),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def async_condition_from_config(config, config_validation):
|
||||
"""Evaluate state based on configuration."""
|
||||
config = CONDITION_SCHEMA(config)
|
||||
condition_type = config[CONF_TYPE]
|
||||
if condition_type in IS_ON:
|
||||
stat = "on"
|
||||
else:
|
||||
stat = "off"
|
||||
state_config = {
|
||||
condition.CONF_CONDITION: "state",
|
||||
condition.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||
condition.CONF_STATE: stat,
|
||||
}
|
||||
|
||||
return condition.state_from_config(state_config, config_validation)
|
||||
|
||||
|
||||
async def async_trigger(hass, config, action, automation_info):
|
||||
"""Listen for state changes based on configuration."""
|
||||
config = TRIGGER_SCHEMA(config)
|
||||
trigger_type = config[CONF_TYPE]
|
||||
if trigger_type in TURNED_ON:
|
||||
from_state = "off"
|
||||
to_state = "on"
|
||||
else:
|
||||
from_state = "on"
|
||||
to_state = "off"
|
||||
state_config = {
|
||||
state.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||
state.CONF_FROM: from_state,
|
||||
state.CONF_TO: to_state,
|
||||
}
|
||||
|
||||
return await state.async_trigger(hass, state_config, action, automation_info)
|
||||
|
||||
|
||||
def _is_domain(entity, domain):
|
||||
return split_entity_id(entity.entity_id)[0] == domain
|
||||
|
||||
|
||||
async def _async_get_automations(hass, device_id, automation_templates, domain):
|
||||
"""List device automations."""
|
||||
automations = []
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entities = async_entries_for_device(entity_registry, device_id)
|
||||
domain_entities = [x for x in entities if _is_domain(x, domain)]
|
||||
for entity in domain_entities:
|
||||
device_class = DEVICE_CLASS_NONE
|
||||
entity_id = entity.entity_id
|
||||
entity = hass.states.get(entity_id)
|
||||
if entity and ATTR_DEVICE_CLASS in entity.attributes:
|
||||
device_class = entity.attributes[ATTR_DEVICE_CLASS]
|
||||
automation_template = automation_templates[device_class]
|
||||
|
||||
for automation in automation_template:
|
||||
automation = dict(automation)
|
||||
automation.update(device_id=device_id, entity_id=entity_id, domain=domain)
|
||||
automations.append(automation)
|
||||
|
||||
return automations
|
||||
|
||||
|
||||
async def async_get_conditions(hass, device_id):
|
||||
"""List device conditions."""
|
||||
automations = await _async_get_automations(
|
||||
hass, device_id, ENTITY_CONDITIONS, DOMAIN
|
||||
)
|
||||
for automation in automations:
|
||||
automation.update(condition="device")
|
||||
return automations
|
||||
|
||||
|
||||
async def async_get_triggers(hass, device_id):
|
||||
"""List device triggers."""
|
||||
automations = await _async_get_automations(hass, device_id, ENTITY_TRIGGERS, DOMAIN)
|
||||
for automation in automations:
|
||||
automation.update(platform="device")
|
||||
return automations
|
93
homeassistant/components/binary_sensor/strings.json
Normal file
93
homeassistant/components/binary_sensor/strings.json
Normal file
|
@ -0,0 +1,93 @@
|
|||
{
|
||||
"device_automation": {
|
||||
"condition_type": {
|
||||
"is_bat_low": "{entity_name} battery is low",
|
||||
"is_not_bat_low": "{entity_name} battery is normal",
|
||||
"is_cold": "{entity_name} is cold",
|
||||
"is_not_cold": "{entity_name} is not cold",
|
||||
"is_connected": "{entity_name} is connected",
|
||||
"is_not_connected": "{entity_name} is disconnected",
|
||||
"is_gas": "{entity_name} is detecting gas",
|
||||
"is_no_gas": "{entity_name} is not detecting gas",
|
||||
"is_hot": "{entity_name} is hot",
|
||||
"is_not_hot": "{entity_name} is not hot",
|
||||
"is_light": "{entity_name} is detecting light",
|
||||
"is_no_light": "{entity_name} is not detecting light",
|
||||
"is_locked": "{entity_name} is locked",
|
||||
"is_not_locked": "{entity_name} is unlocked",
|
||||
"is_moist": "{entity_name} is moist",
|
||||
"is_not_moist": "{entity_name} is dry",
|
||||
"is_motion": "{entity_name} is detecting motion",
|
||||
"is_no_motion": "{entity_name} is not detecting motion",
|
||||
"is_moving": "{entity_name} is moving",
|
||||
"is_not_moving": "{entity_name} is not moving",
|
||||
"is_occupied": "{entity_name} is occupied",
|
||||
"is_not_occupied": "{entity_name} is not occupied",
|
||||
"is_plugged_in": "{entity_name} is plugged in",
|
||||
"is_not_plugged_in": "{entity_name} is unplugged",
|
||||
"is_powered": "{entity_name} is powered",
|
||||
"is_not_powered": "{entity_name} is not powered",
|
||||
"is_present": "{entity_name} is present",
|
||||
"is_not_present": "{entity_name} is not present",
|
||||
"is_problem": "{entity_name} is detecting problem",
|
||||
"is_no_problem": "{entity_name} is not detecting problem",
|
||||
"is_unsafe": "{entity_name} is unsafe",
|
||||
"is_not_unsafe": "{entity_name} is safe",
|
||||
"is_smoke": "{entity_name} is detecting smoke",
|
||||
"is_no_smoke": "{entity_name} is not detecting smoke",
|
||||
"is_sound": "{entity_name} is detecting sound",
|
||||
"is_no_sound": "{entity_name} is not detecting sound",
|
||||
"is_vibration": "{entity_name} is detecting vibration",
|
||||
"is_no_vibration": "{entity_name} is not detecting vibration",
|
||||
"is_open": "{entity_name} is open",
|
||||
"is_not_open": "{entity_name} is closed",
|
||||
"is_on": "{entity_name} is on",
|
||||
"is_off": "{entity_name} is off"
|
||||
},
|
||||
"trigger_type": {
|
||||
"bat_low": "{entity_name} battery low",
|
||||
"not_bat_low": "{entity_name} battery normal",
|
||||
"cold": "{entity_name} became cold",
|
||||
"not_cold": "{entity_name} became not cold",
|
||||
"connected": "{entity_name} connected",
|
||||
"not_connected": "{entity_name} disconnected",
|
||||
"gas": "{entity_name} started detecting gas",
|
||||
"no_gas": "{entity_name} stopped detecting gas",
|
||||
"hot": "{entity_name} became hot",
|
||||
"not_hot": "{entity_name} became not hot",
|
||||
"light": "{entity_name} started detecting light",
|
||||
"no_light": "{entity_name} stopped detecting light",
|
||||
"locked": "{entity_name} locked",
|
||||
"not_locked": "{entity_name} unlocked",
|
||||
"moist§": "{entity_name} became moist",
|
||||
"not_moist": "{entity_name} became dry",
|
||||
"motion": "{entity_name} started detecting motion",
|
||||
"no_motion": "{entity_name} stopped detecting motion",
|
||||
"moving": "{entity_name} started moving",
|
||||
"not_moving": "{entity_name} stopped moving",
|
||||
"occupied": "{entity_name} became occupied",
|
||||
"not_occupied": "{entity_name} became not occupied",
|
||||
"plugged_in": "{entity_name} plugged in",
|
||||
"not_plugged_in": "{entity_name} unplugged",
|
||||
"powered": "{entity_name} powered",
|
||||
"not_powered": "{entity_name} not powered",
|
||||
"present": "{entity_name} present",
|
||||
"not_present": "{entity_name} not present",
|
||||
"problem": "{entity_name} started detecting problem",
|
||||
"no_problem": "{entity_name} stopped detecting problem",
|
||||
"unsafe": "{entity_name} became unsafe",
|
||||
"not_unsafe": "{entity_name} became safe",
|
||||
"smoke": "{entity_name} started detecting smoke",
|
||||
"no_smoke": "{entity_name} stopped detecting smoke",
|
||||
"sound": "{entity_name} started detecting sound",
|
||||
"no_sound": "{entity_name} stopped detecting sound",
|
||||
"vibration": "{entity_name} started detecting vibration",
|
||||
"no_vibration": "{entity_name} stopped detecting vibration",
|
||||
"opened": "{entity_name} opened",
|
||||
"closed": "{entity_name} closed",
|
||||
"turned_on": "{entity_name} turned on",
|
||||
"turned_off": "{entity_name} turned off"
|
||||
|
||||
}
|
||||
}
|
||||
}
|
309
tests/components/binary_sensor/test_device_automation.py
Normal file
309
tests/components/binary_sensor/test_device_automation.py
Normal file
|
@ -0,0 +1,309 @@
|
|||
"""The test for binary_sensor device automation."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN, DEVICE_CLASSES
|
||||
from homeassistant.components.binary_sensor.device_automation import (
|
||||
ENTITY_CONDITIONS,
|
||||
ENTITY_TRIGGERS,
|
||||
)
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.components.device_automation import (
|
||||
_async_get_device_automations as async_get_device_automations,
|
||||
)
|
||||
from homeassistant.helpers import device_registry
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
async_mock_service,
|
||||
mock_device_registry,
|
||||
mock_registry,
|
||||
)
|
||||
|
||||
|
||||
@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")
|
||||
|
||||
|
||||
def _same_lists(a, b):
|
||||
if len(a) != len(b):
|
||||
return False
|
||||
|
||||
for d in a:
|
||||
if d not in b:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
async def test_get_actions(hass, device_reg, entity_reg):
|
||||
"""Test we get the expected actions from a binary_sensor."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
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",
|
||||
platform.ENTITIES["battery"].unique_id,
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
|
||||
expected_actions = []
|
||||
actions = await async_get_device_automations(
|
||||
hass, "async_get_actions", device_entry.id
|
||||
)
|
||||
assert _same_lists(actions, expected_actions)
|
||||
|
||||
|
||||
async def test_get_conditions(hass, device_reg, entity_reg):
|
||||
"""Test we get the expected conditions from a binary_sensor."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
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")},
|
||||
)
|
||||
for device_class in DEVICE_CLASSES:
|
||||
entity_reg.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES[device_class].unique_id,
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
|
||||
expected_conditions = [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": condition["type"],
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": platform.ENTITIES[device_class].entity_id,
|
||||
}
|
||||
for device_class in DEVICE_CLASSES
|
||||
for condition in ENTITY_CONDITIONS[device_class]
|
||||
]
|
||||
conditions = await async_get_device_automations(
|
||||
hass, "async_get_conditions", device_entry.id
|
||||
)
|
||||
assert _same_lists(conditions, expected_conditions)
|
||||
|
||||
|
||||
async def test_get_triggers(hass, device_reg, entity_reg):
|
||||
"""Test we get the expected triggers from a binary_sensor."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
|
||||
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")},
|
||||
)
|
||||
for device_class in DEVICE_CLASSES:
|
||||
entity_reg.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
platform.ENTITIES[device_class].unique_id,
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
|
||||
expected_triggers = [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": trigger["type"],
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": platform.ENTITIES[device_class].entity_id,
|
||||
}
|
||||
for device_class in DEVICE_CLASSES
|
||||
for trigger in ENTITY_TRIGGERS[device_class]
|
||||
]
|
||||
triggers = await async_get_device_automations(
|
||||
hass, "async_get_triggers", device_entry.id
|
||||
)
|
||||
assert _same_lists(triggers, expected_triggers)
|
||||
|
||||
|
||||
async def test_if_fires_on_state_change(hass, calls):
|
||||
"""Test for on and off triggers firing."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
|
||||
sensor1 = platform.ENTITIES["battery"]
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": sensor1.entity_id,
|
||||
"type": "bat_low",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "bat_low {{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"for",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": sensor1.entity_id,
|
||||
"type": "not_bat_low",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "not_bat_low {{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"for",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(sensor1.entity_id).state == STATE_ON
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set(sensor1.entity_id, STATE_OFF)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "not_bat_low state - {} - on - off - None".format(
|
||||
sensor1.entity_id
|
||||
)
|
||||
|
||||
hass.states.async_set(sensor1.entity_id, STATE_ON)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["some"] == "bat_low state - {} - off - on - None".format(
|
||||
sensor1.entity_id
|
||||
)
|
||||
|
||||
|
||||
async def test_if_state(hass, calls):
|
||||
"""Test for turn_on and turn_off conditions."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
|
||||
platform.init()
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
|
||||
sensor1 = platform.ENTITIES["battery"]
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event1"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": sensor1.entity_id,
|
||||
"type": "is_bat_low",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "is_on {{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(("platform", "event.event_type"))
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"trigger": {"platform": "event", "event_type": "test_event2"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": "",
|
||||
"entity_id": sensor1.entity_id,
|
||||
"type": "is_not_bat_low",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "is_off {{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(("platform", "event.event_type"))
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(sensor1.entity_id).state == STATE_ON
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "is_on event - test_event1"
|
||||
|
||||
hass.states.async_set(sensor1.entity_id, STATE_OFF)
|
||||
hass.bus.async_fire("test_event1")
|
||||
hass.bus.async_fire("test_event2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["some"] == "is_off event - test_event2"
|
50
tests/testing_config/custom_components/test/binary_sensor.py
Normal file
50
tests/testing_config/custom_components/test/binary_sensor.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
"""
|
||||
Provide a mock binary sensor platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
from homeassistant.components.binary_sensor import BinarySensorDevice, DEVICE_CLASSES
|
||||
from tests.common import MockEntity
|
||||
|
||||
|
||||
ENTITIES = {}
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
global ENTITIES
|
||||
|
||||
ENTITIES = (
|
||||
{}
|
||||
if empty
|
||||
else {
|
||||
device_class: MockBinarySensor(
|
||||
name=f"{device_class} sensor",
|
||||
is_on=True,
|
||||
unique_id=f"unique_{device_class}",
|
||||
device_class=device_class,
|
||||
)
|
||||
for device_class in DEVICE_CLASSES
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass, config, async_add_entities_callback, discovery_info=None
|
||||
):
|
||||
"""Return mock entities."""
|
||||
async_add_entities_callback(list(ENTITIES.values()))
|
||||
|
||||
|
||||
class MockBinarySensor(MockEntity, BinarySensorDevice):
|
||||
"""Mock Binary Sensor class."""
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self._handle("is_on")
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the class of this sensor."""
|
||||
return self._handle("device_class")
|
Loading…
Add table
Add a link
Reference in a new issue