openhab-scripting 2.11.0 → 2.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 506ce31e641f76fe22663d36003bdbfcec17d01042c882bf240e85d18f622525
4
- data.tar.gz: d4b390223605105da4308a6b11a6124f9e953cd916d6bfdb7cd4788da01ef008
3
+ metadata.gz: 376e7ceca67ad5ca1dfedcacee9096e97819f3a382404ddde4f7beb579416e7d
4
+ data.tar.gz: bdffa11dab63e1910d38a1232c38e2b9d64deaac1d8ea3610de464792d75173e
5
5
  SHA512:
6
- metadata.gz: 987a35a048c611e33e990dc9b3c9d83fd592ba844ee90781c976d2c66b43bdb10837c2c3652c4140b5acc7af6214315a7015f3b64c088943ecaa2a82e65a6477
7
- data.tar.gz: f70fbff98656f3ad15a45ebd2a693462a43dce48515724818f5421bfaf91c194aab9db0c2011c7f1379b38fd6c315fb1ba329664c53cca330dd41970a4831f81
6
+ metadata.gz: 712aa971a831d8b700ecbe23b429a083518deb3bf7c2b68aa91cc0f3e5253ac9a32087025994705b94d268db58ef73e27767dcb3ce182e4477bfa115c6c476e0
7
+ data.tar.gz: e450d32857df9dd5aea47cc5e0baa7043e06901282324d2dfd4623cb2eac03c8fb60335e3de2e680911f1d4ad24095c2533fd3a9d5c90501694f849019e2d225
@@ -22,7 +22,17 @@ module OpenHAB
22
22
  item = $ir.getItem(name)
23
23
  # rubocop: enable Style/GlobalVars
24
24
  (item.is_a? GroupItem) ? nil : item
25
+ rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
26
+ nil
25
27
  end
28
+
29
+ # Returns true if the given item name exists
30
+ # @param name [String] Item name to check
31
+ # @return [Boolean] true if the item exists, false otherwise
32
+ def include?(name)
33
+ !$ir.getItems(name).empty?
34
+ end
35
+ alias key? include?
26
36
  end
27
37
 
28
38
  java_import org.openhab.core.items.GroupItem
@@ -14,6 +14,7 @@ Dimmer = DimmerItem
14
14
  #
15
15
  # rubocop:disable Style/ClassAndModuleChildren
16
16
  class Java::OrgOpenhabCoreLibraryItems::DimmerItem
17
+ include Comparable
17
18
  # rubocop:enable Style/ClassAndModuleChildren
18
19
  java_import org.openhab.core.library.types.DecimalType
19
20
  java_import org.openhab.core.library.types.IncreaseDecreaseType
@@ -85,6 +86,38 @@ class Java::OrgOpenhabCoreLibraryItems::DimmerItem
85
86
  target
86
87
  end
87
88
 
89
+ #
90
+ # Compare DimmerItem to supplied object
91
+ #
92
+ # @param [Object] other object to compare to
93
+ #
94
+ # @return [Integer] -1,0,1 or nil depending on value supplied, nil comparison to supplied object is not possible.
95
+ #
96
+ def <=>(other)
97
+ logger.trace("Comparing #{self} to #{other}")
98
+ case other
99
+ when DimmerItem, NumberItem
100
+ state <=> other.state
101
+ when DecimalType
102
+ state <=> other
103
+ else
104
+ to_i <=> other.to_i
105
+ end
106
+ end
107
+
108
+ #
109
+ # Compare DimmerItem to supplied object.
110
+ # The == operator needs to be overridden because the parent java object
111
+ # has .equals which overrides the <=> operator above
112
+ #
113
+ # @param [Object] other object to compare to
114
+ #
115
+ # @return [Integer] true if the two objects contain the same value, false otherwise
116
+ #
117
+ def ==(other)
118
+ (self <=> other).zero?
119
+ end
120
+
88
121
  #
89
122
  # Check if dimmer has a state and state is not zero
90
123
  #
@@ -105,6 +138,15 @@ class Java::OrgOpenhabCoreLibraryItems::DimmerItem
105
138
 
106
139
  alias to_int to_i
107
140
 
141
+ #
142
+ # Return the string representation of the dimmer item
143
+ #
144
+ # @return [String] String version of the dimmer value
145
+ #
146
+ def to_s
147
+ to_i.to_s
148
+ end
149
+
108
150
  #
109
151
  # Check if dimmer is on
110
152
  #
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'core/duration'
4
-
5
3
  module OpenHAB
6
4
  module Core
7
5
  module DSL
@@ -14,44 +12,18 @@ module OpenHAB
14
12
  include OpenHAB::Core
15
13
 
16
14
  #
17
- # Create Duration with unit of seconds
18
- #
19
- # @return [OpenHAB::Core::Duration] Duration with number of seconds from self
20
- #
21
- def seconds
22
- Duration.new(temporal_unit: :SECONDS, amount: self)
23
- end
24
-
25
- #
26
- # Create Duration with unit of milliseconds
27
- #
28
- # @return [OpenHAB::Core::Duration] Duration with number of milliseconds from self
29
- #
30
- def milliseconds
31
- Duration.new(temporal_unit: :MILLISECONDS, amount: self)
32
- end
33
-
34
- #
35
- # Create Duration with unit of minutes
36
- #
37
- # @return [OpenHAB::Core::Duration] Duration with number of minutes from self
38
- #
39
- def minutes
40
- Duration.new(temporal_unit: :MINUTES, amount: self)
41
- end
42
-
43
- #
44
- # Create Duration with unit of hours
15
+ # Create Duration with the specified unit
45
16
  #
46
- # @return [OpenHAB::Core::Duration] Duration with number of hours from self
17
+ # @return [Java::JavaTime::Duration] Duration with number of units from self
47
18
  #
48
- def hours
49
- Duration.new(temporal_unit: :HOURS, amount: self)
19
+ %w[millis seconds minutes hours].each do |unit|
20
+ define_method(unit) { Java::JavaTime::Duration.public_send("of_#{unit}", self) }
50
21
  end
51
22
 
52
23
  alias second seconds
53
- alias millisecond milliseconds
54
- alias ms milliseconds
24
+ alias millisecond millis
25
+ alias milliseconds millis
26
+ alias ms millis
55
27
  alias minute minutes
56
28
  alias hour hours
57
29
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'java'
4
- require 'core/duration'
5
4
  require 'core/dsl/time_of_day'
6
5
  require 'core/cron'
7
6
 
@@ -64,8 +63,10 @@ module OpenHAB
64
63
  expression_map = EXPRESSION_MAP[value]
65
64
  expression_map = at_condition(expression_map, at) if at
66
65
  cron(map_to_cron(expression_map))
67
- when Duration
68
- cron(map_to_cron(value.cron_map))
66
+ when Java::JavaTime::Duration
67
+ raise ArgumentError, '"at" cannot be used with duration' if at
68
+
69
+ cron(map_to_cron(duration_to_map(value)))
69
70
  else
70
71
  raise ArgumentExpression, 'Unknown interval' unless expression_map
71
72
  end
@@ -93,6 +94,28 @@ module OpenHAB
93
94
  %i[second minute hour dom month dow].map { |field| map.fetch(field) }.join(' ')
94
95
  end
95
96
 
97
+ #
98
+ # Convert a Java Duration to a map for the map_to_cron method
99
+ #
100
+ # @param duration [Java::JavaTime::Duration] The duration object
101
+ #
102
+ # @return [Hash] a map suitable for map_to_cron
103
+ #
104
+ def duration_to_map(duration)
105
+ if duration.to_millis_part.zero? && duration.to_nanos_part.zero? && duration.to_days.zero?
106
+ %i[second minute hour].each do |unit|
107
+ to_unit_part = duration.public_send("to_#{unit}s_part")
108
+ next unless to_unit_part.positive?
109
+
110
+ to_unit = duration.public_send("to_#{unit}s")
111
+ break unless to_unit_part == to_unit
112
+
113
+ return EXPRESSION_MAP[unit].merge(unit => "*/#{to_unit}")
114
+ end
115
+ end
116
+ raise ArgumentError, "Cron Expression not supported for duration: #{duration}"
117
+ end
118
+
96
119
  #
