This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+58 -48
View File
@@ -5,9 +5,16 @@ import logging
import voluptuous as vol
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON,
SERVICE_TOGGLE, SERVICE_RELOAD, STATE_ON, CONF_ALIAS,
EVENT_SCRIPT_STARTED, ATTR_NAME)
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
SERVICE_TOGGLE,
SERVICE_RELOAD,
STATE_ON,
CONF_ALIAS,
EVENT_SCRIPT_STARTED,
ATTR_NAME,
)
from homeassistant.loader import bind_hass
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent
@@ -18,31 +25,30 @@ from homeassistant.helpers.script import Script
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'script'
ATTR_CAN_CANCEL = 'can_cancel'
ATTR_LAST_ACTION = 'last_action'
ATTR_LAST_TRIGGERED = 'last_triggered'
ATTR_VARIABLES = 'variables'
DOMAIN = "script"
ATTR_CAN_CANCEL = "can_cancel"
ATTR_LAST_ACTION = "last_action"
ATTR_LAST_TRIGGERED = "last_triggered"
ATTR_VARIABLES = "variables"
CONF_SEQUENCE = 'sequence'
CONF_SEQUENCE = "sequence"
ENTITY_ID_FORMAT = DOMAIN + '.{}'
ENTITY_ID_FORMAT = DOMAIN + ".{}"
GROUP_NAME_ALL_SCRIPTS = 'all scripts'
GROUP_NAME_ALL_SCRIPTS = "all scripts"
SCRIPT_ENTRY_SCHEMA = vol.Schema({
CONF_ALIAS: cv.string,
vol.Required(CONF_SEQUENCE): cv.SCRIPT_SCHEMA,
})
SCRIPT_ENTRY_SCHEMA = vol.Schema(
{CONF_ALIAS: cv.string, vol.Required(CONF_SEQUENCE): cv.SCRIPT_SCHEMA}
)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: cv.schema_with_slug_keys(SCRIPT_ENTRY_SCHEMA)
}, extra=vol.ALLOW_EXTRA)
CONFIG_SCHEMA = vol.Schema(
{DOMAIN: cv.schema_with_slug_keys(SCRIPT_ENTRY_SCHEMA)}, extra=vol.ALLOW_EXTRA
)
SCRIPT_SERVICE_SCHEMA = vol.Schema(dict)
SCRIPT_TURN_ONOFF_SCHEMA = ENTITY_SERVICE_SCHEMA.extend({
vol.Optional(ATTR_VARIABLES): dict,
})
SCRIPT_TURN_ONOFF_SCHEMA = ENTITY_SERVICE_SCHEMA.extend(
{vol.Optional(ATTR_VARIABLES): dict}
)
RELOAD_SERVICE_SCHEMA = vol.Schema({})
@@ -55,7 +61,8 @@ def is_on(hass, entity_id):
async def async_setup(hass, config):
"""Load the scripts from the configuration."""
component = EntityComponent(
_LOGGER, DOMAIN, hass, group_name=GROUP_NAME_ALL_SCRIPTS)
_LOGGER, DOMAIN, hass, group_name=GROUP_NAME_ALL_SCRIPTS
)
await _async_process_config(hass, config, component)
@@ -73,8 +80,9 @@ async def async_setup(hass, config):
# one way to do it. Otherwise no easy way to detect invocations.
var = service.data.get(ATTR_VARIABLES)
for script in await component.async_extract_from_service(service):
await hass.services.async_call(DOMAIN, script.object_id, var,
context=service.context)
await hass.services.async_call(
DOMAIN, script.object_id, var, context=service.context
)
async def turn_off_service(service):
"""Cancel a script."""
@@ -84,30 +92,32 @@ async def async_setup(hass, config):
if not scripts:
return
await asyncio.wait([
script.async_turn_off() for script
in scripts
])
await asyncio.wait([script.async_turn_off() for script in scripts])
async def toggle_service(service):
"""Toggle a script."""
for script in await component.async_extract_from_service(service):
await script.async_toggle(context=service.context)
hass.services.async_register(DOMAIN, SERVICE_RELOAD, reload_service,
schema=RELOAD_SERVICE_SCHEMA)
hass.services.async_register(DOMAIN, SERVICE_TURN_ON, turn_on_service,
schema=SCRIPT_TURN_ONOFF_SCHEMA)
hass.services.async_register(DOMAIN, SERVICE_TURN_OFF, turn_off_service,
schema=SCRIPT_TURN_ONOFF_SCHEMA)
hass.services.async_register(DOMAIN, SERVICE_TOGGLE, toggle_service,
schema=SCRIPT_TURN_ONOFF_SCHEMA)
hass.services.async_register(
DOMAIN, SERVICE_RELOAD, reload_service, schema=RELOAD_SERVICE_SCHEMA
)
hass.services.async_register(
DOMAIN, SERVICE_TURN_ON, turn_on_service, schema=SCRIPT_TURN_ONOFF_SCHEMA
)
hass.services.async_register(
DOMAIN, SERVICE_TURN_OFF, turn_off_service, schema=SCRIPT_TURN_ONOFF_SCHEMA
)
hass.services.async_register(
DOMAIN, SERVICE_TOGGLE, toggle_service, schema=SCRIPT_TURN_ONOFF_SCHEMA
)
return True
async def _async_process_config(hass, config, component):
"""Process script configuration."""
async def service_handler(service):
"""Execute a service call to script.<script name>."""
entity_id = ENTITY_ID_FORMAT.format(service.service)
@@ -115,8 +125,7 @@ async def _async_process_config(hass, config, component):
if script.is_on:
_LOGGER.warning("Script %s already running.", entity_id)
return
await script.async_turn_on(variables=service.data,
context=service.context)
await script.async_turn_on(variables=service.data, context=service.context)
scripts = []
@@ -125,7 +134,8 @@ async def _async_process_config(hass, config, component):
script = ScriptEntity(hass, object_id, alias, cfg[CONF_SEQUENCE])
scripts.append(script)
hass.services.async_register(
DOMAIN, object_id, service_handler, schema=SCRIPT_SERVICE_SCHEMA)
DOMAIN, object_id, service_handler, schema=SCRIPT_SERVICE_SCHEMA
)
await component.async_add_entities(scripts)
@@ -167,19 +177,19 @@ class ScriptEntity(ToggleEntity):
async def async_turn_on(self, **kwargs):
"""Turn the script on."""
context = kwargs.get('context')
context = kwargs.get("context")
self.async_set_context(context)
self.hass.bus.async_fire(EVENT_SCRIPT_STARTED, {
ATTR_NAME: self.script.name,
ATTR_ENTITY_ID: self.entity_id,
}, context=context)
self.hass.bus.async_fire(
EVENT_SCRIPT_STARTED,
{ATTR_NAME: self.script.name, ATTR_ENTITY_ID: self.entity_id},
context=context,
)
try:
await self.script.async_run(
kwargs.get(ATTR_VARIABLES), context)
await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
except Exception as err: # pylint: disable=broad-except
self.script.async_log_exception(
_LOGGER, "Error executing script {}".format(self.entity_id),
err)
_LOGGER, "Error executing script {}".format(self.entity_id), err
)
raise err
async def async_turn_off(self, **kwargs):