openhab-scripting 5.4.1 → 5.4.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63a0ce6f1514492eb9e576ddcdcad35b2d10311f0eb4f25bd27dccef521495be
4
- data.tar.gz: df77ba04c98b1cbc0a3595ccb6a6813159ac89b34fe33b914ae20398a9936934
3
+ metadata.gz: 98baaf8e8f9abf971d25737d726127c8b7396f600e0a34079f8b4c8ae26eef64
4
+ data.tar.gz: ea340479297eb523bed7ea18ab033c381c3b0479ca6b7b289689c87e90fcab47
5
5
  SHA512:
6
- metadata.gz: 124a9d294d2e55204af25ec01861d3891d74fbbb03b9b96b181e8d5b8f0967926d245e7f6c42772894c99763f9a3caa586b81a5137960583c38304eb56609fd1
7
- data.tar.gz: f6255e787a44419ea44ef763fff95a3a6609fe8d2beb179fb81d78ee4ff44485196d5bb1ada9ae4dd1dc7962ddac2817076420d306ed2662c2cf2d24ad700fa4
6
+ metadata.gz: 8059407518f7f621aa0ed19b11f1d3d51c423c6692dcf90e6342b39907f72021325127c28b28008089f507747debaa7ea07876d276c7252e4501f81de911e46e
7
+ data.tar.gz: 6896b15e4bfc5cada7ce6eda2ae11f551d94fbf1e9911b649059dc59be9f949064d0ade274396a620810b58cdf7c67898423e56410974f8a317bc081e48bd2b4
@@ -218,9 +218,19 @@ module OpenHAB
218
218
  # @example
219
219
  # event.item.tagged?("Setpoint")
220
220
  #
221
+ # @example
222
+ # event.item.tagged?(Semantics::Switch)
223
+ #
221
224
  def tagged?(*tags)
222
225
  tags.map! do |tag|
223
- tag.is_a?(Module) ? tag.simple_name : tag
226
+ # @deprecated OH3.4
227
+ if tag.is_a?(Module)
228
+ tag.simple_name
229
+ elsif defined?(Semantics::SemanticTag) && tag.is_a?(Semantics::SemanticTag)
230
+ tag.name
231
+ else
232
+ tag
233
+ end
224
234
  end
225
235
  !(self.tags.to_a & tags).empty?
