openhab-scripting 4.13.5 → 4.15.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36ea22254771f9c0d47227ab45818c8eb460b78079134907a73347b09165d536
4
- data.tar.gz: 8536ce77360c6156b654db1c2225128fa3aa5bb2373d8f05b4dedd47e0c0fc9c
3
+ metadata.gz: fa5e8f47863d4b84f260403bcfc62b027fe118a9db4475044c8d25e202a84837
4
+ data.tar.gz: 7e5b9b690dd7c734c2d4bebda07ed5280d0ff718be1c760ce5f2bfb7846a2c43
5
5
  SHA512:
6
- metadata.gz: 4ccaa0e34778eaacb554c9c533b4d22db5cc62274df0d61e3f18adc00e3bdda0bb85e7d423967bf8ab27bab73591d0eee5f040232ef1fa973aa03e7c7d7065c1
7
- data.tar.gz: 84b569a30270cc7458f4ff16210a773154c2fc3a8da46706106b13dad498b6ed1ae94c964694afe6478b275636078ac49eb79f294845202d1168aef3df1fe8c5
6
+ metadata.gz: 0a9e776aa7e5c784a62c803d6cbb99218ff592ffa60217b0f6604929aea5f3df4ce05a74647780592f87a673fed8cf120087d9bcb519446c3e1beb1072ce947a
7
+ data.tar.gz: b4d4d3dd48427e67be2dd19e1c91757b0ed8e44ab9550af1804aa2f60ec6beef507ffa316e120867371df83efc5f887092d9023107533e0961c24290a752736d
@@ -1,39 +1,76 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'openhab/log/logger'
4
- require 'openhab/dsl/rules/rule'
5
- require 'openhab/dsl/timers'
6
4
 
7
5
  # OpenHAB main module
8
6
  module OpenHAB
9
7
  module Core
10
8
  #
11
- # Manages script loading and unloading
9
+ # Provide callback mechanisms for script handling
12
10
  #
13
11
  module ScriptHandling
12
+ module_function
13
+
14
+ # Add a callback to be run when the script has been fully loaded
15
+ def script_loaded(&block)
16
+ ScriptHandlingCallbacks.script_loaded_hooks << block
17
+ end
18
+
19
+ # Add a callback to be run when the script is unloaded
20
+ def script_unloaded(&block)
21
+ ScriptHandlingCallbacks.script_unloaded_hooks << block
22
+ end
23
+ end
24
+
25
+ #
26
+ # Manages script loading and unloading
27
+ #
28
+ module ScriptHandlingCallbacks
14
29
  include OpenHAB::Log
15
30
 
31
+ class << self
32
+ #
33
+ # Return script_loaded_hooks
34
+ #
35
+ # @!visibility private
36
+ def script_loaded_hooks
37
+ @script_loaded_hooks ||= []
38
+ end
39
+
40
+ #
41
+ # Return script_unloaded_hooks
42
+ #
43
+ # @!visibility private
44
+ def script_unloaded_hooks
45
+ @script_unloaded_hooks ||= []
46
+ end
47
+ end
48
+
16
49
  #
17
50
  # Executed when OpenHAB unloads a script file
18
51
  #
19
- # rubocop:disable Naming/MethodName
20
- # method name dictacted by OpenHAB
21
- def scriptUnloaded
52
+ # @!visibility private
53
+ def scriptUnloaded # rubocop:disable Naming/MethodName method name dictated by OpenHAB
22
54
  logger.trace('Script unloaded')
23
- OpenHAB::DSL::Rules.cleanup_rules
24
- OpenHAB::DSL::Timers.cancel_all
55
+ ScriptHandlingCallbacks.script_unloaded_hooks.each do |hook|
56
+ hook.call
57
+ rescue StandardError => e
58
+ logger.error("Failed to call script_unloaded hook #{hook}: #{e}")
59
+ end
25
60
  end
26
- # rubocop:enable Naming/MethodName
27
61
 
28
62
  #
29
63
  # Executed when OpenHAB loads a script file
30
64
  #
31
- # rubocop:disable Naming/MethodName
32
- # method name dictacted by OpenHAB
33
- def scriptLoaded(filename)
65
+ # @!visibility private
66
+ def scriptLoaded(filename) # rubocop:disable Naming/MethodName method name dictated by OpenHAB
34
67
  logger.trace("Script loaded: #{filename}")
