Skip to content

Commit

Permalink
Add CallItem and StringListType support (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Jun 27, 2024
1 parent 46daa05 commit 65f5830
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 1 deletion.
29 changes: 29 additions & 0 deletions lib/openhab/core/items/call_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative "generic_item"

module OpenHAB
module Core
module Items
java_import org.openhab.core.library.items.CallItem

#
# A {CallItem} identifies a telephone call by its origin and destination.
#
# @!attribute [r] state
# @return [StringListType, nil]
#
class CallItem < GenericItem
# @!visibility private
def format_type(command)
return command if command.is_a?(Types::StringListType)
return Types::StringListType.new(command.to_ary.map(&:to_s)) if command.respond_to?(:to_ary)

super
end
end
end
end
end

# @!parse CallItem = OpenHAB::Core::Items::CallItem
1 change: 1 addition & 0 deletions lib/openhab/core/items/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def config_eql?(other)
end
end

def_type_predicate(:call)
def_type_predicate(:color)
def_type_predicate(:contact)
def_type_predicate(:date_time)
Expand Down
55 changes: 55 additions & 0 deletions lib/openhab/core/types/string_list_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "forwardable"

require_relative "type"

module OpenHAB
module Core
module Types
StringListType = org.openhab.core.library.types.StringListType

# {StringListType} can be used for items that are dealing with telephony functionality.
#
# The entries can be accessed like an array.
#
# @example
# string_list = StringListType.new("a", "b", "c")
# logger.info "first entry: #{string_list.first}" # => "a"
# logger.info "second entry: #{string_list[1]}" # => "b"
# logger.info "last entry: #{string_list.last}" # => "c"
# logger.info "length: #{string_list.size}" # => 3
#
class StringListType
extend Forwardable

field_reader :typeDetails

# @!parse include Command, State

# @!visibility private
def inspect
"#<OpenHAB::Core::Types::StringListType #{to_a.inspect}>"
end

# @return [Array<String>] the values as an array
def to_a
typeDetails.to_a
end

# @!visibility private
def ==(other)
return super if other.is_a?(StringListType)
return to_a == other.to_a if other.respond_to?(:to_a)

super
end

# any method that exists on Array gets forwarded to states
delegate (Array.instance_methods - instance_methods) => :to_a
end
end
end
end

# @!parse StringListType = OpenHAB::Core::Types::StringListType
2 changes: 2 additions & 0 deletions lib/openhab/dsl/items/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def #{method}_item(*args, **kwargs, &block) # def dimmer_item(*args, **k
end
end

# @return [CallItem]
def_item_method(:call)
# @return [ColorItem]
def_item_method(:color)
# @return [ContactItem]
Expand Down
27 changes: 27 additions & 0 deletions spec/openhab/core/items/call_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

RSpec.describe OpenHAB::Core::Items::CallItem do
subject(:item) { CallOne }

before do
items.build do
group_item "Calls" do
call_item "CallOne"
call_item "CallTwo"
end
end
end

it "is a call item" do
expect(item).to be_a_call_item
end

it "is not a group" do
expect(item).not_to be_a_group_item
end

it "works with grep" do
items.build { switch_item "SwitchOne" }
expect(items.grep(CallItem)).to match_array [CallOne, CallTwo]
end
end
40 changes: 40 additions & 0 deletions spec/openhab/core/types/string_list_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

RSpec.describe OpenHAB::Core::Types::StringListType do
let(:state) { StringListType.new(%w[a b]) }

it "is inspectable" do
expect(state.inspect).to eql '#<OpenHAB::Core::Types::StringListType ["a", "b"]>'
end

it "converts to an array" do
expect(state.to_a).to eql %w[a b]
end

it "supports array operations" do
expect(state.first).to eql "a"
expect(state.last).to eql "b"
expect(state.size).to be 2
end

describe "comparisons" do
let(:state2) { StringListType.new(%w[a b]) }

specify { expect(state == %w[a b]).to be true }
specify { expect(state == %w[a c]).to be false }
specify { expect(state != %w[a b]).to be false }
specify { expect(state != %w[a c]).to be true }
specify { expect(state == state2).to be true }
specify { expect(state != state2).to be false }
end

describe "#eql?" do
it "works" do
expect(state).to eql StringListType.new(%w[a b])
end

it "returns false when compared against a plain array" do
expect(state).not_to eql %w[a b]
end
end
end
2 changes: 1 addition & 1 deletion spec/openhab/dsl/items/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe OpenHAB::DSL::Items::Builder do
%i[color contact date_time dimmer group image location number player rollershutter string switch].each do |type|
%i[call color contact date_time dimmer group image location number player rollershutter string switch].each do |type|
it "can create a #{type} item" do
items.build { send(:"#{type}_item", "MyItem", "My Label") }
expect(MyItem.label).to eql "My Label"
Expand Down

0 comments on commit 65f5830

Please sign in to comment.