From 9f8882acd8c701ef5c17315b88d0a61e92727b59 Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Mon, 22 Aug 2022 15:14:45 -0400 Subject: [PATCH] rules: add rule condition to test whether a device is active --- lib/logitech_receiver/diversion.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/logitech_receiver/diversion.py b/lib/logitech_receiver/diversion.py index 0b11dcb3..2e35f3e5 100644 --- a/lib/logitech_receiver/diversion.py +++ b/lib/logitech_receiver/diversion.py @@ -579,7 +579,8 @@ class Report(Condition): if warn: _log.warn('rule Report argument not an integer: %s', report) self.report = -1 - self.report = report + else: + self.report = report def __str__(self): return 'Report: ' + str(self.report) @@ -832,6 +833,25 @@ class MouseGesture(Condition): return {'MouseGesture': [str(m) for m in self.movements]} +class Active(Condition): + def __init__(self, devID, warn=True): + if not (isinstance(devID, str)): + if warn: + _log.warn('rule Active argument not a string: %s', devID) + self.devID = '' + self.devID = devID + + def __str__(self): + return 'Active: ' + str(self.devID) + + def evaluate(self, feature, notification, device, status, last_result): + dev = _Device.find(self.devID) + return bool(dev and dev.ping()) + + def data(self): + return {'Active': self.devID} + + class Action(RuleComponent): def __init__(self, *args): pass @@ -1089,6 +1109,7 @@ COMPONENTS = { 'Test': Test, 'TestBytes': TestBytes, 'MouseGesture': MouseGesture, + 'Active': Active, 'KeyPress': KeyPress, 'MouseScroll': MouseScroll, 'MouseClick': MouseClick,