openhab-scripting 4.1.3 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +1 -57
  3. data/lib/openhab/dsl/actions.rb +2 -3
  4. data/lib/openhab/dsl/dsl.rb +8 -12
  5. data/lib/openhab/dsl/group.rb +1 -5
  6. data/lib/openhab/dsl/items/comparable_item.rb +49 -0
  7. data/lib/openhab/dsl/items/contact_item.rb +41 -0
  8. data/lib/openhab/dsl/items/date_time_item.rb +64 -0
  9. data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
  10. data/lib/openhab/dsl/items/ensure.rb +93 -0
  11. data/lib/openhab/dsl/items/generic_item.rb +174 -0
  12. data/lib/openhab/dsl/items/group_item.rb +121 -89
  13. data/lib/openhab/dsl/items/image_item.rb +5 -41
  14. data/lib/openhab/dsl/items/item_equality.rb +36 -0
  15. data/lib/openhab/dsl/items/item_registry.rb +49 -0
  16. data/lib/openhab/dsl/items/items.rb +80 -35
  17. data/lib/openhab/dsl/items/metadata.rb +325 -0
  18. data/lib/openhab/dsl/items/number_item.rb +6 -312
  19. data/lib/openhab/dsl/items/numeric_item.rb +68 -0
  20. data/lib/openhab/dsl/items/persistence.rb +122 -0
  21. data/lib/openhab/dsl/items/player_item.rb +49 -40
  22. data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
  23. data/lib/openhab/dsl/items/string_item.rb +16 -58
  24. data/lib/openhab/dsl/items/switch_item.rb +62 -0
  25. data/lib/openhab/dsl/lazy_array.rb +8 -6
  26. data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
  27. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
  28. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
  29. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
  30. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
  31. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
  32. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
  33. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
  34. data/lib/openhab/dsl/rules/terse.rb +24 -0
  35. data/lib/openhab/dsl/states.rb +1 -1
  36. data/lib/openhab/dsl/time_of_day.rb +3 -5
  37. data/lib/openhab/dsl/types/comparable_type.rb +21 -0
  38. data/lib/openhab/dsl/types/date_time_type.rb +334 -0
  39. data/lib/openhab/dsl/types/decimal_type.rb +187 -0
  40. data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
  41. data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
  42. data/lib/openhab/dsl/types/numeric_type.rb +39 -0
  43. data/lib/openhab/dsl/types/on_off_type.rb +29 -0
  44. data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
  45. data/lib/openhab/dsl/types/percent_type.rb +68 -0
  46. data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
  47. data/lib/openhab/dsl/types/quantity_type.rb +275 -0
  48. data/lib/openhab/dsl/types/refresh_type.rb +18 -0
  49. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
  50. data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
  51. data/lib/openhab/dsl/types/string_type.rb +88 -0
  52. data/lib/openhab/dsl/types/type.rb +72 -0
  53. data/lib/openhab/dsl/types/types.rb +77 -0
  54. data/lib/openhab/dsl/types/un_def_type.rb +22 -0
  55. data/lib/openhab/dsl/types/up_down_type.rb +32 -0
  56. data/lib/openhab/dsl/units.rb +11 -6
  57. data/lib/openhab/version.rb +1 -1
  58. data/lib/openhab.rb +0 -1
  59. metadata +34 -28
  60. data/lib/openhab/dsl/items/datetime_item.rb +0 -75
  61. data/lib/openhab/dsl/items/item_command.rb +0 -90
  62. data/lib/openhab/dsl/items/item_delegate.rb +0 -125
  63. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
  64. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
  65. data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
  66. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
  67. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
  68. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
  69. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
  70. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
  71. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
  72. data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
  73. data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
  74. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
  75. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
  76. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
  77. data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
  78. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
  79. data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
  80. data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
  81. data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
  82. data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
  83. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
  84. data/lib/openhab/dsl/types/datetime.rb +0 -338
  85. data/lib/openhab/dsl/types/quantity.rb +0 -300
@@ -1,81 +1,121 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'delegate'
4
- require 'forwardable'
5
- require 'java'
6
-
7
- require 'openhab/core/entity_lookup'
8
- require 'openhab/dsl/items/item_command'
9
- require 'openhab/dsl/items/item_delegate'
3
+ require_relative 'comparable_item'
10
4
  require 'openhab/dsl/lazy_array'
11
5
 
12
6
  module OpenHAB
13
7
  module DSL
14
8
  module Items
15
- #
16
- # Class for indicating to triggers that a group trigger should be used
17
- #
18
- class GroupMembers
19
- include LazyArray
20
-
21
- attr_reader :group
9
+ java_import org.openhab.core.items.GroupItem
22
10
 
