From 7697c1c6ad601076a9b99a84b3f58fdc57d7ee48 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 2 Apr 2023 02:49:08 +0200 Subject: [PATCH] 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. --- .../backend/sensor/envirophat/__init__.py | 51 -------- .../backend/sensor/envirophat/manifest.yaml | 13 -- .../plugins/sensor/envirophat/__init__.py | 122 ++++++++++++++++-- .../plugins/sensor/envirophat/manifest.yaml | 7 +- 4 files changed, 119 insertions(+), 74 deletions(-) delete mode 100644 platypush/backend/sensor/envirophat/__init__.py delete mode 100644 platypush/backend/sensor/envirophat/manifest.yaml diff --git a/platypush/backend/sensor/envirophat/__init__.py b/platypush/backend/sensor/envirophat/__init__.py deleted file mode 100644 index fc98db7d6..000000000 --- a/platypush/backend/sensor/envirophat/__init__.py +++ /dev/null @@ -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: diff --git a/platypush/backend/sensor/envirophat/manifest.yaml b/platypush/backend/sensor/envirophat/manifest.yaml deleted file mode 100644 index 22a4ec22c..000000000 --- a/platypush/backend/sensor/envirophat/manifest.yaml +++ /dev/null @@ -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 diff --git a/platypush/plugins/sensor/envirophat/__init__.py b/platypush/plugins/sensor/envirophat/__init__.py index fcfa2c5a3..33c22cdd1 100644 --- a/platypush/plugins/sensor/envirophat/__init__.py +++ b/platypush/plugins/sensor/envirophat/__init__.py @@ -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.gpio.sensor import GpioSensorPlugin +from platypush.plugins.sensor import SensorPlugin -class GpioSensorEnvirophatPlugin(GpioSensorPlugin): +@dataclass +class SensorEntityMapping: """ - Plugin to interact with a `Pimoroni enviropHAT `_ device. - You can use an enviropHAT device to read e.g. temperature, pressure, altitude, accelerometer, magnetometer and - luminosity data, plus control the status of its RGB LEDs. + Maps sensor attributes returned by + :meth:`SensorEnvirophatPlugin.get_measurement` to native sensor entities. + """ + + 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 + `_ device. + + You can use an enviropHAT device to read e.g. temperature, pressure, + altitude, accelerometer, magnetometer and luminosity data. Requires: * ``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 - 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) - :type qnh: float + :param: qnh: Local value for atmospheric pressure adjusted to sea level + (default: 1020) :returns: dict. Example: @@ -69,5 +156,24 @@ class GpioSensorEnvirophatPlugin(GpioSensorPlugin): 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: diff --git a/platypush/plugins/sensor/envirophat/manifest.yaml b/platypush/plugins/sensor/envirophat/manifest.yaml index 6793f4fcd..3a3beabbe 100644 --- a/platypush/plugins/sensor/envirophat/manifest.yaml +++ b/platypush/plugins/sensor/envirophat/manifest.yaml @@ -1,7 +1,10 @@ manifest: - events: {} + events: + platypush.message.event.sensor.SensorDataAboveThresholdEvent: + platypush.message.event.sensor.SensorDataBelowThresholdEvent: + platypush.message.event.sensor.SensorDataChangeEvent: install: pip: - envirophat - package: platypush.plugins.gpio.sensor.envirophat + package: platypush.plugins.sensor.envirophat type: plugin