openhab-jrubyscripting 5.0.0.rc2 → 5.0.0.rc4

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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions.rb +6 -2
  3. data/lib/openhab/core/items/generic_item.rb +13 -5
  4. data/lib/openhab/core/items/metadata/hash.rb +19 -35
  5. data/lib/openhab/core/items/persistence.rb +3 -1
  6. data/lib/openhab/core/items/semantics/enumerable.rb +6 -4
  7. data/lib/openhab/core/items/semantics.rb +18 -22
  8. data/lib/openhab/core/profile_factory.rb +15 -3
  9. data/lib/openhab/core/provider.rb +11 -4
  10. data/lib/openhab/core/rules/module.rb +26 -0
  11. data/lib/openhab/core/rules/provider.rb +40 -0
  12. data/lib/openhab/core/rules/registry.rb +76 -0
  13. data/lib/openhab/core/rules/rule.rb +150 -0
  14. data/lib/openhab/core/rules.rb +25 -0
  15. data/lib/openhab/core/script_handling.rb +32 -3
  16. data/lib/openhab/core/timer.rb +5 -7
  17. data/lib/openhab/core/types.rb +1 -1
  18. data/lib/openhab/core.rb +0 -16
  19. data/lib/openhab/core_ext/java/list.rb +436 -0
  20. data/lib/openhab/core_ext/java/map.rb +66 -0
  21. data/lib/openhab/core_ext/java/month.rb +1 -1
  22. data/lib/openhab/core_ext/java/zoned_date_time.rb +1 -2
  23. data/lib/openhab/core_ext/ruby/date.rb +3 -0
  24. data/lib/openhab/core_ext/ruby/date_time.rb +53 -0
  25. data/lib/openhab/core_ext/ruby/time.rb +88 -86
  26. data/lib/openhab/dsl/events/watch_event.rb +1 -1
  27. data/lib/openhab/dsl/items/builder.rb +8 -3
  28. data/lib/openhab/dsl/items/ensure.rb +6 -2
  29. data/lib/openhab/dsl/items/timed_command.rb +10 -11
  30. data/lib/openhab/dsl/rules/automation_rule.rb +36 -13
  31. data/lib/openhab/dsl/rules/builder.rb +100 -9
  32. data/lib/openhab/dsl/rules/name_inference.rb +0 -5
  33. data/lib/openhab/dsl/rules/rule_triggers.rb +1 -1
  34. data/lib/openhab/dsl/rules/terse.rb +1 -2
  35. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +18 -54
  36. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +0 -3
  37. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +1 -1
  38. data/lib/openhab/dsl/rules.rb +0 -21
  39. data/lib/openhab/dsl/thread_local.rb +2 -2
  40. data/lib/openhab/dsl/timer_manager.rb +3 -1
  41. data/lib/openhab/dsl/version.rb +1 -1
  42. data/lib/openhab/dsl.rb +12 -105
  43. data/lib/openhab/log.rb +9 -2
  44. data/lib/openhab/rspec/example_group.rb +42 -0
  45. data/lib/openhab/rspec/helpers.rb +31 -8
  46. data/lib/openhab/rspec/hooks.rb +6 -10
  47. data/lib/openhab/rspec/karaf.rb +50 -27
  48. data/lib/openhab/rspec/mocks/synchronous_executor.rb +11 -4
  49. data/lib/openhab/rspec/mocks/timer.rb +2 -1
  50. data/lib/openhab/rspec/suspend_rules.rb +4 -2
  51. metadata +24 -3
  52. data/lib/openhab/dsl/script_handling.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e925ebd72648fa8efb76592f6053d12f2fe9151173fa91d3f47a2d298f268fc
4
- data.tar.gz: 6d84217a8505a72e2da686188a741d6644eee645255595d5d2cf455a2840e015
3
+ metadata.gz: 2e06b71ae524165363db99771e7ef601f542ed14c979572d152b079d70ad82a2
4
+ data.tar.gz: 0d6002391ed26a68bbb907d94787f510df30ef6ef9a3a7cd445ac9c47122851b
5
5
  SHA512:
