openhab-jrubyscripting 5.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/lib/openhab/core/actions.rb +163 -0
  3. data/lib/openhab/core/entity_lookup.rb +144 -0
  4. data/lib/openhab/core/events/abstract_event.rb +17 -0
  5. data/lib/openhab/core/events/item_channel_link.rb +36 -0
  6. data/lib/openhab/core/events/item_command_event.rb +78 -0
  7. data/lib/openhab/core/events/item_event.rb +22 -0
  8. data/lib/openhab/core/events/item_state_changed_event.rb +52 -0
  9. data/lib/openhab/core/events/item_state_event.rb +51 -0
  10. data/lib/openhab/core/events/thing.rb +29 -0
  11. data/lib/openhab/core/events/thing_status_info_event.rb +53 -0
  12. data/lib/openhab/core/events.rb +10 -0
  13. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  14. data/lib/openhab/core/items/color_item.rb +52 -0
  15. data/lib/openhab/core/items/contact_item.rb +52 -0
  16. data/lib/openhab/core/items/date_time_item.rb +58 -0
  17. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  18. data/lib/openhab/core/items/generic_item.rb +344 -0
  19. data/lib/openhab/core/items/group_item.rb +174 -0
  20. data/lib/openhab/core/items/image_item.rb +109 -0
  21. data/lib/openhab/core/items/location_item.rb +34 -0
  22. data/lib/openhab/core/items/metadata/hash.rb +390 -0
  23. data/lib/openhab/core/items/metadata/namespace_hash.rb +469 -0
  24. data/lib/openhab/core/items/metadata.rb +11 -0
  25. data/lib/openhab/core/items/number_item.rb +62 -0
  26. data/lib/openhab/core/items/numeric_item.rb +22 -0
  27. data/lib/openhab/core/items/persistence.rb +327 -0
  28. data/lib/openhab/core/items/player_item.rb +66 -0
  29. data/lib/openhab/core/items/proxy.rb +59 -0
  30. data/lib/openhab/core/items/registry.rb +66 -0
  31. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  32. data/lib/openhab/core/items/semantics/enumerable.rb +152 -0
  33. data/lib/openhab/core/items/semantics.rb +476 -0
  34. data/lib/openhab/core/items/state_storage.rb +53 -0
  35. data/lib/openhab/core/items/string_item.rb +28 -0
  36. data/lib/openhab/core/items/switch_item.rb +78 -0
  37. data/lib/openhab/core/items.rb +114 -0
  38. data/lib/openhab/core/lazy_array.rb +52 -0
  39. data/lib/openhab/core/profile_factory.rb +118 -0
  40. data/lib/openhab/core/script_handling.rb +55 -0
  41. data/lib/openhab/core/things/channel.rb +48 -0
  42. data/lib/openhab/core/things/channel_uid.rb +51 -0
  43. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  44. data/lib/openhab/core/things/profile_callback.rb +52 -0
  45. data/lib/openhab/core/things/proxy.rb +69 -0
  46. data/lib/openhab/core/things/registry.rb +46 -0
  47. data/lib/openhab/core/things/thing.rb +194 -0
  48. data/lib/openhab/core/things.rb +22 -0
  49. data/lib/openhab/core/timer.rb +128 -0
  50. data/lib/openhab/core/types/comparable_type.rb +23 -0
  51. data/lib/openhab/core/types/date_time_type.rb +259 -0
  52. data/lib/openhab/core/types/decimal_type.rb +192 -0
  53. data/lib/openhab/core/types/hsb_type.rb +183 -0
  54. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  55. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  56. data/lib/openhab/core/types/numeric_type.rb +52 -0
  57. data/lib/openhab/core/types/on_off_type.rb +46 -0
  58. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  59. data/lib/openhab/core/types/percent_type.rb +95 -0
  60. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  61. data/lib/openhab/core/types/point_type.rb +117 -0
  62. data/lib/openhab/core/types/quantity_type.rb +327 -0
  63. data/lib/openhab/core/types/raw_type.rb +26 -0
  64. data/lib/openhab/core/types/refresh_type.rb +27 -0
  65. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  66. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  67. data/lib/openhab/core/types/string_type.rb +76 -0
  68. data/lib/openhab/core/types/type.rb +117 -0
  69. data/lib/openhab/core/types/un_def_type.rb +38 -0
  70. data/lib/openhab/core/types/up_down_type.rb +50 -0
  71. data/lib/openhab/core/types.rb +69 -0
  72. data/lib/openhab/core/uid.rb +36 -0
  73. data/lib/openhab/core.rb +85 -0
  74. data/lib/openhab/core_ext/java/duration.rb +115 -0
  75. data/lib/openhab/core_ext/java/local_date.rb +93 -0
  76. data/lib/openhab/core_ext/java/local_time.rb +106 -0
  77. data/lib/openhab/core_ext/java/month.rb +59 -0
  78. data/lib/openhab/core_ext/java/month_day.rb +105 -0
  79. data/lib/openhab/core_ext/java/period.rb +103 -0
  80. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  81. data/lib/openhab/core_ext/java/time.rb +58 -0
  82. data/lib/openhab/core_ext/java/unit.rb +15 -0
  83. data/lib/openhab/core_ext/java/zoned_date_time.rb +116 -0
  84. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  85. data/lib/openhab/core_ext/ruby/class.rb +15 -0
  86. data/lib/openhab/core_ext/ruby/date.rb +89 -0
  87. data/lib/openhab/core_ext/ruby/numeric.rb +190 -0
  88. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  89. data/lib/openhab/core_ext/ruby/time.rb +104 -0
  90. data/lib/openhab/core_ext.rb +18 -0
  91. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  92. data/lib/openhab/dsl/events.rb +9 -0
  93. data/lib/openhab/dsl/gems.rb +3 -0
  94. data/lib/openhab/dsl/items/builder.rb +618 -0
  95. data/lib/openhab/dsl/items/ensure.rb +93 -0
  96. data/lib/openhab/dsl/items/timed_command.rb +236 -0
  97. data/lib/openhab/dsl/rules/automation_rule.rb +308 -0
  98. data/lib/openhab/dsl/rules/builder.rb +1373 -0
  99. data/lib/openhab/dsl/rules/guard.rb +115 -0
  100. data/lib/openhab/dsl/rules/name_inference.rb +160 -0
  101. data/lib/openhab/dsl/rules/property.rb +76 -0
  102. data/lib/openhab/dsl/rules/rule_triggers.rb +96 -0
  103. data/lib/openhab/dsl/rules/terse.rb +63 -0
  104. data/lib/openhab/dsl/rules/triggers/changed.rb +169 -0
  105. data/lib/openhab/dsl/rules/triggers/channel.rb +57 -0
  106. data/lib/openhab/dsl/rules/triggers/command.rb +107 -0
  107. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +161 -0
  108. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +164 -0
  109. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +195 -0
  110. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +127 -0
  111. data/lib/openhab/dsl/rules/triggers/trigger.rb +56 -0
  112. data/lib/openhab/dsl/rules/triggers/updated.rb +130 -0
  113. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +55 -0
  114. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +155 -0
  115. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  116. data/lib/openhab/dsl/rules.rb +29 -0
  117. data/lib/openhab/dsl/script_handling.rb +55 -0
  118. data/lib/openhab/dsl/things/builder.rb +263 -0
  119. data/lib/openhab/dsl/thread_local.rb +48 -0
  120. data/lib/openhab/dsl/timer_manager.rb +191 -0
  121. data/lib/openhab/dsl/version.rb +9 -0
  122. data/lib/openhab/dsl.rb +686 -0
  123. data/lib/openhab/log.rb +348 -0
  124. data/lib/openhab/osgi.rb +70 -0
  125. data/lib/openhab/rspec/configuration.rb +56 -0
  126. data/lib/openhab/rspec/example_group.rb +90 -0
  127. data/lib/openhab/rspec/helpers.rb +439 -0
  128. data/lib/openhab/rspec/hooks.rb +93 -0
  129. data/lib/openhab/rspec/jruby.rb +46 -0
  130. data/lib/openhab/rspec/karaf.rb +811 -0
  131. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  132. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  133. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  134. data/lib/openhab/rspec/mocks/metadata_provider.rb +75 -0
  135. data/lib/openhab/rspec/mocks/persistence_service.rb +140 -0
  136. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  137. data/lib/openhab/rspec/mocks/synchronous_executor.rb +56 -0
  138. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  139. data/lib/openhab/rspec/mocks/timer.rb +95 -0
  140. data/lib/openhab/rspec/openhab/core/actions.rb +26 -0
  141. data/lib/openhab/rspec/openhab/core/items/proxy.rb +27 -0
  142. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  143. data/lib/openhab/rspec/shell.rb +31 -0
  144. data/lib/openhab/rspec/suspend_rules.rb +60 -0
  145. data/lib/openhab/rspec.rb +17 -0
  146. data/lib/openhab/yard/cli/stats.rb +23 -0
  147. data/lib/openhab/yard/code_objects/group_object.rb +17 -0
  148. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  149. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  150. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  151. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  152. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  153. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  154. data/lib/openhab/yard/handlers/jruby/base.rb +49 -0
  155. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  156. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  157. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +27 -0
  158. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  159. data/lib/openhab/yard/html_helper.rb +44 -0
  160. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  161. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  162. data/lib/openhab/yard/tags/library.rb +3 -0
  163. data/lib/openhab/yard.rb +32 -0
  164. metadata +504 -0
