core/homeassistant/components/script.py

160 lines
5.0 KiB
Python
Raw Normal View History

"""
2016-03-08 16:55:57 +00:00
Support for scripts.
Scripts are a sequence of actions that can be triggered manually
by the user or automatically based upon automation events, etc.
2015-10-25 14:14:56 +00:00
For more details about this component, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/script/
"""
import logging
import voluptuous as vol
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
2016-04-21 22:52:20 +00:00
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON,
SERVICE_TOGGLE, STATE_ON, CONF_ALIAS)
2016-01-24 06:49:49 +00:00
from homeassistant.helpers.entity import ToggleEntity, split_entity_id
2016-02-19 05:27:50 +00:00
from homeassistant.helpers.entity_component import EntityComponent
import homeassistant.helpers.config_validation as cv
2016-04-21 22:52:20 +00:00
from homeassistant.helpers.script import Script
DOMAIN = "script"
2015-10-15 06:09:52 +00:00
ENTITY_ID_FORMAT = DOMAIN + '.{}'
DEPENDENCIES = ["group"]
CONF_SEQUENCE = "sequence"
2015-10-15 06:09:52 +00:00
ATTR_VARIABLES = 'variables'
2015-10-15 06:09:52 +00:00
ATTR_LAST_ACTION = 'last_action'
2015-11-14 23:36:27 +00:00
ATTR_CAN_CANCEL = 'can_cancel'
_LOGGER = logging.getLogger(__name__)
_SCRIPT_ENTRY_SCHEMA = vol.Schema({
CONF_ALIAS: cv.string,
2016-04-21 22:52:20 +00:00
vol.Required(CONF_SEQUENCE): cv.SCRIPT_SCHEMA,
})
CONFIG_SCHEMA = vol.Schema({
vol.Required(DOMAIN): {cv.slug: _SCRIPT_ENTRY_SCHEMA}
}, extra=vol.ALLOW_EXTRA)
SCRIPT_SERVICE_SCHEMA = vol.Schema(dict)
SCRIPT_TURN_ONOFF_SCHEMA = vol.Schema({
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
vol.Optional(ATTR_VARIABLES): dict,
})
2015-10-15 06:09:52 +00:00
def is_on(hass, entity_id):
2016-03-08 16:55:57 +00:00
"""Return if the switch is on based on the statemachine."""
2015-10-15 06:09:52 +00:00
return hass.states.is_state(entity_id, STATE_ON)
def turn_on(hass, entity_id, variables=None):
2016-03-07 17:49:31 +00:00
"""Turn script on."""
2015-10-15 06:09:52 +00:00
_, object_id = split_entity_id(entity_id)
hass.services.call(DOMAIN, object_id, variables)
2015-10-15 06:09:52 +00:00
def turn_off(hass, entity_id):
2016-03-07 17:49:31 +00:00
"""Turn script on."""
2015-10-15 06:09:52 +00:00
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
2016-02-21 17:22:38 +00:00
def toggle(hass, entity_id):
2016-03-08 16:55:57 +00:00
"""Toggle the script."""
2016-02-21 17:22:38 +00:00
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
def setup(hass, config):
2016-03-07 17:49:31 +00:00
"""Load the scripts from the configuration."""
2015-10-15 06:09:52 +00:00
component = EntityComponent(_LOGGER, DOMAIN, hass)
def service_handler(service):
2016-03-08 16:55:57 +00:00
"""Execute a service call to script.<script name>."""
2015-10-15 06:09:52 +00:00
entity_id = ENTITY_ID_FORMAT.format(service.service)
script = component.entities.get(entity_id)
if script.is_on:
_LOGGER.warning("Script %s already running.", entity_id)
return
script.turn_on(variables=service.data)
2015-10-15 06:09:52 +00:00
2015-10-28 19:24:33 +00:00
for object_id, cfg in config[DOMAIN].items():
alias = cfg.get(CONF_ALIAS, object_id)
2016-04-21 22:52:20 +00:00
script = ScriptEntity(hass, object_id, alias, cfg[CONF_SEQUENCE])
2015-10-15 06:09:52 +00:00
component.add_entities((script,))
hass.services.register(DOMAIN, object_id, service_handler,
schema=SCRIPT_SERVICE_SCHEMA)
2015-10-15 06:09:52 +00:00
def turn_on_service(service):
2016-03-08 16:55:57 +00:00
"""Call a service to turn script on."""
2015-10-15 06:09:52 +00:00
# We could turn on script directly here, but we only want to offer
# one way to do it. Otherwise no easy way to detect invocations.
2015-10-15 06:09:52 +00:00
for script in component.extract_from_service(service):
turn_on(hass, script.entity_id, service.data.get(ATTR_VARIABLES))
2015-10-15 06:09:52 +00:00
def turn_off_service(service):
2016-03-08 16:55:57 +00:00
"""Cancel a script."""
2015-10-15 06:09:52 +00:00
for script in component.extract_from_service(service):
script.turn_off()
2016-02-21 17:22:38 +00:00
def toggle_service(service):
2016-03-08 16:55:57 +00:00
"""Toggle a script."""
2016-02-21 17:22:38 +00:00
for script in component.extract_from_service(service):
script.toggle()
hass.services.register(DOMAIN, SERVICE_TURN_ON, turn_on_service,
schema=SCRIPT_TURN_ONOFF_SCHEMA)
hass.services.register(DOMAIN, SERVICE_TURN_OFF, turn_off_service,
schema=SCRIPT_TURN_ONOFF_SCHEMA)
hass.services.register(DOMAIN, SERVICE_TOGGLE, toggle_service,
schema=SCRIPT_TURN_ONOFF_SCHEMA)
return True
2016-04-21 22:52:20 +00:00
class ScriptEntity(ToggleEntity):
"""Representation of a script entity."""
2016-03-08 16:55:57 +00:00
2015-10-28 19:24:33 +00:00
# pylint: disable=too-many-instance-attributes
2016-04-21 22:52:20 +00:00
def __init__(self, hass, object_id, name, sequence):
2016-03-08 16:55:57 +00:00
"""Initialize the script."""
2015-10-28 19:24:33 +00:00
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
2016-04-21 22:52:20 +00:00
self.script = Script(hass, sequence, name, self.update_ha_state)
2015-10-15 06:09:52 +00:00
@property
def should_poll(self):
2016-03-07 17:49:31 +00:00
"""No polling needed."""
2015-10-15 06:09:52 +00:00
return False
@property
def name(self):
2016-03-07 17:49:31 +00:00
"""Return the name of the entity."""
2016-04-21 22:52:20 +00:00
return self.script.name
2015-10-15 06:09:52 +00:00
@property
def state_attributes(self):
2016-03-07 17:49:31 +00:00
"""Return the state attributes."""
attrs = {}
2016-04-21 22:52:20 +00:00
if self.script.can_cancel:
attrs[ATTR_CAN_CANCEL] = self.script.can_cancel
if self.script.last_action:
attrs[ATTR_LAST_ACTION] = self.script.last_action
2015-10-15 06:09:52 +00:00
return attrs
@property
def is_on(self):
2016-03-08 16:55:57 +00:00
"""Return true if script is on."""
2016-04-21 22:52:20 +00:00
return self.script.is_running
2015-10-15 06:09:52 +00:00
def turn_on(self, **kwargs):
2016-03-07 17:49:31 +00:00
"""Turn the entity on."""
self.script.run(kwargs.get(ATTR_VARIABLES))
2015-10-15 06:09:52 +00:00
def turn_off(self, **kwargs):
2016-03-07 17:49:31 +00:00
"""Turn script off."""
2016-04-21 22:52:20 +00:00
self.script.stop()