openhab-scripting 4.8.3 → 4.10.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: 6f20bdd4334857cc9d65e95a218f509f90dd6f5b68d7acf550671f942385a426
4
- data.tar.gz: a0bcdfba1ab5328f14b52ca16d9e56555fef6f479626aab11f655229a0c4f81a
3
+ metadata.gz: 14f2ced679ac7bcaa331235fa8e86156b7c25d106628047f36d8bbd8c1ee2e0d
4
+ data.tar.gz: 164856fab952fec915c2e562b1ef7da5e545a560efb2001e286360d6b558da14
5
5
  SHA512:
6
- metadata.gz: dd22e0e28fce41231eb34a2e0ed4c567b2457584f40852f9af7d684de6d981094bb8da5771b616191827fb42817e32cda3796b97aba8730de38fb95a80bb51b3
7
- data.tar.gz: 00aeb56148a1c1117724515596a6ccdf8112246de26418b0ab0e978b75f2efc7766e545458e663a66f196d391c5eb9a281432d9aba6b5ef9b49250e1eae3702e
6
+ metadata.gz: 4d79581b46aa3606f63e79a7cc3a2ece846506f90c244a554df236bde8feb9f46067de014bbbff7639436716ec95b040949f632b60ecd86fc7580cc387039fe5
7
+ data.tar.gz: 2c3f012ee50964c35c6996253fb5001b1253c4cf155f5d677174ba16ff765e8f4982a90bd9907876883d37702d0b2b8ff6c76c37d362f05df4c21c82218456f5
@@ -11,7 +11,6 @@ module OpenHAB
11
11
  class OSGI
12
12
  include OpenHAB::Log
13
13
 
14
- java_import org.openhab.core.model.script.actions.ScriptExecution
15
14
  java_import org.osgi.framework.FrameworkUtil
16
15
 
17
16
  #
@@ -83,6 +83,7 @@ module OpenHAB
83
83
  def state?
84
84
  !raw_state.is_a?(Types::UnDefType)
85
85
  end
86
+ alias truthy? state?
86
87
 
87
88
  #
88
89
  # Get the item state
@@ -5,6 +5,8 @@ require 'set'
5
5
  require 'openhab/core/thread_local'
6
6
  require 'openhab/log/logger'
7
7
 
8
+ require_relative 'item_event'
9
+
8
10
  module OpenHAB
9
11
  module DSL
10
12
  #
@@ -29,6 +31,8 @@ module OpenHAB
29
31
  # @param [Config] config Rule configuration
30
32
  #
31
33
  # Constructor sets a number of variables, no further decomposition necessary
34
+ # rubocop:disable Metrics/MethodLength
35
+ # Metrics disabled because only setters are called or defaults set.
32
36
  def initialize(config:)
33
37
  super()
34
38
  set_name(config.name)
@@ -41,7 +45,9 @@ module OpenHAB
41
45
  @between = between || OpenHAB::DSL::TimeOfDay::ALL_DAY
42
46
  # Convert between to correct range or nil if not set
43
47
  @trigger_delays = config.trigger_delays
48
+ @attachments = config.attachments
44
49
  end
50
+ # rubocop:enable Metrics/MethodLength
45
51
 
46
52
  #
47
53
  # Execute the rule
@@ -51,7 +57,7 @@ module OpenHAB
51
57
  #
52
58
  #
53
59
  def execute(mod = nil, inputs = nil)
54
- logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs&.pretty_inspect}" }
60
+ logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs&.pretty_inspect})" }
55
61
  logger.trace { "Event details #{inputs['event'].pretty_inspect}" } if inputs&.key?('event')
56
62
  if trigger_delay inputs
57
63
  trigger_delay = trigger_delay(inputs)
@@ -88,6 +94,15 @@ module OpenHAB
88
94
  end
89
95
  end
90
96
 
97
+ #
98
+ # Get the trigger_id for the trigger that caused the rule creation
99
+ #
100
+ # @return [Hash] Input hash potentially containing trigger id
101
+ #
102
+ def trigger_id(inputs)
103
+ inputs&.keys&.grep(/\.event$/)&.first&.chomp('.event')
104
+ end
105
+
91
106
  #
92
107
  # Returns trigger delay from inputs if it exists
93
108
  #
@@ -100,7 +115,24 @@ module OpenHAB
100
115
  # ["72698819-83cb-498a-8e61-5aab8b812623.event", "oldState", "module", \
101
116
  # "72698819-83cb-498a-8e61-5aab8b812623.oldState", "event", "newState",\
102
117
  # "72698819-83cb-498a-8e61-5aab8b812623.newState"]
