Added battery sensor backend

This commit is contained in:
Fabio Manganiello 2020-01-08 14:55:58 +01:00
parent 6738ff832a
commit 9d592fe370
3 changed files with 33 additions and 3 deletions

View file

@ -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:

View file

@ -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
)

View file

@ -544,7 +544,7 @@ class SystemPlugin(Plugin):
return SensorBatteryResponse(
percent=stats.percent,
secsleft=stats.secsleft,
secs_left=stats.secsleft,
power_plugged=stats.power_plugged,
)