226
236
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ module Semantics
7
+ #
8
+ # Provides {SemanticTag SemanticTags} created in Ruby to openHAB
9
+ #
10
+ class Provider < Core::Provider
11
+ begin
12
+ include org.openhab.core.semantics.SemanticTagProvider
13
+ rescue NameError
14
+ # @deprecated OH3.4
15
+ end
16
+
17
+ class << self
18
+ #
19
+ # The SemanticTag registry
20
+ #
21
+ # @return [org.openhab.core.semantics.SemanticTagRegistry, nil]
22
+ # @since openHAB 4.0
23
+ #
24
+ def registry
25
+ unless instance_variable_defined?(:@registry)
26
+ @registry = OSGi.service("org.openhab.core.semantics.SemanticTagRegistry")
27
+ end
28
+ @registry
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @deprecated OH3.4
4
+ return unless OpenHAB::Core::Items::Semantics::Provider.registry
5
+
6
+ module OpenHAB
7
+ module Core
8
+ module Items
9
+ module Semantics
10
+ java_import org.openhab.core.semantics.SemanticTag
11
+
12
+ # @since openHAB 4.0
13
+ module SemanticTag
14
+ # @attribute [r] uid
15
+ #
16
+ # The tag's full UID, including ancestors.
17
+ #
18
+ # @return [String]
19
+
20
+ # @attribute [r] parent_uid
21
+ #
22
+ # The UID of the tag's parent.
23
+ #
24
+ # @return [String]
25
+
26
+ # @attribute [r] name
27
+ #
28
+ # The tag's simple name.
29
+ #
30
+ # @return [String]
31
+
32
+ # @attribute [r] label
33
+ #
34
+ # The tag's human readable label.
35
+ #
36
+ # @return [String]
37
+
38
+ # @attribute [r] description
39
+ #
40
+ # The tag's full description.
41
+ #
42
+ # @return [String]
43
+
44
+ # @attribute [r] synonyms
45
+ #
46
+ # Allowed synonyms for the tag.
47
+ #
48
+ # @return [java.util.List<String>]
49
+
50
+ # @method localized(locale)
51
+ #
52
+ # Returns a new {SemanticTag SemanticTag} localized to the specified locale.
53
+ #
54
+ # @param locale [java.util.Locale] The locale to localize this tag to
55
+ # @return [SemanticTag]
56
+
57
+ # @!visibility private
58
+ def <(other)
59
+ check_type(other)
60
+ uid != other.uid && uid.start_with?(other.uid)
61
+ end
62
+
63
+ # @!visibility private
64
+ def <=(other)
65
+ check_type(other)
66
+ uid.start_with?(other.uid)
67
+ end
68
+
69
+ # @!visibility private
70
+ def ==(other)
71
+ check_type(other)
72
+ uid == other.uid
73
+ end
74
+
75
+ # @!visibility private
76
+ def >=(other)
77
+ check_type(other)
78
+ other.uid.start_with?(uid)
79
+ end
80
+
81
+ # @!visibility private
82
+ def >(other)
83
+ check_type(other)
84
+ uid != other.uid && other.uid.start_with?(uid)
85
+ end
86
+
87
+ # @return [String]
88
+ def inspect
89
+ parent = "(#{parent_uid})" unless parent_uid.empty?
90
+ "#<OpenHAB::Core::Items::Semantics::#{name}#{parent} " \
91
+ "label=#{label.inspect} " \
92
+ "description=#{description.inspect} " \
93
+ "synonyms=#{synonyms.to_a.inspect}>"
94
+ end
95
+
96
+ private
97
+
98
+ def check_type(other)
99
+ raise ArgumentError, "comparison of #{other.class} with SemanticTag failed" unless other.is_a?(SemanticTag)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ module Semantics
7
+ # @deprecated OH3.4 didn't have SemanticTag class
8
+
9
+ #
10
+ # Adds tag attributes to the semantic tag class
11
+ #
12
+ module TagClassMethods
13
+ # @!visibility private
14
+ java_import org.openhab.core.semantics.SemanticTags
15
+
16
+ #
17
+ # Returns the tag's label
18
+ #
19
+ # @param [java.util.Locale] locale The locale that the label should be in, if available.
20
+ # When nil, the system's default locale is used.
21
+ #
22
+ # @return [String] The tag's label
23
+ #
24
+ def label(locale = nil)
25
+ SemanticTags.get_label(java_class, locale || java.util.Locale.default)
26
+ end
27
+
28
+ #
29
+ # Returns the tag's synonyms
30
+ #
31
+ # @param [java.util.Locale] locale The locale that the label should be in, if available.
32
+ # When nil, the system's default locale is used.
33
+ #
34
+ # @return [Array<String>] The list of synonyms in the requested locale.
35
+ #
36
+ def synonyms(locale = nil)
37
+ unless SemanticTags.respond_to?(:get_synonyms)
38
+ return java_class.get_annotation(org.openhab.core.semantics.TagInfo.java_class).synonyms
39
+ .split(",").map(&:strip)
40
+ end
41
+
42
+ SemanticTags.get_synonyms(java_class, locale || java.util.Locale.default).to_a
43
+ end
44
+
45
+ #
46
+ # Returns the tag's description
47
+ #
48
+ # @param [java.util.Locale] locale The locale that the description should be in, if available.
49
+ # When nil, the system's default locale is used.
50
+ #
51
+ # @return [String] The tag's description
52
+ #
53
+ def description(locale = nil)
54
+ unless SemanticTags.respond_to?(:get_description)
55
+ return java_class.get_annotation(org.openhab.core.semantics.TagInfo.java_class).description
56
+ end
57
+
58
+ SemanticTags.get_description(java_class, locale || java.util.Locale.default)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -5,6 +5,7 @@ require "forwardable"
5
5
  require_relative "generic_item"
6
6
  require_relative "group_item"
7
7
  require_relative "semantics/enumerable"
8
+ require_relative "semantics/provider"
8
9
 
9
10
  module OpenHAB
10
11
  module Core
@@ -60,9 +61,11 @@ module OpenHAB
60
61
  # and {#property_type}. They can even be used with
61
62
  # {DSL::Items::ItemBuilder#tag}.
62
63
  #
63
- # The semantic constants in the `Semantics` module are enhanced with {TagClassMethods}
64
- # to provide easy access to the tags' additional attributes: {TagClassMethods.label label},
65
- # {TagClassMethods.synonyms synonyms}, and {TagClassMethods.description description}.
64
+ # In openHAB 4.0, all of the tag objects implement {SemanticTag}.
65
+ #
66
+ # In openHAB 3.4, the constants in the {Semantics} module are enhanced with {TagClassMethods}
67
+ # to provide easy access to the tags' additional attributes: {TagClassMethods#label label},
68
+ # {TagClassMethods#synonyms synonyms}, and {TagClassMethods#description description}.
66
69
  # For example, to get the synonyms for `Semantics::Lightbulb` in German:
