openhab-scripting 4.43.3 → 4.44.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: 70ffac6f2b96c4cd2c758abaf5ca9b7ebedd83e7a1a520cd4fa971a4104ffc0a
4
- data.tar.gz: a1738c2713e5b7d444e785be6e2d990fc58a548bb0487e9677aeafb6cfb61f98
3
+ metadata.gz: 5fc4da63132db46b324f609f104065c23d2ca43f9caa44e1736fcaa5aa0cefc1
4
+ data.tar.gz: db4f92843a5c3cba9a11a83c37a832eadd115bdcc75269d2b30800db61bff84b
5
5
  SHA512:
6
- metadata.gz: ce3b43639725c24ad4109850f7bdea1e395c2e94dc63ff924458ad5788bc9453431262af1e779f9f1ab61cfd2771dcc66c24f0e739e271141f667b00b3a1b8e7
7
- data.tar.gz: 856d1f63ea019d7e07791f7c0bcc588659d53295472edb3c7fa04a655ded3baf78027054b9381fa1d7cbc8aa17dc31862c6f67399be1d92a134f8c37a3160899
6
+ metadata.gz: 62976743367c2cda9c3d88f3fed7701feae81a852226dc93b2c9a41702035ba453e98a21ae3365030a467cc9bd899834eb63d144be2d589fba2716188b7cb2a0
7
+ data.tar.gz: 8f88d0b217d6bcfa7029f601acb551eaf030f63452a99cafba05be514b11ec8b000efbdd9dc3dbc8fed2c98c4e9e19e2e617cdb86749353173679a6ed2118073
@@ -73,7 +73,7 @@ module OpenHAB
73
73
  return if name.count('_') < 3
74
74
 
75
75
  name = name.tr('_', ':')
76
- $things.get(Java::OrgOpenhabCoreThing::ThingUID.new(name))
76
+ $things.get(org.openhab.core.thing.ThingUID.new(name))
77
77
  end
78
78
 
79
79
  #
@@ -35,7 +35,7 @@ module OpenHAB
35
35
  #
36
36
  # Gets the list of action objects associated with a specific ThingUID
37
37
  #
38
- # @param [Java::org::openhab::core::thing::ThingUID] thing_uid to get associated actions for
38
+ # @param [org.openhab.core.thing.ThingUID] thing_uid to get associated actions for
39
39
  #
40
40
  # @return [Array] of action objects associated with thing_uid, may be empty
41
41
  #
@@ -29,7 +29,7 @@ module OpenHAB
29
29
  #
30
30
  # @return [Array]
31
31
  def to_a
32
- group.get_members.to_a
32
+ group.get_members.map { |i| ::OpenHAB::Core::ItemProxy.new(i) }
33
33
  end
34
34
 
35
35
  # Name of the group
@@ -14,8 +14,8 @@ module OpenHAB
14
14
  module Metadata
15
15
  include OpenHAB::Log
16
16
 
17
- java_import Java::OrgOpenhabCoreItems::Metadata
18
- java_import Java::OrgOpenhabCoreItems::MetadataKey
17
+ java_import org.openhab.core.items.Metadata
18
+ java_import org.openhab.core.items.MetadataKey
19
19
 
20
20
  #
21
21
  # Provide the interface to access namespace's value and configuration
@@ -43,7 +43,7 @@ module OpenHAB
43
43
  #
44
44
  # Delete the configuration with the given key
45
45
  #
46
- # @return [Java::Org::openhab::core::items::Metadata] the old metadata
46
+ # @return [org.openhab.core.items.Metadata] the old metadata
47
47
  #
48
48
  def delete(key)
49
49
  configuration = {}.merge(@metadata&.configuration || {})
@@ -55,7 +55,7 @@ module OpenHAB
55
55
  #
56
56
  # Set the metadata value
57
57
  #
58
- # @return [Java::Org::openhab::core::items::Metadata] the old metadata
58
+ # @return [org.openhab.core.items.Metadata] the old metadata
59
59
  #
60
60
  def value=(value)
61
61
  metadata = Metadata.new(@metadata&.uID, value&.to_s, @metadata&.configuration)
@@ -65,7 +65,7 @@ module OpenHAB
65
65
  #
66
66
  # Set the entire configuration to a hash
67
67
  #
