From 42fa8360f3141b90f4993450c96e06200280bf53 Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <blacklight86@gmail.com>
Date: Sun, 15 Jul 2018 17:28:13 +0200
Subject: [PATCH] Only propagate the values above/below threshold in case of a
 sensor threshold crossing, excluding the sensors that haven't crossed the
 threshold

---
 platypush/backend/sensor/__init__.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/platypush/backend/sensor/__init__.py b/platypush/backend/sensor/__init__.py
index db1d87536..4b272e997 100644
--- a/platypush/backend/sensor/__init__.py
+++ b/platypush/backend/sensor/__init__.py
@@ -49,6 +49,9 @@ class SensorBackend(Backend):
             if self.data is None or self.data != new_data:
                 self.bus.post(SensorDataChangeEvent(data=new_data))
 
+            data_below_threshold = {}
+            data_above_threshold = {}
+
             if self.thresholds:
                 if isinstance(self.thresholds, dict) and isinstance(new_data, dict):
                     for (measure, threshold) in self.thresholds.items():
@@ -57,15 +60,22 @@ class SensorBackend(Backend):
 
                         if new_data[measure] > threshold and (self.data is None or (
                                 measure in self.data and self.data[measure] <= threshold)):
-                            self.bus.post(SensorDataAboveThresholdEvent(data=new_data))
+                            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)):
-                            self.bus.post(SensorDataBelowThresholdEvent(data=new_data))
+                            data_below_threshold[measure] = new_data[measure]
                 else:
                     if new_data > threshold and (self.data is None or self.data <= threshold):
-                        self.bus.post(SensorDataAboveThresholdEvent(data=new_data))
+                        data_above_threshold = new_data
                     elif new_data < threshold and (self.data is None or self.data >= threshold):
-                        self.bus.post(SensorDataBelowThresholdEvent(data=new_data))
+                        data_below_threshold = new_data
+
+            if data_below_threshold:
+                self.bus.post(SensorDataBelowThresholdEvent(data=data_below_threshold))
+
+            if data_above_threshold:
+                self.bus.post(SensorDataAboveThresholdEvent(data=data_above_threshold))
+
 
             self.data = new_data