67
70
  # `Semantics::Lightbulb.synonyms(java.util.Locale::GERMAN)`
68
71
  #
@@ -164,7 +167,7 @@ module OpenHAB
164
167
  #
165
168
  # ## Adding Custom Semantic Tags
166
169
  #
167
- # openHAB 4 supports adding custom semantic tags to augment the standard set of tags to better suit
170
+ # openHAB 4.0 supports adding custom semantic tags to augment the standard set of tags to better suit
168
171
  # your particular requirements.
169
172
  #
170
173
  # For more information, see {add}
@@ -173,6 +176,160 @@ module OpenHAB
173
176
  GenericItem.include(self)
174
177
  GroupItem.extend(Forwardable)
175
178
  GroupItem.def_delegators :members, :equipments, :locations
179
+ # This is a marker interface for all semantic tag classes.
180
+ # @interface
181
+ # @deprecated Since openHAB 4.0, {SemanticTag} is the interface that all tags implement.
182
+ # Tags are simple instances, instead of another interface in a hierarchical structure.
183
+ Tag = org.openhab.core.semantics.Tag
184
+
185
+ class << self
186
+ # @!visibility private
187
+ def service
188
+ unless instance_variable_defined?(:@service)
189
+ @service = OSGi.service("org.openhab.core.semantics.SemanticsService")
190
+ end
191
+ @service
192
+ end
193
+
194
+ #
195
+ # Returns all available Semantic tags
196
+ #
197
+ # @return [Array<SemanticTag>] an array containing all the Semantic tags
198
+ #
199
+ def tags
200
+ if Provider.registry
201
+ Provider.registry.all.to_a
202
+ else
203
+ java.util.stream.Stream.of(
204
+ org.openhab.core.semantics.model.point.Points.stream,
205
+ org.openhab.core.semantics.model.property.Properties.stream,
206
+ org.openhab.core.semantics.model.equipment.Equipments.stream,
207
+ org.openhab.core.semantics.model.location.Locations.stream
208
+ ).flat_map(&:itself).map(&:ruby_class).iterator.to_a
209
+ end
210
+ end
211
+
212
+ #
213
+ # Finds a semantic tag using its name, label, or synonyms.
214
+ #
215
+ # @param [String,Symbol] id The tag name, label, or synonym to look up
216
+ # @param [java.util.Locale] locale The locale of the given label or synonym
217
+ #
218
+ # @return [SemanticTag, Module, nil] The semantic tag class if found, or nil if not found.
219
+ # In openHAB 4.0, a SemanticTag instance will be returned. In openHAB 3.4, it will be
220
+ # a Module.
221
+ #
222
+ def lookup(id, locale = java.util.Locale.default)
223
+ id = id.to_s
224
+
225
+ # @deprecated OH3.4 - the Property tag had an ID of "MeasurementProperty" in OH3.4.
226
+ # This was corrected in OH4.
227
+ # Make sure we compare against pre-release versions
228
+ if id == "Property" && Gem::Version.new(Core::VERSION) < Gem::Version.new("4.0.0.M1")
229
+ id = "MeasurementProperty"
230
+ end
231
+
232
+ # @deprecated OH3.4 missing registry
233
+ if Provider.registry
234
+ tag_class = service.get_by_label_or_synonym(id, locale).first ||
235
+ Provider.registry.get_tag_class_by_id(id)
236
+ return unless tag_class
237
+
238
+ Provider.registry.get(Provider.registry.class.build_id(tag_class))
239
+ else
240
+ tag = org.openhab.core.semantics.SemanticTags.get_by_label_or_synonym(id, locale).first ||
241
+ org.openhab.core.semantics.SemanticTags.get_by_id(id)
242
+ return unless tag
243
+
244
+ tag = tag.ruby_class
245
+ tag.singleton_class.include(TagClassMethods)
246
+ tag
247
+ end
248
+ end
249
+
250
+ #
251
+ # Automatically looks up new semantic classes and adds them as constants
252
+ #
253
+ # @!visibility private
254
+ def const_missing(sym)
255
+ logger.trace("const missing, performing Semantics Lookup for: #{sym}")
256
+ lookup(sym)&.tap { |tag| const_set(sym, tag) } || super
257
+ end
258
+ end
259
+
260
+ # @deprecated OH3.4 cannot add a tag
261
+ # this not in the class << self block above because YARD doesn't figure out
262
+ # it's a class method with the conditional
263
+ if Provider.registry || org.openhab.core.semantics.SemanticTags.respond_to?(:add)
264
+ class << self
265
+ #
266
+ # Adds custom semantic tags.
267
+ #
268
+ # @since openHAB 4.0
269
+ # @return [Array<SemanticTag>] An array of tags successfully added.
270
+ #
271
+ # @overload add(**tags)
272
+ # Quickly add one or more semantic tags using the default label, empty synonyms and descriptions.
273
+ #
274
+ # @param [kwargs] **tags Pairs of `tag` => `parent` where tag is either a `Symbol` or a `String`
275
+ # for the tag to be added, and parent is either a {SemanticTag}, a `Symbol` or a `String` of an
276
+ # existing tag.
277
+ # @return [Array<SemanticTag>] An array of tags successfully added.
278
+ #
279
+ # @example Add one semantic tag `Balcony` whose parent is `Semantics::Outdoor` (Location)
280
+ # Semantics.add(Balcony: Semantics::Outdoor)
281
+ #
282
+ # @example Add multiple semantic tags
283
+ # Semantics.add(Balcony: Semantics::Outdoor,
284
+ # SecretRoom: Semantics::Room,
285
+ # Motion: Semantics::Property)
286
+ #
287
+ # @overload add(label: nil, synonyms: "", description: "", **tags)
288
+ # Add a custom semantic tag with extra details.
289
+ #
290
+ # @example
291
+ # Semantics.add(SecretRoom: Semantics::Room, label: "My Secret Room",
292
+ # synonyms: "HidingPlace", description: "A room that requires a special trick to enter")
293
+ #
294
+ # @param [String,nil] label Optional label. When `nil`, infer the label from the tag name,
295
+ # converting `CamelCase` to `Camel Case`
296
+ # @param [String,Symbol,Array<String,Symbol>] synonyms Additional synonyms to refer to this tag.
297
+ # @param [String] description A longer description of the tag.
298
+ # @param [kwargs] **tags Exactly one pair of `tag` => `parent` where tag is either a `Symbol` or a
299
+ # `String` for the tag to be added, and parent is either a {SemanticTag}, a `Symbol` or a
300
+ # `String` of an existing tag.
301
+ # @return [Array<SemanticTag>] An array of tags successfully added.
302
+ #
303
+ def add(label: nil, synonyms: "", description: "", **tags)
304
+ raise ArgumentError, "Tags must be specified" if tags.empty?
305
+ if (tags.length > 1) && !(label.nil? && synonyms.empty? && description.empty?)
306
+ raise ArgumentError, "Additional options can only be specified when creating one tag"
307
+ end
308
+
309
+ synonyms = Array.wrap(synonyms).map(&:to_s).map(&:strip)
310
+
311
+ tags.map do |name, parent|
312
+ # @deprecated OH4.0.0.M4 missing registry
313
+ if Provider.registry
314
+ parent = lookup(parent) unless parent.is_a?(SemanticTag)
315
+ next if lookup(name)
316
+ next unless parent
317
+
318
+ new_tag = org.openhab.core.semantics.SemanticTagImpl.new("#{parent.uid}_#{name}", label, description,
319
+ synonyms)
320
+ Provider.instance.add(new_tag)
321
+ lookup(name)
322
+ else
323
+ parent_is_tag = parent.respond_to?(:java_class) && parent.java_class < Tag.java_class
324
+ parent = parent_is_tag ? parent.java_class : parent.to_s
325
+ org.openhab.core.semantics.SemanticTags
326
+ .add(name.to_s, parent, label, synonyms.join(","), description)
327
+ &.then { lookup(name) }
328
+ end
329
+ end.compact
330
+ end
331
+ end
332
+ end
176
333
 
