diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2023-03-09 21:20:55 +0100 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2023-03-09 21:20:55 +0100 |
commit | 4a299877d9e21f7d5d3ed860a4e8ba0bee74c226 (patch) | |
tree | e44543c948fd37c1ad1dd77e77aff2e27af044a3 | |
parent | cf1425a33a857d386bb3dec8fb6b57fe2bc3ef92 (diff) | |
download | automato-4a299877d9e21f7d5d3ed860a4e8ba0bee74c226.tar.gz |
add repeat and cooldown options to action
repeat: Specifies wether the condition needs to cycle through false to
run agian
cooldown: time in seconds the action won't run again after executing
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | automato/action.py | 26 |
2 files changed, 30 insertions, 2 deletions
@@ -40,6 +40,10 @@ but still inherit the globally set ones. An *If-This-Then-That* style set of *triggers* and *commands*. If all *triggers* are in a triggered state, the commands are executed. +If `repeat` is `False`, the evaluation must cycle through `False` to run again. +`cooldown` specifies the time in seconds the action will wait after running again, +even if conditions are met. +Both `repeat` and `cooldown` are optional and their defaults are `True` and `0`. ## Configuration @@ -75,6 +79,8 @@ conditional: `actions.yml` ```yaml send-hello: + repeat: False + cooldown: 30 trigger: - conditional: interval: 30 diff --git a/automato/action.py b/automato/action.py index 785a0ee..822f2e4 100644 --- a/automato/action.py +++ b/automato/action.py @@ -1,13 +1,18 @@ from typing import Dict import logging -logger = logging.getLogger(__name__) +import time from . import endpoint from . import trigger +logger = logging.getLogger(__name__) + class Action: # TODO: Cooldown, wait fot state change, repeat, etc? - def __init__(self, name: str, config: dict, endpoints: Dict[str, endpoint.Endpoint], triggers: Dict[str, trigger.Trigger]): + def __init__( + self, name: str, config: dict, endpoints: Dict[str, endpoint.Endpoint], + triggers: Dict[str, trigger.Trigger] + ): self._name = name self._trigger_cfg = config['trigger'] self._then_cfg = config['then'] @@ -15,6 +20,11 @@ class Action: self._endpoints = endpoints self._triggers = triggers + self._repeat = config['repeat'] if 'repeat' in config else True + self._cooldown = config['cooldown'] if 'cooldown' in config else 0 + self._last_run = 0 + self._last_state = False + self._configured_trigger_keys = [] self._setup_triggers() @@ -39,9 +49,21 @@ class Action: def execute(self): if not all([self._triggers[b].evaluate(self._name) for b in self._configured_trigger_keys]): + self._last_state = False logger.debug(f'Action "{self._name}" will not execute. Conditions not met.') return + if self._last_state and not self._repeat: + logger.debug(f'Action "{self._name}": Conditions are met but won\'t repeat') + return + + if time.time() - self._last_run <= self._cooldown: + logger.debug(f'Action "{self._name}": Conditions are met but cooldown time not reached') + return + + self._last_run = time.time() + self._last_state = True + logger.info(f'Executing Action "{self._name}". Conditions are met.') for then_item in self._then_cfg: |