diff --git a/lib/openhab/core/items/group_function.rb b/lib/openhab/core/items/group_function.rb new file mode 100644 index 0000000000..840846c254 --- /dev/null +++ b/lib/openhab/core/items/group_function.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module OpenHAB + module Core + module Items + java_import org.openhab.core.items.GroupFunction + + # + # Adds `#to_s` and `#inspect` to group function. + # + # @example + # # Group:SWITCH:OR(ON,OFF) Switches + # logger.info "The Switches group function is: #{Switches.function}" # => "OR" + # logger.info "The Switches group function: #{Switches.function.inspect}" # => "OR(ON,OFF)" + # + module GroupFunction + # + # Returns the group function as an uppercase string + # @return [String] + # + def to_s + self.class.simple_name.upcase + end + + # + # Returns the group function and its parameters as a string + # @return [String] + # + def inspect + params = parameters.map(&:inspect).join(",") + params = "(#{params})" unless params.empty? + "#{self}#{params}" + end + end + end + end +end diff --git a/lib/openhab/core/items/group_item.rb b/lib/openhab/core/items/group_item.rb index 9641a3f1fa..5993133deb 100644 --- a/lib/openhab/core/items/group_item.rb +++ b/lib/openhab/core/items/group_item.rb @@ -149,10 +149,7 @@ def format_type(command) def type_details r = "" r += ":#{base_item.type}#{base_item.__send__(:type_details)}" if base_item - if function && (fn = function.class.java_class.simple_name.upcase) != "EQUALITY" - r += ":#{fn}" - r += "(#{function.parameters.map(&:inspect).join(",")})" unless function.parameters.empty? - end + r += ":#{function.inspect}" if function && function.to_s != "EQUALITY" r end diff --git a/spec/openhab/core/items/group_item_spec.rb b/spec/openhab/core/items/group_item_spec.rb index ffd5e7e3b1..9424b682c6 100644 --- a/spec/openhab/core/items/group_item_spec.rb +++ b/spec/openhab/core/items/group_item_spec.rb @@ -4,7 +4,7 @@ before do items.build do group_item "Sensors" do - group_item "Temperatures" + group_item "Temperatures", type: :number, function: "AVG" end group_item "House" do @@ -64,6 +64,23 @@ end end + describe "#function" do + describe "#to_s" do + it "returns the function name in uppercase" do + expect(Temperatures.function.to_s).to eql "AVG" + end + end + + describe "#inspect" do + it "includes the parameters" do + items.build do + group_item "Switches", type: :switch, function: "OR(ON,OFF)" + end + expect(Switches.function.inspect).to eql "OR(ON,OFF)" + end + end + end + describe "#command" do it "propagates to all items" do GroundFloor.command(60)