From beff88986af9802cb79dd81fd30383f652f52ac8 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 2 Apr 2023 13:38:53 +0200 Subject: [PATCH] Migrated `dht` integration. Removed `backend.sensor.dht` and `gpio.sensor.dht`. They have been merged into the new `sensor.dht` integration, which supports the new `SensorPlugin` API. --- platypush/backend/sensor/dht/__init__.py | 37 ----- platypush/backend/sensor/dht/manifest.yaml | 13 -- platypush/plugins/gpio/sensor/dht/__init__.py | 92 ------------ .../plugins/gpio/sensor/dht/manifest.yaml | 7 - platypush/plugins/sensor/dht/__init__.py | 140 ++++++++++++++++++ platypush/plugins/sensor/dht/manifest.yaml | 10 ++ 6 files changed, 150 insertions(+), 149 deletions(-) delete mode 100644 platypush/backend/sensor/dht/__init__.py delete mode 100644 platypush/backend/sensor/dht/manifest.yaml delete mode 100644 platypush/plugins/gpio/sensor/dht/__init__.py delete mode 100644 platypush/plugins/gpio/sensor/dht/manifest.yaml create mode 100644 platypush/plugins/sensor/dht/__init__.py create mode 100644 platypush/plugins/sensor/dht/manifest.yaml diff --git a/platypush/backend/sensor/dht/__init__.py b/platypush/backend/sensor/dht/__init__.py deleted file mode 100644 index 3cc93a9d..00000000 --- a/platypush/backend/sensor/dht/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -from platypush.backend.sensor import SensorBackend - - -class SensorDhtBackend(SensorBackend): - """ - Backend to poll a DHT11/DHT22/AM2302 temperature/humidity sensor. - - Requires: - - * ``Adafruit_Python_DHT`` (``pip install git+https://github.com/adafruit/Adafruit_Python_DHT.git``) - * The ``gpio.sensor.dht`` plugin configured and enabled. - - 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: bool = True, humidity: bool = True, **kwargs): - """ - :param temperature: Enable temperature sensor poll. - :param humidity: Enable humidity sensor poll. - """ - - enabled_sensors = { - 'humidity': humidity, - 'temperature': temperature, - } - - super().__init__(plugin='gpio.sensor.dht', enabled_sensors=enabled_sensors, **kwargs) - - -# vim:sw=4:ts=4:et: diff --git a/platypush/backend/sensor/dht/manifest.yaml b/platypush/backend/sensor/dht/manifest.yaml deleted file mode 100644 index f20d92f2..00000000 --- a/platypush/backend/sensor/dht/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: - - Adafruit_Python_DHT - package: platypush.backend.sensor.dht - type: backend diff --git a/platypush/plugins/gpio/sensor/dht/__init__.py b/platypush/plugins/gpio/sensor/dht/__init__.py deleted file mode 100644 index 7f59541b..00000000 --- a/platypush/plugins/gpio/sensor/dht/__init__.py +++ /dev/null @@ -1,92 +0,0 @@ -from typing import Optional, Dict - -from platypush.plugins import action -from platypush.plugins.gpio.sensor import GpioSensorPlugin - - -class GpioSensorDhtPlugin(GpioSensorPlugin): - """ - Plugin to interact with a DHT11/DHT22/AM2302 temperature/humidity sensor through GPIO. - - Requires: - - * ``Adafruit_Python_DHT`` (``pip install git+https://github.com/adafruit/Adafruit_Python_DHT.git``) - - """ - - def __init__(self, sensor_type: str, pin: int, retries: int = 5, - retry_seconds: int = 2, **kwargs): - """ - :param sensor_type: Type of sensor to be used (supported types: DHT11, DHT22, AM2302). - :param pin: GPIO PIN where the sensor is connected. - :param retries: Number of retries in case of failed read (default: 5). - :param retry_seconds: Number of seconds to wait between retries (default: 2). - """ - super().__init__(**kwargs) - self.sensor_type = self._get_sensor_type(sensor_type) - self.pin = pin - self.retries = retries - self.retry_seconds = retry_seconds - - @staticmethod - def _get_sensor_type(sensor_type: str) -> int: - import Adafruit_DHT - sensor_type = sensor_type.upper() - assert hasattr(Adafruit_DHT, sensor_type), \ - 'Unknown sensor type: {}. Supported types: DHT11, DHT22, AM2302'.format(sensor_type) - - return getattr(Adafruit_DHT, sensor_type) - - @action - def read(self, sensor_type: Optional[str] = None, pin: Optional[int] = None, - retries: Optional[int] = None, retry_seconds: Optional[int] = None, **__) -> Dict[str, float]: - """ - Read data from the sensor. - - :param sensor_type: Default ``sensor_type`` override. - :param pin: Default ``pin`` override. - :param retries: Default ``retries`` override. - :param retry_seconds: Default ``retry_seconds`` override. - :return: A mapping with the measured temperature and humidity. Example: - - .. code-block:: json - - { - "humidity": 30.0, - "temperature": 25.5 - } - - """ - import Adafruit_DHT - sensor_type = self._get_sensor_type(sensor_type) if sensor_type else self.sensor_type - pin = pin or self.pin - retries = retries or self.retries - retry_seconds = retry_seconds or self.retry_seconds - humidity, temperature = Adafruit_DHT.read_retry(sensor=sensor_type, - pin=pin, retries=retries, delay_seconds=retry_seconds) - - return { - 'humidity': humidity, - 'temperature': temperature, - } - - @action - def get_measurement(self) -> Dict[str, float]: - """ - Get data from the sensor. - - :return: A mapping with the measured temperature and humidity. Example: - - .. code-block:: json - - { - "humidity": 30.0, - "temperature": 25.5 - } - - """ - return self.read() - - - -# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/gpio/sensor/dht/manifest.yaml b/platypush/plugins/gpio/sensor/dht/manifest.yaml deleted file mode 100644 index cecb1984..00000000 --- a/platypush/plugins/gpio/sensor/dht/manifest.yaml +++ /dev/null @@ -1,7 +0,0 @@ -manifest: - events: {} - install: - pip: - - Adafruit_Python_DHT - package: platypush.plugins.gpio.sensor.dht - type: plugin diff --git a/platypush/plugins/sensor/dht/__init__.py b/platypush/plugins/sensor/dht/__init__.py new file mode 100644 index 00000000..c74c7fa2 --- /dev/null +++ b/platypush/plugins/sensor/dht/__init__.py @@ -0,0 +1,140 @@ +from typing import List, Optional, Dict +from typing_extensions import override +from platypush.common.sensors import Numeric +from platypush.entities.devices import Device + +from platypush.entities.humidity import HumiditySensor +from platypush.entities.temperature import TemperatureSensor +from platypush.plugins import action +from platypush.plugins.sensor import SensorPlugin + + +# pylint: disable=too-many-ancestors +class SensorDhtPlugin(SensorPlugin): + """ + Plugin to interact with a DHT11/DHT22/AM2302 temperature/humidity sensor through GPIO. + + Requires: + + * ``Adafruit_Python_DHT`` (``pip install git+https://github.com/adafruit/Adafruit_Python_DHT.git``) + + """ + + def __init__( + self, + sensor_type: str, + pin: int, + retries: int = 5, + retry_seconds: int = 2, + poll_interval: float = 5.0, + **kwargs, + ): + """ + :param sensor_type: Type of sensor to be used (supported types: DHT11, DHT22, AM2302). + :param pin: GPIO PIN where the sensor is connected. + :param retries: Number of retries in case of failed read (default: 5). + :param retry_seconds: Number of seconds to wait between retries (default: 2). + """ + super().__init__(poll_interval=poll_interval, **kwargs) + self.sensor_type = self._get_sensor_type(sensor_type) + self.pin = pin + self.retries = retries + self.retry_seconds = retry_seconds + + @staticmethod + def _get_sensor_type(sensor_type: str) -> int: + import Adafruit_DHT + + sensor_type = sensor_type.upper() + assert hasattr( + Adafruit_DHT, sensor_type + ), f'Unknown sensor type: {sensor_type}. Supported types: DHT11, DHT22, AM2302' + + return getattr(Adafruit_DHT, sensor_type) + + @action + def read( + self, + sensor_type: Optional[str] = None, + pin: Optional[int] = None, + retries: Optional[int] = None, + retry_seconds: Optional[int] = None, + **__, + ) -> Dict[str, float]: + """ + Read data from the sensor. + + :param sensor_type: Default ``sensor_type`` override. + :param pin: Default ``pin`` override. + :param retries: Default ``retries`` override. + :param retry_seconds: Default ``retry_seconds`` override. + :return: A mapping with the measured temperature and humidity. Example: + + .. code-block:: json + + { + "humidity": 30.0, + "temperature": 25.5 + } + + """ + import Adafruit_DHT + + sensor_type = ( # type: ignore + self._get_sensor_type(sensor_type) if sensor_type else self.sensor_type + ) + pin = pin or self.pin + retries = retries or self.retries + retry_seconds = retry_seconds or self.retry_seconds + humidity, temperature = Adafruit_DHT.read_retry( + sensor=sensor_type, pin=pin, retries=retries, delay_seconds=retry_seconds + ) + + return { + 'humidity': humidity, + 'temperature': temperature, + } + + @override + @action + def get_measurement(self, *_, **__) -> Dict[str, float]: + """ + Get data from the sensor. + + :return: A mapping with the measured temperature and humidity. Example: + + .. code-block:: json + + { + "humidity": 30.0, + "temperature": 25.5 + } + + """ + return self.read() # type: ignore + + @override + def transform_entities(self, entities: Dict[str, Numeric]) -> List[Device]: + return [ + Device( + id='dht', + name='DHT Sensor', + children=[ + TemperatureSensor( + id='dht:temperature', + name='temperature', + value=entities['temperature'], + unit='°C', + ), + HumiditySensor( + id='dht:humidity', + name='humidity', + value=entities['humidity'], + unit='%', + ), + ], + ) + ] + + +# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/sensor/dht/manifest.yaml b/platypush/plugins/sensor/dht/manifest.yaml new file mode 100644 index 00000000..8171f43f --- /dev/null +++ b/platypush/plugins/sensor/dht/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: + - Adafruit_Python_DHT + package: platypush.plugins.sensor.dht + type: plugin