rules: add Device condition

This commit is contained in:
Peter F. Patel-Schneider 2023-02-17 13:38:07 -05:00
parent acc559743e
commit df746fd7f4
2 changed files with 25 additions and 1 deletions

View File

@ -112,8 +112,9 @@ or the window's Window manager class or instance name starts with their string a
`MouseProcess` conditions are true if the process for the window under the mouse
or the window's Window manager class or instance name starts with their string argument.
`Device` conditions are true if a particular device originated the notification.
`Active` conditions are true if a particular device is active.
`Active` conditions take one argument, which is the Serial number or Unit ID of a device,
`Device` and `Active` conditions take one argument, which is the Serial number or Unit ID of a device,
as shown in Solaar's detail pane.
`Setting` conditions check the value of a Solaar setting on a device.

View File

@ -968,6 +968,28 @@ class Active(Condition):
return {'Active': self.devID}
class Device(Condition):
def __init__(self, devID, warn=True):
if not (isinstance(devID, str)):
if warn:
_log.warn('rule Device argument not a string: %s', devID)
self.devID = ''
self.devID = devID
def __str__(self):
return 'Device: ' + str(self.devID)
def evaluate(self, feature, notification, device, status, last_result):
if _log.isEnabledFor(_DEBUG):
_log.debug('evaluate condition: %s', self)
dev = _Device.find(self.devID)
return device == dev
def data(self):
return {'Device': self.devID}
class Action(RuleComponent):
def __init__(self, *args):
@ -1265,6 +1287,7 @@ COMPONENTS = {
'TestBytes': TestBytes,
'MouseGesture': MouseGesture,
'Active': Active,
'Device': Device,
'KeyPress': KeyPress,
'MouseScroll': MouseScroll,
'MouseClick': MouseClick,