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.
This commit is contained in:
Fabio Manganiello 2023-04-01 23:16:03 +02:00
parent 8e0f88ea16
commit c1d0f21ead
Signed by: blacklight
GPG key ID: D90FBA7F76362774
6 changed files with 103 additions and 98 deletions

View file

@ -1,37 +0,0 @@
from platypush.backend.sensor import SensorBackend
class SensorLtr559Backend(SensorBackend):
"""
Backend to poll an `LTR559 <https://shop.pimoroni.com/products/ltr-559-light-proximity-sensor-breakout>`_
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:

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:
- ltr559
package: platypush.backend.sensor.ltr559
type: backend

View file

@ -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 <https://shop.pimoroni.com/products/ltr-559-light-proximity-sensor-breakout>`_
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:

View file

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

View file

@ -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 <https://shop.pimoroni.com/products/ltr-559-light-proximity-sensor-breakout>`_
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:

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:
- ltr559
package: platypush.plugins.sensor.ltr559
type: plugin