11
+ # Adds methods to core OpenHAB GroupItem type to make it more natural in
12
+ # Ruby
13
+ class GroupItem < GenericItem
23
14
  #
24
- # Create a new GroupMembers instance from a GroupItem
15
+ # Class for indicating to triggers that a group trigger should be used
25
16
  #
26
- # @param [GroupItem] group_item GroupItem to use as trigger
27
- #
28
- def initialize(group_item)
29
- @group = group_item
30
- end
17
+ class GroupMembers
18
+ include LazyArray
31
19
 
32
- # explicit conversion to array
33
- def to_a
34
- OpenHAB::Core::EntityLookup.decorate_items(group.members.to_a)
35
- end
36
- end
20
+ # @return [GroupItem]
21
+ attr_reader :group
37
22
 
38
- #
39
- # Delegator to OpenHAB Group Item
40
- #
41
- class GroupItem
42
- extend OpenHAB::DSL::Items::ItemCommand
43
- extend OpenHAB::DSL::Items::ItemDelegate
44
- include Enumerable
45
- include Comparable
23
+ # @!visibility private
24
+ def initialize(group_item)
25
+ @group = group_item
26
+ end
46
27
 
47
- def_item_delegator :@group_item
28
+ # Explicit conversion to Array
29
+ #
30
+ # @return [Array]
31
+ def to_a
32
+ group.get_members.to_a
33
+ end
48
34
 
49
- #
50
- # @return [Hash] A hash of lambdas with default filters for `all_members`
51
- #
52
- DEFAULT_FILTERS = {
53
- groups: ->(item) { item.is_a?(Java::OrgOpenhabCoreItems::GroupItem) },
54
- all: -> { true }
55
- }.freeze
35
+ # Send a command to each member of the group
36
+ #
37
+ # @return [GroupMembers] +self+
38
+ def command(command)
39
+ each { |item| item << command }
40
+ end
41
+ alias << command
56
42
 
57
- private_constant :DEFAULT_FILTERS
43
+ # @!method refresh
44
+ # Send the +REFRESH+ command to each member of the group
45
+ # @return [GroupMembers] +self+
58
46
 
59
- #
60
- # Create a new GroupItem
61
- #
62
- # @param [Java::Org::openhab::core::items::GroupItem] group_item OpenHAB GroupItem to delegate to
63
- #
64
- def initialize(group_item)
65
- @group_item = group_item
47
+ # @!method on
48
+ # Send the +ON+ command to each member of the group
49
+ # @return [GroupMembers] +self+
50
+
51
+ # @!method off
52
+ # Send the +OFF+ command to each member of the group
53
+ # @return [GroupMembers] +self+
54
+
55
+ # @!method up
56
+ # Send the +UP+ command to each member of the group
57
+ # @return [GroupMembers] +self+
58
+
59
+ # @!method down
60
+ # Send the +DOWN+ command to each member of the group
61
+ # @return [GroupMembers] +self+
62
+
63
+ # @!method stop
64
+ # Send the +STOP+ command to each member of the group
65
+ # @return [GroupMembers] +self+
66
+
67
+ # @!method move
68
+ # Send the +MOVE+ command to each member of the group
69
+ # @return [GroupMembers] +self+
70
+
71
+ # @!method increase
72
+ # Send the +INCREASE+ command to each member of the group
73
+ # @return [GroupMembers] +self+
74
+
75
+ # @!method desrease
76
+ # Send the +DECREASE+ command to each member of the group
77
+ # @return [GroupMembers] +self+
78
+
79
+ # @!method play
80
+ # Send the +PLAY+ command to each member of the group
81
+ # @return [GroupMembers] +self+
82
+
83
+ # @!method pause
84
+ # Send the +PAUSE+ command to each member of the group
85
+ # @return [GroupMembers] +self+
86
+
87
+ # @!method rewind
88
+ # Send the +REWIND+ command to each member of the group
89
+ # @return [GroupMembers] +self+
66
90
 
67
- item_missing_delegate { @group_item }
68
- item_missing_delegate { OpenHAB::Core::EntityLookup.decorate_item(base_item) }
91
+ # @!method fast_forward
92
+ # Send the +FASTFORWARD+ command to each member of the group
93
+ # @return [GroupMembers] +self+
94
+
95
+ # @!method next
96
+ # Send the +NEXT+ command to each member of the group
97
+ # @return [GroupMembers] +self+
98
+
99
+ # @!method previous
100
+ # Send the +PREVIOUS+ command to each member of the group
101
+ # @return [GroupMembers] +self+
69
102
  end
