rules: allow sub-second delays in Later

This commit is contained in:
Peter F. Patel-Schneider 2024-03-13 15:55:51 -04:00
parent 4e4275c281
commit 154dd7017f
2 changed files with 11 additions and 7 deletions

View File

@ -1355,9 +1355,9 @@ class Later(Action):
if not (isinstance(args, list) and len(args) >= 1):
if warn:
logger.warning("rule Later argument not list with minimum length 1: %s", args)
elif not (isinstance(args[0], int)) or not 0 < args[0] < 101:
elif not (isinstance(args[0], (int, float))) or not 0.01 <= args[0] <= 100:
if warn:
logger.warning("rule Later argument delay not integer between 1 and 100: %s", args)
logger.warning("rule Later delay not between 0.01 and 100: %s", args)
else:
self.delay = args[0]
self.rule = Rule(args[1:], warn=warn)
@ -1368,7 +1368,10 @@ class Later(Action):
def evaluate(self, feature, notification, device, last_result):
if self.delay and self.rule:
GLib.timeout_add_seconds(self.delay, Rule.once, self.rule, feature, notification, device, last_result)
if self.delay >= 1:
GLib.timeout_add_seconds(int(self.delay), Rule.once, self.rule, feature, notification, device, last_result)
else:
GLib.timeout_add(int(self.delay * 1000), Rule.once, self.rule, feature, notification, device, last_result)
return None
def data(self):

View File

@ -1131,20 +1131,21 @@ class OrUI(RuleComponentUI):
class LaterUI(RuleComponentUI):
CLASS = _DIV.Later
MIN_VALUE = 1
MIN_VALUE = 0.01
MAX_VALUE = 100
def create_widgets(self):
self.widgets = {}
self.label = Gtk.Label(valign=Gtk.Align.CENTER, hexpand=True)
self.label.set_text(_("Number of seconds to delay."))
self.label.set_text(_("Number of seconds to delay. Delay between 0 and 1 is done with higher precision."))
self.widgets[self.label] = (0, 0, 1, 1)
self.field = Gtk.SpinButton.new_with_range(self.MIN_VALUE, self.MAX_VALUE, 1)
self.field.set_digits(3)
self.field.set_halign(Gtk.Align.CENTER)
self.field.set_valign(Gtk.Align.CENTER)
self.field.set_hexpand(True)
# self.field.set_vexpand(True)
self.field.connect("changed", self._on_update)
self.field.connect("value-changed", self._on_update)
self.widgets[self.field] = (0, 1, 1, 1)
def show(self, component, editable):
@ -1153,7 +1154,7 @@ class LaterUI(RuleComponentUI):
self.field.set_value(component.delay)
def collect_value(self):
return [int(self.field.get_value())] + self.component.components
return [float(int((self.field.get_value() + 0.0001) * 1000)) / 1000] + self.component.components
@classmethod
def left_label(cls, component):