From 533a81b73ab14e78822e0b7f9378be97bc25ac35 Mon Sep 17 00:00:00 2001 From: Nekmo Date: Wed, 24 Oct 2018 01:16:50 +0200 Subject: [PATCH] Issue #93: Use THEN to execute (send method action) --- amazon_dash/action.py | 13 ++++++++++--- amazon_dash/config.py | 3 ++- amazon_dash/device.py | 14 ++++++++------ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/amazon_dash/action.py b/amazon_dash/action.py index cad5694..f0d4691 100644 --- a/amazon_dash/action.py +++ b/amazon_dash/action.py @@ -7,11 +7,12 @@ class Action: - def __init__(self, use, params=None, condition=None, template=None): + def __init__(self, use, config=None, params=None, condition=None, template=None): self.use = use self.params = params self.condition = condition.lower() if condition else condition self.template = template + self.config = config def is_first_run_action(self): return self.condition not in ALL_COMPLETE_CONDITIONS @@ -19,10 +20,16 @@ def is_first_run_action(self): def evaluate_condition(self, value): return self.condition == value or (value in ACTION_STATUSES and self.condition == COMPLETE_CONDITION) + def send(self, **params): + new_params = self.params.copy() + new_params.update(params) + self.config.templates.use(self.use).render(**new_params).send() + class Actions: - def __init__(self, actions): - self._actions: List[Action] = [Action(**action) for action in actions] + def __init__(self, actions, config): + self._actions: List[Action] = [Action(config=config, **action) for action in actions] + self.config = config def get_first_run_actions(self): return filter(lambda x: x.is_first_run_action(), self._actions) diff --git a/amazon_dash/config.py b/amazon_dash/config.py index 596979a..e08549a 100644 --- a/amazon_dash/config.py +++ b/amazon_dash/config.py @@ -239,7 +239,8 @@ def read(self): self.templates = then.templates(LoadTemplates(self.file)) devices = LoadConfig(self.file, 'devices') devices = {device['mac']: device for device in devices.data} if isinstance(devices.data, list) else devices - devices = {key: Device(key, value.get('name'), value.get('actions', [])) for key, value in devices.items()} + devices = {key: Device(key, value.get('name'), value.get('actions', []), self) + for key, value in devices.items()} self.devices = devices pass # try: diff --git a/amazon_dash/device.py b/amazon_dash/device.py index ef43402..5dc95e0 100644 --- a/amazon_dash/device.py +++ b/amazon_dash/device.py @@ -34,14 +34,14 @@ def __init__(self, src, name, actions, config=None): :param str src: Mac address :param data: device data """ - config = config or {} + config = config actions = actions or [] if isinstance(src, Device): src = src.src self.src = src.lower() self.name = name or self.src - self.actions = Actions(actions) + self.actions = Actions(actions, config) self.config = config def execute(self, root_allowed=False): @@ -59,7 +59,7 @@ def execute(self, root_allowed=False): success = True results = [] for action in self.actions.get_first_run_actions(): - result = self.execute_action(action) + result = self.execute_action(action, {'mac': self.src}) success = result.status results.append(result) if not success and self.on_error == 'fail': @@ -69,11 +69,13 @@ def execute(self, root_allowed=False): self.execute_action(action) return results - def execute_action(self, action): + def execute_action(self, action, params=None): result = Result() + params = params or {} try: - result.message = action.send() - except Exception as e: + result.message = action.send(**params) + # except Exception as e: # TODO: disabled temporally + except ImportError as e: result.message = 'Error executing the device {} in action {}: {}'.format(self.name, action, e) result.exception = e else: