openhab-scripting 2.20.3 → 2.23.1

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: 61465187d78f8e9a3d7ad5aeec2fab9ebd8875b1799397fa0d47ef2bfae50e6b
4
- data.tar.gz: c5d76638ae55a32c23d582dd637b0dd511d22e2dd20c23c8c920e1799f310379
3
+ metadata.gz: 1af0c9d15b41ac54e58540ccab079a6c084931abf80f5add687511d332d3afa2
4
+ data.tar.gz: 5e3c341ecf96ab4fb11b6ca6cefae01a27dc02b2498e9e3896ef4683baa3f6e5
5
5
  SHA512:
6
- metadata.gz: 4a568a92846ef850d66d04b197cf1124d567c177aa2054dac42b9171528c0f5ae8f7ace8f87197c854392c870dfbb3a639f6d764149ace7ed9ab24d2f20d0080
7
- data.tar.gz: 11a98624829df60763fc27d79f2e3f1de6766080fcd7c4b24d6a4eb5fb9bb442c6ba6345eb99d253083e95913eee8aa6f7b524d67b8ed3195d78960c5d55338b
6
+ metadata.gz: e0238451b9d78d45c225f98ede7b86580e26c7c3edfc7681f6942b7f5a7c4dbfb67f98c5c76ccf8a437cfa779bfa2eb9b0c8639b1c4a24206b99b8ca80a3803f
7
+ data.tar.gz: 616d30281aef1da66964cba5d1c6365b24939f4fbec3070e4251551e3c8c7552191835da12e4795d5698e9671add7739a411e53e7b347de65a8b4a9c0fb249a0
@@ -4,6 +4,7 @@ require 'forwardable'
4
4
  require 'java'
5
5
  require 'time'
6
6
  require 'openhab/dsl/types/datetime'
7
+ require 'openhab/dsl/items/item_delegate'
7
8
 
8
9
  module OpenHAB
9
10
  module DSL
@@ -16,6 +17,7 @@ module OpenHAB
16
17
  class DateTimeItem
17
18
  extend Forwardable
18
19
  include Comparable
20
+ include OpenHAB::DSL::Items::ItemDelegate
19
21
 
20
22
  def_delegator :@datetime_item, :to_s
21
23
 
@@ -27,6 +29,8 @@ module OpenHAB
27
29
  #
28
30
  def initialize(datetime_item)
29
31
  @datetime_item = datetime_item
32
+ item_delegate { @datetime_item }
33
+ item_delegate { to_dt }
30
34
  end
31
35
 
32
36
  #
@@ -61,36 +65,6 @@ module OpenHAB
61
65
  def zone
62
66
  to_dt.zone if state?
63
67
  end
64
-
65
- #
66
- # Check if missing method can be delegated to other contained objects
67
- #
68
- # @param [String, Symbol] meth The method name to check for
69
- #
70
- # @return [Boolean] true if DateTimeItem or DateTime responds to the method, false otherwise
71
- #
72
- def respond_to_missing?(meth, *)
73
- @datetime_item.respond_to?(meth) || to_dt.respond_to?(meth)
74
- end
75
-
76
- #
77
- # Forward missing methods to the OpenHAB Item, or a DateTime object wrapping its state
78
- #
79
- # @param [String] meth method name
80
- # @param [Array] args arguments for method
81
- # @param [Proc] block <description>
82
- #
83
- # @return [Object] Value from delegated method in OpenHAB NumberItem
84
- #
85
- def method_missing(meth, *args, &block)
86
- if @datetime_item.respond_to?(meth)
87
- @datetime_item.__send__(meth, *args, &block)
88
- elsif state?
89
- to_dt.send(meth, *args, &block)
90
- else
91
- raise NoMethodError, "undefined method `#{meth}' for #{self.class}"
92
- end
93
- end
94
68
  end
95
69
  end
96
70
  end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+ require 'openhab/log/logger'
5
+
6
+ module OpenHAB
7
+ module DSL
8
+ module Items
9
+ #
10
+ # Holds methods to delegate to items
11
+ #
12
+ module ItemDelegate
13
+ include OpenHAB::Log
14
+
15
+ #
16
+ # Delegates methods to the object returned from the supplied block if
17
+ # they don't exist in the object this is included in. If the supplied block returns nil, no delegation occurs
18
+ # If this item is called more than once delegation occurs in the order of invocation, i.e. the object returned
19
+ # by the first block is delegated to if it responds to the missing method,
20
+ # then the second block is processed, etc.
21
+ #
22
+ # @param [Proc] &delegate delgegate block
23
+ #
24
+ #
25
+ def item_delegate(&delegate)
26
+ @delegates ||= []
27
+ @delegates << delegate
28
+ end
29
+
30
+ #
31
+ # Delegate missing method calls to delegates supplied to item_delgate method
32
+ # if no delegates exist or respond to missing method, super is invoked which will
33
+ # throw the appropriate method missing error
34
+ #
35
+ # @param [String] meth misisng method
36
+ # @param [Array] *args Arguments to the missing method
37
+ # @param [Proc] &block supplied to the missing method
38
+ #
39
+ # @return [Object] Result of missing method invocation
40
+ #
41
+ def method_missing(meth, *args, &block)
42
+ logger.trace("Method (#{meth}) missing for item #{self.class}")
43
+ delegate = delegate_for(meth)
44
+ if delegate
45
+ logger.trace("Delegating #{meth} to #{delegate.class}")
46
+ delegate.__send__(meth, *args, &block)
47
+ else
48
+ super
49
+ end
50
+ end
51
+
52
+ #
53
+ # Checks if any of the supplied delgates respond to a specific method
54
+ #
55
+ # @param [String] meth method to check for
56
+ # @param [Boolean] _include_private if private methods should be checked
57
+ #
58
+ # @return [Boolean] True if any delegates respond to method, false otherwise
59
+ #
60
+ def respond_to_missing?(meth, _include_private = false)
61
+ logger.trace("Checking if (#{self.class}) responds to (#{meth})")
62
+ responds = !delegate_for(meth).nil?
63
+ logger.trace("(#{self.class}) responds to (#{meth}) (#{responds})")
64
+ responds
65
+ end
66
+
67
+ private
68
+
69
+ #
70
+ # Find a delegate for the supplied method
71
+ #
72
+ # @param [String] meth method to find delegate for
73
+ #
74
+ # @return [Boolean] True if any method responds to the supplied delegate, false otherwise
75
+ #
76
+ def delegate_for(meth)
77
+ (@delegates || []).each do |delegate_block|
78
+ delegate = delegate_block.call(meth)
79
+ logger.trace("Checking if delegate (#{delegate.class}) responds to (#{meth})")
80
+ if delegate.respond_to? meth
81
+ logger.trace("Delegate (#{delegate.class}) found for method (#{meth})")
82
+ return delegate
83
+ end
84
+ end
85
+ logger.trace("No delegate found for method (#{meth})")
86
+ nil
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -4,6 +4,7 @@ require 'bigdecimal'
4
4
  require 'forwardable'
5
5
  require 'java'
6
6
  require 'openhab/dsl/types/quantity'
7
+ require 'openhab/dsl/items/item_delegate'
7
8
 
8
9
  module OpenHAB
9
10
  module DSL
@@ -17,6 +18,8 @@ module OpenHAB
17
18
  class NumberItem < Numeric
18
19
  extend Forwardable
19
20
 
21
+ include OpenHAB::DSL::Items::ItemDelegate
22
+
20
23
  def_delegator :@number_item, :to_s
21
24
 
22
25
  java_import org.openhab.core.library.types.DecimalType
@@ -31,6 +34,7 @@ module OpenHAB
31
34
  #
32
35
  def initialize(number_item)
33
36
  @number_item = number_item
37
+ item_delegate { @number_item }
34
38
  super()
35
39
  end
36
40
 
@@ -146,40 +150,6 @@ module OpenHAB
146
150
  @number_item.dimension
147
151
  end
148
152
 