103
- @trigger_delays[inputs&.keys&.grep(/\.event$/)&.first&.chomp('.event')]
118
+ @trigger_delays[trigger_id(inputs)]
119
+ end
120
+
121
+ # If an attachment exists for the trigger for this event add it to the event object
122
+ # @param [Object] Event
123
+ # @param [Hash] Inputs into event
124
+ # @return [Object] Event with attachment added
125
+ #
126
+ def add_attachment(event, inputs)
127
+ attachment = @attachments[trigger_id(inputs)]
128
+ return event unless attachment
129
+
130
+ # Some events, like those from cron triggers don't have an event
131
+ # so an event is created
132
+ event ||= Struct.new(:event).new
133
+
134
+ event.attachment = attachment
135
+ event
104
136
  end
105
137
 
106
138
  #
@@ -268,8 +300,11 @@ module OpenHAB
268
300
  # @param [Map] inputs OpenHAB map object describing rule trigge
269
301
  #
270
302
  #
303
+ # rubocop:disable Metrics/MethodLength
304
+ # No logical way to break this method up
271
305
  def process_queue(run_queue, mod, inputs)
272
306
  event = inputs&.dig('event')
307
+ event = add_attachment(event, inputs)
273
308
 
274
309
  while (task = run_queue.shift)
275
310
  if task.is_a? RuleConfig::Delay
@@ -281,6 +316,7 @@ module OpenHAB
281
316
  rescue StandardError => e
282
317
  print_backtrace(e)
283
318
  end
319
+ # rubocop:enable Metrics/MethodLength
284
320
 
285
321
  #
286
322
  # Dispatch execution block tasks to different methods
@@ -13,13 +13,13 @@ module OpenHAB
13
13
  include OpenHAB::DSL::Rules::Property
14
14
 
15
15
  prop_array(:only_if) do |item|
16
- unless item.is_a?(Proc) || item.respond_to?(:truthy?)
16
+ unless item.is_a?(Proc) || [item].flatten.all? { |it| it.respond_to?(:truthy?) }
17
17
  raise ArgumentError, "Object passed to only_if must respond_to 'truthy?'"
18
18
  end
19
19
  end
20
20
 
21
21
  prop_array(:not_if) do |item|
22
- unless item.is_a?(Proc) || item.respond_to?(:truthy?)
22
+ unless item.is_a?(Proc) || [item].flatten.all? { |it| it.respond_to?(:truthy?) }
23
23
  raise ArgumentError, "Object passed to not_if must respond_to 'truthy?'"
24
24
  end
25
25
  end
@@ -113,7 +113,7 @@ module OpenHAB
113
113
  # @return [Boolean] True if criteria are satisfied, false otherwise
114
114
  #
115
115
  def process_not_if(event, items, procs)
116
- items.none?(&:truthy?) && procs.none? { |proc| proc.call(event) }
116
+ items.flatten.none?(&:truthy?) && procs.none? { |proc| proc.call(event) }
117
117
  end
118
118
 
119
119
  #
@@ -126,7 +126,7 @@ module OpenHAB
126
126
  # @return [Boolean] True if criteria are satisfied, false otherwise
127
127
  #
128
128
  def process_only_if(event, items, procs)
129
- items.all?(&:truthy?) && procs.all? { |proc| proc.call(event) }
129
+ items.flatten.all?(&:truthy?) && procs.all? { |proc| proc.call(event) }
130
130
  end
131
131
  end
132
132
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ module Rules
6
+ #
7
+ # Extends OpenHAB events
8
+ #
9
+ module Events
10
+ java_import org.openhab.core.events.AbstractEvent
11
+
12
+ # Add attachments to ItemEvent
13
+ class AbstractEvent
14
+ attr_accessor :attachment
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -87,7 +87,7 @@ module OpenHAB
87
87
  rule = AutomationRule.new(config: config)
88
88
  Rules.script_rules << rule
89
89
  add_rule(rule)
90
- rule.execute if config.on_start?
90
+ rule.execute(nil, { 'event' => Struct.new(:attachment).new(config.start_attachment) }) if config.on_start?
91
91
  end
92
92
 
93
93
  #
@@ -37,6 +37,9 @@ module OpenHAB
37
37
  # @return [Array] Of trigger delays
38
38
  attr_reader :trigger_delays
39
39
 
40
+ # @return [Hash] Hash of trigger UIDs to attachments
41
+ attr_reader :attachments
42
+
40
43
  # @return [Array] Of trigger guards
41
44
  attr_accessor :guard
42
45
 
@@ -82,6 +85,7 @@ module OpenHAB
82
85
  def initialize(rule_name, caller_binding)
83
86
  @triggers = []
84
87
  @trigger_delays = {}
88
+ @attachments = {}
85
89
  @caller = caller_binding.eval 'self'
86
90
  enabled(true)
87
91
  on_start(false)
@@ -96,8 +100,8 @@ module OpenHAB
96
100
  #
97
101
  # rubocop: disable Style/OptionalBooleanParameter
