openhab-scripting 2.13.1 → 2.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab.rb +3 -0
  3. data/lib/openhab/core/dsl.rb +4 -0
  4. data/lib/openhab/core/dsl/actions.rb +1 -1
  5. data/lib/openhab/core/dsl/entities.rb +41 -4
  6. data/lib/openhab/core/dsl/gems.rb +1 -1
  7. data/lib/openhab/core/dsl/group.rb +3 -1
  8. data/lib/openhab/core/dsl/items/items.rb +3 -1
  9. data/lib/openhab/core/dsl/items/number_item.rb +151 -50
  10. data/lib/openhab/core/dsl/items/string_item.rb +21 -3
  11. data/lib/openhab/core/dsl/monkey_patch/items/items.rb +2 -0
  12. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +66 -42
  13. data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +72 -0
  14. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +2 -1
  15. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +2 -1
  16. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +1 -0
  17. data/lib/openhab/core/dsl/persistence.rb +27 -0
  18. data/lib/openhab/core/dsl/property.rb +15 -4
  19. data/lib/openhab/core/dsl/rule/automation_rule.rb +348 -0
  20. data/lib/openhab/core/dsl/rule/guard.rb +43 -6
  21. data/lib/openhab/core/dsl/rule/rule.rb +80 -354
  22. data/lib/openhab/core/dsl/rule/rule_config.rb +153 -0
  23. data/lib/openhab/core/dsl/rule/triggers/changed.rb +145 -0
  24. data/lib/openhab/core/dsl/rule/{channel.rb → triggers/channel.rb} +22 -8
  25. data/lib/openhab/core/dsl/rule/triggers/command.rb +106 -0
  26. data/lib/openhab/core/dsl/rule/{cron.rb → triggers/cron.rb} +36 -14
  27. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +126 -0
  28. data/lib/openhab/core/dsl/rule/triggers/updated.rb +100 -0
  29. data/lib/openhab/core/dsl/time_of_day.rb +50 -24
  30. data/lib/openhab/core/dsl/timers.rb +2 -6
  31. data/lib/openhab/core/dsl/types/quantity.rb +106 -69
  32. data/lib/openhab/core/log.rb +3 -8
  33. data/lib/openhab/core/startup_delay.rb +1 -0
  34. data/lib/openhab/osgi.rb +7 -0
  35. data/lib/openhab/version.rb +1 -1
  36. metadata +12 -6
  37. data/lib/openhab/core/dsl/rule/item.rb +0 -208
  38. data/lib/openhab/core/dsl/rule/triggers.rb +0 -77
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '089e0d97f175f04c563675de122c80d0f5988be5ea38be8833736e4e73341a75'
4
- data.tar.gz: 4b98810dd2e4f01772272fc10e457b57e81ade54c19b1dc59a72afd85c368b5a
3
+ metadata.gz: e06a1b4a2697fe4ac5d03467fb324ce1cf4d6e674946a7d96dae8928974b2460
4
+ data.tar.gz: 522268dcd796de02ee8dde9417d5a044dab87ef8a8d8100957268fc544443d3b
5
5
  SHA512:
6
- metadata.gz: 67abc23128c151fdeb509f7a6c770696e0221b6a41a6e8af36628c891aa959c72e7ed7123d1e6d9711da3fe862eba2adaac9e3692e1026193cc7638e11de926a
7
- data.tar.gz: 4348d10e590f9e8179c3559c05ae2417cd9a75150d069e98f1566316e60a5d5ed12b4057b0d67272e97d92c813cbf337c97dac8d69b1cc5538b06576aa3084f7
6
+ metadata.gz: 51ea02b7ccc10fc831a17627e31f0b0d7e250e778a08c8e7d226118c61cb98a07761eabee1299e9c6a4a83e949b448c20f1c7ebb7e5877939b601f1108929cec
7
+ data.tar.gz: 1ea8435d4826e815c212e422e714ac0480266b2ebc283c4471116cf08f45b3336758c75a1b74d00d631f1c7b729e078620dd72d4e9b853465eee06010e2ea027
data/lib/openhab.rb CHANGED
@@ -36,4 +36,7 @@ module OpenHAB
36
36
  end
37
37
 
38
38
  # Extend caller with OpenHAB methods
39
+
40
+ # rubocop: disable Style/MixinUsage
39
41
  extend OpenHAB
42
+ # rubocop: enable Style/MixinUsage
@@ -19,6 +19,7 @@ require 'core/dsl/gems'
19
19
  require 'core/dsl/units'
20
20
  require 'core/dsl/types/quantity'
21
21
  require 'core/dsl/states'
22
+ require 'core/dsl/persistence'
22
23
 
23
24
  module OpenHAB
24
25
  #
@@ -30,6 +31,7 @@ module OpenHAB
30
31
  #
31
32
  module DSL
32
33
  # Extend the calling module/class with the DSL
34
+ # rubocop: disable Metrics/MethodLength
33
35
  def self.extended(base)
34
36
  base.send :include, OpenHAB::Core::DSL::Rule
35
37
  base.send :include, OpenHAB::Core::DSL::Items
@@ -40,8 +42,10 @@ module OpenHAB
40
42
  base.send :include, OpenHAB::Core::DSL::Timers
41
43
  base.send :include, OpenHAB::Core::DSL::States
42
44
  base.send :include, OpenHAB::Core::DSL::Tod
45
+ base.send :include, OpenHAB::Core::DSL::Persistence
43
46
  base.send :include, Things
44
47
  end
48
+ # rubocop: enable Metrics/MethodLength
45
49
  end
46
50
  end
47
51
  end
@@ -46,7 +46,7 @@ module OpenHAB
46
46
  # rubocop: disable Style/GlobalVars
47
47
  action_keys = $actions.action_keys
48
48
  # rubocop: enable Style/GlobalVars
49
- logger.trace( "Registered actions: '#{action_keys}' for thing '#{thing_uid}'")
49
+ logger.trace("Registered actions: '#{action_keys}' for thing '#{thing_uid}'")
50
50
  action_keys.map { |action_key| action_key.split('-', 2) }
51
51
  .select { |action_pair| action_pair.last == thing_uid }
52
52
  .map(&:first)
@@ -4,6 +4,7 @@ require 'pp'
4
4
  require 'java'
5
5
  require 'set'
6
6
  require 'core/dsl/group'
7
+ require 'core/log'
7
8
  require 'core/dsl/items/number_item'
8
9
  require 'core/dsl/items/string_item'
9
10
 
@@ -25,6 +26,7 @@ end
25
26
  # Manages access to OpenHAB entities
26
27
  #
27
28
  module EntityLookup
29
+ include Logging
28
30
  #
29
31
  # Decorate items with Ruby wrappers
30
32
  #
@@ -32,14 +34,13 @@ module EntityLookup
32
34
  #
33
35
  # @return [Array] Array of decorated items
34
36
  #
37
+ # rubocop: disable Metrics/MethodLength
38
+ # Disabled line length - case dispatch pattern
35
39
  def self.decorate_items(*items)
36
40
  items.flatten.map do |item|
37
41
  case item
38
42
  when GroupItem
39
- group = item
40
- item = OpenHAB::Core::DSL::Groups::Group.new(Set.new(EntityLookup.decorate_items(item.all_members.to_a)))
41
- item.group = group
42
- item
43
+ decorate_group(item)
43
44
  when Java::Org.openhab.core.library.items::NumberItem
44
45
  OpenHAB::Core::DSL::Items::NumberItem.new(item)
45
46
  when Java::Org.openhab.core.library.items::StringItem
@@ -49,6 +50,7 @@ module EntityLookup
49
50
  end
50
51
  end
51
52
  end
53
+ # rubocop: enable Metrics/MethodLength
52
54
 
53
55
  #
54
56
  # Loops up a Thing in the OpenHAB registry replacing '_' with ':'
@@ -58,6 +60,7 @@ module EntityLookup
58
60
  # @return [Thing] if found, nil otherwise
59
61
  #
60
62
  def self.lookup_thing(name)
63
+ logger.trace("Looking up thing(#{name})")
61
64
  # Convert from : syntax to underscore
62
65
  name = name.to_s if name.is_a? Symbol
63
66
 
@@ -78,6 +81,7 @@ module EntityLookup
78
81
  # @return [Item] OpenHAB item if registry contains a matching item, nil othewise
79
82
  #
80
83
  def self.lookup_item(name)
84
+ logger.trace("Looking up item(#{name})")
81
85
  name = name.to_s if name.is_a? Symbol
82
86
  # rubocop: disable Style/GlobalVars
83
87
  item = $ir.get(name)
@@ -98,6 +102,39 @@ module EntityLookup
98
102
  return if method.to_s == 'scriptLoaded'