177
334
  # @!parse
178
335
  # class Items::GroupItem
@@ -200,26 +357,29 @@ module OpenHAB
200
357
  # end
201
358
  #
202
359
 
203
- # This is a marker interface for all semantic tag classes.
204
- # @interface
205
- Tag = org.openhab.core.semantics.Tag
360
+ # @!visibility private
361
+ def translate_tag(tag)
362
+ return unless tag
363
+
364
+ if Provider.registry
365
+ Provider.registry.get(Provider.registry.class.build_id(tag))
366
+ else
367
+ tag.ruby_class
368
+ end
369
+ end
206
370
 
207
371
  # @!parse
208
- # # This is the super interface for all types that represent a Location.
209
- # # @interface
210
- # Location = org.openhab.core.semantics.Location
372
+ # # This is the parent tag for all tags that represent a Location.
373
+ # Location = SemanticTag
211
374
  #
212
- # # This is the super interface for all types that represent an Equipment.
213
- # # @interface
214
- # Equipment = org.openhab.core.semantics.Equipment
375
+ # # This is the parent tag for all tags that represent an Equipment.
376
+ # Equipment = SemanticTag
215
377
  #
216
- # # This is the super interface for all types that represent a Point.
217
- # # @interface
218
- # Point = org.openhab.core.semantics.Point
378
+ # # This is the parent tag for all tags that represent a Point.
379
+ # Point = SemanticTag
219
380
  #
