diff --git a/lib/openhab/dsl/items/builder.rb b/lib/openhab/dsl/items/builder.rb index 89a6bd94f4..da4b008516 100644 --- a/lib/openhab/dsl/items/builder.rb +++ b/lib/openhab/dsl/items/builder.rb @@ -258,6 +258,8 @@ def normalize_tags(*tags) # @param tags [String, Symbol, Semantics::Tag, Array, 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)>, nil] @@ -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, @@ -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) @@ -418,6 +422,7 @@ def group(*groups) # end # def channel(channel, config = {}) + channel = "#{@thing}:#{channel}" if @thing && !channel.include?(":") @channels << [channel, config] end diff --git a/spec/openhab/dsl/items/builder_spec.rb b/spec/openhab/dsl/items/builder_spec.rb index 496974635f..c6caec23c5 100644 --- a/spec/openhab/dsl/items/builder_spec.rb +++ b/spec/openhab/dsl/items/builder_spec.rb @@ -326,6 +326,19 @@ 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" }] @@ -333,6 +346,39 @@ 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 @@ -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