@@ -0,0 +1,469 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ module Metadata
7
+ #
8
+ # {NamespaceHash} represents the full metadata for the item.
9
+ #
10
+ # It implements the entire interface of {::Hash}.
11
+ #
12
+ # Keys are namespaces, values are always {Hash}, though assignment allows
13
+ # using a {Hash}, a String, a `::Hash`, or an array of two items:
14
+ # `[String, ::Hash]`.
15
+ #
16
+ # All keys are converted to strings.
17
+ #
18
+ class NamespaceHash
19
+ class << self
20
+ #
21
+ # @return [org.openhab.core.items.MetadataRegistry]
22
+ #
23
+ # @!visibility private
24
+ def registry
25
+ @registry ||= OSGi.service("org.openhab.core.items.MetadataRegistry")
26
+ end
27
+ end
28
+
29
+ java_import org.openhab.core.items.MetadataKey
30
+ private_constant :MetadataKey
31
+
32
+ include Enumerable
33
+ extend Forwardable
34
+
35
+ def_delegators :to_hash,
36
+ :<,
37
+ :<=,
38
+ :==,
39
+ :>,
40
+ :>=,
41
+ :filter,
42
+ :flatten,
43
+ :invert,
44
+ :key,
45
+ :merge,
46
+ :rassoc,
47
+ :reject,
48
+ :select,
49
+ :to_a,
50
+ :transform_keys,
51
+ :transform_values
52
+
53
+ def_delegators :keys,
54
+ :length,
55
+ :size
56
+
57
+ # @!visibility private
58
+ def initialize(item_name = nil, hash = nil)
59
+ @item_name = item_name || "-"
60
+ @hash = item_name.nil? ? (hash || {}) : nil
61
+ end
62
+
63
+ # Is this object attached to an actual Item?
64
+ # @return [true,false]
65
+ def attached?
66
+ @hash.nil?
67
+ end
68
+
69
+ # @!visibility private
70
+ def dup
71
+ self.class.new(nil, transform_values(&:dup))
72
+ end
73
+
74
+ # Implicit conversion to {::Hash}
75
+ # @return [::Hash]
76
+ def to_hash
77
+ each.to_h { |namespace, meta| [namespace, meta] }
78
+ end
79
+
80
+ # @return [Hash, nil]
81
+ #
82
+ # @!visibility private
83
+ def [](namespace)
84
+ fetch(namespace, nil)
85
+ end
86
+
87
+ #
88
+ # Set the metadata namespace. If the namespace does not exist, it will be created
89
+ #
90
+ # @param value [Hash, Array[String, ::Hash], ::Hash<String, Object>]
91
+ #
92
+ # @return [Hash]
93
+ #
94
+ # @!visibility private
95
+ def []=(namespace, value)
96
+ metadata = Hash.from_item(@item_name, namespace, value)
97
+ return @hash[metadata.uid.namespace] = metadata unless attached? # rubocop:disable Lint/ReturnInVoidContext
98
+
99
+ # registry.get can be omitted, but registry.update will log a warning for nonexistent metadata
100
+ if self.class.registry.managed_provider.get.get(metadata.uid)
101
+ metadata.commit
102
+ else
103
+ metadata.create
104
+ end
105
+ metadata # rubocop:disable Lint/Void
106
+ end
107
+ alias_method :store, :[]=
108
+
109
+ # @!visibility private
110
+ def assoc(key)
111
+ to_hash.assoc(key.to_s)
112
+ end
113
+
114
+ #
115
+ # Remove all the namespaces
116
+ #
117
+ # @!visibility private
118
+ def clear
119
+ if attached?
120
+ self.class.registry.remove_item_metadata(@item_name)
121
+ else
122
+ @hash.clear
123
+ end
124
+ end
125
+
126
+ # @!visibility private
127
+ def compact
128
+ to_hash
129
+ end
130
+
131
+ # @!visibility private
132
+ def compact!
133
+ # no action; impossible to have nil keys
134
+ self
135
+ end
136
+
137
+ # @!visibility private
138
+ def compare_by_identity
139
+ raise NotImplementedError
140
+ end
141
+
142
+ # @!visibility private
143
+ def compare_by_identity?
144
+ false
145
+ end
146
+
147
+ # @!visibility private
148
+ def deconstruct_keys
149
+ self
150
+ end
151
+
152
+ # @!visibility private
153
+ def default(*)
154
+ nil
155
+ end
156
+
157
+ # @!visibility private
158
+ def default=(*)
159
+ raise NotImplementedError
160
+ end
161
+
162
+ # @!visibility private
163
+ def default_proc
164
+ nil
165
+ end
166
+
167
+ # @!visibility private
168
+ def default_proc=(*)
169
+ raise NotImplementedError
170
+ end
171
+
172
+ # @!visibility private
173
+ def delete(namespace, &block)
174
+ namespace = namespace.to_s
175
+
176
+ return @hash.delete(namespace, &block) unless attached?
177
+
178
+ r = self.class.registry.remove(MetadataKey.new(namespace, @item_name))
179
+ return yield(namespace) if block && !r
180
+
181
+ Hash.new(r) if r
182
+ end
183
+
184
+ # @!visibility private
185
+ def delete_if
186
+ raise NotImplementedError unless block_given?
187
+
188
+ each { |k, v| delete(k) if yield(k, v) }
189
+ self
190
+ end
191
+
192
+ # @!visibility private
193
+ def dig(key, *keys)
194
+ m = self[key.to_s]
195
+ return m if keys.empty?
196
+
197
+ m&.dig(*keys)
198
+ end
199
+
200
+ #
201
+ # Enumerates through all the namespaces
202
+ #
203
+ # @yieldparam [String] namespace
204
+ # @yieldparam [Hash] metadata
205
+ #
206
+ # @!visibility private
207
+ def each(&block)
208
+ return @hash.each(&block) unless attached?
209
+ return to_enum(:each) unless block
210
+
211
+ self.class.registry.all.each do |meta|
212
+ yield meta.uid.namespace, Hash.new(meta) if meta.uid.item_name == @item_name
213
+ end
214
+ self
215
+ end
216
+ alias_method :each_pair, :each
217
+
218
+ # @!visibility private
219
+ def each_key(&block)
220
+ return @hash.each_key(&block) unless attached?
221
+ return to_enum(:each_key) unless block
222
+
223
+ self.class.registry.all.each do |meta|
224
+ yield meta.uid.namespace if meta.uid.item_name == @item_name
225
+ end
226
+ self
227
+ end
228
+
229
+ # @!visibility private
230
+ def each_value
231
+ return @hash.each_value(&block) unless attached?
232
+ return to_enum(:each_value) unless block_given?
233
+
234
+ self.class.registry.all.each do |meta|
235
+ yield Hash.new(meta) if meta.uid.item_name == @item_name
236
+ end
237
+ self
238
+ end
239
+
240
+ # @!visibility private
241
+ def empty?
242
+ return @hash.empty? unless attached?
243
+
244
+ self.class.registry.all.each do |meta|
245
+ return false if meta.uid.item_name == @item_name
246
+ end
247
+ true
248
+ end
249
+
250
+ # @!visibility private
251
+ def except(*keys)
252
+ to_hash.except(*keys.map(&:to_s))
253
+ end
254
+
255
+ # @!visibility private
256
+ def fetch(key, *default_value, &block)
257
+ key = key.to_s
258
+ return @hash.fetch(key, *default_value, &block) unless attached?
259
+
260
+ if default_value.length > 1
261
+ raise "wrong number of arguments (given #{default_value.length + 1}, expected 1..2)",
262
+ ArgumentError
263
+ end
264
+
265
+ logger.trace("Getting metadata for item: #{@item_name}, namespace '#{key}'")
266
+ if (m = self.class.registry.get(MetadataKey.new(key, @item_name)))
267
+ Hash.new(m)
268
+ elsif block
269
+ yield key
270
+ elsif !default_value.empty?
271
+ default_value.first
272
+ else
273
+ raise "key not found #{key.inspect}", KeyError
274
+ end
275
+ end
276
+
277
+ # @!visibility private
278
+ def fetch_values(*keys)
279
+ return @hash.fetch_values(keys.map(&:to_s)) if attached?
280
+
281
+ keys.each_with_object([]) do |key, res|
282
+ key = key.to_s
283
+ if (m = self.class.registry.get(MetadataKey.new(key, @item_name)))
284
+ res << Hash.new(m)
285
+ elsif block_given?
286
+ res << yield(key)
287
+ end
288
+ end
289
+ end
290
+
291
+ # @!visibility private
292
+ def hash
293
+ "metadata_namespace_hash".hash + @item_name.hash
294
+ end
295
+
296
+ # @!visibility private
297
+ def keep_if(&block)
298
+ select!(&block)
299
+ self
300
+ end
301
+
302
+ #
303
+ # @return [true,false] True if the given namespace exists, false otherwise
304
+ #
305
+ # @!visibility private
306
+ def key?(key)
307
+ key = key.to_s
308
+ return @hash.key?(key) unless attached?
309
+
310
+ !self.class.registry.get(MetadataKey.new(key, @item_name)).nil?
311
+ end
312
+ alias_method :has_key?, :key?
313
+ alias_method :member?, :key?
314
+ alias_method :include?, :key?
315
+
316
+ # @!visibility private
317
+ def keys
318
+ each_key.to_a
319
+ end
320
+
321
+ #
322
+ # Merge the given hash with the current metadata. Existing namespace that matches the name
323
+ # of the new namespace will be overwritten. Others will be added.
324
+ #
325
+ # @!visibility private
326
+ def merge!(*others)
327
+ return self if others.empty?
328
+
329
+ others.each do |other|
330
+ case other
331
+ when ::Hash, NamespaceHash
332
+ other.each do |namespace, new_meta|
333
+ self[namespace] = new_meta
334
+ next unless block_given?
335
+
336
+ current_meta = self[namespace]
337
+ new_meta = yield key, current_meta, new_meta unless current_meta.nil?
338
+ self[namespace] = new_meta unless new_meta.equal?(current_meta)
339
+ end
340
+ else
341
+ raise ArgumentError, "merge only supports Hash, or another item's metadata"
342
+ end
343
+ end
344
+ self
345
+ end
346
+
347
+ # @!visibility private
348
+ def select!
349
+ raise NotImplementedError unless block_given?
350
+
351
+ removed = false
352
+ each do |k, v|
353
+ unless yield(k, v)
354
+ delete(k)
355
+ removed = true
356
+ end
357
+ end
358
+ return nil unless removed
359
+
360
+ self
361
+ end
362
+ alias_method :filter!, :select!
363
+
364
+ # @!visibility private
365
+ def shift
366
+ raise NotImplementedError
367
+ end
368
+
369
+ # @!visibility private
370
+ def slice(*keys)
371
+ keys.map!(&:to_s)
372
+ return @hash.slice(*keys) unless attached?
373
+
374
+ keys = keys.to_set
375
+ r = {}
376
+ self.class.registry.all.each do |meta|
377
+ if meta.uid.item_name == @item_name && keys.include?(meta.uid.namespace)
378
+ r[meta.uid.namespace] =
379
+ Hash.new(meta)
380
+ end
381
+ end
382
+ r
383
+ end
384
+
385
+ # @!visibility private
386
+ def reject!
387
+ raise NotImplementedError unless block_given?
388
+
389
+ removed = false
390
+ each do |k, v|
391
+ if yield(k, v)
392
+ delete(k)
393
+ removed = true
394
+ end
395
+ end
396
+ return nil unless removed
397
+
398
+ self
399
+ end
400
+
401
+ # @!visibility private
402
+ def replace(other)
403
+ clear
404
+ merge!(other)
405
+ end
406
+
407
+ # @!visibility private
408
+ def to_proc
409
+ ->(k) { self[k] }
410
+ end
411
+
412
+ # @!visibility private
413
+ def transform_keys!(hash2 = nil)
414
+ hash2 = hash2&.transform_keys(&:to_s)
415
+ each_key do |k|
416
+ if hash2
417
+ next unless hash2.key?(k)
418
+
419
+ self[hash2[k]] = delete(k)
420
+ else
421
+ new_k = yield k
422
+ self[new_k] = delete(k)
423
+ end
424
+ end
425
+ self
426
+ end
427
+
428
+ # @!visibility private
429
+ def transform_values!
430
+ raise NotImplementedError unless block_given?
431
+
432
+ each do |k, v|
433
+ new_v = yield(k, v)
434
+ next if new_v.equal?(v)
435
+
436
+ self[k] = new_v
437
+ end
438
+ end
439
+
440
+ # @!visibility private
441
+ def value?(value)
442
+ each_value { |v| return true if v == value }
443
+ false
444
+ end
445
+ alias_method :has_value?, :value?
446
+
447
+ # @!visibility private
448
+ def values
449
+ each_value.to_a
450
+ end
451
+
452
+ # @!visibility private
453
+ def values_at(*keys)
454
+ keys.map(&self)
455
+ end
456
+
457
+ #
458
+ # @return [String]
459
+ #
460
+ # @!visibility private
461
+ def inspect
462
+ to_hash.inspect
463
+ end
464
+ alias_method :to_s, :inspect
465
+ end
466
+ end
467
+ end
468
+ end
469
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ # Contains classes wrapping interactions with item metadata.
7
+ module Metadata
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generic_item"
4
+ require_relative "numeric_item"
5
+
6
+ module OpenHAB
7
+ module Core
8
+ module Items
9
+ java_import org.openhab.core.library.items.NumberItem
10
+
11
+ #
12
+ # A NumberItem has a decimal value and is usually used for all kinds
13
+ # of sensors, like temperature, brightness, wind, etc.
14
+ # It can also be used as a counter or as any other thing that can be expressed
15
+ # as a number.
16
+ #
17
+ # Non-dimensioned numbers will have a state of {DecimalType}, while
18
+ # dimensioned numbers will have a state of {QuantityType}. Be sure
19
+ # to read the documentation for those two classes for how to work with
20
+ # the different states of a {NumberItem}.
21
+ #
22
+ # @!attribute [r] dimension
23
+ # @return [Class, nil] The dimension of the number item.
24
+ # @!attribute [r] unit
25
+ # @return [javax.measure.Unit, nil]
26
+ # @!attribute [r] state
27
+ # @return [DecimalType, QuantityType, nil]
28
+ #
29
+ # @example Number Items can be selected in an enumerable with grep.
30
+ # # Get all NumberItems
31
+ # items.grep(NumberItem)
32
+ # .each { |number| logger.info("#{item.name} is a Number Item") }
33
+ #
34
+ class NumberItem < GenericItem
35
+ include NumericItem
36
+
37
+ # raw numbers translate directly to {DecimalType}, not a string
38
+ # @!visibility private
39
+ def format_type(command)
40
+ if command.is_a?(Numeric)
41
+ if unit && (target_unit = DSL.unit(unit.dimension) || unit)
42
+ return Types::QuantityType.new(command, target_unit)
43
+ end
44
+
45
+ return Types::DecimalType.new(command)
46
+ end
47
+
48
+ super
49
+ end
50
+
51
+ protected
52
+
53
+ # Adds the unit dimension
54
+ def type_details
55
+ ":#{dimension}" if dimension
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ # @!parse NumberItem = OpenHAB::Core::Items::NumberItem
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ # Mixin for implementing type coercion for number-like items
9
+ module NumericItem
10
+ %i[positive? negative? zero?].each do |predicate|
11
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
12
+ def #{predicate} # def positive?
13
+ return false unless state? # return false unless state?
14
+ #
15
+ state.#{predicate} # state.positive?
16
+ end # end
17
+ RUBY
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end