From 6e867e9fb28f6ff07845aa84ea36b1b7e9220568 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 25 Aug 2018 12:29:20 +0200 Subject: [PATCH] Support for multiple thresholds for the same sensor --- platypush/backend/sensor/__init__.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/platypush/backend/sensor/__init__.py b/platypush/backend/sensor/__init__.py index 08e883178..1ce1f86f2 100644 --- a/platypush/backend/sensor/__init__.py +++ b/platypush/backend/sensor/__init__.py @@ -54,16 +54,20 @@ class SensorBackend(Backend): if self.thresholds: if isinstance(self.thresholds, dict) and isinstance(new_data, dict): - for (measure, threshold) in self.thresholds.items(): + for (measure, thresholds) in self.thresholds.items(): if measure not in new_data: continue - if new_data[measure] > threshold and (self.data is None or ( - measure in self.data and self.data[measure] <= threshold)): - data_above_threshold[measure] = new_data[measure] - elif new_data[measure] < threshold and (self.data is None or ( - measure in self.data and self.data[measure] >= threshold)): - data_below_threshold[measure] = new_data[measure] + if not isinstance(thresholds, list): + thresholds = [thresholds] + + for threshold in thresholds: + if new_data[measure] > threshold and (self.data is None or ( + measure in self.data and self.data[measure] <= threshold)): + data_above_threshold[measure] = new_data[measure] + elif new_data[measure] < threshold and (self.data is None or ( + measure in self.data and self.data[measure] >= threshold)): + data_below_threshold[measure] = new_data[measure] if data_below_threshold: self.bus.post(SensorDataBelowThresholdEvent(data=data_below_threshold))