Added TimeDurationSensor entity.

This commit is contained in:
Fabio Manganiello 2023-02-23 01:02:13 +01:00
parent c3e16f9f9d
commit d961e2a997
Signed by: blacklight
GPG key ID: D90FBA7F76362774
4 changed files with 38 additions and 1 deletions

View file

@ -143,6 +143,14 @@
} }
}, },
"time_duration_sensor": {
"name": "Sensor",
"name_plural": "Sensors",
"icon": {
"class": "fas fa-clock"
}
},
"muted": { "muted": {
"name": "Switch", "name": "Switch",
"name_plural": "Switches", "name_plural": "Switches",

View file

@ -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__,
}

View file

@ -27,6 +27,7 @@ from platypush.entities.motion import MotionSensor
from platypush.entities.sensors import BinarySensor, NumericSensor, RawSensor from platypush.entities.sensors import BinarySensor, NumericSensor, RawSensor
from platypush.entities.steps import StepsSensor from platypush.entities.steps import StepsSensor
from platypush.entities.temperature import TemperatureSensor from platypush.entities.temperature import TemperatureSensor
from platypush.entities.time import TimeDurationSensor
@dataclass @dataclass
@ -55,6 +56,7 @@ class NullSensor:
# Maps property names to transformer methods (first mapper choice). # Maps property names to transformer methods (first mapper choice).
_property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = { _property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = {
'activity heart rate': lambda value, _: HeartRateSensor(value=value),
'battery': lambda value, conf: Battery( 'battery': lambda value, conf: Battery(
value=value, value=value,
unit=conf.get('unit', '%'), unit=conf.get('unit', '%'),
@ -65,11 +67,14 @@ _property_to_entity: Dict[str, Callable[[Any, Dict[str, Any]], Entity]] = {
value=value, value=value,
unit=conf.get('unit', 'A'), unit=conf.get('unit', 'A'),
), ),
'duration': lambda value, conf: TimeDurationSensor(
value=value,
unit=conf.get('unit'),
),
'energy': lambda value, conf: EnergySensor( 'energy': lambda value, conf: EnergySensor(
value=value, value=value,
unit=conf.get('unit', 'kWh'), unit=conf.get('unit', 'kWh'),
), ),
'activity heart rate': lambda value, _: HeartRateSensor(value=value),
'humidity': lambda value, conf: HumiditySensor( 'humidity': lambda value, conf: HumiditySensor(
value=value, value=value,
unit=conf.get('unit', '%'), unit=conf.get('unit', '%'),