Skip to content

Commit

Permalink
Allow changed trigger on any Thing (#296)
Browse files Browse the repository at this point in the history
Note, triggers on item "*" isn't currently supported in core, so it's
not added here.

Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored Jul 2, 2024
1 parent d382487 commit 3e960c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/openhab/dsl/rules/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ def channel_unlinked(item: nil, channel: nil, attach: nil)
# {Core::Events::ThingStatusInfoChangedEvent} depending on if the
# triggering element was an item or a thing.
#
# @param [Item, GroupItem::Members, Thing] items Objects to create trigger for.
# @param [Item, GroupItem::Members, Thing, ThingUID, Things::Registry] items Objects to create trigger for.
# @param [State, Array<State>, #===, nil] from
# Only execute rule if previous state matches `from` state(s).
# @param [State, Array<State>, #===, nil] to
Expand Down Expand Up @@ -1147,6 +1147,14 @@ def channel_unlinked(item: nil, channel: nil, attach: nil)
# run { |event| logger.info("Thing #{event.uid} status <trigger> to #{event.status}") }
# end
#
# @example Trigger when any Thing changes status
# rule "Thing status monitoring" do
# changed things, to: :offline
# run do |event|
# notify("Thing #{event.thing.uid} is offline")
# end
# end
#
# @example Real World Example
# rule "Log (or notify) when an exterior door is left open for more than 5 minutes" do
# changed ExteriorDoors.members, to: OPEN, for: 5.minutes
Expand All @@ -1166,11 +1174,13 @@ def changed(*items, to: nil, from: nil, for: nil, attach: nil)
case item
when Core::Things::Thing,
Core::Things::ThingUID,
Core::Things::Registry,
Core::Items::Item,
Core::Items::GroupItem::Members
nil
else
raise ArgumentError, "items must be an Item, GroupItem::Members, Thing, or ThingUID"
raise ArgumentError,
"items must be an Item, GroupItem::Members, Thing, ThingUID, or Things::Registry"
end

logger.trace { "Creating changed trigger for entity(#{item}), to(#{to.inspect}), from(#{from.inspect})" }
Expand Down
2 changes: 2 additions & 0 deletions lib/openhab/dsl/rules/triggers/changed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def changed_trigger(item:, from:, to:, attach: nil, conditions: nil, label: nil)
when Core::Things::Thing,
Core::Things::ThingUID
thing(thing: item, from: from, to: to)
when Core::Things::Registry
thing(thing: "*", from: from, to: to)
else
item(item: item, from: from, to: to)
end
Expand Down
21 changes: 21 additions & 0 deletions spec/openhab/dsl/rules/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,27 @@ def self.test_changed_trigger(item,
thing.disable
expect(triggered).to be true
end

it "can trigger on any Thing" do
moon = things.build { thing "astro:moon:home", config: { "geolocation" => "0,0" } }
sun = thing

# When it's online to disabled, we are getting two events for each thing
# so we test from disabled to online instead
sun.disable
moon.disable

triggered = []
changed things, to: :online do |event|
triggered << event.thing
end
sun.enable
expect(triggered).to match([sun])

triggered = []
moon.enable
expect(triggered).to match([moon])
end
end
end

Expand Down

0 comments on commit 3e960c8

Please sign in to comment.