6
- metadata.gz: 5330bd233b45839cf1780815e86d170ff8a33b4093debfe85cae7e09e631d7a14c7bb2c7e84500da6a41fd0b89f81a160e4279659312323e6fb207bd0da6e632
7
- data.tar.gz: 28f42c9a625aa33ef258f4d789e9fe116a1bf36e63bc58e2574d1a884ed8bde74bac03484566abd04909462ec4c9bdef543973416a2420fa02fea274122e3889
6
+ metadata.gz: 05a3016219eaf41fc4af71f1476029e2b8d0b320afc7bed99e9777b7726afc6897328d9aa92f643132e66d647aae8742eca36f26c703ab3763e1738c55d249e1
7
+ data.tar.gz: d5a7d65543b3628e5c3abfa61cb2aa6d6f10e2ab2bb3334d9f38e1ee8952da655da6360c156d926bd53b25c013c86c7672dc1b492c85aaa056e898579a7be7dd
@@ -66,11 +66,15 @@ module OpenHAB
66
66
  #
67
67
  module Actions
68
68
  OSGi.services("org.openhab.core.model.script.engine.action.ActionService")&.each do |service|
69
- java_import service.action_class.ruby_class
69
+ klass = (java_import service.action_class.ruby_class).first
70
70
  logger.trace("Loaded ACTION: #{service.action_class}")
71
+ Object.const_set(service.action_class.simple_name, klass)
71
72
  end
72
73
  # Import common actions
73
- %w[Exec HTTP Ping].each { |action| java_import "org.openhab.core.model.script.actions.#{action}" }
74
+ %w[Exec HTTP Ping].each do |action|
75
+ klass = (java_import "org.openhab.core.model.script.actions.#{action}").first
76
+ Object.const_set(action, klass)
77
+ end
74
78
 
75
79
  module_function
76
80
 
@@ -65,16 +65,23 @@ module OpenHAB
65
65
  #
66
66
  # Send a command to this item
67
67
  #
68
+ # When this method is chained after the {OpenHAB::DSL::Items::Ensure::Ensurable#ensure ensure}
69
+ # method, or issued inside an {OpenHAB::DSL.ensure_states ensure_states} block,
70
+ # the command will only be sent if the item is not already in the same state.
71
+ #
68
72
  # @param [Command] command command to send to the item
69
- # @return [self]
73
+ # @return [self, nil] nil when `ensure` is in effect and the item was already in the same state,
74
+ # otherwise the item.
70
75
  #
71
- # @see DSL::Items::TimedCommand#command
76
+ # @see DSL::Items::TimedCommand#command Timed Command
77
+ # @see OpenHAB::DSL.ensure_states ensure_states
78
+ # @see DSL::Items::Ensure::Ensurable#ensure ensure
72
79
  #
73
80
  def command(command)
74
81
  command = format_command(command)
75
82
  logger.trace "Sending Command #{command} to #{name}"
76
83
  $events.send_command(self, command)
77
- self
84
+ Proxy.new(self)
78
85
  end
79
86
 
80
87
  # not an alias to allow easier stubbing and overriding
@@ -88,13 +95,14 @@ module OpenHAB
88
95
  # Send an update to this item
89
96
  #
90
97
  # @param [State] state
91
- # @return [self]
98
+ # @return [self, nil] nil when `ensure` is in effect and the item was already in the same state,
99
+ # otherwise the item.
92
100
  #
93
101
  def update(state)
94
102
  state = format_update(state)
95
103
  logger.trace "Sending Update #{state} to #{name}"
96
104
  $events.post_update(self, state)
97
- self
105
+ Proxy.new(self)
98
106
  end
99
107
 
100
108
  #
@@ -26,7 +26,7 @@ module OpenHAB
26
26
 
27
27
  extend Forwardable
28
28
  def_delegators :@metadata, :configuration, :hash, :to_s, :uid, :value
29
- protected :configuration
29
+ private :configuration
30
30
 