149
- #
150
- # Forward missing methods to Openhab Number Item if they are defined
151
- #
152
- # @param [String] meth method name
153
- # @param [Array] args arguments for method
154
- # @param [Proc] block <description>
155
- #
156
- # @return [Object] Value from delegated method in OpenHAB NumberItem
157
- #
158
- def method_missing(meth, *args, &block)
159
- logger.trace("Method missing, performing dynamic lookup for: #{meth}")
160
- if @number_item.respond_to?(meth)
161
- @number_item.__send__(meth, *args, &block)
162
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
163
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
164
- else
165
- super(meth, *args, &block)
166
- end
167
- end
168
-
169
- #
170
- # Checks if this method responds to the missing method
171
- #
172
- # @param [String] method_name Name of the method to check
173
- # @param [Boolean] _include_private boolean if private methods should be checked
174
- #
175
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
176
- #
177
- def respond_to_missing?(method_name, _include_private = false)
178
- @number_item.respond_to?(method_name) ||
179
- ::Kernel.method_defined?(method_name) ||
180
- ::Kernel.private_method_defined?(method_name)
181
- end
182
-
183
153
  %w[+ - * /].each do |operation|
184
154
  define_method(operation) do |other|
185
155
  logger.trace("Execution math operation '#{operation}' on #{inspect} with #{other.inspect}")
@@ -3,6 +3,7 @@
3
3
  require 'forwardable'
4
4
  require 'java'
5
5
  require 'openhab/dsl/items/item_command'
6
+ require 'openhab/dsl/items/item_delegate'
6
7
 
7
8
  module OpenHAB
8
9
  module DSL
@@ -13,6 +14,7 @@ module OpenHAB
13
14
  class RollershutterItem < Numeric
14
15
  extend Forwardable
15
16
  extend OpenHAB::DSL::Items::ItemCommand
17
+ include OpenHAB::DSL::Items::ItemDelegate
16
18
  include Comparable
17
19
 
18
20
  def_delegator :@rollershutter_item, :to_s
@@ -35,6 +37,9 @@ module OpenHAB
35
37
  logger.trace("Wrapping #{rollershutter_item}")
36
38
  @rollershutter_item = rollershutter_item
37
39
 
40
+ item_delegate { @rollershutter_item }
41
+ item_delegate { position }
42
+
38
43
  super()
39
44
  end
40
45
 
@@ -79,19 +84,6 @@ module OpenHAB
79
84
  end
80
85
  end
81
86
 
82
- #
83
- # Case equality
84
- #
85
- # @param [Java::OrgOpenhabCoreLibraryTypes::UpDownType, Numeric] other Other object to compare against
86
- #
87
- # @return [Boolean] true if self can be defined as other, false otherwise
88
- #
89
- def ===(other)
90
- super unless other.is_a? UpDownType
91
-
92
- state.as(UpDownType).equals(other)
93
- end
94
-
95
87
  #
96
88
  # Define math operations
97
89
  #
@@ -101,38 +93,6 @@ module OpenHAB
101
93
  left.send(operator, right)
102
94
  end
103
95
  end
104
-
105
- #
106
- # Checks if this method responds to the missing method
107
- #
108
- # @param [String] meth Name of the method to check
109
- # @param [Boolean] _include_private boolean if private methods should be checked
110
- #
111
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
112
- #
113
- def respond_to_missing?(meth, _include_private = false)
114
- @rollershutter_item.respond_to?(meth) || position.respond_to?(meth)
115
- end
116
-
117
- #
118
- # Forward missing methods to Openhab RollershutterItem or the PercentType representing it's state
119
- # if they are defined
120
- #
121
- # @param [String] meth method name
122
- # @param [Array] args arguments for method
123
- # @param [Proc] block <description>
124
- #
125
- # @return [Object] Value from delegated method in OpenHAB NumberItem
126
- #
127
- def method_missing(meth, *args, &block)
128
- if @rollershutter_item.respond_to?(meth)
129
- @rollershutter_item.__send__(meth, *args, &block)
130
- elsif position.respond_to?(meth)
131
- position.__send__(meth, *args, &block)
132
- else
133
- raise NoMethodError, "No method `#{meth}' defined for #{self.class}"
134
- end
135
- end
136
96
  end
137
97
  end
138
98
  end
@@ -3,6 +3,7 @@
3
3
  require 'bigdecimal'
4
4
  require 'forwardable'
5
5
  require 'java'
6
+ require 'openhab/dsl/items/item_delegate'
6
7
 
7
8
  module OpenHAB
8
9
  module DSL
@@ -12,7 +13,9 @@ module OpenHAB
12
13
  #
13
14
  class StringItem
14
15
  extend Forwardable
16
+
15
17
  include Comparable
18
+ include OpenHAB::DSL::Items::ItemDelegate
16
19
 
17
20
  # @return [Regex] Regular expression matching blank strings
18
21
  BLANK_RE = /\A[[:space:]]*\z/.freeze
@@ -27,6 +30,10 @@ module OpenHAB
27
30
  #
28
31
  def initialize(string_item)
29
32
  @string_item = string_item
33
+
34
+ item_delegate { @string_item }
35
+ item_delegate { @string_item.state&.to_full_string&.to_s }
36
+
30
37
  super()
31
38
  end
32
39
 
@@ -78,42 +85,6 @@ module OpenHAB
78
85
  @string_item.state <=> other
79
86
  end
80
87
  end
81
-
82
- #
83
- # Forward missing methods to Openhab String Item or String representation of the item if they are defined
84
- #
85
- # @param [String] meth method name
86
- # @param [Array] args arguments for method
87
- # @param [Proc] block <description>
88
- #
89
- # @return [Object] Value from delegated method in OpenHAB StringItem or Ruby String
90
- #
91
- def method_missing(meth, *args, &block)
92
- if @string_item.respond_to?(meth)
93
- @string_item.__send__(meth, *args, &block)
94
- elsif @string_item.state&.to_full_string&.to_s.respond_to?(meth)
95
- @string_item.state.to_full_string.to_s.__send__(meth, *args, &block)
96
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
97
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
98
- else
99
- super(meth, *args, &block)
100
- end
101
- end
102
-
103
- #
104
- # Checks if this method responds to the missing method
105
- #
106
- # @param [String] method_name Name of the method to check
107
- # @param [Boolean] _include_private boolean if private methods should be checked
108
- #
109
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
110
- #
111
- def respond_to_missing?(method_name, _include_private = false)
112
- @string_item.respond_to?(method_name) ||
113
- @string_item.state&.to_full_string&.to_s.respond_to?(method_name) ||
114
- ::Kernel.method_defined?(method_name) ||
115
- ::Kernel.private_method_defined?(method_name)
116
- end
117
88
  end
118
89
  end
119
90
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'openhab/dsl/monkey_patch/events/item_state'
3
4
  require 'openhab/dsl/monkey_patch/events/item_state_changed'
4
5
  require 'openhab/dsl/monkey_patch/events/thing_status_info'
5
6
  require 'openhab/dsl/monkey_patch/events/item_command'
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB events
10
+ #
11
+ module Events
12
+ java_import Java::OrgOpenhabCoreItemsEvents::ItemStateEvent
13
+
14
+ #
15
+ # MonkeyPatch with ruby style accessors
16
+ #
17
+ class ItemStateEvent
18
+ alias state item_state
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'java'
4
+ require 'openhab/dsl/items/item_command'
5
+ require 'forwardable'
4
6
 
5
7
  module OpenHAB
6
8
  module DSL
@@ -25,31 +27,13 @@ module OpenHAB
25
27
  java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
26
28
  java_import Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
27
29
 
28
- #
29
- # Add the current dimmer value to the supplied object
30
- #
31
- # @param [Object] other object to add the dimmer value to
32
- #
33
- # @return [Integer] Current dimmer value plus value of supplied object
34
- #
35
- def +(other)
36
- return unless state?
30
+ extend Forwardable
31
+ extend OpenHAB::DSL::Items::ItemCommand
37
32
 
38
- state.to_big_decimal.intValue + other
39
- end
33
+ item_state Java::OrgOpenhabCoreLibraryTypes::OnOffType
34
+ item_command Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
40
35
 
