hass-core/tests/components/automation/common.py
Paulus Schoutsen 02d9ed5e36
Do not select all entities when omitting entity ID in service call (#29178)
* Do not select all entities when omitting entity ID

* Address comments Matthew

* Require either area_id or entity_id

* Fix tests

* Fix test
2019-12-02 16:23:12 -08:00

49 lines
1.5 KiB
Python

"""Collection of helper methods.
All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly.
"""
from homeassistant.components.automation import DOMAIN, SERVICE_TRIGGER
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_ON,
SERVICE_TURN_OFF,
SERVICE_TOGGLE,
SERVICE_RELOAD,
ENTITY_MATCH_ALL,
)
from homeassistant.loader import bind_hass
@bind_hass
async def async_turn_on(hass, entity_id=ENTITY_MATCH_ALL):
"""Turn on specified automation or all."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data)
@bind_hass
async def async_turn_off(hass, entity_id=ENTITY_MATCH_ALL):
"""Turn off specified automation or all."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data)
@bind_hass
async def async_toggle(hass, entity_id=ENTITY_MATCH_ALL):
"""Toggle specified automation or all."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data)
@bind_hass
async def async_trigger(hass, entity_id=ENTITY_MATCH_ALL):
"""Trigger specified automation or all."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
await hass.services.async_call(DOMAIN, SERVICE_TRIGGER, data)
@bind_hass
async def async_reload(hass):
"""Reload the automation from config."""
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)