31
31
  # @!method to_hash
32
32
  # Implicit conversion to {::Hash}.
@@ -37,6 +37,9 @@ module OpenHAB
37
37
  # still others (mutators) must be manually implemented below.
38
38
  def_delegators :configuration,
39
39
  :any?,
40
+ :compact,
41
+ :compare_by_identity?,
42
+ :deconstruct_keys,
40
43
  :default,
41
44
  :default_proc,
42
45
  :each,
@@ -47,6 +50,8 @@ module OpenHAB
47
50
  :filter,
48
51
  :flatten,
49
52
  :has_value?,
53
+ :invert,
54
+ :key,
50
55
  :keys,
51
56
  :length,
52
57
  :rassoc,
@@ -57,8 +62,10 @@ module OpenHAB
57
62
  :to_a,
58
63
  :to_h,
59
64
  :to_hash,
65
+ :transform_keys,
66
+ :transform_values,
67
+ :values,
60
68
  :value?
61
- def_delegators :to_h, :invert, :merge, :transform_keys, :transform_values
62
69
 
63
70
  def_delegator :uid, :namespace
64
71
 
@@ -88,7 +95,7 @@ module OpenHAB
88
95
  end
89
96
 
90
97
  # @!visibility private
91
- def initialize(metadata)
98
+ def initialize(metadata = nil)
92
99
  @metadata = metadata
93
100
  end
94
101
 
@@ -156,7 +163,7 @@ module OpenHAB
156
163
  return false unless value == other.value
157
164
  end
158
165
 
159
- to_h < other
166
+ configuration < other
160
167
  end
161
168
 
162
169
  # @!visibility private
@@ -166,7 +173,7 @@ module OpenHAB
166
173
  return false unless value == other.value
167
174
  end
168
175
 
169
- to_h <= other
176
+ configuration <= other
170
177
  end
171
178
 
172
179
  # @!visibility private
@@ -176,7 +183,7 @@ module OpenHAB
176
183
 
177
184
  return configuration == other.configuration
178
185
  elsif value.empty? && other.respond_to?(:to_hash)
179
- return configuration.to_h == other.to_hash
186
+ return configuration == other.to_hash
180
187
  end
181
188
  false
182
189
  end
@@ -188,7 +195,7 @@ module OpenHAB
188
195
  return false unless value == other.value
189
196
  end
190
197
 
191
- to_h > other
198
+ configuration > other
192
199
  end
193
200
 
194
201
  # @!visibility private
@@ -198,7 +205,7 @@ module OpenHAB
198
205
  return false unless value == other.value
199
206
  end
200
207
 
201
- to_h >= other
208
+ configuration >= other
202
209
  end
203
210
 
204
211
  # @!visibility private
@@ -226,13 +233,9 @@ module OpenHAB
226
233
  replace({})
227
234
  end
228
235
 
229
- # @!visibility private
230
- alias_method :compact, :to_h
231
-
232
236
  # @!visibility private
233
237
  def compact!
234
- # no action; impossible to have nil keys
235
- self
238
+ replace(compact)
236
239
  end
237
240
 
238
241
  # @!visibility private
@@ -240,16 +243,6 @@ module OpenHAB
240
243
  raise NotImplementedError
241
244
  end
242
245
 
243
- # @!visibility private
244
- def compare_by_identity?
245
- false
246
- end
247
-
248
- # @!visibility private
249
- def deconstruct_keys
250
- self
251
- end
252
-
253
246
  # @!visibility private
254
247
  def default=(*)
255
248
  raise NotImplementedError
@@ -304,11 +297,6 @@ module OpenHAB
304
297
  self
305
298
  end
306
299
 
307
- # @!visibility private
308
- def key(value)
309
- rassoc(value)&.first
310
- end
311
-
312
300
  # @!visibility private
313
301
  def key?(key)
314
302
  configuration.key?(key.to_s)
@@ -360,7 +348,7 @@ module OpenHAB
360
348
 
361
349
  # @!visibility private
362
350
  def slice(*keys)
