openhab-scripting 2.16.0 → 2.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab.rb +12 -16
  3. data/lib/openhab/core/entity_lookup.rb +162 -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 +47 -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/items.rb +46 -0
  11. data/lib/openhab/dsl/items/number_item.rb +352 -0
  12. data/lib/openhab/dsl/items/string_item.rb +120 -0
  13. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
  14. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
  15. data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
  16. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
  17. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
  18. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
  19. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
  20. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
  21. data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
  22. data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
  23. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
  24. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
  25. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
  26. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
  27. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
  28. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +7 -0
  29. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
  30. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
  31. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
  32. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
  33. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
  34. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
  35. data/lib/openhab/dsl/monkey_patch/types/types.rb +8 -0
  36. data/lib/openhab/dsl/persistence.rb +25 -0
  37. data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
  38. data/lib/openhab/dsl/rules/guard.rb +134 -0
  39. data/lib/openhab/dsl/rules/property.rb +102 -0
  40. data/lib/openhab/dsl/rules/rule.rb +116 -0
  41. data/lib/openhab/dsl/rules/rule_config.rb +151 -0
  42. data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
  43. data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
  44. data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
  45. data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
  46. data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
  47. data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
  48. data/lib/openhab/dsl/states.rb +61 -0
  49. data/lib/openhab/dsl/things.rb +91 -0
  50. data/lib/openhab/dsl/time_of_day.rb +228 -0
  51. data/lib/openhab/dsl/timers.rb +77 -0
  52. data/lib/openhab/dsl/types/quantity.rb +290 -0
  53. data/lib/openhab/dsl/units.rb +39 -0
  54. data/lib/openhab/log/configuration.rb +21 -0
  55. data/lib/openhab/log/logger.rb +172 -0
  56. data/lib/openhab/version.rb +1 -1
  57. metadata +55 -58
  58. data/lib/openhab/configuration.rb +0 -16
  59. data/lib/openhab/core/cron.rb +0 -27
  60. data/lib/openhab/core/debug.rb +0 -34
  61. data/lib/openhab/core/dsl.rb +0 -51
  62. data/lib/openhab/core/dsl/actions.rb +0 -107
  63. data/lib/openhab/core/dsl/entities.rb +0 -140
  64. data/lib/openhab/core/dsl/group.rb +0 -93
  65. data/lib/openhab/core/dsl/items/items.rb +0 -51
  66. data/lib/openhab/core/dsl/items/number_item.rb +0 -323
  67. data/lib/openhab/core/dsl/items/string_item.rb +0 -122
  68. data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
  69. data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
  70. data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
  71. data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
  72. data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
  73. data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
  74. data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
  75. data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
  76. data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
  77. data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
  78. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
  79. data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
  80. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
  81. data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
  82. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
  83. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
  84. data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
  85. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
  86. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  87. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  88. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  89. data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
  90. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
  91. data/lib/openhab/core/dsl/persistence.rb +0 -27
  92. data/lib/openhab/core/dsl/property.rb +0 -96
  93. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -348
  94. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  95. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  96. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  97. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  98. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  99. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  100. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  101. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  102. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  103. data/lib/openhab/core/dsl/states.rb +0 -63
  104. data/lib/openhab/core/dsl/things.rb +0 -93
  105. data/lib/openhab/core/dsl/time_of_day.rb +0 -229
  106. data/lib/openhab/core/dsl/timers.rb +0 -79
  107. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  108. data/lib/openhab/core/dsl/units.rb +0 -41
  109. data/lib/openhab/core/log.rb +0 -170
  110. data/lib/openhab/core/patch_load_path.rb +0 -7
  111. data/lib/openhab/core/startup_delay.rb +0 -23
  112. data/lib/openhab/osgi.rb +0 -59