98
102
  # Disabled cop due to use in a DSL
99
- def on_start(run_on_start = true)
100
- @on_start = run_on_start
103
+ def on_start(run_on_start = true, attach: nil)
104
+ @on_start = Struct.new(:enabled, :attach).new(run_on_start, attach)
101
105
  end
102
106
  # rubocop: enable Style/OptionalBooleanParameter
103
107
 
@@ -107,7 +111,16 @@ module OpenHAB
107
111
  # @return [Boolean] True if rule should run on start, false otherwise.
108
112
  #
109
113
  def on_start?
110
- @on_start
114
+ @on_start.enabled
115
+ end
116
+
117
+ #
118
+ # Get the optional start attachment
119
+ #
120
+ # @return [Object] optional user provided attachment to the on_start method
121
+ #
122
+ def start_attachment
123
+ @on_start.attach
111
124
  end
112
125
 
113
126
  #
@@ -144,7 +157,8 @@ module OpenHAB
144
157
  "Run blocks: (#{run}) " \
145
158
  "on_start: (#{on_start?}) " \
146
159
  "Trigger Waits: #{trigger_delays} " \
147
- "Trigger UIDs: #{triggers.map(&:id).join(', ')}"
160
+ "Trigger UIDs: #{triggers.map(&:id).join(', ')}" \
161
+ "Attachments: #{attachments} "
148
162
  end
149
163
  end
150
164
  end
@@ -30,16 +30,16 @@ module OpenHAB
30
30
  #
31
31
  # @return [Trigger] OpenHAB trigger
32
32
  #
33
- def changed(*items, to: nil, from: nil, for: nil)
33
+ def changed(*items, to: nil, from: nil, for: nil, attach: nil)
34
34
  separate_groups(items).map do |item|
35
35
  logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
36
36
  # for is a reserved word in ruby, so use local_variable_get :for
37
37
  if (wait_duration = binding.local_variable_get(:for))
38
- changed_wait(item, to: to, from: from, duration: wait_duration)
38
+ changed_wait(item, to: to, from: from, duration: wait_duration, attach: attach)
39
39
  else
40
40
  # Place in array and flatten to support multiple to elements or single or nil
41
41
  [to].flatten.map do |to_state|
42
- [from].flatten.map { |from_state| create_changed_trigger(item, from_state, to_state) }
42
+ [from].flatten.map { |from_state| create_changed_trigger(item, from_state, to_state, attach) }
43
43
  end
44
44
  end
45
45
  end.flatten
@@ -57,8 +57,8 @@ module OpenHAB
57
57
  #
58
58
  # @return [Trigger] OpenHAB trigger
59
59
  #
60
- def changed_wait(item, duration:, to: nil, from: nil)
61
- trigger = create_changed_trigger(item, nil, nil)
60
+ def changed_wait(item, duration:, to: nil, from: nil, attach: nil)
61
+ trigger = create_changed_trigger(item, nil, nil, attach)
62
62
  logger.trace("Creating Changed Wait Change Trigger for #{item}")
63
63
  @trigger_delays[trigger.id] = TriggerDelay.new(to: to, from: from, duration: duration)
64
64
  trigger
@@ -72,14 +72,14 @@ module OpenHAB
72
72
  # @param [String] to state restrict trigger to
73
73
  #
74
74
  #
75
- def create_changed_trigger(item, from, to)
75
+ def create_changed_trigger(item, from, to, attach)
76
76
  trigger, config = case item
77
77
  when OpenHAB::DSL::Items::GroupItem::GroupMembers
78
78
  create_group_changed_trigger(item, from, to)
79
79
  when Thing then create_thing_changed_trigger(item, from, to)
80
80
  else create_item_changed_trigger(item, from, to)
81
81
  end
82
- append_trigger(trigger, config)
82
+ append_trigger(trigger, config, attach: attach)
83
83
  end
84
84
 
85
85
  #
@@ -21,12 +21,12 @@ module OpenHAB
21
21
  # @param [String] triggered specific triggering condition to match for trigger
22
22
  #
23
23
  #
24
- def channel(*channels, thing: nil, triggered: nil)
24
+ def channel(*channels, thing: nil, triggered: nil, attach: nil)
25
25
  channels.flatten.each do |channel|
26
26
  channel = [thing, channel].join(':') if thing
27
27
  logger.trace("Creating channel trigger for channel(#{channel}), thing(#{thing}), trigger(#{triggered})")
28
28
  [triggered].flatten.each do |trigger|
29
- create_channel_trigger(channel, trigger)
29
+ create_channel_trigger(channel, trigger, attach)
30
30
  end
31
31
  end
32
32
  end
@@ -40,12 +40,12 @@ module OpenHAB
40
40
  # @param [Trigger] trigger specific channel trigger to match
41
41
  #
42
42
  #