68
+ ScriptHandlingCallbacks.script_loaded_hooks.each do |hook|
69
+ hook.call
70
+ rescue StandardError => e
71
+ logger.error("Failed to call script_loaded hook #{hook}: #{e}")
72
+ end
35
73
  end
36
- # rubocop:enable Naming/MethodName
37
74
  end
38
75
  end
39
76
  end
@@ -87,7 +87,7 @@ module OpenHAB
87
87
  # @return [Queue] <description>
88
88
  #
89
89
  def create_queue(inputs)
90
- case check_guards(event: inputs&.dig('event'))
90
+ case check_guards(event: extract_event(inputs))
91
91
  when true
92
92
  @run_queue.dup.grep_v(RuleConfig::Otherwise)
93
93
  when false
@@ -95,13 +95,30 @@ module OpenHAB
95
95
  end
96
96
  end
97
97
 
98
+ #
99
+ # Extract the event object from inputs
100
+ # and merge other inputs keys/values into the event
101
+ #
102
+ # @param [Map] inputs rule inputs
103
+ #
104
+ # @return [Object] event object
105
+ #
106
+ def extract_event(inputs)
107
+ event = inputs&.dig('event')
108
+ unless event
109
+ event = Struct.new(:event, :attachment, :command).new
110
+ event.command = inputs&.dig('command')
111
+ end
112
+ add_attachment(event, inputs)
113
+ end
114
+
98
115
  #
99
116
  # Get the trigger_id for the trigger that caused the rule creation
100
117
  #
101
118
  # @return [Hash] Input hash potentially containing trigger id
102
119
  #
103
120
  def trigger_id(inputs)
104
- inputs&.keys&.grep(/\.event$/)&.first&.chomp('.event')
121
+ inputs&.dig('module')
105
122
  end
106
123
 
107
124
  #
@@ -128,10 +145,6 @@ module OpenHAB
128
145
  attachment = @attachments[trigger_id(inputs)]
129
146
  return event unless attachment
130
147
 
131
- # Some events, like those from cron triggers don't have an event
132
- # so an event is created
133
- event ||= Struct.new(:event).new
134
-
135
148
  event.attachment = attachment
136
149
  event
137
150
  end
@@ -301,11 +314,9 @@ module OpenHAB
301
314
  # @param [Map] inputs OpenHAB map object describing rule trigge
302
315
  #
303
316
  #
304
- # rubocop:disable Metrics/MethodLength
305
317
  # No logical way to break this method up
306
318
  def process_queue(run_queue, mod, inputs)
307
- event = inputs&.dig('event')
308
- event = add_attachment(event, inputs)
319
+ event = extract_event(inputs)
309
320
 
310
321
  while (task = run_queue.shift)
311
322
  if task.is_a? RuleConfig::Delay
@@ -317,7 +328,6 @@ module OpenHAB
317
328
  rescue StandardError => e
318
329
  print_backtrace(e)
319
330
  end
320
- # rubocop:enable Metrics/MethodLength
321
331
 
322
332
  #
323
333
  # Dispatch execution block tasks to different methods
@@ -62,6 +62,7 @@ module OpenHAB
62
62
  def self.cleanup_rules
63
63
  @script_rules.each(&:cleanup)
64
64
  end
65
+ Core::ScriptHandling.script_unloaded { cleanup_rules }
65
66
 
66
67
  private
67
68
 
@@ -8,6 +8,7 @@ require 'openhab/dsl/rules/triggers/changed'
8
8
  require 'openhab/dsl/rules/triggers/channel'
9
9
  require 'openhab/dsl/rules/triggers/command'
10
10
  require 'openhab/dsl/rules/triggers/updated'
11
+ require 'openhab/dsl/rules/triggers/generic'
11
12
  require 'openhab/dsl/rules/guard'
12
13
  require 'openhab/core/entity_lookup'
