Skip to content

Commit

Permalink
Warn when creating a tag that already exists (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Oct 14, 2023
1 parent e07b20c commit 2515c24
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/openhab/core/items/semantics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,18 @@ def add(label: nil, synonyms: "", description: "", **tags)
synonyms = Array.wrap(synonyms).map { |s| s.to_s.strip }

tags.map do |name, parent|
if (existing_tag = lookup(name))
logger.warn("Tag already exists: #{existing_tag.inspect}")
next
end

unless parent.is_a?(SemanticTag)
parent_tag = lookup(parent)
raise ArgumentError, "Unknown parent: #{parent}" unless parent_tag

parent = parent_tag
end

next if lookup(name)

new_tag = org.openhab.core.semantics.SemanticTagImpl.new("#{parent.uid}_#{name}",
label,
description,
Expand Down
6 changes: 6 additions & 0 deletions spec/openhab/core/items/semantics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ def points(*args)
expect(Semantics.lookup("Syn1")).to be Semantics::ArraySynonyms
expect(Semantics.lookup("Syn2")).to be Semantics::ArraySynonyms
end

it "warns when trying to create a tag that already exists" do
Semantics.add(ExistingTag: Semantics::Equipment)
expect(Semantics.logger).to receive(:warn).with(/already exists/)
Semantics.add(ExistingTag: Semantics::Point)
end
end

describe "#remove" do
Expand Down

0 comments on commit 2515c24

Please sign in to comment.