openhab-scripting 2.14.3 → 2.16.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) 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 -54
  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 -47
  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 -318
  67. data/lib/openhab/core/dsl/items/string_item.rb +0 -120
  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 -167
  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 -130
  78. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
  79. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
  80. data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
  81. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
  82. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -6
  83. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -24
  84. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  85. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  86. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  87. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -7
  88. data/lib/openhab/core/dsl/property.rb +0 -96
  89. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -348
  90. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  91. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  92. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  93. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  94. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  95. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  96. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  97. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  98. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  99. data/lib/openhab/core/dsl/states.rb +0 -63
  100. data/lib/openhab/core/dsl/things.rb +0 -93
  101. data/lib/openhab/core/dsl/time_of_day.rb +0 -229
  102. data/lib/openhab/core/dsl/timers.rb +0 -79
  103. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  104. data/lib/openhab/core/dsl/units.rb +0 -41
  105. data/lib/openhab/core/log.rb +0 -170
  106. data/lib/openhab/core/patch_load_path.rb +0 -7
  107. data/lib/openhab/core/startup_delay.rb +0 -23
  108. data/lib/openhab/osgi.rb +0 -59
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Monkey patch Group Item
5
- #
6
- # rubocop:disable Style/ClassAndModuleChildren
7
- class Java::OrgOpenhabCoreItems::GroupItem
8
- # rubocop:enable Style/ClassAndModuleChildren
9
-
10
- #
11
- # Get all items in a group
12
- #
13
- # @return [Array] Array of items in the group
14
- #
15
- def items
16
- to_a
17
- end
18
-
19
- #
20
- # Get all items in the group as an Array
21
- #
22
- # @return [Array] All items in the group
23
- #
24
- def to_a
25
- all_members.each_with_object([]) { |item, arr| arr << item }
26
- end
27
- end
@@ -1,130 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/core/log'
5
- require 'bigdecimal'
6
-
7
- # Monkey patch items
8
- require 'openhab/core/dsl/monkey_patch/items/metadata'
9
- require 'openhab/core/dsl/monkey_patch/items/contact_item'
10
- require 'openhab/core/dsl/monkey_patch/items/dimmer_item'
11
- require 'openhab/core/dsl/monkey_patch/items/switch_item'
12
- require 'openhab/core/dsl/monkey_patch/items/group_item'
13
-
14
- module OpenHAB
15
- module Core
16
- module DSL
17
- module MonkeyPatch
18
- #
19
- # Monkeypatches Items
20
- #
21
- module Items
22
- #
23
- # Extensions for Items
24
- #
25
- module ItemExtensions
26
- include Logging
27
- java_import org.openhab.core.model.script.actions.BusEvent
28
- java_import org.openhab.core.types.UnDefType
29
-
30
- #
31
- # Send a command to this item
32
- #
33
- # @param [Object] command to send to object
34
- #
35
- #
36
- def command(command)
37
- command = command.to_java.strip_trailing_zeros if command.is_a? BigDecimal
38
- logger.trace "Sending Command #{command} to #{id}"
39
- BusEvent.sendCommand(self, command.to_s)
40
- end
41
-
42
- alias << command
43
-
44
- #
45
- # Send an update to this item
46
- #
47
- # @param [Object] update the item
48
- #
49
- #
50
- def update(update)
51
- logger.trace "Sending Update #{update} to #{id}"
52
- BusEvent.postUpdate(self, update.to_s)
53
- end
54
-
55
- #
56
- # Check if the item state == UNDEF
57
- #
58
- # @return [Boolean] True if the state is UNDEF, false otherwise
59
- #
60
- def undef?
61
- # Need to explicitly call the super method version because this state will never return UNDEF
62
- method(:state).super_method.call == UnDefType::UNDEF
63
- end
64
-
65
- #
66
- # Check if the item state == NULL
67
- #
68
- # @return [Boolean] True if the state is NULL, false otherwise
69
- def null?
70
- # Need to explicitly call the super method version because this state will never return NULL
71
- method(:state).super_method.call == UnDefType::NULL
72
- end
73
-
74
- #
75
- # Check if the item has a state (not UNDEF or NULL)
76
- #
77
- # @return [Boolean] True if state is not UNDEF or NULL
78
- #
79
- def state?
80
- undef? == false && null? == false
81
- end
82
-
83
- #
84
- # Get the item state
85
- #
86
- # @return [State] OpenHAB item state if state is not UNDEF or NULL, nil otherwise
87
- #
88
- def state
89
- super if state?
90
- end
91
-
92
- #
93
- # Get an ID for the item, using the item label if set, otherwise item name
94
- #
95
- # @return [String] label if set otherwise name
96
- #
97
- def id
98
- label || name
99
- end
100
-
101
- #
102
- # Get the string representation of the state of the item
103
- #
104
- # @return [String] State of the item as a string if not UNDEF or NULL, nil otherwise
105
- #
106
- def to_s
107
- state&.to_s
108
- end
109
-
110
- #
111
- # Inspect the item
112
- #
113
- # @return [String] details of the item
114
- #
115
- def inspect
116
- toString
117
- end
118
- end
119
- end
120
- end
121
- end
122
- end
123
- end
124
-
125
- # rubocop:disable Style/ClassAndModuleChildren
126
- class Java::OrgOpenhabCoreItems::GenericItem
127
- # rubocop:enable Style/ClassAndModuleChildren
128
- prepend OpenHAB::Core::DSL::MonkeyPatch::Items::ItemExtensions
129
- prepend OpenHAB::Core::DSL::MonkeyPatch::Items::Metadata
130
- end
@@ -1,283 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'delegate'
5
- require 'pp'
6
- require 'forwardable'
7
- require 'openhab/osgi'
8
- require 'openhab/core/log'
9
-
10
- module OpenHAB
11
- module Core
12
- module DSL
13
- module MonkeyPatch
14
- module Items
15
- #
16
- # Metadata extension for Items
17
- #
18
- module Metadata
19
- include Logging
20
-
21
- java_import org.openhab.core.items.Metadata
22
- java_import org.openhab.core.items.MetadataKey
23
-
24
- #
25
- # Provide the interface to access namespace's value and configuration
26
- #
27
- class MetadataItem < SimpleDelegator
28
- extend Forwardable
29
-
30
- def_delegator :@metadata, :value
31
-
32
- def initialize(metadata: nil, key: nil, value: nil, config: nil)
33
- @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value, config)
34
- super(@metadata&.configuration)
35
- end
36
-
37
- #
38
- # Updates the metadata configuration associated with the key
39
- #
40
- def []=(key, value)
41
- configuration = {}.merge(@metadata&.configuration || {}).merge({ key => value })
42
- metadata = Metadata.new(@metadata&.uID, @metadata&.value, configuration)
43
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
44
- end
45
-
46
- #
47
- # Delete the configuration with the given key
48
- #
49
- # @return [Java::Org::openhab::core::items::Metadata] the old metadata
50
- #
51
- def delete(key)
52
- configuration = {}.merge(@metadata&.configuration || {})
53
- configuration.delete(key)
54
- metadata = Metadata.new(@metadata&.uID, @metadata&.value, configuration)
55
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
56
- end
57
-
58
- #
59
- # Set the metadata value
60
- #
61
- # @return [Java::Org::openhab::core::items::Metadata] the old metadata
62
- #
63
- def value=(value)
64
- raise ArgumentError, 'Value must be a string' unless value.is_a? String
65
-
66
- metadata = Metadata.new(@metadata&.uID, value, @metadata&.configuration)
67
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
68
- end
69
-
70
- #
71
- # Set the entire configuration to a hash
72
- #
73
- # @return [Java::Org::openhab::core::items::Metadata] the old metadata
74
- #
75
- def config=(config)
76
- raise ArgumentError, 'Configuration must be a hash' unless config.is_a? Hash
77
-
78
- metadata = Metadata.new(@metadata&.uID, @metadata&.value, config)
79
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
80
- end
81
- alias configuration= config=
82
-
83
- #
84
- # Convert the metadata to an array
85
- #
86
- # @return [Array[2]] An array of [value, configuration]
87
- #
88
- def to_a
89
- [@metadata&.value, @metadata&.configuration || {}]
90
- end
91
- end
92
-
93
- #
94
- # Provide the interface to access item metadata
95
- #
96
- class NamespaceAccessor
97
- include Enumerable
98
-
99
- def initialize(item_name:)
100
- @item_name = item_name
101
- end
102
-
103
- #
104
- # Return the metadata namespace
105
- #
106
- # @return [OpenHAB::Core::DSL::MonkeyPatch::Items::MetadataItem], or nil if the namespace doesn't exist
107
- #
108
- def [](namespace)
109
- logger.trace("Namespaces (#{NamespaceAccessor.registry.getAll})")
110
- logger.trace("Namespace (#{NamespaceAccessor.registry.get(MetadataKey.new(namespace, @item_name))})")
111
- metadata = NamespaceAccessor.registry.get(MetadataKey.new(namespace, @item_name))
112
- MetadataItem.new(metadata: metadata) if metadata
113
- end
114
-
115
- #
116
- # Set the metadata namespace. If the namespace does not exist, it will be created
117
- #
118
- # @param value [Object] The assigned value can be a OpenHAB::Core::DSL::MonkeyPatch::Items::MetadataItem,
119
- # Java::Org::openhab::core::items::Metadata, Array[2] of [value, configuration],
120
- # A String to set the value and clear the configuration,
121
- # or a Hash to set the configuration and set the value to nil
122
- #
123
- # @return [OpenHAB::Core::DSL::MonkeyPatch::Items::MetadataItem]
124
- #
125
- def []=(namespace, value)
126
- meta_value, configuration = update_from_value(value)
127
-
128
- key = MetadataKey.new(namespace, @item_name)
129
- metadata = Metadata.new(key, meta_value, configuration)
130
- # registry.get can be omitted, but registry.update will log a warning for nonexistent metadata
131
- if NamespaceAccessor.registry.get(key)
132
- NamespaceAccessor.registry.update(metadata)
133
- else
134
- NamespaceAccessor.registry.add(metadata)
135
- end
136
- end
137
-
138
- #
139
- # Enumerates through all the namespaces
140
- #
141
- def each
142
- return unless block_given?
143
-
144
- NamespaceAccessor.registry.getAll.each do |meta|
145
- yield meta.uID.namespace, meta.value, meta.configuration if meta.uID.itemName == @item_name
146
- end
147
- end
148
-
149
- #
150
- # Remove all the namespaces
151
- #
152
- def clear
153
- NamespaceAccessor.registry.removeItemMetadata @item_name
154
- end
155
-
156
- #
157
- # Delete a specific namespace
158
- #
159
- # @param namespace [String] The namespace to delete
160
- #
161
- def delete(namespace)
162
- NamespaceAccessor.registry.remove(MetadataKey.new(namespace, @item_name))
163
- end
164
-
165
- alias delete_all clear
166
-
167
- #
168
- # @return [Boolean] True if the given namespace exists, false otherwise
169
- #
170
- def key?(namespace)
171
- !NamespaceAccessor.registry.get(MetadataKey.new(namespace, @item_name)).nil?
172
- end
173
-
174
- alias has_key? key?
175
- alias include? key?
176
-
177
- #
178
- # Merge the given hash with the current metadata. Existing namespace that matches the name
179
- # of the new namespace will be overwritten. Others will be added.
180
- #
181
- def merge!(*others)
182
- return self if others.empty?
183
-
184
- others.each do |other|
185
- case other
186
- when Hash then merge_hash!(other)
187
- when self.class then merge_metadata!(other)
188
- else raise ArgumentError, "merge only supports Hash, or another item's metadata"
189
- end
190
- end
191
- self
192
- end
193
-
194
- #
195
- # @return [String] the string representation of all the namespaces with their value and config
196
- #
197
- def to_s
198
- namespaces = []
199
- each { |ns, value, config| namespaces << "\"#{ns}\"=>[\"#{value}\",#{config}]" }
200
- "{#{namespaces.join(',')}}"
201
- end
202
-
203
- #
204
- # @return [Java::org::openhab::core::items::MetadataRegistry]
205
- #
206
- def self.registry
207
- @registry ||= OpenHAB::OSGI.service('org.openhab.core.items.MetadataRegistry')
208
- end
209
-
210
- private
211
-
212
- #
213
- # perform an updated based on the supplied value
214
- #
215
- # @param [MetadataItem,Metadata,Array,Hash] value to perform update from
216
- #
217
- # @return [Array<Object,Hash>] Array containing the value and configuration based on the
218
- # the supplied object
219
- #
220
- def update_from_value(value)
221
- case value
222
- when MetadataItem then [value.value, value.__getobj__]
223
- when Metadata then [value.value, value.configuration]
224
- when Array
225
- raise ArgumentError, 'Array must contain 2 elements: value, config' if value.length != 2
226
-
227
- value
228
- when Hash then [nil, value]
229
- else [value, nil]
230
- end
231
- end
232
-
233
- #
234
- # Merge the metadata from the supplied other metadata object
235
- #
236
- # @param [Hash] other metadata object to merge
237
- # @yield [key, current_metadata, new_meta] to process merge
238
- #
239
- #
240
- def merge_metadata!(other)
241
- other.each do |key, new_value, new_config|
242
- new_meta = new_value, new_config
243
- if block_given?
244
- current_meta = self[key]&.to_a
245
- new_meta = yield key, current_meta, new_meta unless current_meta.nil?
246
- end
247
- self[key] = new_meta
248
- end
249
- end
250
-
251
- #
252
- # Merge a hash into the metadata
253
- #
254
- # @param [Hash] other to merge into metadata
255
- # @yield [key, current_metadata, new_meta] to process merge
256
- #
257
- #
258
- def merge_hash!(other)
259
- other.each do |key, new_meta|
260
- if block_given?
261
- current_meta = self[key]&.to_a
262
- new_meta = yield key, current_meta, new_meta unless current_meta.nil?
263
- end
264
- self[key] = new_meta
265
- end
266
- end
267
- end
268
-
269
- #
270
- # Accessor to the item's metadata
271
- #
272
- # @return [NamespaceAccessor] an Enumerable object to access item's namespaces
273
- #
274
- def meta
275
- @meta ||= NamespaceAccessor.new(item_name: name)
276
- end
277
- alias metadata meta
278
- end
279
- end
280
- end
281
- end
282
- end
283
- end
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- java_import org.openhab.core.library.items.SwitchItem
4
-
5
- # Alias class names for easy is_a? comparisons
6
- Switch = SwitchItem
7
-
8
- #
9
- # Monkeypatching SwitchItem to add Ruby Support methods
10
- #
11
- # rubocop:disable Style/ClassAndModuleChildren
12
- class Java::OrgOpenhabCoreLibraryItems::SwitchItem
13
- java_import org.openhab.core.library.types.OnOffType
14
- # rubocop:enable Style/ClassAndModuleChildren
15
-
16
- #
17
- # Send the OFF command to the switch
18
- #
19
- #
20
- def off
21
- command(OnOffType::OFF)
22
- end
23
-
24
- #
25
- # Send the OFF command to the switch
26
- #
27
- #
28
- def on
29
- command(OnOffType::ON)
30
- end
31
-
32
- #
33
- # Check if a switch is on
34
- #
35
- # @return [Boolean] True if the switch is on, false otherwise
36
- #
37
- def on?
38
- state? && state == OnOffType::ON
39
- end
40
-
41
- alias truthy? on?
42
-
43
- #
44
- # Check if a switch is off
45
- #
46
- # @return [Boolean] True if the switch is off, false otherwise
47
- #
48
- def off?
49
- state? && state == OnOffType::OFF
50
- end
51
-
52
- #
53
- # Send a command to invert the state of the switch
54
- #
55
- # @return [OnOffType] Inverted state
56
- #
57
- def toggle
58
- self << !self
59
- end
60
-
61
- #
62
- # Return the inverted state of the switch: ON if the switch is OFF, UNDEF or NULL; OFF if the switch is ON
63
- #
64
- # @return [OnOffType] Inverted state
65
- #
66
- def !
67
- return !state if state?
68
-
69
- OnOffType::ON
70
- end
71
-
72
- #
73
- # Check for equality against supplied object
74
- #
75
- # @param [Object] other object to compare to
76
- #
77
- # @return [Boolean] True if other is a OnOffType and other equals state for this switch item,
78
- # otherwise result from super
79
- #
80
- def ==(other)
81
- if other.is_a? OnOffType
82
- state? && state == other
83
- else
84
- super
85
- end
86
- end
87
- end