70
103
 
104
+ include Enumerable
105
+ include ComparableItem
106
+
107
+ remove_method :==
108
+
71
109
  #
72
- # Create a GroupMembers object for use in triggers
110
+ # Get an Array-like object representing the members of the group
73
111
  #
74
- # @return [GroupMembers] A GroupMembers object
112
+ # @return [GroupMembers]
75
113
  #
76
114
  def members
77
- GroupMembers.new(@group_item)
115
+ GroupMembers.new(self)
78
116
  end
117
+
118
+ # @deprecated
79
119
  alias items members
80
120
 
81
121
  #
@@ -89,51 +129,43 @@ module OpenHAB
89
129
  # Get all members of the group recursively. Optionally filter the items to only return
90
130
  # Groups or regular Items
91
131
  #
92
- # @param [Symbol] filter Either :groups or :items
132
+ # @param [Symbol] filter Either +:groups+ or +:items+
93
133
  #
94
134
  # @return [Array] An Array containing all descendants of the Group, optionally filtered
95
135
  #
96
136
  def all_members(filter = nil, &block)
97
- predicate = DEFAULT_FILTERS[filter] || block
137
+ filter = nil if filter == :items
138
+ raise ArgumentError, 'filter must be :groups or :items' unless [:groups, nil].include?(filter)
98
139
 
99
- return OpenHAB::Core::EntityLookup.decorate_items(@group_item.all_members.to_a) unless predicate
140
+ block = ->(i) { i.is_a?(GroupItem) } if filter
100
141
 
101
- OpenHAB::Core::EntityLookup.decorate_items(@group_item.get_members(&predicate).to_a)
102
- end
103
-
104
- #
105
- # Test for equality
106
- #
107
- # @param [Object] other Other object to compare against
108
- #
109
- # @return [Boolean] true if self and other can be considered equal, false otherwise
110
- #
111
- def ==(other)
112
- if other.respond_to?(:java_class) && accepted_data_types.include?(other.java_class)
113
- get_state_as(other.class) == other
114
- elsif other.respond_to?(:state)
115
- base_item ? OpenHAB::Core::EntityLookup.decorate_item(base_item) == other.state : self == other.state
142
+ if block
143
+ get_members(&block).to_a
116
144
  else
117
- super
145
+ get_all_members.to_a
118
146
  end
119
147
  end
120
148
 
121
- #
122
- # Compare GroupItem to supplied object
123
- #
124
- # @param [Object] other object to compare to
125
- #
126
- # @return [Integer] -1,0,1 or nil depending on value supplied,
127
- # nil comparison to supplied object is not possible.
128
- #
129
- def <=>(other)
130
- if base_item
131
- OpenHAB::Core::EntityLookup.decorate_item(base_item) <=> other
132
- elsif state?
133
- -(other <=> state)
134
- else
135
- super
136
- end
149
+ # Delegate missing methods to +base_item+ if possible
150
+ def method_missing(method, *args, &block)
151
+ return base_item.__send__(method, *args, &block) if base_item.respond_to?(method)
152
+
153
+ super
154
+ end
155
+
156
+ # @!visibility private
157
+ def respond_to_missing?(method, include_private = false)
158
+ return true if base_item.respond_to?(method)
159
+
160
+ super
161
+ end
162
+
163
+ # give the base item type a chance to format commands
164
+ # @!visibility private
165
+ def format_type(command)
166
+ return super unless base_item
167
+
168
+ base_item.format_type(command)
137
169
  end
138
170
  end
139
171
  end
@@ -3,52 +3,16 @@
3
3
  require 'base64'
4
4
  require 'pathname'
5
5
  require 'net/http'
6
- require 'java'
7
6
  require 'marcel'
8
- require 'openhab/dsl/items/item_command'
9
- require 'openhab/dsl/items/item_delegate'
10
7
 
11
8
  module OpenHAB
12
9
  module DSL
13
10
  module Items
