Skip to content

Commit

Permalink
Add thing parameter for ItemBuilder (#137)
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored Sep 7, 2023
1 parent 3f92519 commit a99db48
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/openhab/dsl/items/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def normalize_tags(*tags)
# @param tags [String, Symbol, Semantics::Tag, Array<String, Symbol, Semantics::Tag>, nil]
# 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.
# @param alexa [String, Symbol, Array<(String, Hash<String, Object>)>, nil]
Expand All @@ -279,6 +281,7 @@ def initialize(type, name = nil, label = nil,
tag: nil,
tags: nil,
autoupdate: nil,
thing: nil,
channel: nil,
expire: nil,
alexa: nil,
Expand Down Expand Up @@ -308,6 +311,7 @@ def initialize(type, name = nil, label = nil,
@metadata.merge!(metadata) if metadata
@autoupdate = autoupdate
@channels = []
@thing = thing
@expire = nil
if expire
expire = Array(expire)
Expand Down Expand Up @@ -418,6 +422,7 @@ def group(*groups)
# end
#
def channel(channel, config = {})
channel = "#{@thing}:#{channel}" if @thing && !channel.include?(":")
@channels << [channel, config]
end

Expand Down
58 changes: 58 additions & 0 deletions spec/openhab/dsl/items/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,59 @@
expect(StringItem1.thing).to be things["astro:sun:home"]
end

it "can link an item to multiple channels" do
things.build do
thing "astro:moon:home", "Astro Moon Data", config: { "geolocation" => "0,0" }
end
items.build do
date_time_item "DateTime1" do
channel "astro:sun:home:rise#start"
channel "astro:moon:home:rise#start"
end
end
expect(DateTime1.things).to match_array([things["astro:sun:home"], things["astro:moon:home"]])
end

it "can link to an item channel with a profile" do
items.build do
date_time_item "LastUpdated", channel: ["astro:sun:home:season#name", { profile: "system:timestamp-update" }]
end
expect(LastUpdated.thing).to be things["astro:sun:home"]
end

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

it "combines thing and channel" do
items.build do
string_item "StringItem1", thing: things["astro:sun:home"], channel: "season#name"
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"
end
expect(StringItem1.thing).to be things["astro:sun:home"]
end

it "allows mixing short and fully qualified channels" do
things.build do
thing "astro:moon:home", "Astro Moon Data", config: { "geolocation" => "0,0" }
end
items.build do
string_item "StringItem1", thing: "astro:moon:home", channel: "astro:sun:home:rise#start" do
channel "rise#start"
end
end
expect(StringItem1.things).to match_array [things["astro:sun:home"], things["astro:moon:home"]]
end

it "implicitly assumes a group's thing (string) for channels" do
items.build do
group_item "MyGroup", thing: "astro:sun:home" do
Expand Down Expand Up @@ -372,5 +418,17 @@
end
expect(StringItem1.thing).to be things["astro:sun:home"]
end

it "item's thing overrides group's thing" do
things.build do
thing "astro:moon:home", "Astro Moon Data", config: { "geolocation" => "0,0" }
end
items.build do
group_item "MyGroup", thing: "astro:sun:home" do
string_item "StringItem1", thing: "astro:moon:home", channel: "set#start"
end
end
expect(StringItem1.thing).to be things["astro:moon:home"]
end
end
end

0 comments on commit a99db48

Please sign in to comment.