43
- def create_channel_trigger(channel, trigger)
43
+ def create_channel_trigger(channel, trigger, attach)
44
44
  config = { 'channelUID' => channel }
45
45
  config['event'] = trigger.to_s unless trigger.nil?
46
46
  config['channelUID'] = channel
47
47
  logger.trace("Creating Change Trigger for #{config}")
48
- @triggers << Trigger.trigger(type: Trigger::CHANNEL_EVENT, config: config)
48
+ append_trigger(Trigger::CHANNEL_EVENT, config, attach: attach)
49
49
  end
50
50
  end
51
51
  end
@@ -21,14 +21,14 @@ module OpenHAB
21
21
  # @param [Array] commands commands to match for trigger
22
22
  #
23
23
  #
24
- def received_command(*items, command: nil, commands: nil)
24
+ def received_command(*items, command: nil, commands: nil, attach: nil)
25
25
  separate_groups(items).map do |item|
26
26
  logger.trace("Creating received command trigger for item(#{item})"\
27
27
  "command(#{command}) commands(#{commands})")
28
28
 
29
29
  # Combine command and commands, doing union so only a single nil will be in the combined array.
30
30
  combined_commands = combine_commands(command, commands)
31
- create_received_trigger(combined_commands, item)
31
+ create_received_trigger(combined_commands, item, attach)
32
32
  end.flatten
33
33
  end
34
34
 
@@ -41,7 +41,7 @@ module OpenHAB
41
41
  # @param [Object] item to create trigger for
42
42
  #
43
43
  #
44
- def create_received_trigger(commands, item)
44
+ def create_received_trigger(commands, item, attach)
45
45
  commands.map do |command|
46
46
  if item.is_a? OpenHAB::DSL::Items::GroupItem::GroupMembers
47
47
  config, trigger = create_group_command_trigger(item)
@@ -49,7 +49,7 @@ module OpenHAB
49
49
  config, trigger = create_item_command_trigger(item)
50
50
  end
51
51
  config['command'] = command.to_s unless command.nil?
52
- append_trigger(trigger, config)
52
+ append_trigger(trigger, config, attach: attach)
53
53
  end
54
54
  end
55
55
 
@@ -10,9 +10,6 @@ module OpenHAB
10
10
  # Cron type rules
11
11
  #
12
12
  module Triggers
13
- java_import org.openhab.core.automation.util.TriggerBuilder
14
- java_import org.openhab.core.config.core.Configuration
15
-
16
13
  #
17
14
  # Returns a default map for cron expressions that fires every second
18
15
  # This map is usually updated via merge by other methods to refine cron type triggers.
@@ -46,9 +46,10 @@ module OpenHAB
46
46
  #
47
47
  # @return [Trigger] OpenHAB trigger
48
48
  #
49
- def append_trigger(type, config)
49
+ def append_trigger(type, config, attach: nil)
50
50
  logger.trace("Creating trigger of type #{type} for #{config}")
51
51
  trigger = Trigger.trigger(type: type, config: config)
52
+ @attachments[trigger.id] = attach if attach
52
53
  @triggers << trigger
53
54
  trigger
54
55
  end
@@ -19,12 +19,12 @@ module OpenHAB
19
19
  #
20
20
  # @return [Trigger] Trigger for updated entity
21
21
  #
22
- def updated(*items, to: nil)
22
+ def updated(*items, to: nil, attach: nil)
23
23
  separate_groups(items).map do |item|
24
24
  logger.trace("Creating updated trigger for item(#{item}) to(#{to})")
25
25
  [to].flatten.map do |to_state|
26
26
  trigger, config = create_update_trigger(item, to_state)
27
- append_trigger(trigger, config)
27
+ append_trigger(trigger, config, attach: attach)
28
28
  end
29
29
  end.flatten
30
30
  end
@@ -47,7 +47,7 @@ module OpenHAB
47
47
 
48
48
  private
49
49
 
50
- java_import 'org.openhab.core.automation.annotation.RuleAction'
50
+ java_import org.openhab.core.automation.annotation.RuleAction
51
51
 
52
52
  #
53
53
  # Define methods from actions mapped to this thing
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.8.3'
8
+ VERSION = '4.10.0'
9
9
  end
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.8.3
4
+ version: 4.10.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-10-31 00:00:00.000000000 Z
11
+ date: 2021-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -93,6 +93,7 @@ files:
93
93
  - lib/openhab/dsl/persistence.rb
94
94
  - lib/openhab/dsl/rules/automation_rule.rb
95
95
  - lib/openhab/dsl/rules/guard.rb
96
+ - lib/openhab/dsl/rules/item_event.rb
96
97
  - lib/openhab/dsl/rules/property.rb
97
98
  - lib/openhab/dsl/rules/rule.rb
98
99
  - lib/openhab/dsl/rules/rule_config.rb