99
103
  return if method.to_s == 'scriptUnloaded'
100
104
 
105
+ logger.trace("method missing, performing OpenHab Lookup for: #{method}")
101
106
  EntityLookup.lookup_item(method) || EntityLookup.lookup_thing(method) || super
102
107
  end
108
+
109
+ #
110
+ # Checks if this method responds to the missing method
111
+ #
112
+ # @param [String] method_name Name of the method to check
113
+ # @param [Boolean] _include_private boolean if private methods should be checked
114
+ #
115
+ # @return [Boolean] true if this object will respond to the supplied method, false otherwise
116
+ #
117
+ def respond_to_missing?(method_name, _include_private = false)
118
+ logger.trace("Checking if OpenHAB entites exist for #{method_name}")
119
+ method_name = method_name.to_s if method_name.is_a? Symbol
120
+
121
+ method_name == 'scriptLoaded' ||
122
+ method_name == 'scriptUnloaded' ||
123
+ EntityLookup.lookup_item(method_name) ||
124
+ EntityLookup.lookup_thing(method_name) ||
125
+ super
126
+ end
127
+
128
+ #
129
+ # Decorate a group from an item base
130
+ #
131
+ # @param [OpenHAB item] item item to convert to a group item
132
+ #
133
+ # @return [OpenHAB::Core::DSL::Groups::Group] Group created from supplied item
134
+ #
135
+ def self.decorate_group(item)
136
+ group = OpenHAB::Core::DSL::Groups::Group.new(Set.new(EntityLookup.decorate_items(item.all_members.to_a)))
137
+ group.group = item
138
+ group
139
+ end
103
140
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'core/log'
4
- require 'bundler/inline'
4
+ require 'bundler/inline'
@@ -28,7 +28,7 @@ module OpenHAB
28
28
  #
29
29
  def[](name)
30
30
  group = EntityLookup.lookup_item(name)
31
- (group.is_a? Group) ? group : nil
31
+ group.is_a?(Group) ? group : nil
32
32
  end
33
33
  end
34
34
 
@@ -38,7 +38,9 @@ module OpenHAB
38
38
  # @return [Set] of OpenHAB Groups
39
39
  #
40
40
  def groups
41
+ # rubocop: disable Style/GlobalVars
41
42
  Groups.new(EntityLookup.decorate_items($ir.items.select { |item| item.is_a? GroupItem }))
43
+ # rubocop: enable Style/GlobalVars
42
44
  end
43
45
 
44
46
  # Group class that provides access to OpenHAB group object and delegates other methods to
@@ -21,7 +21,7 @@ module OpenHAB
21
21
  # rubocop: disable Style/GlobalVars
22
22
  item = $ir.getItem(name)
23
23
  # rubocop: enable Style/GlobalVars
24
- (item.is_a? GroupItem) ? nil : item
24
+ item.is_a?(GroupItem) ? nil : item
25
25
  rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
26
26
  nil
27
27
  end
@@ -30,7 +30,9 @@ module OpenHAB
30
30
  # @param name [String] Item name to check
31
31
  # @return [Boolean] true if the item exists, false otherwise
32
32
  def include?(name)
33
+ # rubocop: disable Style/GlobalVars
33
34
  !$ir.getItems(name).empty?
35
+ # rubocop: enable Style/GlobalVars
34
36
  end
35
37
  alias key? include?
36
38
  end
@@ -12,6 +12,9 @@ module OpenHAB
12
12
  #
13
13
  # Delegation to OpenHAB Number Item
14
14
  #
15
+ # rubocop: disable Metrics/ClassLength
16
+ # Disabled because this class has a single responsibility, there does not appear a logical
17
+ # way of breaking it up into multiple classes
15
18
  class NumberItem < Numeric
16
19
  extend Forwardable
17
20
 
@@ -46,21 +49,14 @@ module OpenHAB
46
49
  #
47
50
  # @param [Object] other object to coerce to a NumberItem if possible
48
51
  #
49
- # @return [Object] NumberItem, QuantityTypes, BigDecimal or nil depending on NumberItem configuration and/or supplied object
52
+ # @return [Object] NumberItem, QuantityTypes, BigDecimal or nil depending on NumberItem configuration
53
+ # and/or supplied object
50
54
  #
51
55
  def coerce(other)
52
56
  logger.trace("Coercing #{self} as a request from #{other.class}")
53
57
  case other