220
- # # This is the super interface for all property tags.
221
- # # @interface
222
- # Property = org.openhab.core.semantics.Property
381
+ # # This is the parent tag for all property tags.
382
+ # Property = SemanticTag
223
383
 
224
384
  # put ourself into the global namespace, replacing the action
225
385
  Object.send(:remove_const, :Semantics)
@@ -286,14 +446,14 @@ module OpenHAB
286
446
  #
287
447
  # @!attribute [r] location_type
288
448
  #
289
- # Returns the sub-class of {Location} related to this Item.
449
+ # Returns the sub-tag of {Location} related to this Item.
290
450
  #
291
451
  # In other words, the {#semantic_type} of this Item's {Location}.
292
452
  #
293
- # @return [Class, nil]
453
+ # @return [SemanticTag, nil]
294
454
  #
295
455
  def location_type
296
- Actions::Semantics.get_location_type(self)&.ruby_class
456
+ translate_tag(Actions::Semantics.get_location_type(self))
297
457
  end
298
458
 
299
459
  #
@@ -312,50 +472,50 @@ module OpenHAB
312
472
  #
313
473
  # @!attribute [r] equipment_type
314
474
  #
315
- # Returns the sub-class of {Equipment} related to this Item.
475
+ # Returns the sub-tag of {Equipment} related to this Item.
316
476
  #
317
477
  # In other words, the {#semantic_type} of this Item's {Equipment}.
318
478
  #
319
- # @return [Class, nil]
479
+ # @return [SemanticTag, nil]
320
480
  #
321
481
  def equipment_type
322
- Actions::Semantics.get_equipment_type(self)&.ruby_class
482
+ translate_tag(Actions::Semantics.get_equipment_type(self))
323
483
  end
324
484
 
325
485
  #
326
486
  # @!attribute [r] point_type
327
487
  #
328
- # Returns the sub-class of {Point} this Item is tagged with.
488
+ # Returns the sub-tag of {Point} this Item is tagged with.
329
489
  #
330
- # @return [Class, nil]
490
+ # @return [SemanticTag, nil]
331
491
  #
332
492
  def point_type
333
- Actions::Semantics.get_point_type(self)&.ruby_class
493
+ translate_tag(Actions::Semantics.get_point_type(self))
334
494
  end
335
495
 
336
496
  #
337
497
  # @!attribute [r] property_type
338
498
  #
339
- # Returns the sub-class of {Property} this Item is tagged with.
499
+ # Returns the sub-tag of {Property} this Item is tagged with.
340
500
  #
341
- # @return [Class, nil]
501
+ # @return [SemanticTag, nil]
342
502
  #
343
503
  def property_type
344
- Actions::Semantics.get_property_type(self)&.ruby_class
504
+ translate_tag(Actions::Semantics.get_property_type(self))
345
505
  end
346
506
 
347
507
  # @!attribute [r] semantic_type
348
508
  #
349
- # Returns the sub-class of {Tag} this Item is tagged with.
509
+ # Returns the {SemanticTag} this Item is tagged with.
350
510
  #
351
511
  # It will only return the first applicable Tag, preferring
352
- # a sub-class of {Location}, {Equipment}, or {Point} first,
512
+ # a sub-tag of {Location}, {Equipment}, or {Point} first,
353
513
  # and if none of those are found, looks for a {Property}.
354
514
  #
355
- # @return [Class, nil]
515
+ # @return [SemanticTag, nil]
356
516
  #
357
517
  def semantic_type
358
- Actions::Semantics.get_semantic_type(self)&.ruby_class
518
+ translate_tag(Actions::Semantics.get_semantic_type(self))
359
519
  end
360
520
 
361
521
  #
@@ -379,7 +539,7 @@ module OpenHAB
379
539
  # @example Given a A/V receiver's input item, search for its power item
380
540
  # FamilyReceiver_Input.points(Semantics::Switch) # => [FamilyReceiver_Switch]
381
541
  #
382
- # @param [Class] point_or_property_types
542
+ # @param [SemanticTag] point_or_property_types
383
543
  # Pass 1 or 2 classes that are sub-classes of {Point} or {Property}.
384
544
  # Note that when comparing against semantic tags, it does a sub-class check.