97
120
  # If an at time is provided, parse that and merge the new fields into the expression.
98
121
  #
@@ -35,9 +35,6 @@ module OpenHAB
35
35
  # @return [Array] Array of current TriggerDelay objects
36
36
  #
37
37
  def changed_wait(item, duration:, to: nil, from: nil)
38
- # Convert to testing the group if group specified rather than item
39
- item = item.group if item.is_a? Group
40
-
41
38
  # If GroupItems specified, use the group state trigger instead
42
39
  if item.is_a? GroupItems
43
40
  config = { 'groupName' => item.group.name }
@@ -128,7 +125,6 @@ module OpenHAB
128
125
  #
129
126
  def changed(*items, to: nil, from: nil, for: nil)
130
127
  items.flatten.each do |item|
131
- item = item.group if item.is_a? Group
132
128
  logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
133
129
  # for is a reserved word in ruby, so use local_variable_get :for
134
130
  if (wait_duration = binding.local_variable_get(:for))
@@ -113,6 +113,19 @@ module OpenHAB
113
113
  def my(&block)
114
114
  @caller.instance_eval(&block)
115
115
  end
116
+
117
+ #
118
+ # Create a logger where name includes rule name if name is set
119
+ #
120
+ # @return [Logging::Logger] Logger with name that appended with rule name if rule name is set
121
+ #
122
+ def logger
123
+ if name
124
+ Logging.logger(name.chomp.gsub(/\s+/, '_'))
125
+ else
126
+ super
127
+ end
128
+ end
116
129
  end
117
130
 
118
131
  #
@@ -274,7 +287,7 @@ module OpenHAB
274
287
  process_trigger_delay(mod, inputs)
275
288
  else
276
289
  logger.trace("Item changed to #{state} for #{trigger_delay}, rescheduling timer.")
277
- trigger_delay.timer.reschedule(ZonedDateTime.now.plus(Java::JavaTime::Duration.ofMillis(duration.to_ms)))
290
+ trigger_delay.timer.reschedule(ZonedDateTime.now.plus(duration))
278
291
  end
279
292
  end
280
293
  else
@@ -303,6 +316,23 @@ module OpenHAB
303
316
  false
304
317
  end
305
318
 
319
+ #
320
+ # Patch event to include event.item when it doesn't exist
321
+ # This is to patch a bug see https://github.com/boc-tothefuture/openhab-jruby/issues/75
322
+ # It may be fixed in the openhab core in the future, in which case, this patch will no longer be necessary
323
+ #
324
+ # @param [OpenHAB Event] event to check for item accessor
325
+ # @param [OpenHAB Event Inputs] inputs inputs to running rule
326
+ #
327
+ def add_event_item(event, inputs)
328
+ return if event.nil? || defined?(event.item)
329
+
330
+ class << event
331
+ attr_accessor :item
332
+ end
333
+ event.item = inputs&.dig('triggeringItem')
334
+ end
335
+
306
336
  #
307
337
  # Process the run queue
308
338
  #
@@ -317,7 +347,7 @@ module OpenHAB
317
347
  when RuleConfig::Run
318
348
 
319
349
  event = inputs&.dig('event')
320
-
350
+ add_event_item(event, inputs)
321
351
  logger.trace { "Executing rule '#{name}' run block with event(#{event})" }
322
352
  task.block.call(event)
323
353
  when RuleConfig::Trigger
@@ -4,8 +4,6 @@ require 'java'
4
4
  require 'delegate'
5
5
  require 'forwardable'
6
6
 
7
- require 'core/duration'
8
-
9
7
  module OpenHAB