14
- #
15
- # Delegator to OpenHAB Player Item
16
- #
17
- class ImageItem
18
- extend OpenHAB::DSL::Items::ItemCommand
19
- extend OpenHAB::DSL::Items::ItemDelegate
20
-
21
- def_item_delegator :@image_item
22
-
23
- item_type Java::OrgOpenhabCoreLibraryItems::ImageItem
24
-
25
- #
26
- # Creates a new ImageItem
27
- #
28
- # @param [Java::OrgOpenhabCoreLibraryItems::ImageItem] image_item
29
- # The OpenHAB ImageItem to delegate to
30
- #
31
- def initialize(image_item)
32
- logger.trace("Wrapping #{image_item}")
33
- @image_item = image_item
34
-
35
- item_missing_delegate { @image_item }
36
-
37
- super()
38
- end
39
-
40
- #
41
- #
42
- # Update image with base64 encoded OpenHAB compatable image
43
- #
44
- # @param [String] base_64 base_64 encoding of an image
45
- #
46
- #
47
- def update(base_64_encoded_image)
48
- logger.trace { "Updating #{self} with Base64 image #{base_64_encoded_image}" }
49
- @image_item.update base_64_encoded_image
50
- end
11
+ java_import org.openhab.core.library.items.ImageItem
51
12
 
13
+ # Adds methods to core OpenHAB ImageItem type to make it more natural in
14
+ # Ruby
15
+ class ImageItem < GenericItem
52
16
  #
53
17
  # Update image from file
54
18
  #
@@ -87,7 +51,7 @@ module OpenHAB
87
51
  def update_from_bytes(bytes, mime_type: nil)
88
52
  mime_type ||= detect_mime_from_bytes(bytes: bytes)
89
53
  base_64_image = encode_image(mime_type: mime_type, bytes: bytes)
90
- update base_64_image
54
+ update(base_64_image)
91
55
  end
92
56
 
