openhab-scripting 2.16.2 → 2.19.0

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 (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,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,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OpenHAB
4
- module Core
5
- module DSL
6
- module MonkeyPatch
7
- module Items
8
- #
9
- # Persistence extension for Items
10
- #
11
- module Persistence
12
- %w[persist last_update].each do |method|
13
- define_method(method) do |service = nil|
14
- service ||= persistence_service
15
- PersistenceExtensions.public_send(method, self, service&.to_s)
16
- end
17
- end
18
-
19
- #
20
- # Return the previous state of the item
21
- #
22
- # @param skip_equal [Boolean] if true, skips equal state values and
23
- # searches the first state not equal the current state
24
- # @param service [String] the name of the PersistenceService to use
25
- #
26
- # @return the previous state or nil if no previous state could be found,
27
- # or if the default persistence service is not configured or
28
- # does not refer to a valid service
29
- #
30
- def previous_state(service = nil, skip_equal: false)
31
- service ||= persistence_service
32
- PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
33
- end
34
-
35
- %w[
36
- average_since
37
- changed_since
38
- delta_since
39
- deviation_since
40
- evolution_rate
41
- historic_state
42
- maximum_since
43
- minimum_since
44
- sum_since
45
- updated_since
46
- variance_since
47
- ].each do |method|
48
- define_method(method) do |timestamp, service = nil|
49
- service ||= persistence_service
50
- if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
51
- timestamp = Java::JavaTime::ZonedDateTime.now.minus(timestamp)
52
- end
53
- PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
54
- end
55
- end
56
-
57
- private
58
-
59
- #
60
- # Get the specified persistence service from the current thread local variable
61
- #
62
- # @return [Object] Persistence service name as String or Symbol, or nil if not set
63
- #
64
- def persistence_service
65
- Thread.current.thread_variable_get(:persistence_service)
66
- end
67
- end
68
- end
69
- end
70
- end
71
- end
72
- 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
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OpenHAB
4
- module Core
5
- module DSL
6
- module MonkeyPatch
7
- module Ruby
8
- #
9
- # Extend integer to create duration object
10
- #
11
- module IntegerExtensions
12
- include OpenHAB::Core
13
-
14
- #
15
- # Create Duration with the specified unit
16
- #
17
- # @return [Java::JavaTime::Duration] Duration with number of units from self
18
- #
19
- %w[millis seconds minutes hours].each do |unit|
20
- define_method(unit) { Java::JavaTime::Duration.public_send("of_#{unit}", self) }
21
- end
22
-
23
- alias second seconds
24
- alias millisecond millis
25
- alias milliseconds millis
26
- alias ms millis
27
- alias minute minutes
28
- alias hour hours
29
- end
30
- end
31
- end
32
- end
33
- end
34
- end
35
-
36
- #
37
- # Prepend Integer class with duration extensions
38
- #
39
- class Integer
40
- prepend OpenHAB::Core::DSL::MonkeyPatch::Ruby::IntegerExtensions
41
- end