10
8
  module Core
11
9
  module DSL
@@ -49,7 +47,7 @@ module OpenHAB
49
47
 
50
48
  semaphore.synchronize do
51
49
  @timer = ScriptExecution.createTimer(
52
- ZonedDateTime.now.plus(Java::JavaTime::Duration.ofMillis(@duration.to_ms)), @block
50
+ ZonedDateTime.now.plus(@duration), @block
53
51
  )
54
52
  super(@timer)
55
53
  end
@@ -64,7 +62,7 @@ module OpenHAB
64
62
  #
65
63
  def reschedule(duration = nil)
66
64
  duration ||= @duration
67
- @timer.reschedule(ZonedDateTime.now.plus(Java::JavaTime::Duration.ofMillis(duration.to_ms)))
65
+ @timer.reschedule(ZonedDateTime.now.plus(duration))
68
66
  end
69
67
  end
70
68
 
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.11.0'
8
+ VERSION = '2.14.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: 2.11.0
4
+ version: 2.14.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-02-01 00:00:00.000000000 Z
11
+ date: 2021-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,7 +76,6 @@ files:
76
76
  - lib/openhab/core/dsl/timers.rb
77
77
  - lib/openhab/core/dsl/types/quantity.rb
78
78
  - lib/openhab/core/dsl/units.rb
79
- - lib/openhab/core/duration.rb
80
79
  - lib/openhab/core/log.rb
81
80
  - lib/openhab/core/patch_load_path.rb
82
81
  - lib/openhab/core/startup_delay.rb
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'openhab/core/cron'
4
-
5
- module OpenHAB
6
- module Core
7
- #
8
- # This class represents a duration of time
9
- #
10
- class Duration
11
- include OpenHAB::Core::Cron
12
-
13
- # @return [Array] of supported temperal units (milliseconds, seconds, minutes and hours)
14
- TEMPORAL_UNITS = %i[MILLISECONDS SECONDS MINUTES HOURS].freeze
15
-
16
- #
17
- # Create a new Duration object
18
- #
19
- # @param [Symbol] temporal_unit Unit for duration
20
- # @param [Integer] amount of that unit
21
- #
22
- def initialize(temporal_unit:, amount:)
23
- unless TEMPORAL_UNITS.include? temporal_unit
24
- raise ArgumentError,
25
- "Unexpected Temporal Unit: #{temporal_unit}"
26
- end
27
-
28
- @temporal_unit = temporal_unit
29
- @amount = amount
30
- end
31
-
32
- #
33
- # Return a map
34
- #
35
- # @return [Map] Map with fields representing this duration @see OpenHAB::Core::Cron
36
- #
37
- def cron_map
38
- case @temporal_unit
39
- when :SECONDS
40
- cron_expression_map.merge(second: "*/#{@amount}")
41
- when :MINUTES
42
- cron_expression_map.merge(minute: "*/#{@amount}")
43
- when :HOURS
44
- cron_expression_map.merge(hour: "*/#{@amount}")
45
- else
46
- raise ArgumentError, "Cron Expression not supported for temporal unit: #{temporal_unit}"
47
- end
48
- end
49
-
50
- #
51
- # Convert the duration to milliseconds
52
- #
53
- # @return [Integer] Duration in milliseconds
54
- #
55
- def to_ms
56
- case @temporal_unit
57
- when :MILLISECONDS
58
- @amount
59
- when :SECONDS
60
- @amount * 1000
61
- when :MINUTES
62
- @amount * 1000 * 60
63
- when :HOURS
64
- @amount * 1000 * 60 * 60
65
- end
66
- end
67
-
68
- #
69
- # Return a string representation of the duration
70
- #
71
- # @return [String] Duration in string
72
- #
73
- def to_s
74
- "#{@amount} #{(@amount == 1) ? @temporal_unit.to_s.downcase.delete_suffix('s') : @temporal_unit.to_s.downcase}"
75
- end
76
- end
77
- end
78
- end