41
- #
42
- # Subtract the supplied object from the current value of the dimmer
43
- #
44
- # @param [Object] other object to subtract from the dimmer value
45
- #
46
- # @return [Integer] Current dimmer value minus value of supplied object
47
- #
48
- def -(other)
49
- return unless state?
50
-
51
- state.to_big_decimal.intValue - other
52
- end
36
+ def_delegator :state, :to_s
53
37
 
54
38
  #
55
39
  # Dim the dimmer
@@ -59,17 +43,9 @@ module OpenHAB
59
43
  # @return [Integer] level target for dimmer
60
44
  #
61
45
  def dim(amount = 1)
62
- return unless state?
63
-
64
- target = [state.to_big_decimal.intValue - amount, 0].max
65
-
66
- if amount == 1
67
- command(IncreaseDecreaseType::DECREASE)
68
- else
69
- command(target)
70
- end
71
-
72
- target
46
+ [state&.to_big_decimal&.intValue&.-(amount), 0].compact
47
+ .max
48
+ .tap { |target| command(target) }
73
49
  end
74
50
 
75
51
  #
@@ -80,16 +56,7 @@ module OpenHAB
80
56
  # @return [Integer] level target for dimmer
81
57
  #
82
58
  def brighten(amount = 1)
83
- return unless state?
84
-
85
- target = state.to_big_decimal.intValue + amount
86
-
87
- if amount == 1
88
- command(IncreaseDecreaseType::INCREASE)
89
- else
90
- command(target)
91
- end
92
- target
59
+ state&.to_big_decimal&.intValue&.+(amount)&.tap { |target| command(target) }
93
60
  end
94
61
 
95
62
  #
@@ -120,10 +87,8 @@ module OpenHAB
120
87
  def coerce(other)
121
88
  logger.trace("Coercing #{self} as a request from #{other.class}")
122
89
  case other
123
- when Numeric
124
- [other, state.to_big_decimal.to_d]
125
- else
126
- [other, state]
90
+ when Numeric then [other, state.to_big_decimal.to_d]
91
+ else [other, state]
127
92
  end
128
93
  end
129
94
 
@@ -140,6 +105,16 @@ module OpenHAB
140
105
  (self <=> other).zero?
141
106
  end
142
107
 
108
+ #
109
+ # Define math operations
110
+ #
111
+ %i[+ - / *].each do |operator|
112
+ define_method(operator) do |other|
113
+ right, left = coerce(other)
114
+ left.send(operator, right)
115
+ end
116
+ end
117
+
143
118
  #
144
119
  # Check if dimmer has a state and state is not zero
145
120
  #
@@ -159,33 +134,6 @@ module OpenHAB
159
134
  end
160
135
 
161
136
  alias to_int to_i
162
-
163
- #
164
- # Return the string representation of the dimmer item
165
- #
166
- # @return [String] String version of the dimmer value
167
- #
168
- def to_s
169
- to_i.to_s
170
- end
171
-
172
- #
173
- # Check if dimmer is on
174
- #
175
- # @return [Boolean] True if item is not UNDEF or NULL and has a value greater than 0
176
- #
177
- def on?
178
- state&.to_big_decimal&.intValue&.positive?
179
- end
180
-
181
- #
182
- # Check if dimmer is off
183
- #
184
- # @return [Boolean] True if item is not UNDEF or NULL and has a state of 0
185
- #
186
- def off?
187
- state&.to_big_decimal&.intValue&.zero?
188
- end
189
137
  end
190
138
  end
191
139
  end
@@ -29,7 +29,7 @@ module OpenHAB
29
29
  def_delegator :@metadata, :value
30
30
 
31
31
  def initialize(metadata: nil, key: nil, value: nil, config: nil)
32
- @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value, config)
32
+ @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value&.to_s, config)
33
33
  super(@metadata&.configuration)
34
34
  end
35
35
 
@@ -60,9 +60,7 @@ module OpenHAB
60
60
  # @return [Java::Org::openhab::core::items::Metadata] the old metadata
61
61
  #
62
62
  def value=(value)
