aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md6
-rw-r--r--automato/action.py26
2 files changed, 30 insertions, 2 deletions
diff --git a/README.md b/README.md
index 4058db8..54e6566 100644
--- a/README.md
+++ b/README.md
@@ -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: