openhab-scripting 2.16.2 → 2.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab.rb +12 -16
  3. data/lib/openhab/core/entity_lookup.rb +173 -0
  4. data/lib/openhab/core/openhab_setup.rb +31 -0
  5. data/lib/openhab/core/osgi.rb +61 -0
  6. data/lib/openhab/dsl/actions.rb +105 -0
  7. data/lib/openhab/dsl/dsl.rb +49 -0
  8. data/lib/openhab/{core/dsl → dsl}/gems.rb +0 -1
  9. data/lib/openhab/dsl/group.rb +100 -0
  10. data/lib/openhab/dsl/items/datetime_item.rb +97 -0
  11. data/lib/openhab/dsl/items/items.rb +46 -0
  12. data/lib/openhab/dsl/items/number_item.rb +352 -0
  13. data/lib/openhab/dsl/items/rollershutter_item.rb +179 -0
  14. data/lib/openhab/dsl/items/string_item.rb +120 -0
  15. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
  16. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
  17. data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
  18. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
  19. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
  20. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
  21. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
  22. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
  23. data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
  24. data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
  25. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
  26. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
  27. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
  28. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
  29. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
  30. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +8 -0
  31. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
  32. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +32 -0
  33. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
  34. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
  35. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
  36. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
  37. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
  38. data/lib/openhab/dsl/monkey_patch/types/types.rb +9 -0
  39. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +33 -0
  40. data/lib/openhab/dsl/persistence.rb +25 -0
  41. data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
  42. data/lib/openhab/dsl/rules/guard.rb +134 -0
  43. data/lib/openhab/dsl/rules/property.rb +102 -0
  44. data/lib/openhab/dsl/rules/rule.rb +116 -0
  45. data/lib/openhab/dsl/rules/rule_config.rb +151 -0
  46. data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
  47. data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
  48. data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
  49. data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
  50. data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
  51. data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
  52. data/lib/openhab/dsl/states.rb +61 -0
  53. data/lib/openhab/dsl/things.rb +91 -0
  54. data/lib/openhab/dsl/time_of_day.rb +232 -0
  55. data/lib/openhab/dsl/timers.rb +77 -0
  56. data/lib/openhab/dsl/types/datetime.rb +326 -0
  57. data/lib/openhab/dsl/types/quantity.rb +290 -0
  58. data/lib/openhab/dsl/units.rb +39 -0
  59. data/lib/openhab/log/configuration.rb +21 -0
  60. data/lib/openhab/log/logger.rb +172 -0
  61. data/lib/openhab/version.rb +1 -1
  62. metadata +60 -58
  63. data/lib/openhab/configuration.rb +0 -16
  64. data/lib/openhab/core/cron.rb +0 -27
  65. data/lib/openhab/core/debug.rb +0 -34
  66. data/lib/openhab/core/dsl.rb +0 -51
  67. data/lib/openhab/core/dsl/actions.rb +0 -107
  68. data/lib/openhab/core/dsl/entities.rb +0 -147
  69. data/lib/openhab/core/dsl/group.rb +0 -102
  70. data/lib/openhab/core/dsl/items/items.rb +0 -51
  71. data/lib/openhab/core/dsl/items/number_item.rb +0 -323
  72. data/lib/openhab/core/dsl/items/string_item.rb +0 -122
  73. data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
  74. data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
  75. data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
  76. data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
  77. data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
  78. data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
  79. data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
  80. data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
  81. data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
  82. data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
  83. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
  84. data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
  85. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
  86. data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
  87. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
  88. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
  89. data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
  90. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
  91. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  92. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  93. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  94. data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
  95. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
  96. data/lib/openhab/core/dsl/persistence.rb +0 -27
  97. data/lib/openhab/core/dsl/property.rb +0 -96
  98. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -345
  99. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  100. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  101. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  102. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  103. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  104. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  105. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  106. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  107. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  108. data/lib/openhab/core/dsl/states.rb +0 -63
  109. data/lib/openhab/core/dsl/things.rb +0 -93
  110. data/lib/openhab/core/dsl/time_of_day.rb +0 -231
  111. data/lib/openhab/core/dsl/timers.rb +0 -79
  112. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  113. data/lib/openhab/core/dsl/units.rb +0 -41
  114. data/lib/openhab/core/log.rb +0 -170
  115. data/lib/openhab/core/patch_load_path.rb +0 -7
  116. data/lib/openhab/core/startup_delay.rb +0 -23
  117. data/lib/openhab/osgi.rb +0 -59
