From d0b9362978f95c3fc91a62707eb26aee133ea501 Mon Sep 17 00:00:00 2001 From: Jimmy Tanagra Date: Thu, 11 Jul 2024 11:32:42 +1000 Subject: [PATCH] Support attachment for `every` trigger Signed-off-by: Jimmy Tanagra --- lib/openhab/dsl/rules/builder.rb | 7 +++-- spec/openhab/dsl/rules/builder_spec.rb | 36 +++++++++++++++++--------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/openhab/dsl/rules/builder.rb b/lib/openhab/dsl/rules/builder.rb index 77995335d..a2c0cbe26 100644 --- a/lib/openhab/dsl/rules/builder.rb +++ b/lib/openhab/dsl/rules/builder.rb @@ -1369,9 +1369,12 @@ def every(value, at: nil, attach: nil) @ruby_triggers << [:every, value, { at: at }] if value == :day && at.is_a?(Item) - raise ArgumentError, "Attachments are not supported with dynamic datetime triggers" unless attach.nil? + # @!deprecated OH 3.4 - attachments are supported in OH 4.0+ + if Core.version <= Core::V4_0 + raise ArgumentError, "Attachments are not supported with dynamic datetime triggers" + end - return trigger("timer.DateTimeTrigger", itemName: at.name, timeOnly: true) + return trigger("timer.DateTimeTrigger", itemName: at.name, timeOnly: true, attach: attach) end cron_expression = case value diff --git a/spec/openhab/dsl/rules/builder_spec.rb b/spec/openhab/dsl/rules/builder_spec.rb index 8fda9b4ce..54e91dc4d 100644 --- a/spec/openhab/dsl/rules/builder_spec.rb +++ b/spec/openhab/dsl/rules/builder_spec.rb @@ -1323,23 +1323,35 @@ def self.generate(name, cron_expression, *every_args, attach: nil, **kwargs) generate("can use MonthDay as a string", "0 0 12 17 11 ? *", "11-17", at: LocalTime.parse("12:00")) generate("can use LocalTime a string", "0 0 12 17 11 ? *", MonthDay.parse("11-17"), at: "12:00") - it "supports dynamic `at`" do - items.build { date_time_item MyDateTimeItem } + context "with dynamic `at`" do + let(:item) { items.build { date_time_item MyDateTimeItem } } - triggered = false - every(:day, id: "dynamic_at_rule", at: MyDateTimeItem) { triggered = true } + it "works" do + triggered = false + every(:day, id: "dynamic_at_rule", at: item) { triggered = true } - rule = rules["dynamic_at_rule"] - trigger = rule.triggers.first + rule = rules["dynamic_at_rule"] + trigger = rule.triggers.first - handler = OpenHAB::Core::Rules.manager.get_module_handler_factory(trigger.type_uid) - .get_handler(trigger, rule.uid) + handler = OpenHAB::Core::Rules.manager.get_module_handler_factory(trigger.type_uid) + .get_handler(trigger, rule.uid) - expect(handler.getTemporalAdjuster).not_to be_nil + expect(handler.getTemporalAdjuster).not_to be_nil - MyDateTimeItem.update(Time.now + 2 - 2.days) - wait(4.seconds) do - expect(triggered).to be true + item.update(Time.now + 2 - 2.days) + wait(4.seconds) do + expect(triggered).to be true + end + end + + it "supports attachments", if: OpenHAB::Core.version >= OpenHAB::Core::V4_0 do + triggered = nil + every(:day, id: "dynamic_at_rule", at: item, attach: 1) { |event| triggered = event.attachment } + + item.update(Time.now + 2 - 2.days) + wait(4.seconds) do + expect(triggered).to be 1 + end end end