385
545
  # So if you search for [Control], you'll get items tagged with [Switch].
@@ -393,169 +553,6 @@ module OpenHAB
393
553
  result.delete(self)
394
554
  result
395
555
  end
396
-
397
- # @deprecated OH3.4 - this check is only needed for OH3.4
398
- if org.openhab.core.semantics.SemanticTags.respond_to?(:add)
399
-
400
- #
401
- # Adds custom semantic tags.
402
- #
403
- # @return [Array<Tag>] An array of tags successfully added.
404
- #
405
- # @overload self.add(**tags)
406
- # Quickly add one or more semantic tags using the default label, empty synonyms and descriptions.
407
- #
408
- # @param [kwargs] **tags Exactly one pair of `tag` => `parent` where tag is either a Symbol or a String
409
- # for the tag to be added, and parent is either a {Tag}, a symbol or a string of an existing tag.
410
- # @return [Array<Tag>] An array of tags successfully added.
411
- #
412
- # @example Add one semantic tag `Balcony` whose parent is `Semantics::Outdoor` (Location)
413
- # Semantics.add(Balcony: Semantics::Outdoor)
414
- #
415
- # @example Add multiple semantic tags
416
- # Semantics.add(Balcony: Semantics::Outdoor,
417
- # SecretRoom: Semantics::Room,
418
- # Motion: Semantics::Property)
419
- #
420
- # @overload self.add(label: nil, synonyms: "", description: "", **tags)
421
- # Add a custom semantic tag with extra details.
422
- #
423
- # @example
424
- # Semantics.add(SecretRoom: Semantics::Room, label: "My Secret Room",
425
- # synonyms: "HidingPlace", description: "A room that requires a special trick to enter")
426
- #
427
- # @param [String,nil] label Optional label. When nil, infer the label from the tag name,
428
- # converting `CamelCase` to `Camel Case`
429
- # @param [String,Array<String,Symbol>] synonyms An array of synonyms, or a string containing a
430
- # comma separated list of synonyms for this tag.
431
- # @param [String] description A longer description of the tag.
432
- # @param [kwargs] **tags Exactly one pair of `tag` => `parent` where tag is either a Symbol or a String
433
- # for the tag to be added, and parent is either a {Tag}, a symbol or a string of an existing tag.
434
- # @return [Array<Tag>] An array of tags successfully added.
435
- #
436
- def self.add(label: nil, synonyms: "", description: "", **tags)
437
- raise "Tags must be specified" if tags.empty?
438
- if (tags.length > 1) && !(label.nil? && synonyms.empty? && description.empty?)
439
- raise "Additional options can only be specified when creating one tag"
440
- end
441
-
442
- synonyms = synonyms.map(&:to_s).map(&:strip).join(",") if synonyms.is_a?(Array)
443
-
444
- tags.map do |name, parent|
445
- parent_is_tag = parent.respond_to?(:java_class) && parent.java_class < Tag.java_class
446
- parent = parent_is_tag ? parent.java_class : parent.to_s
447
- name = name.to_s
448
- org.openhab.core.semantics.SemanticTags.add(name, parent, label, synonyms, description)
449
- &.then { const_missing(name) }
450
- end.compact
451
- end
452
- end
453
-
454
- #
455
- # Returns all available Semantic tags
456
- #
457
- # @return [Array<Tag>] an array containing all the Semantic tags
458
- #
459
- def self.tags
460
- java.util.stream.Stream.of(
461
- org.openhab.core.semantics.model.point.Points.stream,
462
- org.openhab.core.semantics.model.property.Properties.stream,
463
- org.openhab.core.semantics.model.equipment.Equipments.stream,
464
- org.openhab.core.semantics.model.location.Locations.stream
465
- ).flat_map(&:itself).map(&:ruby_class).iterator.to_a
466
- end
467
-
468
- #
469
- # Finds the semantic tag using its name, label, or synonyms.
470
- #
471
- # @param [String,Symbol] id The tag name, label, or synonym to look up
472
- # @param [java.util.Locale] locale The locale of the given label or synonym
473
- #
474
- # @return [Tag,nil] The semantic tag class if found, or nil if not found.
475
- #
476
- def self.lookup(id, locale = nil)
477
- id = id.to_sym
478
- return const_get(id) if constants.include?(id) || const_missing(id)
479
-
480
- locale = java.util.Locale.default if locale.nil?
481
- org.openhab.core.semantics.SemanticTags.get_by_label_or_synonym(id.to_s, locale).first&.ruby_class
482
- end
483
-
484
- #
485
- # Automatically looks up new semantic classes and adds them as `constants`
486
- #
487
- # @return [Tag, nil]
488
- #
489
- # @!visibility private
490
- def self.const_missing(sym)
491
- logger.trace("const missing, performing Semantics Lookup for: #{sym}")
492
- # @deprecated OH3.4 - the Property tag had an ID of "MeasurementProperty" in OH3.4. This was corrected in OH4.
493
- # make sure we compare against pre-release versions
494
- target_sym = sym
495
- if sym == :Property && Gem::Version.new(Core::VERSION) < Gem::Version.new("4.0.0.M1")
496
- sym = :MeasurementProperty
497
- end
498
-
499
- org.openhab.core.semantics.SemanticTags.get_by_id(sym.to_s)
500
- &.then do |tag|
501
- tag = tag.ruby_class
502
- tag.singleton_class.include(TagClassMethods)
503
- const_set(target_sym, tag)
504
- end
505
- end
506
-
507
- #
508
- # Adds tag attributes to the semantic tag class
509
- #
510
- module TagClassMethods
511
- # @!visibility private
512
- java_import org.openhab.core.semantics.SemanticTags
513
-
514
- #
515
- # Returns the tag's label
516
- #
517
- # @param [java.util.Locale] locale The locale that the label should be in, if available.
518
- # When nil, the system's default locale is used.
519
- #
520
- # @return [String] The tag's label
521
- #
522
- def label(locale = nil)
523
- SemanticTags.get_label(java_class, locale || java.util.Locale.default)
524
- end
525
-
526
- #
527
- # Returns the tag's synonyms
528
- #
529
- # @param [java.util.Locale] locale The locale that the label should be in, if available.
530
- # When nil, the system's default locale is used.
531
- #
532
- # @return [Array<String>] The list of synonyms in the requested locale.
533
- #
534
- def synonyms(locale = nil)
535
- unless SemanticTags.respond_to?(:get_synonyms) # @deprecated OH3.4
536
- return java_class.get_annotation(org.openhab.core.semantics.TagInfo.java_class).synonyms
537
- .split(",").map(&:strip)
538
- end
539
-
540
- SemanticTags.get_synonyms(java_class, locale || java.util.Locale.default).to_a
541
- end
542
-
543
- #
544
- # Returns the tag's description
545
- #
546
- # @param [java.util.Locale] locale The locale that the description should be in, if available.
547
- # When nil, the system's default locale is used.
548
- #
549
- # @return [String] The tag's description
550
- #
551
- def description(locale = nil)
552
- unless SemanticTags.respond_to?(:get_description) # @deprecated OH3.4
553
- return java_class.get_annotation(org.openhab.core.semantics.TagInfo.java_class).description
554
- end
555
-
556
- SemanticTags.get_description(java_class, locale || java.util.Locale.default)
557
- end
558
- end
559
556
  end
