From 2dfb3896308b1c161d3fee88919f4d0659b1f1df Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <fabio@manganiello.tech>
Date: Thu, 23 Feb 2023 21:20:41 +0100
Subject: [PATCH] Added remaining `bluetooth` entity types in `_mappers.py`.

---
 .../panels/Entities/ContactSensor.vue         |  1 +
 .../panels/Entities/PresenceSensor.vue        |  2 +-
 .../panels/Entities/WeightSensor.vue          |  1 +
 .../src/components/panels/Entities/meta.json  | 16 +++++++++++++
 platypush/entities/contact.py                 | 23 +++++++++++++++++++
 platypush/entities/presence.py                |  2 +-
 platypush/entities/weight.py                  | 23 +++++++++++++++++++
 platypush/plugins/bluetooth/ble/_mappers.py   | 16 +++++++++++++
 8 files changed, 82 insertions(+), 2 deletions(-)
 create mode 120000 platypush/backend/http/webapp/src/components/panels/Entities/ContactSensor.vue
 create mode 120000 platypush/backend/http/webapp/src/components/panels/Entities/WeightSensor.vue
 create mode 100644 platypush/entities/contact.py
 create mode 100644 platypush/entities/weight.py

diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/ContactSensor.vue b/platypush/backend/http/webapp/src/components/panels/Entities/ContactSensor.vue
new file mode 120000
index 000000000..b00126b9d
--- /dev/null
+++ b/platypush/backend/http/webapp/src/components/panels/Entities/ContactSensor.vue
@@ -0,0 +1 @@
+BinarySensor.vue
\ No newline at end of file
diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/PresenceSensor.vue b/platypush/backend/http/webapp/src/components/panels/Entities/PresenceSensor.vue
index 70b944608..b00126b9d 120000
--- a/platypush/backend/http/webapp/src/components/panels/Entities/PresenceSensor.vue
+++ b/platypush/backend/http/webapp/src/components/panels/Entities/PresenceSensor.vue
@@ -1 +1 @@
-Sensor.vue
\ No newline at end of file
+BinarySensor.vue
\ No newline at end of file
diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/WeightSensor.vue b/platypush/backend/http/webapp/src/components/panels/Entities/WeightSensor.vue
new file mode 120000
index 000000000..70b944608
--- /dev/null
+++ b/platypush/backend/http/webapp/src/components/panels/Entities/WeightSensor.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 ca10b9a72..b484f8871 100644
--- a/platypush/backend/http/webapp/src/components/panels/Entities/meta.json
+++ b/platypush/backend/http/webapp/src/components/panels/Entities/meta.json
@@ -111,6 +111,14 @@
     }
   },
 
+  "contact_sensor": {
+    "name": "Sensor",
+    "name_plural": "Sensors",
+    "icon": {
+      "class": "far fa-hand"
+    }
+  },
+
   "presence_sensor": {
     "name": "Sensor",
     "name_plural": "Sensors",
@@ -119,6 +127,14 @@
     }
   },
 
+  "weight_sensor": {
+    "name": "Sensor",
+    "name_plural": "Sensors",
+    "icon": {
+      "class": "fas fa-weight-scale"
+    }
+  },
+
   "link_quality": {
     "name": "Link Quality",
     "name_plural": "Link Qualities",
diff --git a/platypush/entities/contact.py b/platypush/entities/contact.py
new file mode 100644
index 000000000..00b502b58
--- /dev/null
+++ b/platypush/entities/contact.py
@@ -0,0 +1,23 @@
+from sqlalchemy import Column, Integer, ForeignKey
+
+from platypush.common.db import Base
+
+from .sensors import BinarySensor
+
+
+if 'contact_sensor' not in Base.metadata:
+
+    class ContactSensor(BinarySensor):
+        """
+        A binary sensor that detects contact.
+        """
+
+        __tablename__ = 'contact_sensor'
+
+        id = Column(
+            Integer, ForeignKey(BinarySensor.id, ondelete='CASCADE'), primary_key=True
+        )
+
+        __mapper_args__ = {
+            'polymorphic_identity': __tablename__,
+        }
diff --git a/platypush/entities/presence.py b/platypush/entities/presence.py
index 19ae9c6f4..334bc4c43 100644
--- a/platypush/entities/presence.py
+++ b/platypush/entities/presence.py
@@ -7,7 +7,7 @@ from .sensors import BinarySensor
 
 if 'presence_sensor' not in Base.metadata:
 
-    class PressureSensor(BinarySensor):
+    class PresenceSensor(BinarySensor):
         """
         A binary sensor that detects presence.
         """
diff --git a/platypush/entities/weight.py b/platypush/entities/weight.py
new file mode 100644
index 000000000..37cbe4f45
--- /dev/null
+++ b/platypush/entities/weight.py
@@ -0,0 +1,23 @@
+from sqlalchemy import Column, Integer, ForeignKey
+
+from platypush.common.db import Base
+
+from .sensors import NumericSensor
+
+
+if 'weight_sensor' not in Base.metadata:
+
+    class WeightSensor(NumericSensor):
+        """
+        A sensor that measures weight.
+        """
+
+        __tablename__ = 'weight_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 4145437d8..974513568 100644
--- a/platypush/plugins/bluetooth/ble/_mappers.py
+++ b/platypush/plugins/bluetooth/ble/_mappers.py
@@ -14,6 +14,7 @@ from TheengsGateway._decoder import decodeBLE, getAttribute, getProperties
 from platypush.entities import Entity
 from platypush.entities.batteries import Battery
 from platypush.entities.bluetooth import BluetoothDevice
+from platypush.entities.contact import ContactSensor
 from platypush.entities.electricity import (
     CurrentSensor,
     EnergySensor,
@@ -30,6 +31,7 @@ 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
+from platypush.entities.weight import WeightSensor
 
 
 @dataclass
@@ -69,6 +71,7 @@ _property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = {
         min=conf.get('min', 0),
         max=conf.get('min', 100),
     ),
+    'contact': lambda value, _: ContactSensor(value=value),
     'current': lambda value, conf: CurrentSensor(
         value=value,
         unit=conf.get('unit', 'A'),
@@ -100,7 +103,12 @@ _property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = {
         value=value,
         unit=conf.get('unit'),
     ),
+    'moisture': lambda value, conf: HumiditySensor(
+        value=value,
+        unit=conf.get('unit'),
+    ),
     'motion': lambda value, _: MotionSensor(value=value),
+    'open': lambda value, _: BinarySensor(value=value),
     'power': lambda value, conf: PowerSensor(
         value=value,
         unit=conf.get('unit', 'W'),
@@ -143,10 +151,18 @@ _property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = {
         value=value,
         unit=conf.get('unit', 'C'),
     ),
+    'volt': lambda value, conf: VoltageSensor(
+        value=value,
+        unit=conf.get('unit', 'V'),
+    ),
     'voltage': lambda value, conf: VoltageSensor(
         value=value,
         unit=conf.get('unit', 'V'),
     ),
+    'weight': lambda value, conf: WeightSensor(
+        value=value,
+        unit=conf.get('unit', 'kg'),
+    ),
 }
 
 # Maps reported units to transformer methods (second mapper choice).