From d961e2a997859d4d79abe6ca9ac2bdaf6852535c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 23 Feb 2023 01:02:13 +0100 Subject: [PATCH] Added `TimeDurationSensor` entity. --- .../panels/Entities/TimeDurationSensor.vue | 1 + .../src/components/panels/Entities/meta.json | 8 +++++++ platypush/entities/time.py | 23 +++++++++++++++++++ platypush/plugins/bluetooth/ble/_mappers.py | 7 +++++- 4 files changed, 38 insertions(+), 1 deletion(-) create mode 120000 platypush/backend/http/webapp/src/components/panels/Entities/TimeDurationSensor.vue create mode 100644 platypush/entities/time.py diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/TimeDurationSensor.vue b/platypush/backend/http/webapp/src/components/panels/Entities/TimeDurationSensor.vue new file mode 120000 index 0000000000..70b944608e --- /dev/null +++ b/platypush/backend/http/webapp/src/components/panels/Entities/TimeDurationSensor.vue @@ -0,0 +1 @@ +Sensor.vue \ No newline at end of file diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/meta.json b/platypush/backend/http/webapp/src/components/panels/Entities/meta.json index 8d20a9305e..87e7c157a7 100644 --- a/platypush/backend/http/webapp/src/components/panels/Entities/meta.json +++ b/platypush/backend/http/webapp/src/components/panels/Entities/meta.json @@ -143,6 +143,14 @@ } }, + "time_duration_sensor": { + "name": "Sensor", + "name_plural": "Sensors", + "icon": { + "class": "fas fa-clock" + } + }, + "muted": { "name": "Switch", "name_plural": "Switches", diff --git a/platypush/entities/time.py b/platypush/entities/time.py new file mode 100644 index 0000000000..b6d3c00321 --- /dev/null +++ b/platypush/entities/time.py @@ -0,0 +1,23 @@ +from sqlalchemy import Column, Integer, ForeignKey + +from platypush.common.db import Base + +from .sensors import NumericSensor + + +if 'time_duration_sensor' not in Base.metadata: + + class TimeDurationSensor(NumericSensor): + """ + A sensor that measures a time duration. + """ + + __tablename__ = 'time_duration_sensor' + + id = Column( + Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True + ) + + __mapper_args__ = { + 'polymorphic_identity': __tablename__, + } diff --git a/platypush/plugins/bluetooth/ble/_mappers.py b/platypush/plugins/bluetooth/ble/_mappers.py index 2ebf3809e2..a8be0ef087 100644 --- a/platypush/plugins/bluetooth/ble/_mappers.py +++ b/platypush/plugins/bluetooth/ble/_mappers.py @@ -27,6 +27,7 @@ from platypush.entities.motion import MotionSensor from platypush.entities.sensors import BinarySensor, NumericSensor, RawSensor from platypush.entities.steps import StepsSensor from platypush.entities.temperature import TemperatureSensor +from platypush.entities.time import TimeDurationSensor @dataclass @@ -55,6 +56,7 @@ class NullSensor: # Maps property names to transformer methods (first mapper choice). _property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = { + 'activity heart rate': lambda value, _: HeartRateSensor(value=value), 'battery': lambda value, conf: Battery( value=value, unit=conf.get('unit', '%'), @@ -65,11 +67,14 @@ _property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = { value=value, unit=conf.get('unit', 'A'), ), + 'duration': lambda value, conf: TimeDurationSensor( + value=value, + unit=conf.get('unit'), + ), 'energy': lambda value, conf: EnergySensor( value=value, unit=conf.get('unit', 'kWh'), ), - 'activity heart rate': lambda value, _: HeartRateSensor(value=value), 'humidity': lambda value, conf: HumiditySensor( value=value, unit=conf.get('unit', '%'),