560
557
  end
561
558
  end
@@ -572,7 +569,9 @@ module Enumerable
572
569
  # Returns a new array of items that are a semantics Location (optionally of the given type)
573
570
  # @return [Array<Item>]
574
571
  def locations(type = nil)
575
- if type && (!type.is_a?(Module) || !(type < Semantics::Location))
572
+ begin
573
+ raise ArgumentError if type && !(type < Semantics::Location)
574
+ rescue ArgumentError, TypeError
576
575
  raise ArgumentError, "type must be a subclass of Location"
577
576
  end
578
577
 
@@ -595,7 +594,9 @@ module Enumerable
595
594
  # @example Get all TVs in a room
596
595
  # lGreatRoom.equipments(Semantics::Screen)
597
596
  def equipments(type = nil)
598
- if type && (!type.is_a?(Module) || !(type < Semantics::Equipment))
597
+ begin
598
+ raise ArgumentError if type && !(type < Semantics::Equipment)
599
+ rescue ArgumentError, TypeError
599
600
  raise ArgumentError, "type must be a subclass of Equipment"
600
601
  end
601
602
 
@@ -618,11 +619,13 @@ module Enumerable
618
619
  unless (0..2).cover?(point_or_property_types.length)
619
620
  raise ArgumentError, "wrong number of arguments (given #{point_or_property_types.length}, expected 0..2)"
620
621
  end
621
- unless point_or_property_types.all? do |tag|
622
- tag.is_a?(Module) &&
623
- (tag < Semantics::Point ||
624
- tag < Semantics::Property)
625
- end
622
+
623
+ begin
624
+ raise ArgumentError unless point_or_property_types.all? do |tag|
625
+ (tag < Semantics::Point ||
626
+ tag < Semantics::Property)
627
+ end
628
+ rescue ArgumentError, TypeError
626
629
  raise ArgumentError, "point_or_property_types must all be a subclass of Point or Property"