@@ -1,323 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bigdecimal'
4
- require 'forwardable'
5
- require 'java'
6
- require 'openhab/core/dsl/types/quantity'
7
-
8
- module OpenHAB
9
- module Core
10
- module DSL
11
- module Items
12
- #
13
- # Delegation to OpenHAB Number Item
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
18
- class NumberItem < Numeric
19
- extend Forwardable
20
-
21
- def_delegator :@number_item, :to_s
22
-
23
- java_import org.openhab.core.library.types.DecimalType
24
- java_import org.openhab.core.library.types.QuantityType
25
- java_import 'tec.uom.se.format.SimpleUnitFormat'
26
- java_import 'tec.uom.se.AbstractUnit'
27
-
28
- #
29
- # Create a new NumberItem
30
- #
31
- # @param [Java::Org::openhab::core::library::items::NumberItem] number_item OpenHAB number item to delegate to
32
- #
33
- def initialize(number_item)
34
- @number_item = number_item
35
- super()
36
- end
37
-
38
- #
39
- # Check if NumberItem is truthy? as per defined by library
40
- #
41
- # @return [Boolean] True if item is not in state UNDEF or NULL and value is not zero.
42
- #
43
- def truthy?
44
- @number_item.state? && @number_item.state != DecimalType::ZERO
45
- end
46
-
47
- #
48
- # Coerce objects into a NumberItem
49
- #
50
- # @param [Object] other object to coerce to a NumberItem if possible
51
- #
52
- # @return [Object] NumberItem, QuantityTypes, BigDecimal or nil depending on NumberItem configuration
53
- # and/or supplied object
54
- #
55
- def coerce(other)
56
- logger.trace("Coercing #{self} as a request from #{other.class}")
57
- case other
58
- when Quantity then coerce_from_quantity(other)
59
- when Numeric then coerce_from_numeric(other)
60
- else
61
- logger.trace("#{self} cannot be coerced to #{other.class}")
62
- nil
63
- end
64
- end
65
-
66
- #
67
- # Compare NumberItem to supplied object
68
- #
69
- # @param [Object] other object to compare to
70
- #
71
- # @return [Integer] -1,0,1 or nil depending on value supplied,
72
- # nil comparison to supplied object is not possible.
73
- #
74
- # rubocop: disable Metrics/AbcSize
75
- def <=>(other)
76
- logger.trace("NumberItem #{self} <=> #{other} (#{other.class})")
77
- case other
78
- when NumberItem then number_item_compare(other)
79
- when Numeric then @number_item.state.to_big_decimal.to_d <=> other.to_d
80
- when String then @number_item.state <=> QuantityType.new(other) if dimension
81
- else
82
- other = other.state if other.respond_to? :state
83
- @number_item.state <=> other
84
- end
85
- end
86
- # rubocop: enable Metrics/AbcSize
87
-
88
- #
89
- # Convert NumberItem to a Quantity
90
- #
91
- # @param [Object] other String or Unit representing an OpenHAB Unit
92
- #
93
- # @return [OpenHAB::Core::DSL::Types::Quantity] NumberItem converted to supplied Unit
94
- #
95
- def |(other)
96
- other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
97
-
98
- if dimension
99
- to_qt | other
100
- else
101
- Quantity.new(QuantityType.new(to_d.to_java, other))
102
- end
103
- end
104
-
105
- #
106
- # Convert NumberItem to a Quantity
107
- #
108
- # @return [OpenHAB::Core::DSL::Types::Quantity] NumberItem converted to a QuantityUnit
109
- #
110
- def to_qt
111
- if dimension
112
- Quantity.new(@number_item.get_state_as(QuantityType))
113
- else
114
- Quantity.new(QuantityType.new(to_d.to_java, AbstractUnit::ONE))
115
- end
116
- end
117
-
118
- #
119
- # Converts the NumberItem to an Integer
120
- #
121
- # @return [Integer] NumberItem as an integer
122
- #
123
- def to_i
124
- to_d&.to_i
125
- end
126
-
127
- #
128
- # Converts the NumberItem to a float
129
- #
130
- # @return [Float] NumberItem as a float
131
- #
132
- def to_f
133
- to_d&.to_f
134
- end
135
-
136
- #
137
- # Converts the NumberItem to a BigDecimal
138
- #
139
- # @return [BigDecimal] NumberItem as a BigDecimal
140
- #
141
- def to_d
142
- @number_item.state.to_big_decimal.to_d if @number_item.state.respond_to? :to_big_decimal
143
- end
144
-
145
- #
146
- # Get the Dimension attached to the NumberItem
147
- #
148
- # @return [Java::org::openhab::core::library::types::QuantityType] dimension
149
- #
150
- def dimension
151
- @number_item.dimension
152
- end
153
-
154
- #
155
- # Forward missing methods to Openhab Number Item if they are defined
156
- #
157
- # @param [String] meth method name
158
- # @param [Array] args arguments for method
159
- # @param [Proc] block <description>
160
- #
161
- # @return [Object] Value from delegated method in OpenHAB NumberItem
162
- #
163
- def method_missing(meth, *args, &block)
164
- logger.trace("Method missing, performing dynamic lookup for: #{meth}")
165
- if @number_item.respond_to?(meth)
166
- @number_item.__send__(meth, *args, &block)
167
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
168
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
169
- else
170
- super(meth, *args, &block)
171
- end
172
- end
173
-
174
- #
175
- # Checks if this method responds to the missing method
176
- #
177
- # @param [String] method_name Name of the method to check
178
- # @param [Boolean] _include_private boolean if private methods should be checked
179
- #
180
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
181
- #
182
- def respond_to_missing?(method_name, _include_private = false)
183
- @number_item.respond_to?(method_name) ||
184
- ::Kernel.method_defined?(method_name) ||
185
- ::Kernel.private_method_defined?(method_name)
186
- end
187
-
188
- %w[+ - * /].each do |operation|
189
- define_method(operation) do |other|
190
- logger.trace("Execution math operation '#{operation}' on #{inspect} with #{other.inspect}")
191
- left_operand, right_operand = operands_for_operation(other)
192
- left_operand.public_send(operation, right_operand)
193
- end
194
- end
195
-
196
- private
197
-
198
- #
199
- # Get the operands for any operation
200
- #
201
- # @param [Object] other object to convert to a compatible operand
202
- #
203
- # @return [Array[Object,Object]] of operands where the first value is the left operand
204
- # and the second value is the right operand
205
- #
206
- def operands_for_operation(other)
207
- case other
208
- when NumberItem then number_item_operands(other)
209
- when Numeric then [to_d, other.to_d]
210
- when String then string_operands(other)
211
- else
212
- return other.coerce(to_d) if other.respond_to? :coerce
213
-
214
- raise ArgumentError, "#{other.class} can't be coerced into a NumberItem"
215
- end
216
- end
217
-
218
- #
219
- # Get operands for an operation when the right operand is provided as a string
220
- #
221
- # @param [String] other right operand
222
- #
223
- # @return [Array[QuantityType,QuantiyType]] of operands where the first value is the left operand
224
- # and the second value is the right operand
225
- #
226
- def string_operands(other)
227
- return [to_qt, Quantity.new(other)] if dimension
228
-
229
- raise ArgumentError, 'Strings are only valid operands if NumberItem is dimensions=ed.'
230
- end
231
-
232
- #
233
- # Get operands for an operation when the right operand is provided is another number item
234
- #
235
- # @param [NumberItem] other right operand
236
- #
237
- # @return [Array<QuantityType,QuantityType>,Array<BigDecimal,BigDecimal>] of operands depending on
238
- # if the left or right operand has a dimensions
239
- #
240
- def number_item_operands(other)
241
- if dimension || other.dimension
242
- dimensioned_operands(other)
243
- else
244
- logger.trace("Both objects lack dimension, self='#{self}' other='#{other}'")
245
- # If nothing has a dimension, just use BigDecimals
246
- [to_d, other.to_d]
247
- end
248
- end
249
-
250
- #
251
- # Get operands for an operation when the left or right operand has a dimension
252
- #
253
- # @param [NumberItem] other right operand
254
- #
255
- # @return [Array<QuantityType,QuantityType>] of operands
256
- #
257
- def dimensioned_operands(other)
258
- logger.trace("Dimensions self='#{dimension}' other='#{other.dimension}'")
259
- if dimension
260
- if other.dimension
261
- # If both numbers have dimensions, do the math on the quantity types.
262
- [to_qt, other.to_qt]
263
- else
264
- # If this number has dimension and the other does not,
265
- # do math with this quantity type and the other as a big decimal
266
- [to_qt, other]
267
- end
268
- else
269
- # If this number has no dimension and the other does, convert this into a dimensionless quantity
270
- [to_qt, other]
271
- end
272
- end
273
-
274
- #
275
- # Compare two number items, taking into account any dimensions
276
- #
277
- # @param [NumberItem] other number item
278
- #
279
- # @return [-1,0,1] depending on if other object is less than, equal to or greater than self
280
- #
281
- def number_item_compare(other)
282
- if other.dimension
283
- logger.trace('Other is dimensioned, converting self and other to QuantityTypes to compare')
284
- to_qt <=> other.to_qt
285
- else
286
- @number_item.state <=> other.state
287
- end
288
- end
289
-
290
- #
291
- # Coerce from a numberic object depnding on dimension and state
292
- #
293
- # @param [Numeric] other numeric object to convert
294
- #
295
- # @return [Array<QuantityType,QuantityType>,Array<BigDecimal,BigDecimal>,nil] depending on
296
- # if this object has a dimension or state
297
- #
298
- def coerce_from_numeric(other)
299
- if dimension
300
- [Quantity.new(other), to_qt]
301
- elsif @number_item.state?
302
- [other.to_d, @number_item.state.to_big_decimal.to_d]
303
- end
304
- end
305
-
306
- #
307
- # Coerce when other is a quantity
308
- #
309
- # @param [QuantityType] other
310
- #
311
- # @return [Array<QuanityType,QuantityType] other and self as a quantity type
312
- #
313
- def coerce_from_quantity(other)
314
- as_qt = to_qt
315
- logger.trace("Converted #{self} to a Quantity #{as_qt}")
316
- [other, as_qt]
317
- end
318
- end
319
- end
320
- end
321
- end
322
- end
323
- # rubocop: enable Metrics/ClassLength
@@ -1,122 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bigdecimal'
4
- require 'forwardable'
5
- require 'java'
6
-
7
- module OpenHAB
8
- module Core
9
- module DSL
10
- module Items
11
- #
12
- # Delegator to OpenHAB String Item
13
- #
14
- class StringItem
15
- extend Forwardable
16
- include Comparable
17
-
18
- # @return [Regex] Regular expression matching blank strings
19
- BLANK_RE = /\A[[:space:]]*\z/.freeze
20
- private_constant :BLANK_RE
21
-
22
- def_delegator :@string_item, :to_s
23
-
24
- #
25
- # Create a new StringItem
26
- #
27
- # @param [Java::Org::openhab::core::library::items::StringItem] string_item OpenHAB string item to delegate to
28
- #
29
- def initialize(string_item)
30
- @string_item = string_item
31
- super()
32
- end
33
-
34
- #
35
- # Convert the StringItem into a String
36
- #
37
- # @return [String] String representation of the StringItem or
38
- # nil if underlying OpenHAB StringItem does not have a state
39
- #
40
- def to_str
41
- @string_item.state&.to_full_string&.to_s
42
- end
43
-
44
- #
45
- # Detect if the string is blank (not set or only whitespace)
46
- #
47
- # @return [Boolean] True if string item is not set or contains only whitespace, false otherwise
48
- #
49
- def blank?
50
- return true unless @string_item.state?
51
-
52
- @string_item.state.to_full_string.to_s.empty? || BLANK_RE.match?(self)
53
- end
54
-
55
- #
56
- # Check if StringItem is truthy? as per defined by library
57
- #
58
- # @return [Boolean] True if item is not in state UNDEF or NULL and value is not blank
59
- #
60
- def truthy?
61
- @string_item.state? && blank? == false
62
- end
63
-
64
- #
65
- # Compare StringItem to supplied object
66
- #
67
- # @param [Object] other object to compare to
68
- #
69
- # @return [Integer] -1,0,1 or nil depending on value supplied,
70
- # nil comparison to supplied object is not possible.
71
- #
72
- def <=>(other)
73
- case other
74
- when StringItem
75
- @string_item.state <=> other.state
76
- when String
77
- @string_item.state.to_s <=> other
78
- else
79
- @string_item.state <=> other
80
- end
81
- end
82
-
83
- #
84
- # Forward missing methods to Openhab String Item or String representation of the item if they are defined
85
- #
86
- # @param [String] meth method name
87
- # @param [Array] args arguments for method
88
- # @param [Proc] block <description>
89
- #
90
- # @return [Object] Value from delegated method in OpenHAB StringItem or Ruby String
91
- #
92
- def method_missing(meth, *args, &block)
93
- if @string_item.respond_to?(meth)
94
- @string_item.__send__(meth, *args, &block)
95
- elsif @string_item.state&.to_full_string&.to_s.respond_to?(meth)
96
- @string_item.state.to_full_string.to_s.__send__(meth, *args, &block)
97
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
98
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
99
- else
100
- super(meth, *args, &block)
101
- end
102
- end
103
-
104
- #
105
- # Checks if this method responds to the missing method
106
- #
107
- # @param [String] method_name Name of the method to check
108
- # @param [Boolean] _include_private boolean if private methods should be checked
109
- #
110
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
111
- #
112
- def respond_to_missing?(method_name, _include_private = false)
113
- @string_item.respond_to?(method_name) ||
114
- @string_item.state&.to_full_string&.to_s.respond_to?(method_name) ||
115
- ::Kernel.method_defined?(method_name) ||
116
- ::Kernel.private_method_defined?(method_name)
117
- end
118
- end
119
- end
120
- end
121
- end
122
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Monkey patch actions
4
- require 'openhab/core/dsl/monkey_patch/actions/script_thing_actions'