Skip to content

Commit

Permalink
Support attachment for every with dynamic datetime trigger (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Jul 21, 2024
1 parent 9abfdd7 commit e799691
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
7 changes: 5 additions & 2 deletions lib/openhab/dsl/rules/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 && !attach.nil?
raise ArgumentError, "Attachments are not supported with dynamic datetime triggers in openHAB 3.x"
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
Expand Down
36 changes: 24 additions & 12 deletions spec/openhab/dsl/rules/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1325,23 +1325,35 @@ def self.generate(name, cron_expression, *every_args, attach: nil, **kwargs)
generate("can use day of week", "0 0 0 ? * MON *", :monday)
generate("can use day of week", "0 0 7 ? * MON *", :monday, at: "7am")

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

Expand Down

0 comments on commit e799691

Please sign in to comment.