627
630
  end
628
631
  if point_or_property_types.count { |tag| tag < Semantics::Point } > 1 ||
@@ -632,7 +635,7 @@ module Enumerable
632
635
 
633
636
  select do |point|
634
637
  point.point? && point_or_property_types.all? do |tag|
635
- (tag < Semantics::Point && point.point_type <= tag) ||
638
+ (tag < Semantics::Point && point.point_type&.<=(tag)) ||
636
639
  (tag < Semantics::Property && point.property_type&.<=(tag))
637
640
  end
638
641
  end
@@ -214,7 +214,8 @@ module OpenHAB
214
214
 
215
215
  # @!visibility private
216
216
  def unregister
217
- self.class.registry.remove_provider(self)
217
+ # @deprecated OH3.4 safe navigation only required for missing Semantics registry
218
+ self.class.registry&.remove_provider(self)
218
219
  end
219
220
 
220
221
  private
@@ -222,7 +223,8 @@ module OpenHAB
222
223
  def initialize(unload_priority: nil)
223
224
  super()
224
225
  @elements = java.util.concurrent.ConcurrentHashMap.new
225
- self.class.registry.add_provider(self)
226
+ # @deprecated OH3.4 safe navigation only required for missing Semantics registry
227
+ self.class.registry&.add_provider(self)
226
228
  ScriptHandling.script_unloaded(priority: unload_priority) { unregister }
227
229
  end
228
230
  end
@@ -222,13 +222,17 @@ module OpenHAB
222
222
  #
223
223
  # @!visibility private
224
224
  def normalize_tags(*tags)
225
- semantics = proc { |tag| tag.respond_to?(:java_class) && tag < Semantics::Tag }
225
+ # @deprecated OH3.4 didn't have SemanticTag
226
+ old_semantics = proc { |tag| tag.is_a?(Module) && tag < Semantics::Tag }
227
+ # @deprecated OH3.4 defined? check is unnecessary
228
+ semantics = proc { |tag| defined?(Semantics::SemanticTag) && tag.is_a?(Semantics::SemanticTag) }
226
229
 
227
230
  tags.compact.map do |tag|
228
231
  case tag
229
232
  when String then tag
230
233
  when Symbol then tag.to_s
231
- when semantics then tag.java_class.simple_name
234
+ when old_semantics then tag.java_class.simple_name
235
+ when semantics then tag.name
232
236
  else raise ArgumentError, "`#{tag}` must be a subclass of Semantics::Tag, a `Symbol`, or a `String`."
233
237
  end
234
238
  end
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of openHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.4.1"
7
+ VERSION = "5.4.2"
8
8
  end
9
9
  end
data/lib/openhab/log.rb CHANGED
@@ -241,6 +241,13 @@ module OpenHAB
241
241
  #
242
242
  # @return [:error,:warn,:info,:debug,:trace] The current log level
243
243
  #
244
+ # @example Retrieve the current log level
245
+ # level = OpenHAB::Log.logger("openhab.event.ItemStateChangedEvent").level
246
+ # logger.info "The log level for 'openhab.event.ItemStateChangedEvent' is #{level}"
247
+ #
248
+ # @example Set the log level
249
+ # OpenHAB::Log.logger("openhab.event.ItemStateChangedEvent").level = :warn
250
+ #
244
251
  def level
245
252
  Logger.log_service.get_level(name)[name]&.downcase&.to_sym
246
253
  end
@@ -65,6 +65,7 @@ module OpenHAB
65
65
  # Each spec gets temporary providers
66
66
  [Core::Items::Provider,
67
67
  Core::Items::Metadata::Provider,
68
+ Core::Items::Semantics::Provider,
68
69
  Core::Rules::Provider,
69
70
  Core::Things::Provider,
70
71
  Core::Things::Links::Provider].each do |klass|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.1
4
+ version: 5.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-07-17 00:00:00.000000000 Z
13
+ date: 2023-07-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -434,6 +434,9 @@ files:
434
434
  - lib/openhab/core/items/rollershutter_item.rb
435
435
  - lib/openhab/core/items/semantics.rb
436
436
  - lib/openhab/core/items/semantics/enumerable.rb
437
+ - lib/openhab/core/items/semantics/provider.rb
438
+ - lib/openhab/core/items/semantics/semantic_tag.rb
439
+ - lib/openhab/core/items/semantics/tag_class_methods.rb
437
440
  - lib/openhab/core/items/state_storage.rb
438
441
  - lib/openhab/core/items/string_item.rb
439
442
  - lib/openhab/core/items/switch_item.rb