From fcdda40c4a99de5c0c2337f6e35d83f219aee4c7 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 2 Apr 2023 12:09:45 +0200 Subject: [PATCH] Update the `_last_measurement` only if some events were processed from the new data. --- platypush/plugins/sensor/__init__.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/platypush/plugins/sensor/__init__.py b/platypush/plugins/sensor/__init__.py index d94f9815c..4c5c710d8 100644 --- a/platypush/plugins/sensor/__init__.py +++ b/platypush/plugins/sensor/__init__.py @@ -276,17 +276,20 @@ class SensorPlugin(RunnablePlugin, SensorEntityManager, ABC): def _process_sensor_events( self, old_data: Optional[SensorDataType], new_data: Optional[SensorDataType] - ): + ) -> bool: """ Given the previous and new measurement, it runs the comparison logic against the configured tolerance values and thresholds, and it processes the required sensor data change and above/below threshold events. + + :return: ``True`` if some events were processed for the new data, + ``False`` otherwise. """ # If the new data is missing or there are no changes, there are no # events to process if new_data is None or not self._has_changes(old_data, new_data): - return + return False events = [ SensorDataChangeEvent( @@ -300,6 +303,7 @@ class SensorPlugin(RunnablePlugin, SensorEntityManager, ABC): get_bus().post(event) self.publish_entities(new_data) + return True def _update_last_measurement(self, new_data: SensorDataType): """ @@ -415,8 +419,11 @@ class SensorPlugin(RunnablePlugin, SensorEntityManager, ABC): continue new_data = self._filter_enabled_sensors(new_data) - self._process_sensor_events(self._last_measurement, new_data) - self._update_last_measurement(new_data) + is_event_processed = self._process_sensor_events( + self._last_measurement, new_data + ) + if is_event_processed: + self._update_last_measurement(new_data) self.wait_stop(self.poll_interval)