13
14
  require 'openhab/dsl/between'
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openhab/log/logger'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module Rules
8
+ #
9
+ # Module holds rule triggers
10
+ #
11
+ module Triggers
12
+ include OpenHAB::Log
13
+
14
+ #
15
+ # Create a generic trigger given the trigger type uid and a configuration hash
16
+ #
17
+ # @param [Type] Trigger type UID
18
+ # @param [Configuration] A hash containing the trigger configuration entries
19
+ #
20
+ # @return [Trigger] Trigger object
21
+ #
22
+ def trigger(type, attach: nil, **configuration)
23
+ logger.trace("Creating a generic trigger for type(#{type}) with configuration(#{configuration})")
24
+ append_trigger(type, configuration, attach: attach)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -48,6 +48,7 @@ module OpenHAB
48
48
  #
49
49
  def append_trigger(type, config, attach: nil)
50
50
  logger.trace("Creating trigger of type #{type} for #{config}")
51
+ config.transform_keys!(&:to_s)
51
52
  trigger = Trigger.trigger(type: type, config: config)
52
53
  @attachments[trigger.id] = attach if attach
53
54
  @triggers << trigger
@@ -24,8 +24,6 @@ module OpenHAB
24
24
  include OpenHAB::Log
25
25
  extend Forwardable
26
26
 
27
- def_delegator :@timer, :is_active, :active?
28
- def_delegator :@timer, :is_running, :running?
29
27
  def_delegator :@timer, :has_terminated, :terminated?
30
28
 
31
29
  #
@@ -47,6 +47,7 @@ module OpenHAB
47
47
  def self.cancel_all
48
48
  @timer_manager.cancel_all
49
49
  end
50
+ Core::ScriptHandling.script_unloaded { cancel_all }
50
51
 
51
52
  # Create or reschedule a reentrant time
52
53
  #
@@ -30,11 +30,7 @@ module OpenHAB
30
30
  #
31
31
  # @return [QuantityType] This quantity converted to another unit
32
32
  #
33
- def |(other)
34
- other = org.openhab.core.types.util.UnitUtils.parse_unit(other) if other.is_a?(String)
35
-
36
- to_unit(other)
37
- end
33
+ alias | to_unit
38
34
 
39
35
  #
40
36
  # Comparison
@@ -27,7 +27,7 @@ module OpenHAB
27
27
  #
28
28
  def coerce(other)
29
29
  logger.trace("Coercing #{self} (#{self.class}) as a request from #{other.class}")
30
- return [other.as(self.class), self] if other.is_a?(Type)
30
+ return [other.as(self.class), self] if other.is_a?(Type) && other.respond_to?(:as)
31
31
  end
32
32
 
33
33
  #
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.13.5'
8
+ VERSION = '4.15.0'
9
9
  end
data/lib/openhab.rb CHANGED
@@ -20,20 +20,23 @@ module OpenHAB
20
20
  #
21
21
  #
22
22
  # Number of extensions and includes requires more lines
23
+ # rubocop: disable Metrics/MethodLength
23
24
  def self.extended(base)
24
25
  OpenHAB::Core.wait_till_openhab_ready
25
26
  base.extend Log
27
+ base.extend OpenHAB::Core::ScriptHandling
26
28
  base.extend OpenHAB::Core::EntityLookup
27
29
  base.extend OpenHAB::DSL
28
30
  base.extend OpenHAB::DSL::Between
29
31
 
32
+ base.send :include, OpenHAB::Core::ScriptHandlingCallbacks
30
33
  base.send :include, OpenHAB::DSL::Items
31
34
  base.send :include, OpenHAB::DSL::Types
32
- base.send :include, OpenHAB::Core::ScriptHandling
33
35
  logger.info "OpenHAB JRuby Scripting Library Version #{OpenHAB::VERSION} Loaded"
34
36
 
35
37
  OpenHAB::Core.add_rubylib_to_load_path
36
38
  end
39
+ # rubocop: enable Metrics/MethodLength
37
40
  end
38
41
 
39
42
  # Extend caller with OpenHAB methods
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.13.5
4
+ version: 4.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-14 00:00:00.000000000 Z
11
+ date: 2021-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,6 +103,7 @@ files:
103
103
  - lib/openhab/dsl/rules/triggers/channel.rb
104
104
  - lib/openhab/dsl/rules/triggers/command.rb
105
105
  - lib/openhab/dsl/rules/triggers/cron.rb
106
+ - lib/openhab/dsl/rules/triggers/generic.rb
106
107
  - lib/openhab/dsl/rules/triggers/trigger.rb
107
108
  - lib/openhab/dsl/rules/triggers/updated.rb
108
109
  - lib/openhab/dsl/states.rb