Add support for simultaneous runs of Script helper (#31937)

* Add tests for legacy Script helper behavior

* Add Script helper if_running and run_mode options

- if_running controls what happens if Script run while previous run
  has not completed. Can be:
  - error: Raise an exception
  - ignore: Return without doing anything (previous run continues as-is)
  - parallel: Start run in new task
  - restart: Stop previous run before starting new run
- run_mode controls when call to async_run will return. Can be:
  - background: Returns immediately
  - legacy: Implements previous behavior, which is to return when done,
            or when suspended by delay or wait_template
  - blocking: Returns when run has completed
- If neither is specified, default is run_mode=legacy (and if_running
  is not used.) Otherwise, defaults are if_running=parallel and
  run_mode=background. If run_mode is set to legacy then if_running must
  be None.
- Caller may supply a logger which will be used throughout instead of
  default module logger.
- Move Script running state into new helper classes, comprised of an
  abstract base class and two concrete clases, one for legacy behavior
  and one for new behavior.
- Remove some non-async methods, as well as call_from_config which has
  only been used in tests.
- Adjust tests accordingly.

* Change per review

- Change run_mode default from background to blocking.
- Make sure change listener is called, even when there's an unexpected
  exception.
- Make _ScriptRun.async_stop more graceful by using an asyncio.Event for
  signaling instead of simply cancelling Task.
- Subclass _ScriptRun for background & blocking behavior.

Also:

- Fix timeouts in _ScriptRun by converting timedeltas to float seconds.
- General cleanup.

* Change per review 2

- Don't propagate exceptions if call from user has already returned
  (i.e., for background runs or legacy runs that have suspended.)
- Allow user to specify if exceptions should be logged. They will still
  be logged regardless if exception is not propagated.
- Rename _start_script_delay and _start_wait_template_delay for
  clarity.
- Remove return value from Script.async_run.
- Fix missing await.
- Change call to self.is_running in Script.async_run to direct test of
  self._runs.

* Change per review 3 and add tests

- Remove Script.set_logger().
- Enhance existing tests to check all run modes.
- Add tests for new features.
- Fix a few minor bugs found by tests.
This commit is contained in:
Phil Bruckner authored and GitHub committed 2020-02-24 14:56:00 -08:00
1 parent 309989be89
commit b2d7bc40dc
5 files changed
+1774 -881

No files matched your search

+6 -11
View File
@@ -242,7 +242,9 @@ class ScriptEntity(ToggleEntity):
self.object_id = object_id
self.icon = icon
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, logger=_LOGGER
)
@property
def should_poll(self):
@@ -279,22 +281,15 @@ class ScriptEntity(ToggleEntity):
{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)
async def async_turn_off(self, **kwargs):
"""Turn script off."""
self.script.async_stop()
await self.script.async_stop()
async def async_will_remove_from_hass(self):
"""Stop script and remove service when it will be removed from Home Assistant."""
if self.script.is_running:
self.script.async_stop()
await self.script.async_stop()
# remove service
self.hass.services.async_remove(DOMAIN, self.object_id)