Migrated sensor.bme280 to the new SensorPlugin interface.

Removed the old `backend.sensor.bme280` and the old `gpio.sensor.bme280`
plugin. They have now been merged into the new `sensor.bme280` runnable
plugin, which extends the `SensorPlugin` API and supports entities.
This commit is contained in:
Fabio Manganiello 2023-04-01 22:31:24 +02:00
parent 6f237a1500
commit 5dabfed365
Signed by: blacklight
GPG key ID: D90FBA7F76362774
6 changed files with 165 additions and 122 deletions

View file

@ -1,39 +0,0 @@
from platypush.backend.sensor import SensorBackend
class SensorBme280Backend(SensorBackend):
"""
Backend to poll analog sensor values from a `BME280 <https://shop.pimoroni.com/products/bme280-breakout>`_
environment sensor
Requires:
* ``pimoroni-bme280`` (``pip install pimoroni-bme280``)
Triggers:
* :class:`platypush.message.event.sensor.SensorDataChangeEvent` if the measurements of a sensor have changed
* :class:`platypush.message.event.sensor.SensorDataAboveThresholdEvent` if the measurements of a sensor have
gone above a configured threshold
* :class:`platypush.message.event.sensor.SensorDataBelowThresholdEvent` if the measurements of a sensor have
gone below a configured threshold
"""
def __init__(self, temperature=True, pressure=True, humidity=True, **kwargs):
"""
:param temperature: Enable temperature sensor polling
:param pressure: Enable pressure sensor polling
:param humidity: Enable humidity sensor polling
"""
enabled_sensors = {
'temperature': temperature,
'pressure': pressure,
'humidity': humidity,
}
super().__init__(plugin='gpio.sensor.bme280', enabled_sensors=enabled_sensors, **kwargs)
# vim:sw=4:ts=4:et:

View file

@ -1,13 +0,0 @@
manifest:
events:
platypush.message.event.sensor.SensorDataAboveThresholdEvent: if the measurements
of a sensor havegone above a configured threshold
platypush.message.event.sensor.SensorDataBelowThresholdEvent: if the measurements
of a sensor havegone below a configured threshold
platypush.message.event.sensor.SensorDataChangeEvent: if the measurements of a
sensor have changed
install:
pip:
- pimoroni-bme280
package: platypush.backend.sensor.bme280
type: backend

View file

@ -1,63 +0,0 @@
from platypush.plugins import action
from platypush.plugins.gpio.sensor import GpioSensorPlugin
class GpioSensorBme280Plugin(GpioSensorPlugin):
"""
Plugin to interact with a `BME280 <https://shop.pimoroni.com/products/bme280-breakout>`_ environment sensor for
temperature, humidity and pressure measurements over I2C interface
Requires:
* ``pimoroni-bme280`` (``pip install pimoroni-bme280``)
"""
def __init__(self, port=1, **kwargs):
"""
:param port: I2C port. 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
"""
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)
return self._device
@action
def get_measurement(self):
"""
:returns: dict. Example:
.. code-block:: python
output = {
"temperature": 21.0, # Celsius
"pressure": 101555.08, # Pascals
"humidity": 23.543, # percentage
"altitude": 15.703 # meters
}
"""
device = self._get_device()
return {
'temperature': device.get_temperature(),
'pressure': device.get_pressure()*100,
'humidity': device.get_humidity(),
'altitude': device.get_altitude(),
}
# vim:sw=4:ts=4:et:

View file

@ -1,7 +0,0 @@
manifest:
events: {}
install:
pip:
- pimoroni-bme280
package: platypush.plugins.gpio.sensor.bme280
type: plugin

View file

@ -0,0 +1,155 @@
from dataclasses import dataclass
from typing import Dict, List, Type
from typing_extensions import override
from platypush.common.sensors import Numeric
from platypush.entities.devices import Device
from platypush.entities.distance import DistanceSensor
from platypush.entities.humidity import HumiditySensor
from platypush.entities.pressure import PressureSensor
from platypush.entities.sensors import NumericSensor
from platypush.entities.temperature import TemperatureSensor
from platypush.plugins import action
from platypush.plugins.sensor import SensorPlugin
@dataclass
class SensorEntityMapping:
"""
Maps the dict-like data returned by the plugin to corresponding sensor
entities.
"""
name: str
unit: str
entity_type: Type[NumericSensor]
@property
def id(self):
"""
The standardized external ID for the entity.
"""
return f'bme280:{self.name}'
_sensor_entity_mappings = {
mapping.name: mapping
for mapping in [
SensorEntityMapping(
name='temperature',
unit='°C',
entity_type=TemperatureSensor,
),
SensorEntityMapping(
name='humidity',
unit='%',
entity_type=HumiditySensor,
),
SensorEntityMapping(
name='pressure',
unit='Pa',
entity_type=PressureSensor,
),
SensorEntityMapping(
name='altitude',
unit='m',
entity_type=DistanceSensor,
),
]
}
# pylint: disable=too-many-ancestors
class SensorBme280Plugin(SensorPlugin):
"""
Plugin to interact with a `BME280 <https://shop.pimoroni.com/products/bme280-breakout>`_ environment sensor for
temperature, humidity and pressure measurements over I2C interface
Requires:
* ``pimoroni-bme280`` (``pip install pimoroni-bme280``)
Triggers:
* :class:`platypush.message.event.sensor.SensorDataAboveThresholdEvent`
* :class:`platypush.message.event.sensor.SensorDataBelowThresholdEvent`
* :class:`platypush.message.event.sensor.SensorDataChangeEvent`
"""
def __init__(self, port: int = 1, **kwargs):
"""
:param port: I2C port. 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
"""
super().__init__(**kwargs)
self.port = port
self._bus = None
self._device = None
def _get_device(self):
from smbus import SMBus
from bme280 import BME280
if self._device:
return self._device
self._bus = SMBus(self.port)
self._device = BME280(i2c_dev=self._bus)
return self._device
@override
@action
def get_measurement(self, *_, **__):
"""
:returns: dict. Example:
.. code-block:: python
output = {
"temperature": 21.0, # Celsius
"pressure": 101555.08, # Pascals
"humidity": 23.543, # percentage
"altitude": 15.703 # meters
}
"""
device = self._get_device()
return {
'temperature': device.get_temperature(),
'pressure': device.get_pressure() * 100,
'humidity': device.get_humidity(),
'altitude': device.get_altitude(),
}
@override
def transform_entities(self, entities: Dict[str, Numeric]) -> List[Device]:
sensors = []
for sensor, value in entities.items():
if value is None:
continue
mapping = _sensor_entity_mappings[sensor]
sensors.append(
mapping.entity_type(
id=mapping.id,
name=mapping.name,
value=value,
unit=mapping.unit,
)
)
if not sensors:
return []
return [
Device(
id='bme280',
name='BME280 Sensor',
children=sensors,
)
]
# vim:sw=4:ts=4:et:

View file

@ -0,0 +1,10 @@
manifest:
events:
platypush.message.event.sensor.SensorDataAboveThresholdEvent:
platypush.message.event.sensor.SensorDataBelowThresholdEvent:
platypush.message.event.sensor.SensorDataChangeEvent:
install:
pip:
- pimoroni-bme280
package: platypush.plugins.sensor.bme280
type: plugin