68
- # @return [Java::Org::openhab::core::items::Metadata] the old metadata
68
+ # @return [org.openhab.core.items.Metadata] the old metadata
69
69
  #
70
70
  def config=(config)
71
71
  config = config.to_hash if config.respond_to?(:to_hash)
@@ -114,8 +114,8 @@ module OpenHAB
114
114
  # Convert the given object to Ruby equivalent
115
115
  def self.to_ruby(value)
116
116
  case value
117
- when Hash, Java::JavaUtil::Map then to_ruby_hash(value)
118
- when Array, Java::JavaUtil::List then to_ruby_array(value)
117
+ when Hash, java.util.Map then to_ruby_hash(value)
118
+ when Array, java.util.List then to_ruby_array(value)
119
119
  when String then String.new(value)
120
120
  else value
121
121
  end
@@ -147,7 +147,7 @@ module OpenHAB
147
147
  # Set the metadata namespace. If the namespace does not exist, it will be created
148
148
  #
149
149
  # @param value [Object] The assigned value can be a OpenHAB::DSL::Items::MetadataItem,
150
- # Java::Org::openhab::core::items::Metadata, Array[2] of [value, configuration],
150
+ # org.openhab.core.items.Metadata, Array[2] of [value, configuration],
151
151
  # A String to set the value and clear the configuration,
152
152
  # or a Hash to set the configuration and set the value to nil
153
153
  #
@@ -246,7 +246,7 @@ module OpenHAB
246
246
  end
247
247
 
248
248
  #
249
- # @return [Java::org::openhab::core::items::MetadataRegistry]
249
+ # @return [org.openhab.core.items.MetadataRegistry]
250
250
  #
251
251
  def self.registry
252
252
  @registry ||= OpenHAB::Core::OSGI.service('org.openhab.core.items.MetadataRegistry')
@@ -10,7 +10,7 @@ module OpenHAB
10
10
  # When given a positive Duration, the timestamp will be calculated as ++now-Duration++
11
11
  #
12
12
  module Persistence
13
- java_import Java::OrgOpenhabCoreTypesUtil::UnitUtils
13
+ java_import org.openhab.core.types.util.UnitUtils
14
14
 
15
15
  # A state class with an added timestamp attribute. This is used to hold OpenHAB's HistoricItem.
16
16
  class HistoricState < SimpleDelegator
@@ -253,7 +253,7 @@ module OpenHAB
253
253
  # @return [ZonedDateTime]
254
254
  #
255
255
  def to_zdt(timestamp)
256
- timestamp = timestamp.negated if timestamp.is_a? Java::JavaTime::Duration
256
+ timestamp = timestamp.negated if timestamp.is_a? java.time.Duration
257
257
  OpenHAB::DSL.to_zdt(timestamp)
258
258
  end
259
259
 
@@ -9,8 +9,8 @@ module OpenHAB
9
9
  # Patches OpenHAB events
10
10
  #
11
11
  module Event
12
- java_import Java::OrgOpenhabCoreThingEvents::ThingStatusInfoChangedEvent
13
- java_import Java::OrgOpenhabCoreThingEvents::ThingStatusInfoEvent
12
+ java_import org.openhab.core.thing.events.ThingStatusInfoChangedEvent
13
+ java_import org.openhab.core.thing.events.ThingStatusInfoEvent
14
14
  #
15
15
  # Monkey patch with ruby style accessors
16
16
  #
@@ -11,7 +11,7 @@ module OpenHAB
11
11
  #
12
12
  # Create Duration with the specified unit
13
13
  #
14
- # @return [Java::JavaTime::Duration] Duration with number of units from self
14
+ # @return [java.time.Duration] Duration with number of units from self
15
15
  #
16
16
  %w[millis seconds minutes hours].each do |unit|
17
17
  define_method(unit) { java.time.Duration.public_send("of_#{unit}", self) }
@@ -32,7 +32,7 @@ module OpenHAB
32
32
  #
33
33
  # Create Duration with number of milliseconds
34
34
  #
35
- # @return [Java::JavaTime::Duration] Duration truncated to an integral number of milliseconds from self
35
+ # @return [java.time.Duration] Duration truncated to an integral number of milliseconds from self
36
36
  #
37
37
  def millis
38
38
  java.time.Duration.of_millis(to_i)