54
- when Quantity
55
- as_qt = to_qt
56
- logger.trace("Converted #{self} to a Quantity #{as_qt}")
57
- [other, as_qt]
58
- when Numeric
59
- if dimension
60
- [Quantity.new(other), to_qt]
61
- elsif @number_item.state?
62
- [BigDecimal(other), @number_item.state.to_big_decimal.to_d]
63
- end
58
+ when Quantity then coerce_from_quantity(other)
59
+ when Numeric then coerce_from_numeric(other)
64
60
  else
65
61
  logger.trace("#{self} cannot be coereced to #{other.class}")
66
62
  nil
@@ -72,22 +68,15 @@ module OpenHAB
72
68
  #
73
69
  # @param [Object] other object to compare to
74
70
  #
75
- # @return [Integer] -1,0,1 or nil depending on value supplied, nil comparison to supplied object is not possible.
71
+ # @return [Integer] -1,0,1 or nil depending on value supplied,
72
+ # nil comparison to supplied object is not possible.
76
73
  #
77
74
  def <=>(other)
78
75
  logger.trace("Comparing #{self} to #{other}")
79
76
  case other
80
- when NumberItem
81
- if other.dimension
82
- logger.trace('Other is dimensioned, converting self and other to QuantityTypes to compare')
83
- to_qt <=> other.to_qt
84
- else
85
- @number_item.state <=> other.state
86
- end
87
- when Numeric
88
- @number_item.state.to_big_decimal.to_d <=> BigDecimal(other)
89
- when String
90
- @number_item.state <=> QuantityType.new(other) if dimension
77
+ when NumberItem then number_item_compare(other)
78
+ when Numeric then @number_item.state.to_big_decimal.to_d <=> other.to_d
79
+ when String then @number_item.state <=> QuantityType.new(other) if dimension
91
80
  end
92
81
  end
93
82
 
@@ -167,6 +156,7 @@ module OpenHAB
167
156
  # @return [Object] Value from delegated method in OpenHAB NumberItem
168
157
  #
169
158
  def method_missing(meth, *args, &block)
159
+ logger.trace("Method missing, performing dynamic lookup for: #{meth}")
170
160
  if @number_item.respond_to?(meth)
171
161
  @number_item.__send__(meth, *args, &block)
172
162
  elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
@@ -176,42 +166,153 @@ module OpenHAB
176
166
  end
177
167
  end
178
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
+
179
183
  %w[+ - * /].each do |operation|
180
184
  define_method(operation) do |other|
181
185
  logger.trace("Execution math operation '#{operation}' on #{inspect} with #{other.inspect}")