363
- to_h.slice(*keys.map(&:to_s))
351
+ configuration.slice(*keys.map(&:to_s))
364
352
  end
365
353
 
366
354
  # @!visibility private
@@ -380,11 +368,6 @@ module OpenHAB
380
368
  replace(transform_values(&block))
381
369
  end
382
370
 
383
- # @!visibility private
384
- def values
385
- configuration.values.to_a
386
- end
387
-
388
371
  # @!visibility private
389
372
  def values_at(*keys)
390
373
  configuration.values_at(*keys.map(&:to_s))
@@ -397,6 +380,7 @@ module OpenHAB
397
380
 
398
381
  [value, to_h].inspect
399
382
  end
383
+ remove_method :to_s
400
384
  alias_method :to_s, :inspect
401
385
 
402
386
  #
@@ -416,7 +400,7 @@ module OpenHAB
416
400
  return preferred_provider unless provider
417
401
 
418
402
  unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
419
- raise FrozenError, "Cannot modify metadata from provider #{provider.inspect}"
403
+ raise FrozenError, "Cannot modify metadata from provider #{provider.inspect} for #{uid}."
420
404
  end
421
405
 
422
406
  if preferred_provider != provider
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "delegate"
4
+
3
5
  require_relative "generic_item"
4
6
 
5
7
  module OpenHAB
@@ -284,7 +286,7 @@ module OpenHAB
284
286
  # @return [Object] QuantityType or the original value
285
287
  #
286
288
  def quantify(value)
287
- if value.is_a?(DecimalType) && unit
289
+ if value.is_a?(DecimalType) && respond_to?(:unit) && unit
288
290
  logger.trace("Unitizing #{value} with unit #{unit}")
289
291
  QuantityType.new(value.to_big_decimal, unit)
290
292
  else
@@ -72,15 +72,17 @@ module Enumerable
72
72
  # @!group Items State and Command Methods
73
73
 
74
74
  # Send a command to every item in the collection
75
- # @return [self]
75
+ # @return [self, nil] nil when `ensure` is in effect and all the items were already in the same state,
76
+ # otherwise self
76
77
  def command(command)
77
- each { |i| i.command(command) }
78
+ self if count { |i| i.command(command) }.positive?
78
79
  end
79
80
 
80
81
  # Update the state of every item in the collection
81
- # @return [self]
82
+ # @return [self, nil] nil when `ensure` is in effect and all the items were already in the same state,
83
+ # otherwise self
82
84
  def update(state)
83
- each { |i| i.update(state) }
85
+ self if count { |i| i.update(state) }.positive?
84
86
  end
85
87
 
86
88
  # @!method refresh
@@ -187,11 +187,6 @@ module OpenHAB
187
187
  # end
188
188
  #
189
189
 
190
- # @!visibility private
191
- # import the actual semantics action
192
- SemanticsAction = org.openhab.core.model.script.actions.Semantics
193
- private_constant :SemanticsAction
194
-
195
190
  # import all the semantics constants