93
57
  #
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ module Items
6
+ # Shared method for checking item equality by delegating to state
7
+ module ItemEquality
8
+ # Check equality, with type conversions
9
+ #
10
+ # @param [GenericItem, Types::Type, Object] other object to
11
+ # compare to
12
+ #
13
+ # If this item is +NULL+ or +UNDEF+, and +other+ is nil, they are
14
+ # considered equal
15
+ #
16
+ # If this item is +NULL+ or +UNDEF+, and other is a {GenericItem}, they
17
+ # are only considered equal if the other item is in the exact same
18
+ # state (i.e. +NULL+ != +UNDEF+)
19
+ #
20
+ # Otherwise, the state of this item is compared with +other+
21
+ #
22
+ # @return [Boolean]
23
+ #
24
+ def ==(other)
25
+ logger.trace("(#{self.class}) #{self} == #{other} (#{other.class})")
26
+ return true if equal?(other) || eql?(other)
27
+ return true if !state? && other.nil?
28
+
29
+ return raw_state == other.raw_state if other.is_a?(GenericItem)
30
+
31
+ state == other
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ require 'openhab/core/entity_lookup'
6
+ require 'openhab/dsl/lazy_array'
7
+
8
+ module OpenHAB
9
+ module DSL
10
+ module Items
11
+ #
12
+ # Provides access to all OpenHAB items, and acts like an array.
13
+ #
14
+ class ItemRegistry
15
+ include LazyArray
16
+ include Singleton
17
+
18
+ # Fetches the named item from the the ItemRegistry
19
+ # @param [String] name
20
+ # @return [GenericItem] Item from registry, nil if item missing or requested item is a Group Type
21
+ def [](name)
22
+ OpenHAB::Core::EntityLookup.lookup_item(name)
23
+ rescue org.openhab.core.items.ItemNotFoundException
24
+ nil
25
+ end
26
+
27
+ # Returns true if the given item name exists
28
+ # @param name [String] Item name to check
29
+ # @return [Boolean] true if the item exists, false otherwise
30
+ def include?(name)
31
+ !$ir.getItems(name).empty? # rubocop: disable Style/GlobalVars
32
+ end
33
+ alias key? include?
34
+
35
+ # Explicit conversion to array
36
+ # @return [Array]
37
+ def to_a
38
+ $ir.items.grep_v(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
39
+ end
40
+ end
41
+
42
+ # Fetches all non-group items from the item registry
43
+ # @return [ItemRegistry]
44
+ def items
45
+ ItemRegistry.instance
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,52 +1,97 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
- require 'singleton'
3
+ require 'openhab/dsl/monkey_patch/events/item_command'
5
4
 
6
- require 'openhab/core/entity_lookup'
7
- require 'openhab/dsl/lazy_array'
5
+ require_relative 'item_registry'
6
+
7
+ require_relative 'generic_item'
8
+
9
+ require_relative 'switch_item'
10
+ require_relative 'date_time_item'
11
+ require_relative 'dimmer_item'
12
+
13
+ require_relative 'contact_item'
14
+ require_relative 'group_item'
15
+ require_relative 'image_item'
16
+ require_relative 'number_item'
17
+ require_relative 'player_item'
18
+ require_relative 'rollershutter_item'
19
+ require_relative 'string_item'
20
+
21
+ require_relative 'ensure'
8
22
 
9
23
  module OpenHAB
10
24
  module DSL
11
- #
12
- # Manages OpenHAB items
13
- #
25
+ # Contains all OpenHAB *Item classes, as well as associated support
26
+ # modules
14
27
  module Items
15
- #
16
- # Delegates to underlying set of all OpenHAB Items, provides convenience methods
17
- #
18
- class Items
19
- include LazyArray
20
- include Singleton
21
-
22
- # Fetches the named item from the the ItemRegistry
23
- # @param [String] name
24
- # @return Item from registry, nil if item missing or requested item is a Group Type
25
- def [](name)
26
- OpenHAB::Core::EntityLookup.lookup_item(name)
27
- rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
28
- nil
28
+ class << self
29
+ private
30
+
31
+ # takes an array of Type java classes and returns
32
+ # all the Enum values, in a flat array
33
+ def values_for_enums(enums)
34
+ enums.map(&:ruby_class)
35
+ .select { |k| k < java.lang.Enum }
36
+ .flat_map(&:values)
29
37
  end
30
38
 
31
- # Returns true if the given item name exists
32
- # @param name [String] Item name to check
33
- # @return [Boolean] true if the item exists, false otherwise
34
- def include?(name)
35
- !$ir.getItems(name).empty? # rubocop: disable Style/GlobalVars
39
+ # define predicates for checking if an item is in one of the Enum states
40
+ def def_predicate_methods(klass)
41
+ values_for_enums(klass.ACCEPTED_DATA_TYPES).each do |state|
42
+ _command_predicate, state_predicate = Types::PREDICATE_ALIASES[state.to_s]
43
+ next if klass.instance_methods.include?(state_predicate)
44
+
45
+ OpenHAB::Core.logger.trace("Defining #{klass}##{state_predicate} for #{state}")
46
+ klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
47
+ def #{state_predicate} # def on?
48
+ raw_state == #{state} # raw_state == ON
49
+ end # end
50
+ RUBY
51
+ end
36
52
  end
37
- alias key? []
38
53
 
39
- # explicit conversion to array
40
- def to_a
41
- items = $ir.items.grep_v(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
42
- OpenHAB::Core::EntityLookup.decorate_items(items)
54
+ # defined methods for commanding an item to one of the Enum states
55
+ # as well as predicates for if an ItemCommandEvent is one of those commands
56
+ def def_command_methods(klass) # rubocop:disable Metrics method has single purpose
57
+ values_for_enums(klass.ACCEPTED_COMMAND_TYPES).each do |value|
58
+ command = Types::COMMAND_ALIASES[value.to_s]
59
+ next if klass.instance_methods.include?(command)
60
+
61
+ OpenHAB::Core.logger.trace("Defining #{klass}##{command} for #{value}")
62
+ klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
63
+ def #{command} # def on
64
+ command(#{value}) # command(ON)
65
+ end # end
66
+ RUBY
67
+
68
+ OpenHAB::Core.logger.trace("Defining GroupItem::GroupMembers##{command} for #{value}")
69
+ GroupItem::GroupMembers.class_eval <<~RUBY, __FILE__, __LINE__ + 1
70
+ def #{command} # def on
71
+ each(&:#{command}) # each(&:on)
72
+ end # end
73
+ RUBY
74
+
75
+ OpenHAB::Core.logger.trace("Defining ItemCommandEvent##{command}? for #{value}")
76
+ MonkeyPatch::Events::ItemCommandEvent.class_eval <<~RUBY, __FILE__, __LINE__ + 1
77
+ def #{command}? # def refresh?
78
+ command == #{value} # command == REFRESH
79
+ end # end
80
+ RUBY
81
+ end
43
82
  end
44
83
  end
45
84
 
46
- # Fetches all non-group items from the item registry
47
- # @return [OpenHAB::DSL::Items::Items]
48
- def items
49
- Items.instance
85
+ # sort classes by hierarchy so we define methods on parent classes first
86
+ constants.map { |c| const_get(c) }
87
+ .grep(Module)
88
+ .select { |k| k <= GenericItem && k != GroupItem && k != StringItem }
89
+ .sort { |a, b| a < b ? 1 : -1 }
90
+ .each do |klass|
91
+ klass.field_reader :ACCEPTED_COMMAND_TYPES, :ACCEPTED_DATA_TYPES unless klass == GenericItem
92
+
93
+ def_predicate_methods(klass)
94
+ def_command_methods(klass)
50
95
  end
51
96
  end
52
97
  end