Skip to content

Commit 422d1ff

Browse files
authored
Support source in profiles (#485)
Only applicable to commands from items. Dependent on openhab/openhab-core#4990 Signed-off-by: Cody Cutrer <cody@cutrer.us>
1 parent 13fd0da commit 422d1ff

4 files changed

Lines changed: 24 additions & 2 deletions

File tree

lib/openhab/core.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Core
1515
V4_3 = Gem::Version.new("4.3.0").freeze
1616
# @!visibility private
1717
V5_0 = Gem::Version.new("5.0.0").freeze
18+
# @!visibility private
19+
V5_1 = Gem::Version.new("5.1.0").freeze
1820

1921
# @return [Gem::Version] Returns the current openHAB version as a Gem::Version object
2022
# Note, this strips off snapshots, milestones and RC versions and returns the release version.

lib/openhab/core/profile_factory.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def getProfileTypeUID
3737
end
3838

3939
# @!visibility private
40-
def onCommandFromItem(command)
41-
return unless process_event(:command_from_item, command:) == true
40+
def onCommandFromItem(command, source = nil)
41+
return unless process_event(:command_from_item, command:, source:) == true
4242

4343
logger.trace("Forwarding original command")
4444
@callback.handle_command(command)
@@ -92,6 +92,7 @@ def process_event(event, **params)
9292
params[:command] ||= nil
9393
params[:trigger] ||= nil
9494
params[:time_series] ||= nil
95+
params[:source] ||= nil
9596

9697
kwargs = {}
9798
@block.parameters.each do |(param_type, name)|

lib/openhab/dsl.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def script!(name = nil, description: nil, id: nil, tag: nil, tags: nil, **kwargs
158158
# @yieldparam [Core::Things::ChannelUID] channel_uid The linked channel.
159159
# @yieldparam [Hash] configuration The profile configuration.
160160
# @yieldparam [org.openhab.core.thing.profiles.ProfileContext] context The profile context.
161+
# @yieldparam [String, nil] source The source of the event.
162+
# @since openHAB 5.1
161163
# @yieldreturn [Boolean] Return true from the block in order to have default processing.
162164
# @return [void]
163165
#

spec/openhab/dsl_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@
104104
SeasonColor << "0,100,100"
105105
end
106106

107+
it "receives the command source", if: OpenHAB::Core.version >= OpenHAB::Core::V5_1 do
108+
profile :command_source do |item:, source:|
109+
expect(source).to eql "test_source"
110+
item.update("triggered")
111+
false
112+
end
113+
114+
items.build do
115+
string_item MyString,
116+
channel: ["astro:sun:home:season#name", { profile: "ruby:command_source" }],
117+
autoupdate: false
118+
end
119+
120+
MyString.command("foo", source: "test_source")
121+
expect(MyString.state).to eq "triggered"
122+
end
123+
107124
context "with time series" do
108125
it "supports sending a time series" do
109126
profile :send_as_time_series do |event, callback:, command:|

0 commit comments

Comments
 (0)