@@ -1,93 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'delegate'
4
- require 'forwardable'
5
- require 'openhab/core/dsl/entities'
6
-
7
- module OpenHAB
8
- module Core
9
- module DSL
10
- #
11
- # Provides access to OpenHAB Groups
12
- #
13
- module Groups
14
- #
15
- # Indicator struct interpreted by rules to trigger based on items contained in a group
16
- #
17
- GroupItems = Struct.new(:group, keyword_init: true)
18
-
19
- #
20
- # Provide access to groups as a set
21
- #
22
- class Groups < SimpleDelegator
23
- #
24
- # Get a OpenHAB Group by name
25
- # @param [String] name of the group to retrieve
26
- #
27
- # @return [Set] of OpenHAB Groups
28
- #
29
- def[](name)
30
- group = EntityLookup.lookup_item(name)
31
- group.is_a?(Group) ? group : nil
32
- end
33
- end
34
-
35
- #
36
- # Retreive all OpenHAB groups
37
- #
38
- # @return [Set] of OpenHAB Groups
39
- #
40
- def groups
41
- # rubocop: disable Style/GlobalVars
42
- Groups.new(EntityLookup.decorate_items($ir.items.select { |item| item.is_a? GroupItem }))
43
- # rubocop: enable Style/GlobalVars
44
- end
45
-
46
- # Group class that provides access to OpenHAB group object and delegates other methods to
47
- # a set of group items
48
- class Group < SimpleDelegator
49
- extend Forwardable
50
-
51
- java_import org.openhab.core.items.GroupItem
52
-
53
- # @return [org.openhab.core.items.GroupItem] OpenHAB Java Group Item
54
- attr_accessor :group
55
-
56
- # @!macro [attach] def_delegators
57
- # @!method $2
58
- # Forwards to org.openhab.core.items.GroupItem
59
- # @see org::openhab::core::items::GroupItem
60
- def_delegator :@group, :name
61
- def_delegator :@group, :label
62
-
63
- #
64
- # Gets members of this group that are themselves a group
65
- #
66
- # @return [Set] Set of members that are of type group
67
- #
68
- def groups
69
- group.members.grep(org.openhab.core.items.GroupItem)
70
- end
71
-
72
- #
73
- # Wraps the group in a struct, this method is intended to be called
74
- # as an indicator to the rule method that the user wishes to trigger
75
- # based on changes to group items
76
- #
77
- # @return [GroupItems] Indicator struct used by rules engine to trigger based on item changes
78
- #
79
- def items
80
- GroupItems.new(group: group)
81
- end
82
-
83
- #
84
- # @return [String] List of groups seperated by commas
85
- #
86
- def to_s
87
- "[#{map(&:to_s).join(',')}]"
88
- end
89
- end
90
- end
91
- end
92
- end
93
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/core/dsl/entities'
5
-
6
- module OpenHAB
7
- module Core
8
- module DSL
9
- #
10
- # Manages OpenHAB items
11
- #
12
- module Items
13
- #
14
- # Delegates to underlying set of all OpenHAB Items, provides convenience methods
15
- #
16
- class Items < SimpleDelegator
17
- # Fetches the named item from the the ItemRegistry
18
- # @param [String] name
19
- # @return Item from registry, nil if item missing or requested item is a Group Type
20
- def[](name)
21
- # rubocop: disable Style/GlobalVars
22
- item = $ir.getItem(name)
23
- # rubocop: enable Style/GlobalVars
24
- item.is_a?(GroupItem) ? nil : item
25
- rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
26
- nil
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
- # rubocop: disable Style/GlobalVars
34
- !$ir.getItems(name).empty?
35
- # rubocop: enable Style/GlobalVars
36
- end
37
- alias key? include?
38
- end
39
-
40
- java_import org.openhab.core.items.GroupItem
41
- # Fetches all non-group items from the item registry
42
- # @return [OpenHAB::Core::DSL::Items::Items]
43
- def items
44
- # rubocop: disable Style/GlobalVars
45
- Items.new(EntityLookup.decorate_items($ir.items.reject { |item| item.is_a? GroupItem }))
46
- # rubocop: enable Style/GlobalVars
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -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