196
191
  [org.openhab.core.semantics.model.point.Points,
197
192
  org.openhab.core.semantics.model.property.Properties,
@@ -223,6 +218,7 @@ module OpenHAB
223
218
  # Property = org.openhab.core.semantics.Property
224
219
 
225
220
  # put ourself into the global namespace, replacing the action
221
+ Object.send(:remove_const, :Semantics)
226
222
  ::Semantics = self # rubocop:disable Naming/ConstantName
227
223
 
228
224
  #
@@ -234,7 +230,7 @@ module OpenHAB
234
230
  # @return [true, false]
235
231
  #
236
232
  def location?
237
- SemanticsAction.location?(self)
233
+ Actions::Semantics.location?(self)
238
234
  end
239
235
 
240
236
  #
@@ -246,7 +242,7 @@ module OpenHAB
246
242
  # @return [true, false]
247
243
  #
248
244
  def equipment?
249
- SemanticsAction.equipment?(self)
245
+ Actions::Semantics.equipment?(self)
250
246
  end
251
247
 
252
248
  # Checks if this Item is a {Point}
@@ -257,7 +253,7 @@ module OpenHAB
257
253
  # @return [true, false]
258
254
  #
259
255
  def point?
260
- SemanticsAction.point?(self)
256
+ Actions::Semantics.point?(self)
261
257
  end
262
258
 
263
259
  #
@@ -280,7 +276,7 @@ module OpenHAB
280
276
  # @return [GenericItem, nil]
281
277
  #
282
278
  def location
283
- SemanticsAction.get_location(self)&.then(&Proxy.method(:new))
279
+ Actions::Semantics.get_location(self)&.then(&Proxy.method(:new))
284
280
  end
285
281
 
286
282
  #
@@ -293,7 +289,7 @@ module OpenHAB
293
289
  # @return [Class, nil]
294
290
  #
295
291
  def location_type
296
- SemanticsAction.get_location_type(self)&.ruby_class
292
+ Actions::Semantics.get_location_type(self)&.ruby_class
297
293
  end
298
294
 
299
295
  #
@@ -306,7 +302,7 @@ module OpenHAB
306
302
  # @return [GenericItem, nil]
307
303
  #
308
304
  def equipment
309
- SemanticsAction.get_equipment(self)&.then(&Proxy.method(:new))
305
+ Actions::Semantics.get_equipment(self)&.then(&Proxy.method(:new))
310
306
  end
311
307
 
312
308
  #
@@ -319,7 +315,7 @@ module OpenHAB
319
315
  # @return [Class, nil]
320
316
  #
321
317
  def equipment_type
322
- SemanticsAction.get_equipment_type(self)&.ruby_class
318
+ Actions::Semantics.get_equipment_type(self)&.ruby_class
323
319
  end
324
320
 
325
321
  #
@@ -330,7 +326,7 @@ module OpenHAB
330
326
  # @return [Class, nil]
331
327
  #
332
328
  def point_type
333
- SemanticsAction.get_point_type(self)&.ruby_class
329
+ Actions::Semantics.get_point_type(self)&.ruby_class
334
330
  end
335
331
 
336
332
  #
@@ -341,7 +337,7 @@ module OpenHAB
341
337
  # @return [Class, nil]
342
338
  #
343
339
  def property_type
344
- SemanticsAction.get_property_type(self)&.ruby_class
340
+ Actions::Semantics.get_property_type(self)&.ruby_class
345
341
  end
346
342
 
347
343
  # @!attribute [r] semantic_type
@@ -355,7 +351,7 @@ module OpenHAB
355
351
  # @return [Class, nil]
356
352
  #
357
353
  def semantic_type
358
- SemanticsAction.get_semantic_type(self)&.ruby_class
354
+ Actions::Semantics.get_semantic_type(self)&.ruby_class
359
355
  end
360
356
 
361
357
  #
@@ -409,7 +405,7 @@ module Enumerable
409
405
  # Returns a new array of items that are a semantics Location (optionally of the given type)
410
406
  # @return [Array<GenericItem>]
411
407
  def locations(type = nil)
412
- if type && (!type.is_a?(Module) || !(type < OpenHAB::Core::Items::Semantics::Location))
408
+ if type && (!type.is_a?(Module) || !(type < Semantics::Location))
413
409
  raise ArgumentError, "type must be a subclass of Location"
414
410
  end
415
411
 
@@ -432,7 +428,7 @@ module Enumerable
432
428
  # @example Get all TVs in a room
433
429
  # lGreatRoom.equipments(Semantics::Screen)
434
430
  def equipments(type = nil)
435
- if type && (!type.is_a?(Module) || !(type < OpenHAB::Core::Items::Semantics::Equipment))
431
+ if type && (!type.is_a?(Module) || !(type < Semantics::Equipment))
436
432
  raise ArgumentError, "type must be a subclass of Equipment"
437
433
  end
438
434
 
@@ -457,19 +453,19 @@ module Enumerable
457
453
  end
458
454
  unless point_or_property_types.all? do |tag|
459
455
  tag.is_a?(Module) &&
460
- (tag < OpenHAB::Core::Items::Semantics::Point || tag < OpenHAB::Core::Items::Semantics::Property)
456
+ (tag < Semantics::Point || tag < Semantics::Property)
461
457
  end
462
458
  raise ArgumentError, "point_or_property_types must all be a subclass of Point or Property"
463
459
  end
464
- if point_or_property_types.count { |tag| tag < OpenHAB::Core::Items::Semantics::Point } > 1 ||
465
- point_or_property_types.count { |tag| tag < OpenHAB::Core::Items::Semantics::Property } > 1
460
+ if point_or_property_types.count { |tag| tag < Semantics::Point } > 1 ||
461
+ point_or_property_types.count { |tag| tag < Semantics::Property } > 1
466
462
  raise ArgumentError, "point_or_property_types cannot both be a subclass of Point or Property"
467
463
  end
468
464
 
469
465
  select do |point|
470
466
  point.point? && point_or_property_types.all? do |tag|
471
- (tag < OpenHAB::Core::Items::Semantics::Point && point.point_type <= tag) ||
472
- (tag < OpenHAB::Core::Items::Semantics::Property && point.property_type&.<=(tag))
467
+ (tag < Semantics::Point && point.point_type <= tag) ||
468
+ (tag < Semantics::Property && point.property_type&.<=(tag))
473
469
  end
474
470
  end
475
471
  end
@@ -87,6 +87,8 @@ module OpenHAB
87
87
  DSL::ThreadLocal.thread_local(**@thread_locals) do
88
88
  @block.call(event, **kwargs)
89
89
  rescue Exception => e
90
+ raise if defined?(::RSpec)
91
+
90
92
  @block.binding.eval("self").logger.log_exception(e)
91
93
  end
92
94
  end
@@ -96,6 +98,19 @@ module OpenHAB
96
98
 
97
99
  def initialize
98
100
  @profiles = {}
101
+
102
+ @registration = OSGi.register_service(self)
103
+ ScriptHandling.script_unloaded { unregister }
104
+ end
105
+
106
+ #
107
+ # Unregister the ProfileFactory OSGi service
108
+ #
109
+ # @!visibility private
110
+ # @return [void]
111
+ #
112
+ def unregister
113
+ @registration.unregister
99
114
  end
100
115
 
101
116
  # @!visibility private
@@ -111,8 +126,5 @@ module OpenHAB
111
126
  @profiles.keys
112
127
  end
113
128
  end
114
-
115
- registration = OSGi.register_service(ProfileFactory.instance)
116
- ScriptHandling.script_unloaded { registration.unregister }
117
129
  end
118
130
  end
@@ -13,6 +13,7 @@ module OpenHAB
13
13
  TYPE_TO_PROVIDER_TYPE = {
14
14
  org.openhab.core.items.Item.java_class => :items,
15
15
  org.openhab.core.items.Metadata.java_class => :metadata,
16
+ org.openhab.core.automation.Rule.java_class => :rules,
16
17
  org.openhab.core.thing.Thing.java_class => :things,
17
18
  org.openhab.core.thing.link.ItemChannelLink.java_class => :links
18
19
  }.freeze
@@ -37,6 +38,7 @@ module OpenHAB
37
38
  # @abstract
38
39
  class Provider < org.openhab.core.common.registry.AbstractProvider
39
40
  include org.openhab.core.common.registry.ManagedProvider
41
+ include Enumerable
40
42
  include Singleton
41
43
  public_class_method :new
42
44
 
@@ -140,6 +142,11 @@ module OpenHAB
140
142
  end
141
143
  end
142
144
 
145
+ # @!visibility private
146
+ def each(&block)
147
+ @elements.each_value(&block)
148
+ end
149
+
143
150
  # @return [String]
144
151
  def inspect
145
152
  "#<#{self.class.name}:#{object_id}>"
@@ -192,7 +199,7 @@ module OpenHAB
192
199
  def update(element)
193
200
  old_element = @elements[element.uid]
194
201
  if old_element
195
- @elements[element.uid]
202
+ @elements[element.uid] = element
196
203
  notify_listeners_about_updated_element(old_element, element)
197
204
  end
198
205
  old_element
@@ -205,11 +212,11 @@ module OpenHAB
205
212
 
206
213
  private
207
214
 
208
- def initialize
209
- super
215
+ def initialize(script_unloaded_before: nil)
216
+ super()
210
217
  @elements = {}
211
218
  self.class.registry.add_provider(self)
212
- ScriptHandling.script_unloaded { unregister }
219
+ ScriptHandling.script_unloaded(before: script_unloaded_before) { unregister }
213
220
  end
214
221
  end
215
222
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Rules
6
+ # @interface
7
+ java_import org.openhab.core.automation.Module
8
+
9
+ # @!visibility private
10
+ module Module
11
+ # @return [String]
12
+ def inspect
13
+ r = "#<OpenHAB::Core::Rules::#{self.class.simple_name} #{id} (#{type_uid})"
14
+ r += " #{label.inspect}" if label
15
+ r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
16
+ "#{r}>"
17
+ end
18
+
19
+ # @return [String]
20
+ def to_s
21
+ id
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Rules
6
+ #
7
+ # Provides rules created in Ruby to openHAB
8
+ #
9
+ class Provider < Core::Provider
10
+ include org.openhab.core.automation.RuleProvider
11
+
12
+ class << self
13
+ #
14
+ # The Rule registry
15
+ #
16
+ # @return [org.openhab.core.automation.RuleRegistry]
17
+ #
18
+ def registry
19
+ $rules
20
+ end
21
+ end
22
+
23
+ def initialize
24
+ super(script_unloaded_before: lambda do |callbacks|
25
+ callbacks.index do |cb|
26
+ case cb.binding.receiver
27
+ when Items::Provider,
28
+ Things::Provider,
29
+ DSL::TimerManager
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+ end)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Rules
8
+ #
9
+ # Provides access to all OpenHAB {Rule rules}, and acts like an array.
10
+ #
11
+ class Registry
12
+ include LazyArray
13
+ include Singleton
14
+
15
+ #
16
+ # Gets a specific Rule
17
+ #
18
+ # @param [String] uid Rule UID
19
+ # @return [Rule, nil]
20
+ #
21
+ def [](uid)
22
+ $rules.get(uid)
23
+ end
24
+ alias_method :include?, :[]
25
+ alias_method :key?, :[]
26
+ # @deprecated
27
+ alias_method :has_key?, :[]
28
+
29
+ #
30
+ # Explicit conversion to array
31
+ #
32
+ # @return [Array<Rule>]
33
+ #
34
+ def to_a
35
+ $rules.all.to_a
36
+ end
37
+
38
+ #
39
+ # Enter the Rule Builder DSL.
40
+ # @param (see Core::Provider.current)
41
+ # @yield Block executed in the context of a {DSL::Rules::Builder}.
42
+ # @return [Object] The result of the block.
43
+ #
44
+ def build(preferred_provider = nil, &block)
45
+ DSL::Rules::Builder.new(preferred_provider).instance_eval(&block)
46
+ end
47
+
48
+ #
49
+ # Remove a Rule.
50
+ #
51
+ # The rule must be a managed thing (typically created by Ruby or in the UI).
52
+ #
53
+ # @param [String, Rule] rule_uid
54
+ # @return [Rule, nil] The removed rule, if found.
55
+ #
56
+ # @example
57
+ # my_rule = rule do
58
+ # every :day
59
+ # run { nil }
60
+ # end
61
+ #
62
+ # rules.remove(my_rule)
63
+ #
64
+ def remove(rule_uid)
65
+ rule_uid = rule_uid.uid if rule_uid.is_a?(Rule)
66
+ provider = Provider.registry.provider_for(rule_uid)
67
+ unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
68
+ raise "Cannot remove rule #{rule_uid} from non-managed provider #{provider.inspect}"
69
+ end
70
+
71
+ provider.remove(rule_uid)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end