Skip to content

Commit

Permalink
Allow symbolic channel name in ItemBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored and ccutrer committed Sep 22, 2023
1 parent d1c5684 commit 39530ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/openhab/dsl/items/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ def normalize_tags(*tags)
# Fluent alias for `tag`.
# @param autoupdate [true, false, nil] Autoupdate setting (see {ItemBuilder#autoupdate})
# @param thing [String, Core::Things::Thing, Core::Things::ThingUID, nil]
# A Thing to be used as the base for the channel
# @param channel [String, Core::Things::ChannelUID, nil] Channel to link the item to
# @param expire [String] An expiration specification.
# A Thing to be used as the base for the channel.
# @param channel [String, Core::Things::ChannelUID, nil]
# Channel to link the item to (see {ItemBuilder#channel}).
# @param expire [String] An expiration specification (see {ItemBuilder#expire}).
# @param alexa [String, Symbol, Array<(String, Hash<String, Object>)>, nil]
# Alexa metadata (see {ItemBuilder#alexa})
# @param ga [String, Symbol, Array<(String, Hash<String, Object>)>, nil]
Expand Down Expand Up @@ -413,7 +414,9 @@ def group(*groups)
#
# Add a channel link to this item.
#
# @param config [Hash] Additional configuration, such as profile
# @param [String, Core::Things::ChannelUID, Symbol] channel Channel to link the item to.
# When thing is set, this can be a relative channel name.
# @param [Hash] config Additional configuration, such as profile
# @return [void]
#
# @example
Expand All @@ -423,7 +426,13 @@ def group(*groups)
# end
# end
#
# @example Relative channel name
# items.build do
# switch_item Bedroom_Light, thing: "mqtt:topic:bedroom-light", channel: :power
# end
#
def channel(channel, config = {})
channel = channel.to_s
channel = "#{@thing}:#{channel}" if @thing && !channel.include?(":")
@channels << [channel, config]
end
Expand Down
9 changes: 9 additions & 0 deletions spec/openhab/dsl/items/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@
expect(StringItem1.thing).to be things["astro:sun:home"]
end

it "can use symbolic channel" do
items.build do
string_item "StringItem1", thing: "astro:sun:home" do
channel :"season#name"
end
end
expect(StringItem1.thing).to be things["astro:sun:home"]
end

it "ignores thing when channel contains multiple segments" do
items.build do
string_item "StringItem1", thing: "foo:baz:bar", channel: "astro:sun:home:season#name"
Expand Down

0 comments on commit 39530ad

Please sign in to comment.