63
- raise ArgumentError, 'Value must be a string' unless value.is_a? String
64
-
65
- metadata = Metadata.new(@metadata&.uID, value, @metadata&.configuration)
63
+ metadata = Metadata.new(@metadata&.uID, value&.to_s, @metadata&.configuration)
66
64
  NamespaceAccessor.registry.update(metadata) if @metadata&.uID
67
65
  end
68
66
 
@@ -125,7 +123,7 @@ module OpenHAB
125
123
  meta_value, configuration = update_from_value(value)
126
124
 
127
125
  key = MetadataKey.new(namespace, @item_name)
128
- metadata = Metadata.new(key, meta_value, configuration)
126
+ metadata = Metadata.new(key, meta_value&.to_s, configuration)
129
127
  # registry.get can be omitted, but registry.update will log a warning for nonexistent metadata
130
128
  if NamespaceAccessor.registry.get(key)
131
129
  NamespaceAccessor.registry.update(metadata)
@@ -49,7 +49,12 @@ module OpenHAB
49
49
  if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
50
50
  timestamp = Java::JavaTime::ZonedDateTime.now.minus(timestamp)
51
51
  end
52
- PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
52
+ result = PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
53
+ if result.is_a?(Java::OrgOpenhabCoreLibraryTypes::DecimalType) && respond_to?(:unit) && unit
54
+ Quantity.new(Java::OrgOpenhabCoreLibraryTypes::QuantityType.new(result.to_big_decimal, unit))
55
+ else
56
+ result
57
+ end
53
58
  end
54
59
  end
55
60
 
@@ -63,6 +63,27 @@ module OpenHAB
63
63
  logger.trace("#{self.class} #{self} == #{other} (#{other.class})")
64
64
  (self <=> other).zero?
65
65
  end
66
+
67
+ #
68
+ # Convert DecimalType to a Quantity
69
+ #
70
+ # @param [Object] other String or Unit representing an OpenHAB Unit
71
+ #
72
+ # @return [OpenHAB::Core::DSL::Types::Quantity] NumberItem converted to supplied Unit
73
+ #
74
+ def |(other)
75
+ other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
76
+ Quantity.new(QuantityType.new(to_big_decimal, other))
77
+ end
78
+
79
+ #
80
+ # Provide details about DecimalType object
81
+ #
82
+ # @return [String] Representing details about the DecimalType object
83
+ #
84
+ def inspect
85
+ to_string
86
+ end
66
87
  end
67
88
  end
68
89
  end
@@ -100,8 +100,7 @@ module OpenHAB
100
100
  # @return [Boolean] True if the rule should execute, false if trigger guard prevents execution
101
101
  #
102
102
  def check_trigger_guards(trigger_delay, inputs)
103
- old_state = inputs['oldState']
104
- new_state = inputs['newState']
103
+ new_state, old_state = retrieve_states(inputs)
105
104
  if check_from(trigger_delay, old_state)
106
105
  return true if check_to(trigger_delay, new_state)
107
106
 
@@ -113,6 +112,32 @@ module OpenHAB
113
112
  end
114
113
  end
115
114
 
115
+ #
116
+ # Rerieve the newState and oldState, alternatively newStatus and oldStatus
117
+ # from the input map
118
+ #
119
+ # @param [Map] inputs OpenHAB map object describing rule trigger
120
+ #
121
+ # @return [Array] An array of the values for [newState, oldState] or [newStatus, oldStatus]
122
+ #
123
+ def retrieve_states(inputs)
124
+ old_state = inputs['oldState'] || thing_status_to_sym(inputs['oldStatus'])
125
+ new_state = inputs['newState'] || thing_status_to_sym(inputs['newStatus'])
126
+
127
+ [new_state, old_state]
128
+ end
129
+
130
+ #
131
+ # Converts a ThingStatus object to a ruby Symbol
132
+ #
133
+ # @param [Java::OrgOpenhabCoreThing::ThingStatus] status A ThingStatus instance
134
+ #
135
+ # @return [Symbol] A corresponding symbol, in lower case
136
+ #
137
+ def thing_status_to_sym(status)
138
+ status&.to_s&.downcase&.to_sym
139
+ end
140
+
116
141
  #
