Skip to content

Commit

Permalink
Support creating a trigger profile (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Jul 23, 2024
1 parent 8468844 commit 9eebc86
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/openhab/core/profile_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Profile
else
include org.openhab.core.thing.profiles.StateProfile
end
include org.openhab.core.thing.profiles.TriggerProfile

def initialize(callback, context, uid, thread_locals, block)
unless callback.class.ancestors.include?(Things::ProfileCallback)
Expand Down Expand Up @@ -69,6 +70,11 @@ def onStateUpdateFromItem(state)
process_event(:state_from_item, state: state)
end

# @!visibility private
def onTriggerFromHandler(event)
process_event(:trigger_from_handler, trigger: event)
end

# @deprecated OH 4.0 guard is only needed for < OH 4.1
if OpenHAB::Core.version >= OpenHAB::Core::V4_1
# @!visibility private
Expand All @@ -90,6 +96,7 @@ def process_event(event, **params)
params[:channel_uid] = @callback.link.linked_uid
params[:state] ||= nil
params[:command] ||= nil
params[:trigger] ||= nil
# @deprecated OH 4.0 guard is only needed for < OH 4.1
params[:time_series] ||= nil if OpenHAB::Core.version >= OpenHAB::Core::V4_1

Expand Down
7 changes: 5 additions & 2 deletions lib/openhab/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ def script!(name = nil, description: nil, id: nil, tag: nil, tags: nil, **kwargs
# @param [String, nil] label The label for the profile. When nil, the profile will not be visible in the UI.
# @param [org.openhab.core.config.core.ConfigDescription, nil] config_description
# The configuration description for the profile so that it can be configured in the UI.
# @yield [event, command: nil, state: nil, time_series: nil, callback:, link:, item:, channel_uid:, configuration:, context:]
# @yield [event, command: nil, state: nil, trigger: nil, time_series: nil, callback:, link:, item:, channel_uid:, configuration:, context:]
# All keyword params are optional. Any that aren't defined won't be passed.
# @yieldparam [:command_from_item, :state_from_item, :command_from_handler, :state_from_handler, :time_series_from_handler] event
# @yieldparam [:command_from_item, :state_from_item, :command_from_handler, :state_from_handler, :time_series_from_handler, :trigger_from_handler] event
# The event that needs to be processed.
# @yieldparam [Command, nil] command
# The command being sent for `:command_from_item` and `:command_from_handler` events.
# @yieldparam [State, nil] state
# The state being sent for `:state_from_item` and `:state_from_handler` events.
# @yieldparam [String] trigger
# The event being sent for `:trigger_from_handler` events.
# @yieldparam [TimeSeries] time_series
# The time series for `:time_series_from_handler` events.
# Only available since openHAB 4.1.
Expand All @@ -131,6 +133,7 @@ def script!(name = nil, description: nil, id: nil, tag: nil, tags: nil, **kwargs
# @see org.openhab.core.thing.profiles.Profile
# @see org.openhab.core.thing.profiles.StateProfile
# @see org.openhab.core.thing.profiles.TimeSeriesProfile
# @see org.openhab.core.thing.profiles.TriggerProfile
#
# @example Vetoing a command
# profile(:veto_closing_shades) do |event, item:, command:|
Expand Down
19 changes: 19 additions & 0 deletions spec/openhab/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@
profile(:test, label: "Test Profile", config_description: config_description) { |_event| true }
end
end

context "with trigger channels" do
before do
things.build { thing "astro:sun:home", config: { "geolocation" => "0,0" } }
items.build do
string_item "MyString" do
channel "astro:sun:home:rise#event", profile: "ruby:trigger_profile"
end
end
end

it "works" do
triggered_event = nil
profile(:trigger_profile) { |event, trigger:| triggered_event = trigger if event == :trigger_from_handler }
trigger_channel("astro:sun:home:rise#event", "START")

expect(triggered_event).to eql "START"
end
end
end

describe "#rule" do
Expand Down

0 comments on commit 9eebc86

Please sign in to comment.