core/homeassistant/helpers
Phil Bruckner b2d7bc40dc
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.
2020-02-24 14:56:00 -08:00
..
__init__.py Type hint improvements (#31876) 2020-02-16 13:47:55 +01:00
aiohttp_client.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
area_registry.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
check_config.py Add Safe Mode (#30723) 2020-01-14 13:03:02 -08:00
collection.py Fix person device_trackers null (#31829) 2020-02-14 15:27:31 -08:00
condition.py Find related items scripts/automations (#31293) 2020-01-29 16:19:13 -08:00
config_entry_flow.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
config_entry_oauth2_flow.py Removes unneeded abort if unique id in Oauth2 discovery flow (#30733) 2020-01-13 05:29:19 -08:00
config_validation.py Improve condition validation error msg (#32135) 2020-02-24 09:59:34 +01:00
data_entry_flow.py Use new custom_serializer (#31871) 2020-02-15 15:36:57 -08:00
debounce.py Update the update coordinator API to make it easier to use (#31471) 2020-02-06 09:29:29 -08:00
deprecation.py Adds guards for missing information in call stack frames (#27217) 2019-10-05 11:59:33 +02:00
device_registry.py Fix callback and async (#31281) 2020-01-29 13:59:45 -08:00
discovery.py Various string cleanups (#30435) 2020-01-03 14:47:06 +01:00
dispatcher.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
entity.py Write state if schedule update state from async context (#31758) 2020-02-13 10:22:06 -08:00
entity_component.py Annotate more async functions correctly (#31802) 2020-02-14 10:00:22 -08:00
entity_platform.py Entity Registry to store and restore name/icon (#31714) 2020-02-11 09:40:50 -08:00
entity_registry.py Fix entity registry not saving name/icon (#31932) 2020-02-18 08:32:34 -08:00
entity_values.py Migrate legacy typehints in core to PEP-526 (#26403) 2019-09-03 20:36:04 -07:00
entityfilter.py Black 2019-07-31 12:25:30 -07:00
event.py Update Hue data fetching (#31338) 2020-01-31 14:47:40 -08:00
icon.py Various string cleanups (#30435) 2020-01-03 14:47:06 +01:00
integration_platform.py Add integration platform helper (#29914) 2019-12-13 15:38:41 +01:00
intent.py Various string cleanups (#30435) 2020-01-03 14:47:06 +01:00
json.py Black 2019-07-31 12:25:30 -07:00
location.py Type 2019-07-31 13:08:31 -07:00
logging.py Fix typos found by codespell (#31243) 2020-01-31 08:33:00 -08:00
network.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
restore_state.py Helpers typing improvements (#31865) 2020-02-15 13:03:53 -08:00
script.py Add support for simultaneous runs of Script helper (#31937) 2020-02-24 14:56:00 -08:00
service.py Clean up frontend services and events (#31654) 2020-02-09 19:47:16 -08:00
signal.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
state.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
storage.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
sun.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
system_info.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
temperature.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
template.py Helpers typing improvements (#31865) 2020-02-15 13:03:53 -08:00
translation.py Various string cleanups (#30435) 2020-01-03 14:47:06 +01:00
typing.py Sort imports according to PEP8 for 'homeassistant' folder (#29789) 2019-12-09 16:42:10 +01:00
update_coordinator.py Add missing name to logging in DataUpdateCoordinator (#32023) 2020-02-20 16:51:15 +01:00