Skip to content

Commit

Permalink
Add TagClassMethods
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng committed Apr 15, 2023
1 parent d3c6d8c commit 073a422
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
51 changes: 50 additions & 1 deletion lib/openhab/core/items/semantics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,56 @@ def self.const_missing(sym)
sym = :MeasurementProperty if sym == :Property && Gem::Version.new(Core::VERSION) < "4"

org.openhab.core.semantics.SemanticTags.get_by_id(sym.to_s)
&.then { |tag| const_set(sym, tag.ruby_class) }
&.then do |tag|
tag = tag.ruby_class
tag.singleton_class.include(TagClassMethods)
const_set(sym, tag)
end
end

# Adds tag attributes
module TagClassMethods
java_import org.openhab.core.semantics.SemanticTags

#
# Returns the tag's label
#
# @param [java.util.Locale] locale The locale that the label should be in, if available.
# When nil, the system's default locale is used.
#
# @return [String] The tag's label
#
def label(locale = nil)
SemanticTags.get_label(java_class, locale || java.util.Locale.default)
end

#
# REturns the tag's synonyms
#
# @param [java.util.Locale] locale The locale that the label should be in, if available.
# When nil, the system's default locale is used.
#
# @return [Array<String>] The list of synonyms in the requested locale.
#
def synonyms(locale = nil)
return "" unless SemanticTags.respond_to?(:get_synonyms) # @deprecated OH3.4

SemanticTags.get_synonyms(java_class, locale || java.util.Locale.default)
end

#
# Returns the tag's description
#
# @param [java.util.Locale] locale The locale that the description should be in, if available.
# When nil, the system's default locale is used.
#
# @return [String] The tag's description
#
def description(locale = nil)
return "" unless SemanticTags.respond_to?(:get_description) # @deprecated OH3.4

SemanticTags.get_description(java_class, locale || java.util.Locale.default)
end
end
end
end
Expand Down
20 changes: 19 additions & 1 deletion spec/openhab/core/items/semantics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def points(*args)
description: "Description 3")
java_import org.openhab.core.semantics.SemanticTags
locale = java.util.Locale.default
expect(SemanticTags.get_label(Semantics::Detailed, locale)).to eq "Label 1"
expect(Semantics::Detailed.label).to eq "Label 1"
expect(SemanticTags.get_by_label_or_synonym("Synonym 2", locale).first).to eql Semantics::Detailed.java_class
description = Semantics::Detailed.java_class
.get_annotation(org.openhab.core.semantics.TagInfo.java_class)
Expand Down Expand Up @@ -389,4 +389,22 @@ def points(*args)
end
end
end

describe "Tag Info" do
it "has a label attribute" do
expect(Semantics::LivingRoom.label).to eq "Living Room"
end

# @deprecated OH3.4 - guard can be removed in OH4
if Gem::Version.new(OpenHAB::Core::VERSION) >= "4"
it "has a synonyms attribute" do
expect(Semantics::LivingRoom.synonyms).to include("Living Rooms")
end

it "has a description attribute" do
Semantics.add(TestDescAttr: Semantics::Room, description: "Test Description Attribute")
expect(Semantics::LivingRoom.description).to eq "Test Description Attribute"
end
end
end
end

0 comments on commit 073a422

Please sign in to comment.