@@ -41,7 +41,7 @@ module OpenHAB
41
41
  #
42
42
  # Create Duration with number of seconds
43
43
  #
44
- # @return [Java::JavaTime::Duration] Duration with number of seconds from self
44
+ # @return [java.time.Duration] Duration with number of seconds from self
45
45
  #
46
46
  def seconds
47
47
  (self * 1000).millis
@@ -50,7 +50,7 @@ module OpenHAB
50
50
  #
51
51
  # Create Duration with number of minutes
52
52
  #
53
- # @return [Java::JavaTime::Duration] Duration with number of minutes from self
53
+ # @return [java.time.Duration] Duration with number of minutes from self
54
54
  #
55
55
  def minutes
56
56
  (self * 60).seconds
@@ -59,7 +59,7 @@ module OpenHAB
59
59
  #
60
60
  # Create Duration with number of hours
61
61
  #
62
- # @return [Java::JavaTime::Duration] Duration with number of hours from self
62
+ # @return [java.time.Duration] Duration with number of hours from self
63
63
  #
64
64
  def hours
65
65
  (self * 60).minutes
@@ -37,6 +37,7 @@ module OpenHAB
37
37
  super()
38
38
  set_name(config.name)
39
39
  set_description(config.description)
40
+ set_tags(to_string_set(config.tags))
40
41
  set_triggers(config.triggers)
41
42
  self.uid = config.uid
42
43
  @run_context = config.caller
@@ -263,6 +264,28 @@ module OpenHAB
263
264
  @run_context.instance_exec(event, &task.block)
264
265
  end
265
266
 
267
+ #
268
+ # Convert the given array to a set of strings.
269
+ # Convert Semantics classes to their simple name
270
+ #
271
+ # @example
272
+ # to_string_set("tag1", Semantics::LivingRoom)
273
+ #
274
+ # @param [Array] *tags An array of strings or Semantics classes
275
+ #
276
+ # @return [Set] A set of strings
277
+ #
278
+ def to_string_set(*tags)
279
+ tags = tags.flatten.map do |tag|
280
+ if tag.respond_to?(:java_class) && tag < org.openhab.core.semantics.Tag
281
+ tag.java_class.simple_name
282
+ else
283
+ tag.to_s
284
+ end
285
+ end
286
+ Set.new(tags)
287
+ end
288
+
266
289
  #
267
290
  # Create a new hash in which all elements are converted to strings
