From 9d592fe3709b23451df24429080e75159241a8b8 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 8 Jan 2020 14:55:58 +0100 Subject: [PATCH] Added battery sensor backend --- platypush/backend/sensor/battery.py | 30 +++++++++++++++++++ platypush/message/response/system/__init__.py | 4 +-- platypush/plugins/system/__init__.py | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 platypush/backend/sensor/battery.py diff --git a/platypush/backend/sensor/battery.py b/platypush/backend/sensor/battery.py new file mode 100644 index 000000000..aa40cac30 --- /dev/null +++ b/platypush/backend/sensor/battery.py @@ -0,0 +1,30 @@ +from platypush.backend.sensor import SensorBackend +from platypush.context import get_plugin + + +class SensorBatteryBackend(SensorBackend): + """ + This backend listens for battery full/connect/disconnect/below/above threshold events. + The sensor events triggered by this backend will include any of the following fields: + + - ``battery_percent`` + - ``battery_secs_left`` + - ``battery_power_plugged`` + + Requires: + + - **psutil** (``pip install psutil``) for CPU load and stats. + """ + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def get_measurement(self): + plugin = get_plugin('system') + return { + 'battery_' + name: value + for name, value in plugin.sensors_battery().output.items() + } + + +# vim:sw=4:ts=4:et: diff --git a/platypush/message/response/system/__init__.py b/platypush/message/response/system/__init__.py index 3061aa323..96c896160 100644 --- a/platypush/message/response/system/__init__.py +++ b/platypush/message/response/system/__init__.py @@ -384,13 +384,13 @@ class SensorFanResponse(SensorResponse): class SensorBatteryResponse(SensorResponse): def __init__(self, percent: float, - secsleft: int, + secs_left: int, power_plugged: bool, *args, **kwargs): super().__init__( *args, output={ 'percent': percent, - 'secsleft': secsleft, + 'secs_left': secs_left, 'power_plugged': power_plugged, }, **kwargs ) diff --git a/platypush/plugins/system/__init__.py b/platypush/plugins/system/__init__.py index 5046a805c..2504fec4a 100644 --- a/platypush/plugins/system/__init__.py +++ b/platypush/plugins/system/__init__.py @@ -544,7 +544,7 @@ class SystemPlugin(Plugin): return SensorBatteryResponse( percent=stats.percent, - secsleft=stats.secsleft, + secs_left=stats.secsleft, power_plugged=stats.power_plugged, )