117
142
  # Check the from state against the trigger delay
118
143
  #
@@ -174,7 +199,7 @@ module OpenHAB
174
199
  queue = create_queue(inputs)
175
200
  process_queue(queue, mod, inputs)
176
201
  end
177
- trigger_delay.tracking_to = inputs['newState']
202
+ trigger_delay.tracking_to, = retrieve_states(inputs)
178
203
  end
179
204
 
180
205
  #
@@ -186,7 +211,7 @@ module OpenHAB
186
211
  #
187
212
  #
188
213
  def process_active_timer(inputs, mod, trigger_delay)
189
- state = inputs['newState']
214
+ state, = retrieve_states(inputs)
190
215
  if state == trigger_delay.tracking_to
191
216
  logger.trace("Item changed to #{state} for #{trigger_delay}, rescheduling timer.")
192
217
  trigger_delay.timer.reschedule(ZonedDateTime.now.plus(trigger_delay.duration))
@@ -58,16 +58,8 @@ module OpenHAB
58
58
  # @return [Array] Array of current TriggerDelay objects
59
59
  #
60
60
  def changed_wait(item, duration:, to: nil, from: nil)
61
- # If GroupItems specified, use the group state trigger instead
62
- if item.is_a? GroupItems
63
- config = { 'groupName' => item.group.name }
64
- trigger = Trigger::GROUP_STATE_CHANGE
65
- else
66
- config = { 'itemName' => item.name }
67
- trigger = Trigger::ITEM_STATE_CHANGE
68
- end
69
- logger.trace("Creating Changed Wait Change Trigger for #{config}")
70
- trigger = append_trigger(trigger, config)
61
+ trigger = create_changed_trigger(item, nil, nil)
62
+ logger.trace("Creating Changed Wait Change Trigger for #{item}")
71
63
  @trigger_delays[trigger.id] = TriggerDelay.new(to: to, from: from, duration: duration)
72
64
  end
73
65
 
@@ -22,6 +22,7 @@ module OpenHAB
22
22
  def_delegator :@quantity, :to_s
23
23
 
24
24
  java_import org.openhab.core.library.types.QuantityType
25
+ java_import org.openhab.core.library.types.DecimalType
25
26
  java_import 'tec.uom.se.format.SimpleUnitFormat'
26
27
  java_import 'tec.uom.se.AbstractUnit'
27
28
 
@@ -92,6 +93,7 @@ module OpenHAB
92
93
  case other
93
94
  when Quantity then [other.quantity, quantity]
94
95
  when QuantityType then [other, quantity]
96
+ when DecimalType then [Quantity.new(other.to_big_decimal.to_d), quantity]
95
97
  when NumberItem then [other.to_qt.quantity, quantity]
96
98
  when Numeric, String then [Quantity.new(other), self]
97
99
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.20.3'
8
+ VERSION = '2.23.1'
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.20.3
4
+ version: 2.23.1
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-18 00:00:00.000000000 Z
11
+ date: 2021-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -41,6 +41,7 @@ files:
41
41
  - lib/openhab/dsl/group.rb
42
42
  - lib/openhab/dsl/items/datetime_item.rb
43
43
  - lib/openhab/dsl/items/item_command.rb
44
+ - lib/openhab/dsl/items/item_delegate.rb
44
45
  - lib/openhab/dsl/items/items.rb
45
46
  - lib/openhab/dsl/items/number_item.rb
46
47
  - lib/openhab/dsl/items/rollershutter_item.rb
@@ -49,6 +50,7 @@ files:
49
50
  - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
50
51
  - lib/openhab/dsl/monkey_patch/events/events.rb
51
52
  - lib/openhab/dsl/monkey_patch/events/item_command.rb
53
+ - lib/openhab/dsl/monkey_patch/events/item_state.rb
52
54
  - lib/openhab/dsl/monkey_patch/events/item_state_changed.rb
53
55
  - lib/openhab/dsl/monkey_patch/events/thing_status_info.rb
54
56
  - lib/openhab/dsl/monkey_patch/items/contact_item.rb