268
291
  #
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ module Rules
6
+ # Contains helper methods for inferring a rule name from its triggers
7
+ module NameInference
8
+ # Trigger Type UIDs that we know how to generate a name for
9
+ KNOWN_TRIGGER_TYPES = [
10
+ 'core.ChannelEventTrigger',
11
+ 'core.GroupCommandTrigger',
12
+ 'core.GroupStateChangeTrigger',
13
+ 'core.GroupStateUpdateTrigger',
14
+ 'core.ItemCommandTrigger',
15
+ 'core.ItemStateChangeTrigger',
16
+ 'core.ItemStateUpdateTrigger',
17
+ Triggers::Cron::CRON_TRIGGER_MODULE_ID
18
+ ].freeze
19
+ private_constant :KNOWN_TRIGGER_TYPES
20
+
21
+ class << self
22
+ # get the block's source location, and simplify to a simple filename
23
+ def infer_rule_id_from_block(block)
24
+ file = File.basename(block.source_location.first)
25
+ "#{file}:#{block.source_location.last}"
26
+ end
27
+
28
+ # formulate a readable rule name such as "TestSwitch received command ON" if possible
29
+ def infer_rule_name(config) # rubocop:disable Metrics
30
+ known_triggers, unknown_triggers = config.triggers.partition do |t|
31
+ KNOWN_TRIGGER_TYPES.include?(t.type_uid)
32
+ end
33
+ return nil unless unknown_triggers.empty?
34
+
35
+ cron_triggers = known_triggers.select { |t| t.type_uid == 'jsr223.jruby.CronTrigger' }
36
+ ruby_every_triggers = config.ruby_triggers.select { |t| t.first == :every }
37
+
38
+ # makes sure there aren't any true cron triggers cause we can't format them
39
+ return nil unless cron_triggers.length == ruby_every_triggers.length
40
+ return nil unless config.ruby_triggers.length == 1
41
+
42
+ infer_rule_name_from_trigger(*config.ruby_triggers.first)
43
+ end
44
+
45
+ private
46
+
47
+ # formulate a readable rule name from a single trigger if possible
48
+ def infer_rule_name_from_trigger(trigger, items, kwargs)
49
+ case trigger
50
+ when :every
51
+ infer_rule_name_from_every_trigger(items, **kwargs)
52
+ when :channel
53
+ infer_rule_name_from_channel_trigger(items, **kwargs)
54
+ when :changed, :updated, :received_command
55
+ infer_rule_name_from_item_trigger(trigger, items, kwargs)
56
+ end
57
+ end
58
+
59
+ # formulate a readable rule name from an item-type trigger
60
+ def infer_rule_name_from_item_trigger(trigger, items, kwargs) # rubocop:disable Metrics
61
+ kwargs.delete(:command) if kwargs[:command] == [nil]
62
+ return unless items.length <= 3 &&
63
+ (kwargs.keys - %i[from to command duration]).empty?
64
+ return if kwargs.values_at(:from, :to, :command).compact.any? do |v|
65
+ next false if v.is_a?(Array) && v.length <= 4 # arbitrary length
66
+ next false if v.is_a?(Range)
67
+
68
+ v.is_a?(Proc) || v.is_a?(Enumerable)
69
+ end
70
+
71
+ trigger_name = trigger.to_s.tr('_', ' ')
72
+ item_names = items.map do |item|
73
+ if item.is_a?(GroupItem::GroupMembers)
74
+ "#{item.group.name}.members"
75
+ else
76
+ item.name
77
+ end
78
+ end
79
+ name = "#{format_beginning_of_sentence_array(item_names)} #{trigger_name}"
80
+
81
+ name += " from #{format_inspected_array(kwargs[:from])}" if kwargs[:from]
82
+ name += " to #{format_inspected_array(kwargs[:to])}" if kwargs[:to]
83
+ name += " #{format_inspected_array(kwargs[:command])}" if kwargs[:command]
84
+ name += " for #{kwargs[:duration]}" if kwargs[:duration]
85
+ name.freeze
86
+ end
87
+
88
+ # formulate a readable rule name from an every-style cron trigger
89
+ def infer_rule_name_from_every_trigger(value, at:)
90
+ name = "Every #{value}"
91
+ name += " at #{at}" if at
92
+ name
93
+ end
94
+
95
+ # formulate a readable rule name from a channel trigger
96
+ def infer_rule_name_from_channel_trigger(channels, triggers:)
97
+ triggers = [] if triggers == [nil]
98
+ name = "#{format_beginning_of_sentence_array(channels)} triggered"
99
+ name += " #{format_inspected_array(triggers)}" unless triggers.empty?
100
+ name
101
+ end
102
+
103
+ # format an array of words that will be the beginning of a sentence
104
+ def format_beginning_of_sentence_array(array)
105
+ result = format_array(array)
106
+ if array.length > 2
107
+ result = result.dup
108
+ result[0] = 'A'
109
+ result.freeze
110
+ end
111
+ result
112
+ end
113
+
114
+ # format an array of items that need to be inspected individually
115
+ def format_inspected_array(array)
116
+ return array.inspect if array.is_a?(Range)
117
+
118
+ array = [array] unless array.is_a?(Array)
119
+ format_array(array.map(&:inspect))
120
+ end
121
+
122
+ # format an array of words in a friendly way
123
+ def format_array(array)
124
+ return array[0] if array.length == 1
125
+ return "#{array[0]} or #{array[1]}" if array.length == 2
126
+
127
+ "any of #{array.join(', ')}"
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -8,6 +8,7 @@ require 'openhab/log/logger'
8
8
  require_relative 'rule_config'
9
9
  require_relative 'automation_rule'
10
10
  require_relative 'guard'
11
+ require_relative 'name_inference'
11
12
 
12
13
  module OpenHAB
13
14
  #
@@ -34,32 +35,30 @@ module OpenHAB
34
35
 
35
36
  module_function
36
37
 
37
- # get the block's source location, and simplify to a simple filename
38
- def self.infer_rule_id_from_block(block)
39
- file = File.basename(block.source_location.first)
40
- "#{file}:#{block.source_location.last}"
41
- end
42
-
43
38
  #
