From c1d0f21eadfdcab20d65086a8a61f061d218da78 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 1 Apr 2023 23:16:03 +0200 Subject: [PATCH] Migrated `ltr559` integration to the new API. Merged `backend.sensor.ltr559` and `gpio.sensor.ltr559` into the new `sensor.ltr559` plugin, which extends the new `SensorPlugin` API. --- platypush/backend/sensor/ltr559/__init__.py | 37 -------- platypush/backend/sensor/ltr559/manifest.yaml | 13 --- .../plugins/gpio/sensor/ltr559/__init__.py | 41 -------- .../plugins/gpio/sensor/ltr559/manifest.yaml | 7 -- platypush/plugins/sensor/ltr559/__init__.py | 93 +++++++++++++++++++ platypush/plugins/sensor/ltr559/manifest.yaml | 10 ++ 6 files changed, 103 insertions(+), 98 deletions(-) delete mode 100644 platypush/backend/sensor/ltr559/__init__.py delete mode 100644 platypush/backend/sensor/ltr559/manifest.yaml delete mode 100644 platypush/plugins/gpio/sensor/ltr559/__init__.py delete mode 100644 platypush/plugins/gpio/sensor/ltr559/manifest.yaml create mode 100644 platypush/plugins/sensor/ltr559/__init__.py create mode 100644 platypush/plugins/sensor/ltr559/manifest.yaml diff --git a/platypush/backend/sensor/ltr559/__init__.py b/platypush/backend/sensor/ltr559/__init__.py deleted file mode 100644 index c5ae8b355..000000000 --- a/platypush/backend/sensor/ltr559/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -from platypush.backend.sensor import SensorBackend - - -class SensorLtr559Backend(SensorBackend): - """ - Backend to poll an `LTR559 `_ - light/proximity sensor - - Requires: - - * ``ltr559`` (``pip install ltr559``) - - 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, light=True, proximity=True, **kwargs): - """ - :param light: Enable light sensor - :param proximity: Enable proximity sensor - """ - - enabled_sensors = { - 'light': light, - 'proximity': proximity, - } - - super().__init__(plugin='gpio.sensor.ltr559', enabled_sensors=enabled_sensors, **kwargs) - - -# vim:sw=4:ts=4:et: diff --git a/platypush/backend/sensor/ltr559/manifest.yaml b/platypush/backend/sensor/ltr559/manifest.yaml deleted file mode 100644 index 0cd07a519..000000000 --- a/platypush/backend/sensor/ltr559/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: - - ltr559 - package: platypush.backend.sensor.ltr559 - type: backend diff --git a/platypush/plugins/gpio/sensor/ltr559/__init__.py b/platypush/plugins/gpio/sensor/ltr559/__init__.py deleted file mode 100644 index 2ed58b813..000000000 --- a/platypush/plugins/gpio/sensor/ltr559/__init__.py +++ /dev/null @@ -1,41 +0,0 @@ -from platypush.plugins import action -from platypush.plugins.gpio.sensor import GpioSensorPlugin - - -class GpioSensorLtr559Plugin(GpioSensorPlugin): - """ - Plugin to interact with an `LTR559 `_ - light and proximity sensor - - Requires: - - * ``ltr559`` (``pip install ltr559``) - """ - - def __init__(self, **kwargs): - import ltr559 - super().__init__(**kwargs) - self.ltr = ltr559.LTR559() - - # noinspection PyUnresolvedReferences - @action - def get_measurement(self): - """ - :returns: dict. Example: - - .. code-block:: python - - output = { - "light": 109.3543, # Lux - "proximity": 103 # The higher the value, the nearest the object, within a ~5cm range - } - - """ - self.ltr.update_sensor() - return { - 'light': self.ltr.get_lux(), - 'proximity': self.ltr.get_proximity(), - } - - -# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/gpio/sensor/ltr559/manifest.yaml b/platypush/plugins/gpio/sensor/ltr559/manifest.yaml deleted file mode 100644 index 28740526b..000000000 --- a/platypush/plugins/gpio/sensor/ltr559/manifest.yaml +++ /dev/null @@ -1,7 +0,0 @@ -manifest: - events: {} - install: - pip: - - ltr559 - package: platypush.plugins.gpio.sensor.ltr559 - type: plugin diff --git a/platypush/plugins/sensor/ltr559/__init__.py b/platypush/plugins/sensor/ltr559/__init__.py new file mode 100644 index 000000000..840ffed6f --- /dev/null +++ b/platypush/plugins/sensor/ltr559/__init__.py @@ -0,0 +1,93 @@ +from typing import Dict, List + +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.illuminance import IlluminanceSensor +from platypush.plugins import action +from platypush.plugins.sensor import SensorPlugin + + +# pylint: disable=too-many-ancestors +class SensorLtr559Plugin(SensorPlugin): + """ + Plugin to interact with an `LTR559 `_ + light and proximity sensor + + Requires: + + * ``ltr559`` (``pip install ltr559``) + + Triggers: + + * :class:`platypush.message.event.sensor.SensorDataAboveThresholdEvent` + * :class:`platypush.message.event.sensor.SensorDataBelowThresholdEvent` + * :class:`platypush.message.event.sensor.SensorDataChangeEvent` + + """ + + def __init__(self, **kwargs): + import ltr559 + + super().__init__(**kwargs) + self.ltr = ltr559.LTR559() + + @override + @action + def get_measurement(self, *_, **__): + """ + :returns: dict. Example: + + .. code-block:: python + + output = { + "light": 109.3543, # Lux + "proximity": 103 # The higher the value, the nearest the object, within a ~5cm range + } + + """ + self.ltr.update_sensor() + return { + 'light': self.ltr.get_lux(), + 'proximity': self.ltr.get_proximity(), + } + + @override + def transform_entities(self, entities: Dict[str, Numeric]) -> List[Device]: + sensors = [] + + if entities.get('light') is not None: + sensors.append( + IlluminanceSensor( + id='ltr559:illuminance', + name='illuminance', + value=entities['light'], + unit='lux', + ) + ) + + if entities.get('proximity') is not None: + sensors.append( + DistanceSensor( + id='ltr559:proximity', + name='proximity', + value=entities['proximity'], + unit='mm', + ) + ) + + if not sensors: + return [] + + return [ + Device( + id='ltr559', + name='LTR559 Sensor', + children=sensors, + ) + ] + + +# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/sensor/ltr559/manifest.yaml b/platypush/plugins/sensor/ltr559/manifest.yaml new file mode 100644 index 000000000..6ec27a592 --- /dev/null +++ b/platypush/plugins/sensor/ltr559/manifest.yaml @@ -0,0 +1,10 @@ +manifest: + events: + platypush.message.event.sensor.SensorDataAboveThresholdEvent: + platypush.message.event.sensor.SensorDataBelowThresholdEvent: + platypush.message.event.sensor.SensorDataChangeEvent: + install: + pip: + - ltr559 + package: platypush.plugins.sensor.ltr559 + type: plugin