Skip to content

Commit

Permalink
Issue #93: Use THEN to execute (execute action in device)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmo committed Oct 23, 2018
1 parent 547f34f commit 64e04ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
25 changes: 24 additions & 1 deletion amazon_dash/action.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
from typing import List


COMPLETE_CONDITION = 'complete'
ACTION_STATUSES = ['failure', 'success']
ALL_COMPLETE_CONDITIONS = [COMPLETE_CONDITION] + ACTION_STATUSES


class Action:
def __init__(self, use, params=None, condition=None, template=None):
self.use = use
self.params = params
self.condition = condition
self.condition = condition.lower() if condition else condition
self.template = template

def is_first_run_action(self):
return self.condition not in ALL_COMPLETE_CONDITIONS

def evaluate_condition(self, value):
return self.condition == value or (value in ACTION_STATUSES and self.condition == COMPLETE_CONDITION)


class Actions:
def __init__(self, actions):
self._actions: List[Action] = [Action(**action) for action in actions]

def get_first_run_actions(self):
return filter(lambda x: x.is_first_run_action(), self._actions)

def get_complete_actions(self):
return filter(lambda x: not x.is_first_run_action(), self._actions)
47 changes: 35 additions & 12 deletions amazon_dash/device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from amazon_dash.action import Action
from dataclasses import dataclass
from typing import Union

from amazon_dash.action import Action, Actions
from amazon_dash.confirmations import get_confirmation
from amazon_dash.exceptions import InvalidConfig
from amazon_dash.execute import ExecuteCmd, ExecuteUrl, ExecuteHomeAssistant, ExecuteOpenHab, ExecuteIFTTT, logger
Expand All @@ -12,10 +15,18 @@
}


@dataclass
class Result:
status: Union[bool, None] = None
message: str = ''
exception: Exception = None


class Device(object):
"""Set the execution method for the device
"""
execute_instance = None #: Execute cls instance
on_error = 'fail'

def __init__(self, src, name, actions, config=None):
"""
Expand All @@ -30,7 +41,7 @@ def __init__(self, src, name, actions, config=None):
src = src.src
self.src = src.lower()
self.name = name or self.src
self.actions = [Action(**action) for action in actions]
self.actions = Actions(actions)
self.config = config

def execute(self, root_allowed=False):
Expand All @@ -40,21 +51,33 @@ def execute(self, root_allowed=False):
:return: None
"""
logger.debug('%s device executed (mac %s)', self.name, self.src)
if not self.execute_instance:
msg = '%s: There is not execution method in device conf.'
if not self.actions:
msg = '%s: There is not actions in device conf.'
logger.warning(msg, self.name)
self.send_confirmation(msg % self.name, False)
# self.send_confirmation(msg % self.name, False)
return
success = True
results = []
for action in self.actions.get_first_run_actions():
result = self.execute_action(action)
success = result.status
results.append(result)
if not success and self.on_error == 'fail':
break
for action in self.actions.get_complete_actions():
if action.evaluate_condition('success' if success else 'failure'):
self.execute_action(action)
return results

def execute_action(self, action):
result = Result()
try:
result = self.execute_instance.execute(root_allowed)
result.message = action.send()
except Exception as e:
self.send_confirmation('Error executing the device {}: {}'.format(self.name, e), False)
raise
result.message = 'Error executing the device {} in action {}: {}'.format(self.name, action, e)
result.exception = e
else:
result = 'The {} device has been started and is running right now'.format(self.name) \
if result is None else result
result = result or 'The {} device has been executed successfully'.format(self.name)
self.send_confirmation(result)
result.status = True
return result

def send_confirmation(self, message, success=True):
Expand Down

0 comments on commit 64e04ac

Please sign in to comment.