44
39
  # Create a new rule
45
40
  #
46
- # @param [String] rule_name <description>
41
+ # @param [String] rule_name The rule name
47
42
  # @yield [] Block executed in context of a RuleConfig
48
43
  #
49
44
  #
50
45
  def rule(rule_name = nil, id: nil, script: nil, &block) # rubocop:disable Metrics
51
- id ||= Rule.infer_rule_id_from_block(block)
52
- rule_name ||= id
46
+ id ||= NameInference.infer_rule_id_from_block(block)
53
47
  script ||= block.source rescue nil # rubocop:disable Style/RescueModifier
54
48
 
55
49
  OpenHAB::Core::ThreadLocal.thread_local(RULE_NAME: rule_name) do
56
50
  @rule_name = rule_name
57
51
 
58
- config = RuleConfig.new(rule_name, block.binding)
52
+ config = RuleConfig.new(block.binding)
59
53
  config.uid(id)
60
54
  config.instance_exec(config, &block)
61
55
  config.guard = Guard::Guard.new(run_context: config.caller, only_if: config.only_if,
62
56
  not_if: config.not_if)
57
+
58
+ rule_name ||= NameInference.infer_rule_name(config)
59
+ rule_name ||= id
60
+
61
+ config.name(rule_name)
63
62
  logger.trace { config.inspect }
64
63
  process_rule_config(config, script)
65
64
  end
@@ -142,7 +141,7 @@ module OpenHAB
142
141
  #
143
142
  # Add a rule to the automation managed
144
143
  #
145
- # @param [Java::OrgOpenhabCoreAutomationModuleScriptRulesupportSharedSimple::SimpleRule] rule to add
144
+ # @param [org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule] rule to add
146
145
  #
147
146
  #
148
147
  def add_rule(rule)
@@ -36,6 +36,9 @@ module OpenHAB
36
36
  # @return [Object] object that invoked rule method
37
37
  attr_accessor :caller
38
38
 
39
+ # @return [Array] Of trigger definitions as passed in Ruby
40
+ attr_reader :ruby_triggers
41
+
39
42
  #
40
43
  # Struct holding a run block
41
44
  #
@@ -64,6 +67,7 @@ module OpenHAB
64
67
  prop :uid
65
68
  prop :name
66
69
  prop :description
70
+ prop :tags
67
71
  prop :enabled
68
72
  prop :between
69
73
 
@@ -73,12 +77,13 @@ module OpenHAB
73
77
  # @param [Object] caller_binding The object initializing this configuration.
74
78
  # Used to execute within the object's context
75
79
  #
76
- def initialize(rule_name, caller_binding)
80
+ def initialize(caller_binding)
77
81
  @rule_triggers = RuleTriggers.new
78
82
  @caller = caller_binding.eval 'self'
79
- name(rule_name)
83
+ @ruby_triggers = []
80
84
  enabled(true)
81
85
  on_start(false)
86
+ tags([])
82
87
  end
83
88
 
84
89
  #
@@ -7,10 +7,8 @@ module OpenHAB
7
7
  module TerseRule
8
8
  %i[changed channel cron every updated received_command].each do |trigger|
