""" homeassistant.components.script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Scripts are a sequence of actions that can be triggered manually by the user or automatically based upon automation events, etc. For more details about this component, please refer to the documentation at https://home-assistant.io/components/script/ """ import logging import threading from datetime import timedelta from itertools import islice import homeassistant.util.dt as date_util from homeassistant.const import ( ATTR_ENTITY_ID, EVENT_TIME_CHANGED, SERVICE_TURN_OFF, SERVICE_TURN_ON, SERVICE_TOGGLE, STATE_ON) from homeassistant.helpers.entity import ToggleEntity, split_entity_id from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.event import track_point_in_utc_time from homeassistant.helpers.service import call_from_config from homeassistant.util import slugify DOMAIN = "script" ENTITY_ID_FORMAT = DOMAIN + '.{}' DEPENDENCIES = ["group"] STATE_NOT_RUNNING = 'Not Running' CONF_ALIAS = "alias" CONF_SERVICE = "service" CONF_SERVICE_OLD = "execute_service" CONF_SERVICE_DATA = "data" CONF_SERVICE_DATA_OLD = "service_data" CONF_SEQUENCE = "sequence" CONF_EVENT = "event" CONF_EVENT_DATA = "event_data" CONF_DELAY = "delay" ATTR_LAST_ACTION = 'last_action' ATTR_CAN_CANCEL = 'can_cancel' _LOGGER = logging.getLogger(__name__) def is_on(hass, entity_id): """ Returns if the switch is on based on the statemachine. """ return hass.states.is_state(entity_id, STATE_ON) def turn_on(hass, entity_id): """ Turn script on. """ _, object_id = split_entity_id(entity_id) hass.services.call(DOMAIN, object_id) def turn_off(hass, entity_id): """ Turn script on. """ hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}) def toggle(hass, entity_id): """ Toggles script. """ hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id}) def setup(hass, config): """ Load the scripts from the configuration. """ component = EntityComponent(_LOGGER, DOMAIN, hass) def service_handler(service): """ Execute a service call to script.