Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions homeassistant/components/automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
)
from homeassistant.core import Context, CoreState, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import condition, extract_domain_configs, script
from homeassistant.helpers import condition, extract_domain_configs
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.script import SCRIPT_PARALLEL_CHOICES, Script
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.typing import TemplateVarsType
from homeassistant.loader import bind_hass
from homeassistant.util.dt import parse_datetime, utcnow
from homeassistant.util.dt import parse_datetime

# mypy: allow-untyped-calls, allow-untyped-defs
# mypy: no-check-untyped-defs, no-warn-return-any
Expand All @@ -50,6 +51,7 @@
CONF_TRIGGER = "trigger"
CONF_CONDITION_TYPE = "condition_type"
CONF_INITIAL_STATE = "initial_state"
CONF_PARALLEL_ACTION = "parallel_action"
CONF_SKIP_CONDITION = "skip_condition"

CONDITION_USE_TRIGGER_VALUES = "use_trigger_values"
Expand Down Expand Up @@ -106,6 +108,7 @@ def _platform_validator(config):
vol.Required(CONF_TRIGGER): _TRIGGER_SCHEMA,
vol.Optional(CONF_CONDITION): _CONDITION_SCHEMA,
vol.Required(CONF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_PARALLEL_ACTION): vol.In(SCRIPT_PARALLEL_CHOICES),
}
),
)
Expand Down Expand Up @@ -394,13 +397,16 @@ async def async_trigger(self, variables, skip_condition=False, context=None):
_LOGGER.info("Executing %s", self._name)

try:
await self.action_script.async_run(variables, trigger_context)
except Exception as err: # pylint: disable=broad-except
self.action_script.async_log_exception(
_LOGGER, f"Error while executing automation {self.entity_id}", err
await self.action_script.async_run(
variables,
trigger_context,
_LOGGER,
f"Error while executing automation {self.entity_id}",
Comment thread
pnbruckner marked this conversation as resolved.
)
except Exception: # pylint: disable=broad-except
pass

self._last_triggered = utcnow()
self._last_triggered = self.action_script.last_triggered
await self.async_update_ha_state()

async def async_will_remove_from_hass(self):
Expand Down Expand Up @@ -508,7 +514,12 @@ async def _async_process_config(hass, config, component):
hidden = config_block[CONF_HIDE_ENTITY]
initial_state = config_block.get(CONF_INITIAL_STATE)

action_script = script.Script(hass, config_block.get(CONF_ACTION, {}), name)
action_script = Script(
hass,
config_block.get(CONF_ACTION, {}),
name,
mode=config_block.get(CONF_PARALLEL_ACTION),
)

if CONF_CONDITION in config_block:
cond_func = await _async_process_if(hass, config, config_block)
Expand Down
19 changes: 10 additions & 9 deletions homeassistant/components/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from homeassistant.helpers.config_validation import make_entity_service_schema
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.script import Script
from homeassistant.helpers.script import SCRIPT_PARALLEL_ERROR, Script
from homeassistant.helpers.service import async_set_service_schema
from homeassistant.loader import bind_hass

Expand Down Expand Up @@ -231,7 +231,9 @@ def __init__(self, hass, object_id, name, sequence):
"""Initialize the script."""
self.object_id = object_id
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
self.script = Script(hass, sequence, name, self.async_update_ha_state)
self.script = Script(
hass, sequence, name, self.async_update_ha_state, SCRIPT_PARALLEL_ERROR
Comment thread
pnbruckner marked this conversation as resolved.
)

@property
def should_poll(self):
Expand Down Expand Up @@ -268,13 +270,12 @@ async def async_turn_on(self, **kwargs):
{ATTR_NAME: self.script.name, ATTR_ENTITY_ID: self.entity_id},
context=context,
)
try:
await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
except Exception as err:
self.script.async_log_exception(
_LOGGER, f"Error executing script {self.entity_id}", err
)
raise err
await self.script.async_run(
kwargs.get(ATTR_VARIABLES),
context,
_LOGGER,
f"Error executing script {self.entity_id}",
)

async def async_turn_off(self, **kwargs):
"""Turn script off."""
Expand Down
Loading