9
9
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
10
- def #{trigger}(*args, name: nil, **kwargs, &block) # def changed(*args, name: nil, **kwargs, &block)
11
- name ||= infer_rule_name(#{trigger.inspect}, args, kwargs) # name ||= infer_rule_name(:changed, args, kwargs)
12
- id = Rule.infer_rule_id_from_block(block) # id = Rule.infer_rule_id_from_block(block)
13
- name ||= id # name ||= id
10
+ def #{trigger}(*args, name: nil, id: nil, **kwargs, &block) # def changed(*args, name: nil, id: nil, **kwargs, &block)
11
+ id ||= NameInference.infer_rule_id_from_block(block) # id ||= NameInference.infer_rule_id_from_block(block)
14
12
  script = block.source rescue nil # script = block.source rescue nil
15
13
  rule name, id: id, script: script do # rule name, id: id, script: script do
16
14
  #{trigger}(*args, **kwargs) # changed(*args, **kwargs)
@@ -20,31 +18,6 @@ module OpenHAB
20
18
  module_function #{trigger.inspect} # module_function :changed
21
19
  RUBY
22
20
  end
23
-
24
- private
25
-
26
- # formulate a readable rule name such as "TestSwitch received command ON" if possible
27
- def infer_rule_name(trigger, args, kwargs) # rubocop:disable Metrics
28
- return unless %i[changed updated received_command].include?(trigger) &&
29
- args.length == 1 &&
30
- (kwargs.keys - %i[from to command]).empty?
31
- return if kwargs[:from].is_a?(Enumerable)
32
- return if kwargs[:to].is_a?(Enumerable)
33
- return if kwargs[:command].is_a?(Enumerable)
34
-
35
- trigger_name = trigger.to_s.tr('_', ' ')
36
- name = if args.first.is_a?(GroupItem::GroupMembers) # rubocop:disable Style/CaseLikeIf === doesn't work with GenericItem
37
- "#{args.first.group.name}.members #{trigger_name}"
38
- elsif args.first.is_a?(GenericItem)
39
- "#{args.first.name} #{trigger_name}"
40
- end
41
- return unless name
42
-
43
- name += " from #{kwargs[:from].inspect}" if kwargs[:from]
44
- name += " to #{kwargs[:to].inspect}" if kwargs[:to]
45
- name += " #{kwargs[:command].inspect}" if kwargs[:command]
46
- name
47
- end
48
21
  end
49
22
  end
50
23
  end
@@ -28,14 +28,16 @@ module OpenHAB
28
28
  #
29
29
  def changed(*items, to: nil, from: nil, for: nil, attach: nil)
30
30
  changed = Changed.new(rule_triggers: @rule_triggers)
31
- Changed.flatten_items(items).map do |item|
32
- logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
31
+ # for is a reserved word in ruby, so use local_variable_get :for
32
+ duration = binding.local_variable_get(:for)
33
33
 
34
- # for is a reserved word in ruby, so use local_variable_get :for
35
- wait_duration = binding.local_variable_get(:for)
34
+ flattened_items = Changed.flatten_items(items)
35
+ @ruby_triggers << [:changed, flattened_items, { to: to, from: from, duration: duration }]
36
+ flattened_items.map do |item|
37
+ logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
36
38
 
37
39
  Changed.each_state(from, to) do |from_state, to_state|
38
- changed.trigger(item: item, from: from_state, to: to_state, duration: wait_duration, attach: attach)
40
+ changed.trigger(item: item, from: from_state, to: to_state, duration: duration, attach: attach)
39
41
  end
40
42
  end.flatten
41
43
  end
@@ -26,8 +26,11 @@ module OpenHAB
26
26
  #
27
27
  def channel(*channels, thing: nil, triggered: nil, attach: nil)
28
28
  channel_trigger = Channel.new(rule_triggers: @rule_triggers)
29
- Channel.channels(channels: channels, thing: thing).each do |channel|
30
- [triggered].flatten.each do |trigger|
29
+ flattened_channels = Channel.channels(channels: channels, thing: thing)
30
+ triggers = [triggered].flatten
31
+ @ruby_triggers << [:channel, flattened_channels, { triggers: triggers }]
32
+ flattened_channels.each do |channel|
33
+ triggers.each do |trigger|
31
34
  channel_trigger.trigger(channel: channel, trigger: trigger, attach: attach)
32
35
  end
33
36
  end
@@ -28,8 +28,10 @@ module OpenHAB
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 = Command.combine_commands(command: command, commands: commands)
31
+ flattened_items = Command.flatten_items(items)
32
+ @ruby_triggers << [:received_command, flattened_items, { command: combined_commands }]
31
33
 
32
- Command.flatten_items(items).map do |item|
34
+ flattened_items.map do |item|
33
35
  combined_commands.map do |cmd|
34
36
  logger.states 'Creating received command trigger', item: item, command: cmd
35
37
 
@@ -86,7 +86,7 @@ module OpenHAB
86
86
  #
87
87
  # Converts a ThingStatus object to a ruby Symbol
88
88
  #
89
- # @param [Java::OrgOpenhabCoreThing::ThingStatus] status A ThingStatus instance
89
+ # @param [org.openhab.core.thing.ThingStatus] status A ThingStatus instance
90
90
  #
91
91
  # @return [Symbol] A corresponding symbol, in lower case
92
92
  #
@@ -21,10 +21,12 @@ module OpenHAB
21
21
  def every(value, at: nil, attach: nil)
22
22
  return every(MonthDay.parse(value), at: at, attach: attach) if value.is_a? String
23
23
 
24
+ @ruby_triggers << [:every, value, { at: at }]
25
+
24
26
  cron_expression = case value
25
27
  when Symbol then Cron.from_symbol(value, at)
26
- when Java::JavaTime::Duration then Cron.from_duration(value, at)
27
- when Java::JavaTime::MonthDay then Cron.from_monthday(value, at)
28
+ when java.time.Duration then Cron.from_duration(value, at)
29
+ when java.time.MonthDay then Cron.from_monthday(value, at)
28
30
  else raise ArgumentError, 'Unknown interval'
29
31
  end
30
32
  cron(cron_expression, attach: attach)
@@ -110,7 +112,7 @@ module OpenHAB
110
112
  #
111
113
  # Create a cron map from a duration
112
114
  #
113
- # @param [Java::JavaTime::Duration] duration
115
+ # @param [java.time.Duration] duration
114
116
  # @param [Object] at TimeOfDay or String representing time of day
115
117
  #
116
118
  # @return [Hash] map describing cron expression
@@ -124,7 +126,7 @@ module OpenHAB
124
126
  #
125
127
  # Create a cron map from a MonthDay
126
128
  #
127
- # @param [Java::JavaTime::MonthDay] monthday a {MonthDay} object
129
+ # @param [java.time.MonthDay] monthday a {MonthDay} object
128
130
  # @param [Object] at TimeOfDay or String representing time of day
129
131
  #
130
132
  # @return [Hash] map describing cron expression
@@ -176,7 +178,7 @@ module OpenHAB
176
178
  #
177
179
  # Convert a Java Duration to a map for the map_to_cron method
178
180
  #
179
- # @param duration [Java::JavaTime::Duration] The duration object
181
+ # @param duration [java.time.Duration] The duration object
180
182
  #
181
183
  # @return [Hash] a map suitable for map_to_cron
182
184
  #
@@ -62,6 +62,7 @@ module OpenHAB
62
62
  def initialize(trigger)
63
63
  @trigger = trigger
64
64
  @scheduler = OpenHAB::Core::OSGI.service('org.openhab.core.scheduler.CronScheduler')
65
+ @schedule = nil
65
66
  @expression = trigger.configuration.get('cronExpression')
66
67
  super(trigger)
67
68
  end
@@ -73,7 +74,7 @@ module OpenHAB
73
74
  def setCallback(callback) # rubocop:disable Naming/MethodName
74
75
  synchronized do
75
76
  super(callback)
76
- @scheduler.schedule(self, @expression)
77
+ @schedule = @scheduler.schedule(self, @expression)
77
78
  logger.trace("Scheduled cron job '#{@expression}' for trigger '#{@trigger.id}'.")
78
79
  end
79
80
  end
@@ -102,7 +103,8 @@ module OpenHAB
102
103
  super
103
104
  return unless @schedule
104
105
 
105
- @schedule&.cancel(true)
106
+ @schedule.cancel(true)
107
+ @schedule = nil
106
108
  end
107
109
  logger.trace("cancelled job for trigger '#{@trigger.id}'.")
108
110
  end
@@ -23,7 +23,9 @@ module OpenHAB
23
23
  #
24
24
  def updated(*items, to: nil, attach: nil)
25
25
  updated = Updated.new(rule_triggers: @rule_triggers)
26
- Updated.flatten_items(items).map do |item|
26
+ flattened_items = Updated.flatten_items(items)
27
+ @ruby_triggers << [:updated, flattened_items, { to: to }]
28
+ flattened_items.map do |item|
27
29
  logger.trace("Creating updated trigger for item(#{item}) to(#{to})")
28
30
  [to].flatten.map do |to_state|
29
31
  updated.trigger(item: item, to: to_state, attach: attach)
@@ -70,9 +70,9 @@ module OpenHAB
70
70
  def delete(timer)
71
71
  logger.trace("Removing #{timer} from timers")
72
72
  @timers.delete(timer)
73
- if timer.respond_to? :id
74
- @timer_ids[timer.id]&.delete(timer)
75
- @timer_ids.delete(timer.id) unless @timer_ids[timer.id].any?
73
+ if timer.respond_to?(:id) && (timers = @timer_ids[timer.id])
74
+ timers.delete(timer)
75
+ @timer_ids.delete(timer.id) if timers.empty?
76
76
  end
77
77
  @reentrant_timers.delete(timer.reentrant_id) if timer.respond_to? :reentrant_id
78
78
  end
@@ -103,7 +103,7 @@ module OpenHAB
103
103
  return unless timestamp
104
104
 
105
105
  case timestamp
106
- when Java::JavaTimeTemporal::TemporalAmount then ZonedDateTime.now.plus(timestamp)
106
+ when java.time.temporal.TemporalAmount then ZonedDateTime.now.plus(timestamp)
107
107
  when ZonedDateTime then timestamp
108
108
  when Time then timestamp.to_java(ZonedDateTime)
109
109
  else
@@ -10,7 +10,7 @@ module OpenHAB
10
10
  DateTimeType = org.openhab.core.library.types.DateTimeType
11
11
 
12
12
  # global alias - required for jrubyscripting addon <= OH3.2.0
13
- ::DateTimeType = DateTimeType if ::DateTimeType.is_a?(Java::JavaLang::Class)
13
+ ::DateTimeType = DateTimeType if ::DateTimeType.is_a?(java.lang.Class)
14
14
 
15
15
  # @deprecated
16
16
  # Backwards-compatible alias
@@ -9,7 +9,7 @@ module OpenHAB
9
9
  HSBType = org.openhab.core.library.types.HSBType
10
10
 
11
11
  # global alias - required for jrubyscripting addon <= OH3.2.0
12
- ::HSBType = HSBType if ::HSBType.is_a?(Java::JavaLang::Class)
12
+ ::HSBType = HSBType if ::HSBType.is_a?(java.lang.Class)
13
13
 
14
14
  # Adds methods to core OpenHAB HSBType to make it more natural in Ruby
15
15
  class HSBType < PercentType
@@ -8,7 +8,7 @@ module OpenHAB
8
8
  PercentType = org.openhab.core.library.types.PercentType
9
9
 
10
10
  # global alias - required for jrubyscripting addon <= OH3.2.0
11
- ::PercentType = PercentType if ::PercentType.is_a?(Java::JavaLang::Class)
11
+ ::PercentType = PercentType if ::PercentType.is_a?(java.lang.Class)
12
12
 
13
13
  # Adds methods to core OpenHAB PercentType to make it more natural in Ruby
14
14
  class PercentType < DecimalType
@@ -7,7 +7,7 @@ module OpenHAB
7
7
 
8
8
  # global scope - required for jrubyscripting addon <= OH3.2.0
9
9
  # @!visibility private
10
- ::PointType = PointType if ::PointType.is_a?(Java::JavaLang::Class)
10
+ ::PointType = PointType if ::PointType.is_a?(java.lang.Class)
11
11
 
12
12
  # Adds methods to core OpenHAB PointType to make it more natural in Ruby
13
13
  class PointType
@@ -8,7 +8,7 @@ module OpenHAB
8
8
  QuantityType = org.openhab.core.library.types.QuantityType
9
9
 
10
10
  # global alias - required for jrubyscripting addon <= OH3.2.0
11
- ::QuantityType = QuantityType if ::QuantityType.is_a?(Java::JavaLang::Class)
11
+ ::QuantityType = QuantityType if ::QuantityType.is_a?(java.lang.Class)
12
12
 
13
13
  # @deprecated
14
14
  # Backwards-compatible alias
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.43.3'
8
+ VERSION = '4.44.2'
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.43.3
4
+ version: 4.44.2
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: 2022-07-25 00:00:00.000000000 Z
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,6 +116,7 @@ files:
116
116
  - lib/openhab/dsl/rules/automation_rule.rb
117
117
  - lib/openhab/dsl/rules/guard.rb
118
118
  - lib/openhab/dsl/rules/item_event.rb
119
+ - lib/openhab/dsl/rules/name_inference.rb
119
120
  - lib/openhab/dsl/rules/property.rb
120
121
  - lib/openhab/dsl/rules/rule.rb
121
122
  - lib/openhab/dsl/rules/rule_config.rb