From 4a34fcd7fd7386a8efdc6729352aedeba3b716f7 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 14 Aug 2019 22:45:50 +0200 Subject: [PATCH] More robust sensor tolerance measurement in case of non-numeric sensor data --- platypush/backend/sensor/__init__.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/platypush/backend/sensor/__init__.py b/platypush/backend/sensor/__init__.py index 54ec26c3..522b3b46 100644 --- a/platypush/backend/sensor/__init__.py +++ b/platypush/backend/sensor/__init__.py @@ -118,15 +118,25 @@ class SensorBackend(Backend): if v is None: continue - if isinstance(self.tolerance, dict): - tolerance = float(self.tolerance.get(k, self.default_tolerance)) - else: - try: - tolerance = float(self.tolerance) - except (TypeError, ValueError): - continue + tolerance = None + isNaN = False - if abs(v - self.data.get(k)) >= tolerance: + try: + v = float(v) + except (TypeError, ValueError): + isNaN = True + + if not isNaN: + if isinstance(self.tolerance, dict): + tolerance = float(self.tolerance.get(k, self.default_tolerance)) + else: + try: + tolerance = float(self.tolerance) + except (TypeError, ValueError): + pass + + if tolerance is None or isNaN or \ + abs(v - self.data.get(k)) >= tolerance: ret[k] = v return ret