From 693b38ef513bcca9c877bb062d9e15e940c7c07e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 14 Aug 2019 20:50:06 +0200 Subject: [PATCH] Cache the BME280 object instead of initializing it on each get_measurement call The BME280 needs some warmup time before picking up the results. --- platypush/plugins/gpio/sensor/bme280.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/platypush/plugins/gpio/sensor/bme280.py b/platypush/plugins/gpio/sensor/bme280.py index 7a2cb5074e..996162ded3 100644 --- a/platypush/plugins/gpio/sensor/bme280.py +++ b/platypush/plugins/gpio/sensor/bme280.py @@ -19,9 +19,21 @@ class GpioSensorBme280Plugin(GpioSensorPlugin): super().__init__(**kwargs) self.port = port + self._bus = None + self._device = None # noinspection PyPackageRequirements # noinspection PyUnresolvedReferences + def _get_device(self): + if self._device: + return self._device + + from smbus import SMBus + from bme280 import BME280 + + self._bus = SMBus(self.port) + self._device = BME280(i2c_dev=self._bus) + @action def get_measurement(self): """ @@ -35,11 +47,7 @@ class GpioSensorBme280Plugin(GpioSensorPlugin): """ - from smbus import SMBus - from bme280 import BME280 - - bus = SMBus(self.port) - device = BME280(i2c_dev=bus) + device = self._get_device() return { 'temperature': device.get_temperature(), 'pressure': device.get_pressure()*100,