Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thing parameter for ItemBuilder #137

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -295,13 +295,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"
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -341,5 +387,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