Migrated envirophat to the new SensorPlugin API.

Removed `backend.sensor.envirophat` and `gpio.sensor.envirophat` plugin.
They have now been merged into the new `sensor.envirophat` plugin.
This commit is contained in:
Fabio Manganiello 2023-04-02 02:49:08 +02:00
parent 3cd42c9e45
commit 7697c1c6ad
Signed by: blacklight
GPG key ID: D90FBA7F76362774
4 changed files with 119 additions and 74 deletions

View file

@ -1,51 +0,0 @@
from platypush.backend.sensor import SensorBackend
class SensorEnvirophatBackend(SensorBackend):
"""
Backend to poll analog sensor values from an enviroPHAT sensor pHAT
(https://shop.pimoroni.com/products/enviro-phat)
Requires:
* ``envirophat`` (``pip install envirophat``)
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, altitude=True, luminosity=True,
analog=True, accelerometer=True, magnetometer=True, qnh=1020, **kwargs):
"""
:param temperature: Enable temperature sensor polling
:param pressure: Enable pressure sensor polling
:param altitude: Enable altitude sensor polling
:param luminosity: Enable luminosity sensor polling
:param analog: Enable analog sensors polling
:param accelerometer: Enable accelerometer polling
:param magnetometer: Enable magnetometer polling
:param qnh: Base reference for your sea level pressure (for altitude sensor)
"""
enabled_sensors = {
'temperature': temperature,
'pressure': pressure,
'altitude': altitude,
'luminosity': luminosity,
'analog': analog,
'accelerometer': accelerometer,
'magnetometer': magnetometer,
}
super().__init__(plugin='gpio.sensor.envirophat',
plugin_args={'qnh': qnh},
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:
- envirophat
package: platypush.backend.sensor.envirophat
type: backend

View file

@ -1,23 +1,110 @@
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Type
from typing_extensions import override
from platypush.common.sensors import Numeric
from platypush.entities.acceleration import Accelerometer
from platypush.entities.devices import Device
from platypush.entities.distance import DistanceSensor
from platypush.entities.illuminance import IlluminanceSensor
from platypush.entities.magnetism import Magnetometer
from platypush.entities.pressure import PressureSensor
from platypush.entities.sensors import BinarySensor, RawSensor, Sensor
from platypush.entities.temperature import TemperatureSensor
from platypush.plugins import action from platypush.plugins import action
from platypush.plugins.gpio.sensor import GpioSensorPlugin from platypush.plugins.sensor import SensorPlugin
class GpioSensorEnvirophatPlugin(GpioSensorPlugin): @dataclass
class SensorEntityMapping:
""" """
Plugin to interact with a `Pimoroni enviropHAT <https://shop.pimoroni.com/products/enviro-phat>`_ device. Maps sensor attributes returned by
You can use an enviropHAT device to read e.g. temperature, pressure, altitude, accelerometer, magnetometer and :meth:`SensorEnvirophatPlugin.get_measurement` to native sensor entities.
luminosity data, plus control the status of its RGB LEDs. """
name: str
entity_type: Type[Sensor]
unit: Optional[str] = None
kwargs: Dict[str, Any] = field(default_factory=dict)
@property
def id(self):
"""
Standard format for an Envirophat sensor external ID.
"""
return f'envirophat:{self.name}'
_sensor_entity_mappings: Dict[str, SensorEntityMapping] = {
mapping.name: mapping
for mapping in [
SensorEntityMapping(
name='temperature',
entity_type=TemperatureSensor,
unit='°C',
),
SensorEntityMapping(
name='pressure',
entity_type=PressureSensor,
unit='Pa',
),
SensorEntityMapping(
name='altitude',
entity_type=DistanceSensor,
unit='m',
),
SensorEntityMapping(
name='illuminance',
entity_type=IlluminanceSensor,
unit='lux',
),
SensorEntityMapping(
name='accelerometer',
entity_type=Accelerometer,
),
SensorEntityMapping(
name='magnetometer',
entity_type=Magnetometer,
),
SensorEntityMapping(
name='analog',
entity_type=RawSensor,
),
SensorEntityMapping(
name='leds',
entity_type=BinarySensor,
),
]
}
# pylint: disable=too-many-ancestors
class SensorEnvirophatPlugin(SensorPlugin):
"""
Plugin to interact with a `Pimoroni enviropHAT
<https://shop.pimoroni.com/products/enviro-phat>`_ device.
You can use an enviropHAT device to read e.g. temperature, pressure,
altitude, accelerometer, magnetometer and luminosity data.
Requires: Requires:
* ``envirophat`` (``pip install envirophat``) * ``envirophat`` (``pip install envirophat``)
Triggers:
* :class:`platypush.message.event.sensor.SensorDataAboveThresholdEvent`
* :class:`platypush.message.event.sensor.SensorDataBelowThresholdEvent`
* :class:`platypush.message.event.sensor.SensorDataChangeEvent`
""" """
@override
@action @action
def get_measurement(self, qnh=1020.0): def get_measurement(self, *_, qnh: float = 1020.0, **__):
""" """
:param: qnh: Local value for atmospheric pressure adjusted to sea level (default: 1020) :param: qnh: Local value for atmospheric pressure adjusted to sea level
:type qnh: float (default: 1020)
:returns: dict. Example: :returns: dict. Example:
@ -69,5 +156,24 @@ class GpioSensorEnvirophatPlugin(GpioSensorPlugin):
return ret return ret
@override
def transform_entities(self, entities: Dict[str, Numeric]) -> List[Device]:
return [
Device(
id='envirophat',
name='EnviroPHAT',
children=[
_sensor_entity_mappings[sensor].entity_type(
id=_sensor_entity_mappings[sensor].id,
name=_sensor_entity_mappings[sensor].name,
unit=_sensor_entity_mappings[sensor].unit,
**_sensor_entity_mappings[sensor].kwargs,
)
for sensor, value in entities.items()
if value is not None
],
)
]
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -1,7 +1,10 @@
manifest: manifest:
events: {} events:
platypush.message.event.sensor.SensorDataAboveThresholdEvent:
platypush.message.event.sensor.SensorDataBelowThresholdEvent:
platypush.message.event.sensor.SensorDataChangeEvent:
install: install:
pip: pip:
- envirophat - envirophat
package: platypush.plugins.gpio.sensor.envirophat package: platypush.plugins.sensor.envirophat
type: plugin type: plugin