182
- if other.is_a? NumberItem
183
- logger.trace('Math operations is between two NumberItems.')
184
- if dimension && other.dimension
185
- # If both numbers have dimensions, do the math on the quantity types.
186
- logger.trace("Both objects have dimensions self='#{dimension}' other='#{other.dimension}'")
187
- to_qt.public_send(operation, other.to_qt)
188
- elsif dimension && !other.dimension
189
- # If this number has dimension and the other does not, do math with this quantity type and the other as a big decimal
190
- logger.trace("Self has dimension self='#{dimension}' other lacks dimension='#{other}'")
191
- to_qt.public_send(operation, other)
192
- elsif other.dimension
193
- # If this number has no dimension and the other does, convert this into a dimensionless quantity
194
- logger.trace("Self has no dimension self='#{self}' other has dimension='#{other.dimension}'")
195
- to_qt.public_send(operation, other)
196
- else
197
- logger.trace("Both objects lack dimension, self='#{self}' other='#{other}'")
198
- # If nothing has a dimension, just use BigDecimals
199
- to_d.public_send(operation, other.to_d)
200
- end
201
- elsif other.is_a? Numeric
202
- to_d.public_send(operation, BigDecimal(other))
203
- elsif dimension && other.is_a?(String)
204
- to_qt.public_send(operation, Quantity.new(other))
205
- elsif other.respond_to? :coerce
206
- a, b = other.coerce(to_d)
207
- a.public_send(operation, b)
186
+ left_operand, right_operand = operands_for_operation(other)
187
+ left_operand.public_send(operation, right_operand)
188
+ end
189
+ end
190
+
191
+ private
192
+
193
+ #
194
+ # Get the operands for any operation
195
+ #
196
+ # @param [Object] other object to convert to a compatible operand
197
+ #
198
+ # @return [Array[Object,Object]] of operands where the first value is the left operand
199
+ # and the second value is the right operand
200
+ #
201
+ def operands_for_operation(other)
202
+ case other
203
+ when NumberItem then number_item_operands(other)
204
+ when Numeric then [to_d, other.to_d]
205
+ when String then string_operands(other)
206
+ else
207
+ return other.coerce(to_d) if other.respond_to? :coerce
208
+
209
+ raise ArgumentError, "#{other.class} can't be coerced into a NumberItem"
210
+ end
211
+ end
212
+
213
+ #
214
+ # Get operands for an operation when the right operand is provided as a string
215
+ #
216
+ # @param [String] other right operand
217
+ #
218
+ # @return [Array[QuantityType,QuantiyType]] of operands where the first value is the left operand
219
+ # and the second value is the right operand
220
+ #
221
+ def string_operands(other)
222
+ return [to_qt, Quantity.new(other)] if dimension
223
+
224
+ raise ArgumentError, 'Strings are only valid operands if NumberItem is dimensions=ed.'
225
+ end
226
+
227
+ #
228
+ # Get operands for an operation when the right operand is provided is another number item
229
+ #
230
+ # @param [NumberItem] other right operand
231
+ #
232
+ # @return [Array<QuantityType,QuantityType>,Array<BigDecimal,BigDecimal>] of operands depending on
233
+ # if the left or right operand has a dimensions
234
+ #
235
+ def number_item_operands(other)
236
+ if dimension || other.dimension
237
+ dimensioned_operands(other)
238
+ else
239
+ logger.trace("Both objects lack dimension, self='#{self}' other='#{other}'")
240
+ # If nothing has a dimension, just use BigDecimals
241
+ [to_d, other.to_d]
242
+ end
243
+ end
244
+
245
+ #
246
+ # Get operands for an operation when the left or right operand has a dimension
247
+ #
248
+ # @param [NumberItem] other right operand
249
+ #
250
+ # @return [Array<QuantityType,QuantityType>] of operands
251
+ #
252
+ def dimensioned_operands(other)
253
+ logger.trace("Dimensions self='#{dimension}' other='#{other.dimension}'")
254
+ if dimension
255
+ if other.dimension
256
+ # If both numbers have dimensions, do the math on the quantity types.
257
+ [to_qt, other.to_qt]
208
258
  else
209
- raise TypeError, "#{other.class} can't be coerced into a NumberItem"
259
+ # If this number has dimension and the other does not,
260
+ # do math with this quantity type and the other as a big decimal
261
+ [to_qt, other]
210
262
  end
263
+ else
264
+ # If this number has no dimension and the other does, convert this into a dimensionless quantity
265
+ [to_qt, other]
266
+ end
267
+ end
268
+
269
+ #
270
+ # Compare two number items, taking into account any dimensions
271
+ #
272
+ # @param [NumberItem] other number item
273
+ #
274
+ # @return [-1,0,1] depending on if other object is less than, equal to or greater than self
275
+ #
276
+ def number_item_compare(other)
277
+ if other.dimension
278
+ logger.trace('Other is dimensioned, converting self and other to QuantityTypes to compare')
279
+ to_qt <=> other.to_qt
280
+ else
281
+ @number_item.state <=> other.state
211
282
  end
212
283
  end
284
+
285
+ #
286
+ # Coerce from a numberic object depnding on dimension and state
287
+ #
288
+ # @param [Numeric] other numeric object to convert
289
+ #
290
+ # @return [Array<QuantityType,QuantityType>,Array<BigDecimal,BigDecimal>,nil] depending on
291
+ # if this object has a dimension or state
292
+ #
293
+ def coerce_from_numeric(other)
294
+ if dimension
295
+ [Quantity.new(other), to_qt]
296
+ elsif @number_item.state?
297
+ [other.to_d, @number_item.state.to_big_decimal.to_d]
298
+ end
299
+ end
300
+
301
+ #
302
+ # Coerce when other is a quantity
303
+ #
304
+ # @param [QuantityType] other
305
+ #
306
+ # @return [Array<QuanityType,QuantityType] other and self as a quantity type
307
+ #
308
+ def coerce_from_quantity(other)
309
+ as_qt = to_qt
310
+ logger.trace("Converted #{self} to a Quantity #{as_qt}")
311
+ [other, as_qt]
312
+ end
213
313
  end
214
314
  end
215
315
  end
216
316
  end
217
317
  end
318
+ # rubocop: enable Metrics/ClassLength