openhab-scripting 5.18.1 → 5.19.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfc9ae60d0f82fc677a157ac0efd58c12fc597f5ac8f3d79d7aa9c472e2c5d3a
4
- data.tar.gz: b8bf1721d064b2f88554722518496aa6c53b89407a9fb212880972725e49901f
3
+ metadata.gz: 806939292f4f749b0fd1db95f1b99a35365f8de972e96b90bb824a217f151743
4
+ data.tar.gz: 783bd04b6bcb2d6fedef08d69aecb150f3578a9a6a011015b968cb4614949263
5
5
  SHA512:
6
- metadata.gz: 8e50f4ce2c5832a39b3ab9c7b7a489b22697a23b20ce803a633f28ea9b44ed2661a820cdefd00bc54c71cb212a2c1a1d96c35c413937cd3ac514aec55a4c13da
7
- data.tar.gz: a298851e7255074cb21d9fd4e495a8c8dc750425b0db3319ab17353c2e07106e0f669a40912bbe98cc6db9f252ff70ebe68b9c94fb7366c4b257200da13eefda
6
+ metadata.gz: 11e236b0c0c642d154e0340ce1330311fcaec8f60c180af58ec4bfc2fd013a2de64d54ae327316b90bf565f5255c76c1f3d810595225d9c8b22ff40c8fb12e7b
7
+ data.tar.gz: 47d995352bb04ced923551adcf16f7022823b2cf4e2aed9d758be688fa9a75cc40b0778700e2e0e04f15e8092453ae2c0323565de9dc73047faaeff37d0bd397
@@ -27,6 +27,11 @@ module OpenHAB
27
27
  def channel_uid
28
28
  Things::ChannelUID.new(channelUID)
29
29
  end
30
+
31
+ # @return [String]
32
+ def to_s
33
+ "#{item_name} -> #{channelUID}"
34
+ end
30
35
  end
31
36
  end
32
37
  end
@@ -288,7 +288,7 @@ module OpenHAB
288
288
  end
289
289
 
290
290
  # @!attribute channel [r]
291
- # Return the the channel this item is linked to. If an item is linked to more than one channel,
291
+ # Return the channel this item is linked to. If an item is linked to more than one channel,
292
292
  # this method only returns the first channel.
293
293
  #
294
294
  # @return [Things::Channel, nil]
@@ -320,7 +320,7 @@ module OpenHAB
320
320
  # @see unlink
321
321
  #
322
322
  def links
323
- ItemChannelLinks.new(self, Things::Links::Provider.registry.get_links(name))
323
+ ItemChannelLinks.new(name, Things::Links::Provider.registry.get_links(name))
324
324
  end
325
325
 
326
326
  #
@@ -12,13 +12,13 @@ module OpenHAB
12
12
  #
13
13
  class ItemChannelLinks < SimpleDelegator
14
14
  #
15
- # @param [Item] item The item that the links belong to
15
+ # @param [String, UID] owner The owner that the links belong to
16
16
  # @param [Set<ItemChannelLink>] links The set of links to delegate to
17
17
  #
18
18
  # @!visibility private
19
- def initialize(item, links)
19
+ def initialize(owner, links)
20
20
  super(links)
21
- @item = item
21
+ @owner = owner
22
22
  end
23
23
 
24
24
  #
@@ -27,7 +27,11 @@ module OpenHAB
27
27
  #
28
28
  def clear
29
29
  Things::Links::Provider.registry.all.each do |link|
30
- next unless link.item_name == @item.name
30
+ if @owner.is_a?(String)
31
+ next unless link.item_name == @owner
32
+ else
33
+ next unless link.linked_uid == @owner
34
+ end
31
35
 
32
36
  provider = Things::Links::Provider.registry.provider_for(link.uid)
33
37
  if provider.is_a?(ManagedProvider)
@@ -33,7 +33,7 @@ module OpenHAB
33
33
  # logger.info("The power usage exceeded its 15 min average)
34
34
  # end
35
35
  #
36
- # @example HistoricState
36
+ # @example PersistedState
37
37
  # max = Power_Usage.maximum_since(LocalTime::MIDNIGHT)
38
38
  # logger.info("Max power usage today: #{max}, at: #{max.timestamp})
39
39
  #
@@ -41,69 +41,50 @@ module OpenHAB
41
41
  GenericItem.prepend(self)
42
42
 
43
43
  #
44
- # A state class with an added timestamp attribute.
44
+ # A wrapper for {org.openhab.core.persistence.HistoricItem HistoricItem} that delegates to its state.
45
45
  #
46
- # This wraps {org.openhab.core.persistence.HistoricItem HistoricItem}
47
- # to allow implicitly treating the object as its state, and wrapping of
48
- # that state into a {QuantityType} as necessary.
46
+ # @example
47
+ # max = Power_Usage.maximum_since(LocalTime::MIDNIGHT)
48
+ # logger.info "Highest power usage: #{max} occurred at #{max.timestamp}" if max > 5 | "kW"
49
49
  #
50
- class HistoricState < SimpleDelegator
50
+ class PersistedState < SimpleDelegator
51
+ extend Forwardable
52
+
51
53
  # @!attribute [r] state
52
54
  # @return [Types::State]
53
55
  alias_method :state, :__getobj__
54
56
 
55
- def initialize(state, historic_item)
56
- @historic_item = historic_item
57
- super(state)
58
- end
59
-
60
57
  # @!attribute [r] timestamp
61
58
  # @return [ZonedDateTime]
62
- def timestamp
63
- @historic_item.timestamp
59
+
60
+ # @!attribute [r] name
61
+ # @return [String] Item name
62
+
63
+ delegate %i[timestamp name] => :@historic_item
64
+
65
+ def initialize(historic_item, state = nil)
66
+ @historic_item = historic_item
67
+ super(state || historic_item.state)
64
68
  end
65
69
  end
66
70
 
67
- # All persistence methods that could return a QuantityType
68
- QUANTITY_METHODS = %i[average_since
69
- delta_since
70
- deviation_since
71
- sum_since
72
- variance_since].freeze
73
-
74
- # All persistence methods that require a timestamp
75
- # Note the _between methods are automatically created from the _since methods
76
- PERSISTENCE_METHODS = (QUANTITY_METHODS +
77
- %i[ changed_since?
78
- count_since
79
- count_state_changes_since
80
- historic_state
81
- maximum_since
82
- minimum_since
83
- updated_since?])
84
-
85
- # @deprecated OH3.4 - in openHAB 4, just add :get_all_states_since and freeze the list above
86
- PERSISTENCE_METHODS << :get_all_states_since if OpenHAB::Core.version >= OpenHAB::Core::V4_0
87
- PERSISTENCE_METHODS.freeze
88
-
89
- private_constant :QUANTITY_METHODS, :PERSISTENCE_METHODS
90
-
91
- # @!method persist(service = nil)
92
- # Persists the state of the item
93
- # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
94
- # @return [void]
95
-
96
- # @!method last_update(service = nil)
97
- # Returns the time the item was last updated.
98
- # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
99
- # @return [ZonedDateTime, nil] The timestamp of the last update
71
+ # @deprecated Use {PersistedState} instead
72
+ HistoricState = PersistedState
100
73
 
101
74
  # @!method average_since(timestamp, service = nil)
102
75
  # Returns the average value of the item's state since the given time
103
76
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
104
77
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
105
78
  # @return [DecimalType, QuantityType, nil] The average value since `timestamp`,
106
- # or nil if no previous state could be found.
79
+ # or nil if no previous states could be found.
80
+
81
+ # @!method average_until(timestamp, service = nil)
82
+ # Returns the average value of the item's state between now until the given time
83
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
84
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
85
+ # @return [DecimalType, QuantityType, nil] The average value until `timestamp`,
86
+ # or nil if no future states could be found.
87
+ # @since openHAB 4.2
107
88
 
108
89
  # @!method average_between(start, finish, service = nil)
109
90
  # Returns the average value of the item's state between two points in time
@@ -111,14 +92,22 @@ module OpenHAB
111
92
  # @param [#to_zoned_date_time] finish The point in time to which to search
112
93
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
113
94
  # @return [DecimalType, QuantityType, nil] The average value between `start` and `finish`,
114
- # or nil if no previous state could be found.
95
+ # or nil if no states could be found.
115
96
 
116
97
  # @!method delta_since(timestamp, service = nil)
117
98
  # Returns the difference value of the item's state since the given time
118
99
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
119
100
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
120
101
  # @return [DecimalType, QuantityType, nil] The difference value since `timestamp`,
121
- # or nil if no previous state could be found.
102
+ # or nil if no previous states could be found.
103
+
104
+ # @!method delta_until(timestamp, service = nil)
105
+ # Returns the difference value of the item's state between now until the given time
106
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
107
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
108
+ # @return [DecimalType, QuantityType, nil] The difference value until `timestamp`,
109
+ # or nil if no future states could be found.
110
+ # @since openHAB 4.2
122
111
 
123
112
  # @!method delta_between(start, finish, service = nil)
124
113
  # Returns the difference value of the item's state between two points in time
@@ -126,14 +115,22 @@ module OpenHAB
126
115
  # @param [#to_zoned_date_time] finish The point in time to which to search
127
116
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
128
117
  # @return [DecimalType, QuantityType, nil] The difference value between `start` and `finish`,
129
- # or nil if no previous state could be found.
118
+ # or nil if no states could be found.
130
119
 
131
120
  # @!method deviation_since(timestamp, service = nil)
132
121
  # Returns the standard deviation of the item's state since the given time
133
122
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
134
123
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
135
124
  # @return [DecimalType, QuantityType, nil] The standard deviation since `timestamp`,
136
- # or nil if no previous state could be found.
125
+ # or nil if no previous states could be found.
126
+
127
+ # @!method deviation_until(timestamp, service = nil)
128
+ # Returns the standard deviation of the item's state beetween now until the given time
129
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
130
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
131
+ # @return [DecimalType, QuantityType, nil] The standard deviation until `timestamp`,
132
+ # or nil if no future states could be found.
133
+ # @since openHAB 4.2
137
134
 
138
135
  # @!method deviation_between(start, finish, service = nil)
139
136
  # Returns the standard deviation of the item's state between two points in time
@@ -141,14 +138,22 @@ module OpenHAB
141
138
  # @param [#to_zoned_date_time] finish The point in time to which to search
142
139
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
143
140
  # @return [DecimalType, QuantityType, nil] The standard deviation between `start` and `finish`,
144
- # or nil if no previous state could be found.
141
+ # or nil if no states could be found.
145
142
 
146
143
  # @!method sum_since(timestamp, service = nil)
147
144
  # Returns the sum of the item's state since the given time
148
145
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
149
146
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
150
147
  # @return [DecimalType, QuantityType, nil] The sum since `timestamp`,
151
- # or nil if no previous state could be found.
148
+ # or nil if no previous states could be found.
149
+
150
+ # @!method sum_until(timestamp, service = nil)
151
+ # Returns the sum of the item's state between now until the given time
152
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
153
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
154
+ # @return [DecimalType, QuantityType, nil] The sum until `timestamp`,
155
+ # or nil if no future states could be found.
156
+ # @since openHAB 4.2
152
157
 
153
158
  # @!method sum_between(start, finish, service = nil)
154
159
  # Returns the sum of the item's state between two points in time
@@ -156,14 +161,22 @@ module OpenHAB
156
161
  # @param [#to_zoned_date_time] finish The point in time to which to search
157
162
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
158
163
  # @return [DecimalType, QuantityType, nil] The sum between `start` and `finish`,
159
- # or nil if no previous state could be found.
164
+ # or nil if no states could be found.
160
165
 
161
166
  # @!method variance_since(timestamp, service = nil)
162
167
  # Returns the variance of the item's state since the given time
163
168
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
164
169
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
165
170
  # @return [DecimalType, QuantityType, nil] The variance since `timestamp`,
166
- # or nil if no previous state could be found.
171
+ # or nil if no previous states could be found.
172
+
173
+ # @!method variance_until(timestamp, service = nil)
174
+ # Returns the variance of the item's state between now until the given time
175
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
176
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
177
+ # @return [DecimalType, QuantityType, nil] The variance until `timestamp`,
178
+ # or nil if no future states could be found.
179
+ # @since openHAB 4.2
167
180
 
168
181
  # @!method variance_between(start, finish, service = nil)
169
182
  # Returns the variance of the item's state between two points in time
@@ -171,7 +184,7 @@ module OpenHAB
171
184
  # @param [#to_zoned_date_time] finish The point in time to which to search
172
185
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
173
186
  # @return [DecimalType, QuantityType, nil] The variance between `start` and `finish`,
174
- # or nil if no previous state could be found.
187
+ # or nil if no states could be found.
175
188
 
176
189
  # @!method changed_since?(timestamp, service = nil)
177
190
  # Whether the item's state has changed since the given time
@@ -179,6 +192,13 @@ module OpenHAB
179
192
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
180
193
  # @return [true,false] True if the item's state has changed since the given `timestamp`, False otherwise.
181
194
 
195
+ # @!method changed_until?(timestamp, service = nil)
196
+ # Whether the item's state has changed between now until the given time
197
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
198
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
199
+ # @return [true,false] True if the item's state has changed until the given `timestamp`, False otherwise.
200
+ # @since openHAB 4.2
201
+
182
202
  # @!method changed_between?(start, finish, service = nil)
183
203
  # Whether the item's state changed between two points in time
184
204
  # @param [#to_zoned_date_time] start The point in time from which to search
@@ -188,58 +208,111 @@ module OpenHAB
188
208
 
189
209
  # @!method evolution_rate(timestamp, service = nil)
190
210
  # Returns the evolution rate of the item's state
191
- # @return [DecimalType, QuantityType, nil] The evolution rate since `timestamp`,
192
- # or nil if no previous state could be found.
211
+ # @return [DecimalType, nil] The evolution rate or nil if no previous state could be found.
212
+ # @deprecated This method has been deprecated in openHAB 4.2.
213
+ # Use {#evolution_rate_since} or {#evolution_rate_between} instead.
193
214
  # @overload evolution_rate(timestamp, service = nil)
194
215
  # Returns the evolution rate of the item's state since the given time
195
216
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
196
217
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
197
- # @return [DecimalType, QuantityType, nil] The evolution rate since `timestamp`,
218
+ # @return [DecimalType, nil] The evolution rate since `timestamp`,
198
219
  # or nil if no previous state could be found.
220
+ # @deprecated In openHAB 4.2, use {#evolution_rate_since} instead
199
221
  # @overload evolution_rate(start, finish, service = nil)
200
222
  # Returns the evolution rate of the item's state between two points in time
201
223
  # @param [#to_zoned_date_time] start The point in time from which to search
202
224
  # @param [#to_zoned_date_time] finish The point in time to which to search
203
225
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
204
- # @return [DecimalType, QuantityType, nil] The evolution rate between `start` and `finish`,
226
+ # @return [DecimalType, nil] The evolution rate between `start` and `finish`,
205
227
  # or nil if no previous state could be found.
228
+ # @deprecated In openHAB 4.2, use {#evolution_rate_between} instead
229
+
230
+ # @!method evolution_rate_since(timestamp, service = nil)
231
+ # Returns the evolution rate of the item's state since the given time
232
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
233
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
234
+ # @return [DecimalType, nil] The evolution rate since `timestamp`,
235
+ # or nil if no previous states could be found.
236
+ # @since openHAB 4.2
237
+
238
+ # @!method evolution_rate_until(timestamp, service = nil)
239
+ # Returns the evolution rate of the item's state between now until the given time
240
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
241
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
242
+ # @return [DecimalType, nil] The evolution rate until `timestamp`,
243
+ # or nil if no future states could be found.
244
+ # @since openHAB 4.2
245
+
246
+ # @!method evolution_rate_between(start, finish, service = nil)
247
+ # Returns the evolution rate of the item's state between two points in time
248
+ # @param [#to_zoned_date_time] start The point in time from which to search
249
+ # @param [#to_zoned_date_time] finish The point in time to which to search
250
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
251
+ # @return [DecimalType, nil] The evolution rate between `start` and `finish`,
252
+ # or nil if no states could be found.
253
+ # @since openHAB 4.2
206
254
 
207
255
  # @!method historic_state(timestamp, service = nil)
208
256
  # Returns the the item's state at the given time
209
257
  # @param [#to_zoned_date_time] timestamp The point in time at which to search
210
258
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
211
- # @return [HistoricState, nil] The item's state at `timestamp`,
259
+ # @return [PersistedState, nil] The item's state at `timestamp`,
212
260
  # or nil if no previous state could be found.
261
+ # @deprecated In openHAB 4.2, use {#persisted_state} instead
262
+
263
+ # @!method persisted_state(timestamp, service = nil)
264
+ # Returns the the item's state at the given time
265
+ # @param [#to_zoned_date_time] timestamp The point in time at which to search
266
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
267
+ # @return [PersistedState, nil] The item's state at `timestamp`,
268
+ # or nil if no state could be found.
269
+ # @since openHAB 4.2
213
270
 
214
271
  # @!method maximum_since(timestamp, service = nil)
215
272
  # Returns the maximum value of the item's state since the given time
216
273
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
217
274
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
218
- # @return [HistoricState, nil] The maximum value since `timestamp`,
219
- # or nil if no previous state could be found.
275
+ # @return [PersistedState, nil] The maximum value since `timestamp`,
276
+ # or nil if no previous states could be found.
277
+
278
+ # @!method maximum_until(timestamp, service = nil)
279
+ # Returns the maximum value of the item's state between now until the given time
280
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
281
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
282
+ # @return [PersistedState, nil] The maximum value until `timestamp`,
283
+ # or nil if no future states could be found.
284
+ # @since openHAB 4.2
220
285
 
221
286
  # @!method maximum_between(start, finish, service = nil)
222
287
  # Returns the maximum value of the item's state between two points in time
223
288
  # @param [#to_zoned_date_time] start The point in time from which to search
224
289
  # @param [#to_zoned_date_time] finish The point in time to which to search
225
290
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
226
- # @return [HistoricState, nil] The maximum value between `start` and `finish`,
227
- # or nil if no previous state could be found.
291
+ # @return [PersistedState, nil] The maximum value between `start` and `finish`,
292
+ # or nil if no states could be found.
228
293
 
229
294
  # @!method minimum_since(timestamp, service = nil)
230
295
  # Returns the minimum value of the item's state since the given time
231
296
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
232
297
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
233
- # @return [HistoricState, nil] The minimum value since `timestamp`,
234
- # or nil if no previous state could be found.
298
+ # @return [PersistedState, nil] The minimum value since `timestamp`,
299
+ # or nil if no previous states could be found.
300
+
301
+ # @!method minimum_until(timestamp, service = nil)
302
+ # Returns the minimum value of the item's state between now until the given time
303
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
304
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
305
+ # @return [PersistedState, nil] The minimum value until `timestamp`,
306
+ # or nil if no future states could be found.
307
+ # @since openHAB 4.2
235
308
 
236
309
  # @!method minimum_between(start, finish, service = nil)
237
310
  # Returns the minimum value of the item's state between two points in time
238
311
  # @param [#to_zoned_date_time] start The point in time from which to search
239
312
  # @param [#to_zoned_date_time] finish The point in time to which to search
240
313
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
241
- # @return [HistoricState, nil] The minimum value between `start` and `finish`,
242
- # or nil if no previous state could be found.
314
+ # @return [PersistedState, nil] The minimum value between `start` and `finish`,
315
+ # or nil if no states could be found.
243
316
 
244
317
  # @!method updated_since?(timestamp, service = nil)
245
318
  # Whether the item's state has been updated since the given time
@@ -247,6 +320,13 @@ module OpenHAB
247
320
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
248
321
  # @return [true,false] True if the item's state has been updated since the given `timestamp`, False otherwise.
249
322
 
323
+ # @!method updated_until?(timestamp, service = nil)
324
+ # Whether the item's state will be updated between now until the given time
325
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
326
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
327
+ # @return [true,false] True if the item's state will be updated until the given `timestamp`, False otherwise.
328
+ # @since openHAB 4.2
329
+
250
330
  # @!method updated_between?(start, finish, service = nil)
251
331
  # Whether the item's state was updated between two points in time
252
332
  # @param [#to_zoned_date_time] start The point in time from which to search
@@ -260,8 +340,15 @@ module OpenHAB
260
340
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
261
341
  # @return [Integer] The number of values persisted for this item.
262
342
 
343
+ # @!method count_until(timestamp, service = nil)
344
+ # Returns the number of available data points between now until the given time.
345
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
346
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
347
+ # @return [Integer] The number of values persisted for this item.
348
+ # @since openHAB 4.2
349
+
263
350
  # @!method count_between(start, finish, service = nil)
264
- # Returns the number of available historic data points between two points in time.
351
+ # Returns the number of available data points between two points in time.
265
352
  # @param [#to_zoned_date_time] start The point in time from which to search
266
353
  # @param [#to_zoned_date_time] finish The point in time to which to search
267
354
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -273,83 +360,293 @@ module OpenHAB
273
360
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
274
361
  # @return [Integer] The number of values persisted for this item.
275
362
 
363
+ # @!method count_state_changes_until(timestamp, service = nil)
364
+ # Returns the number of changes in data points between now until the given time.
365
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
366
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
367
+ # @return [Integer] The number of values persisted for this item.
368
+ # @since openHAB 4.2
369
+
276
370
  # @!method count_state_changes_between(start, finish, service = nil)
277
- # Returns the number of changes in historic data points between two points in time.
371
+ # Returns the number of changes in data points between two points in time.
278
372
  # @param [#to_zoned_date_time] start The point in time from which to search
279
373
  # @param [#to_zoned_date_time] finish The point in time to which to search
280
374
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
281
375
  # @return [Integer] The number of values persisted for this item.
282
376
 
283
377
  # @!method all_states_since(timestamp, service = nil)
284
- # @since openHAB 4.0
285
378
  # Returns all the states from a point in time until now.
286
379
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
287
380
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
288
- # @return [Array<HistoricState>] An array of {HistoricState} persisted for this item.
381
+ # @return [Array<PersistedState>] An array of {PersistedState} persisted for this item.
382
+ # @since openHAB 4.0
383
+
384
+ # @!method all_states_until(timestamp, service = nil)
385
+ # Returns all the states between now until the given time.
386
+ # @param [#to_zoned_date_time] timestamp The point in time until which to search
387
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
388
+ # @return [Array<PersistedState>] An array of {PersistedState} persisted for this item.
389
+ # @since openHAB 4.2
289
390
 
290
391
  # @!method all_states_between(start, finish, service = nil)
291
- # @since openHAB 4.0
292
392
  # Returns all the states between two points in time.
293
393
  # @param [#to_zoned_date_time] start The point in time from which to search
294
394
  # @param [#to_zoned_date_time] finish The point in time to which to search
295
395
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
296
- # @return [Array<HistoricState>] An array of {HistoricState} persisted for this item.
396
+ # @return [Array<PersistedState>] An array of {PersistedState} persisted for this item.
397
+ # @since openHAB 4.0
398
+
399
+ # @!method remove_all_states_since(timestamp, service = nil)
400
+ # Removes persisted data points since a certain point in time.
401
+ # @param [#to_zoned_date_time] timestamp The point in time from which to remove
402
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
403
+ # @return [void]
404
+ # @since openHAB 4.2
405
+
406
+ # @!method remove_all_states_until(timestamp, service = nil)
407
+ # Removes persisted data points from now until the given point in time.
408
+ # @param [#to_zoned_date_time] timestamp The point in time until which to remove
409
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
410
+ # @return [void]
411
+ # @since openHAB 4.2
412
+
413
+ # @!method remove_all_states_between(start, finish, service = nil)
414
+ # Removes persisted data points between two points in time.
415
+ # @param [#to_zoned_date_time] start The point in time from which to remove
416
+ # @param [#to_zoned_date_time] finish The point in time to which to remove
417
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
418
+ # @return [void]
419
+ # @since openHAB 4.2
420
+
421
+ #
422
+ # Persist item state to the persistence service
423
+ #
424
+ # @overload persist(service = nil)
425
+ # Persists the current state of the item
426
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
427
+ # @return [void]
428
+ #
429
+ # @overload persist(timestamp, state, service = nil)
430
+ # Persists a state at a given timestamp
431
+ # @param [#to_zoned_date_time] timestamp The timestamp for the given state to be stored
432
+ # @param [Types::State] state The state to be stored
433
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
434
+ # @return [void]
435
+ # @since openHAB 4.2
436
+ #
437
+ # @overload persist(time_series, service = nil)
438
+ # Persists a time series
439
+ # @param [Types::TimeSeries] time_series The time series of states to be stored
440
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
441
+ # @return [void]
442
+ # @since openHAB 4.2
443
+ #
444
+ def persist(*args)
445
+ # @deprecated OH 4.1 this if block content can be removed when dropping OH 4.1 support
446
+ if Core.version < Core::V4_2
447
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..1)" if args.size > 1
448
+
449
+ service = args.last || persistence_service
450
+ Actions::PersistenceExtensions.persist(self, service&.to_s)
451
+ return
452
+ end
453
+
454
+ first_arg = args.first
455
+ if first_arg.is_a?(TimeSeries)
456
+ if args.size > 2
457
+ raise ArgumentError,
458
+ "wrong number of arguments to persist a time series (given #{args.size}, expected 1..2)"
459
+ end
460
+
461
+ service = args[1] || persistence_service
462
+ Actions::PersistenceExtensions.java_send :persist,
463
+ [Item.java_class, Types::TimeSeries.java_class, java.lang.String],
464
+ self,
465
+ first_arg,
466
+ service&.to_s
467
+ elsif first_arg.respond_to?(:to_zoned_date_time)
468
+ unless args.size.between?(2, 3)
469
+ raise ArgumentError, "wrong number of arguments to persist a state (given #{args.size}, expected 2..3)"
470
+ end
471
+
472
+ timestamp = first_arg.to_zoned_date_time
473
+ state = format_update(args[1])
474
+ service = args[2] || persistence_service
475
+ Actions::PersistenceExtensions.java_send :persist,
476
+ [Item.java_class,
477
+ ZonedDateTime.java_class,
478
+ org.openhab.core.types.State,
479
+ java.lang.String],
480
+ self,
481
+ timestamp,
482
+ state,
483
+ service&.to_s
484
+
485
+ else
486
+ if args.size > 1
487
+ raise ArgumentError,
488
+ "wrong number of arguments to persist the current state (given #{args.size}, expected 0..1)"
489
+ end
490
+ service = first_arg || persistence_service
491
+ Actions::PersistenceExtensions.java_send :persist,
492
+ [Item.java_class, java.lang.String],
493
+ self,
494
+ service&.to_s
297
495
 
298
- %i[persist last_update].each do |method|
299
- define_method(method) do |service = nil|
300
- service ||= persistence_service
301
- Actions::PersistenceExtensions.public_send(method, self, service&.to_s)
302
496
  end
303
497
  end
304
498
 
499
+ # @!method last_update(service = nil)
500
+ # Returns the time the item was last updated.
501
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
502
+ # @return [ZonedDateTime, nil] The timestamp of the last update
503
+
504
+ # @!method next_update(service = nil)
505
+ # Returns the first future update time of the item.
506
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
507
+ # @return [ZonedDateTime, nil] The timestamp of the next update
508
+ # @see last_update
509
+ # @since openHAB 4.2
510
+
511
+ %i[last_update next_update].each do |method|
512
+ # @deprecated OH 4.1 remove this guard when dropping OH 4.1
513
+ next unless Actions::PersistenceExtensions.respond_to?(method)
514
+
515
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
516
+ def #{method}(service = nil) # def last_update(service = nil)
517
+ service ||= persistence_service # service ||= persistence_service
518
+ result = Actions::PersistenceExtensions.#{method}( # result = Actions::PersistenceExtensions.last_update(
519
+ self, # self,
520
+ service&.to_s # service&.to_s
521
+ ) # )
522
+ wrap_result(result) # wrap_result(result)
523
+ end # end
524
+ RUBY
525
+ end
526
+
527
+ # @!method previous_state(service = nil, skip_equal: false)
528
+ # Return the previous state of the item
529
+ #
530
+ # @param skip_equal [true,false] if true, skips equal state values and
531
+ # searches the first state not equal the current state
532
+ # @param service [String] the name of the PersistenceService to use
305
533
  #
306
- # Return the previous state of the item
534
+ # @return [PersistedState, nil] the previous state or nil if no previous state could be found,
535
+ # or if the default persistence service is not configured or
536
+ # does not refer to a valid service
537
+
538
+ # @!method next_state(service = nil, skip_equal: false)
539
+ # Return the next state of the item
307
540
  #
308
- # @param skip_equal [true,false] if true, skips equal state values and
309
- # searches the first state not equal the current state
310
- # @param service [String] the name of the PersistenceService to use
541
+ # @param skip_equal [true,false] if true, skips equal state values and
542
+ # searches the first state not equal the current state
543
+ # @param service [String] the name of the PersistenceService to use
311
544
  #
312
- # @return [HistoricState, nil] the previous state or nil if no previous state could be found,
313
- # or if the default persistence service is not configured or
314
- # does not refer to a valid service
545
+ # @return [PersistedState, nil] the previous state or nil if no previous state could be found,
546
+ # or if the default persistence service is not configured or
547
+ # does not refer to a valid service
315
548
  #
316
- def previous_state(service = nil, skip_equal: false)
317
- service ||= persistence_service
318
- result = Actions::PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
319
- HistoricState.new(quantify(result.state), result) if result
549
+ # @since openHAB 4.2
550
+
551
+ %i[previous_state next_state].each do |method|
552
+ # @deprecated OH 4.1 remove this guard when dropping OH 4.1
553
+ next unless Actions::PersistenceExtensions.respond_to?(method)
554
+
555
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
556
+ def #{method}(service = nil, skip_equal: false) # def previous_state(service = nil, skip_equal: false)
557
+ service ||= persistence_service # service ||= persistence_service
558
+ result = Actions::PersistenceExtensions.#{method}( # result = Actions::PersistenceExtensions.previous_state(
559
+ self, # self,
560
+ skip_equal, # skip_equal,
561
+ service&.to_s # service&.to_s
562
+ ) # )
563
+ wrap_result(result, quantify: true) # wrap_result(result, quantify: true)
564
+ end # end
565
+ RUBY
320
566
  end
321
567
 
322
- PERSISTENCE_METHODS.each do |method|
323
- define_method(method) do |timestamp, service = nil|
324
- service ||= persistence_service
325
- result = Actions::PersistenceExtensions.public_send(
326
- method.to_s.delete_suffix("?"),
327
- self,
328
- timestamp.to_zoned_date_time,
329
- service&.to_s
330
- )
331
- wrap_result(result, method)
568
+ class << self
569
+ # @!visibility private
570
+ def def_persistence_method(method, quantify: false)
571
+ method = method.to_s.dup
572
+ suffix = method.delete_suffix!("?") && "?"
573
+
574
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
575
+ def #{method}#{suffix}(timestamp, service = nil) # def changed_since?(timestamp, service = nil)
576
+ service ||= persistence_service # service ||= persistence_service
577
+ result = Actions::PersistenceExtensions.#{method}( # result = Actions::PersistenceExtensions.changed_since(
578
+ self, # self,
579
+ timestamp.to_zoned_date_time, # timestamp.to_zoned_date_time,
580
+ service&.to_s # service&.to_s
581
+ ) # )
582
+ wrap_result(result, quantify: #{quantify}) # wrap_result(result, quantify: false)
583
+ end # end
584
+ RUBY
332
585
  end
333
586
 
334
- next unless /_since\??$/.match?(method.to_s)
587
+ # @!visibility private
588
+ def def_persistence_methods(method, quantify: false)
589
+ method = method.to_s.dup
590
+ suffix = method.delete_suffix!("?") && "?"
335
591
 
336
- between_method = method.to_s.sub("_since", "_between").to_sym
337
- define_method(between_method) do |start, finish, service = nil|
338
- service ||= persistence_service
339
- result = Actions::PersistenceExtensions.public_send(
340
- between_method.to_s.delete_suffix("?"),
341
- self,
342
- start.to_zoned_date_time,
343
- finish.to_zoned_date_time,
344
- service&.to_s
345
- )
346
- wrap_result(result, method)
592
+ def_persistence_method("#{method}_since#{suffix}", quantify: quantify)
593
+ # @deprecated OH 4.1 remove if guard, keeping the content, when dropping OH 4.1
594
+ if OpenHAB::Core.version >= OpenHAB::Core::V4_2
595
+ def_persistence_method("#{method}_until#{suffix}", quantify: quantify)
596
+ end
597
+
598
+ method = "#{method}_between"
599
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
600
+ def #{method}#{suffix}(start, finish, service = nil) # def changed_between?(start, finish, service = nil)
601
+ service ||= persistence_service # service ||= persistence_service
602
+ result = Actions::PersistenceExtensions.#{method}( # result = Actions::PersistenceExtensions.changed_between?(
603
+ self, # self,
604
+ start.to_zoned_date_time, # start.to_zoned_date_time,
605
+ finish.to_zoned_date_time, # finish.to_zoned_date_time,
606
+ service&.to_s # service&.to_s
607
+ ) # )
608
+ wrap_result(result, quantify: #{quantify}) # wrap_result(result, quantify: false)
609
+ end # end
610
+ RUBY
347
611
  end
348
612
  end
349
613
 
350
- # evolution_rate's "between" method is overloaded with the same name
351
- method = :evolution_rate
352
- define_method(method) do |start, finish_or_service = nil, service = nil|
614
+ def_persistence_methods(:average, quantify: true)
615
+ def_persistence_methods(:delta, quantify: true)
616
+ def_persistence_methods(:deviation, quantify: true)
617
+ def_persistence_methods(:sum, quantify: true)
618
+ def_persistence_methods(:variance, quantify: true)
619
+
620
+ def_persistence_methods(:changed?)
621
+ def_persistence_methods(:count)
622
+ def_persistence_methods(:count_state_changes)
623
+ alias_method :state_changes_since, :count_state_changes_since
624
+ alias_method :state_changes_until, :count_state_changes_until if OpenHAB::Core.version >= OpenHAB::Core::V4_2
625
+ alias_method :state_changes_between, :count_state_changes_between
626
+
627
+ # @deprecated OH 4.2 - this still exists in OH 4.2 but logs a deprecation warning
628
+ def_persistence_method(:historic_state, quantify: true)
629
+
630
+ def_persistence_methods(:maximum, quantify: true)
631
+ def_persistence_methods(:minimum, quantify: true)
632
+ def_persistence_methods(:updated?)
633
+
634
+ if OpenHAB::Core.version >= OpenHAB::Core::V4_0
635
+ def_persistence_methods(:get_all_states, quantify: true)
636
+ alias_method :all_states_since, :get_all_states_since
637
+ alias_method :all_states_until, :get_all_states_until if OpenHAB::Core.version >= OpenHAB::Core::V4_2
638
+ alias_method :all_states_between, :get_all_states_between
639
+ end
640
+
641
+ if OpenHAB::Core.version >= OpenHAB::Core::V4_2
642
+ def_persistence_method(:persisted_state) # already quantified in core
643
+
644
+ def_persistence_methods(:evolution_rate)
645
+ def_persistence_methods(:remove_all_states)
646
+ end
647
+
648
+ # @deprecated OH 4.2 this method is deprecated in OH 4.2 and may be removed in a future version
649
+ def evolution_rate(start, finish_or_service = nil, service = nil)
353
650
  if service.nil?
354
651
  if finish_or_service.respond_to?(:to_zoned_date_time)
355
652
  service = persistence_service
@@ -362,31 +659,20 @@ module OpenHAB
362
659
  finish = finish_or_service
363
660
  end
364
661
 
365
- result = if finish
366
- Actions::PersistenceExtensions.public_send(
367
- method,
368
- self,
369
- start.to_zoned_date_time,
370
- finish.to_zoned_date_time,
371
- service&.to_s
372
- )
373
- else
374
- Actions::PersistenceExtensions.public_send(
375
- method,
376
- self,
377
- start.to_zoned_date_time,
378
- service&.to_s
379
- )
380
- end
381
- wrap_result(result, method)
382
- end
383
-
384
- alias_method :state_changes_since, :count_state_changes_since
385
- alias_method :state_changes_between, :count_state_changes_between
386
- # @deprecated OH 3.4 - if guard is unnecessary in OH4
387
- if OpenHAB::Core.version >= OpenHAB::Core::V4_0
388
- alias_method :all_states_since, :get_all_states_since
389
- alias_method :all_states_between, :get_all_states_between
662
+ if finish
663
+ Actions::PersistenceExtensions.evolution_rate(
664
+ self,
665
+ start.to_zoned_date_time,
666
+ finish.to_zoned_date_time,
667
+ service&.to_s
668
+ )
669
+ else
670
+ Actions::PersistenceExtensions.java_send :evolutionRate,
671
+ [Item.java_class, ZonedDateTime.java_class, java.lang.String],
672
+ self,
673
+ start.to_zoned_date_time,
674
+ service&.to_s
675
+ end
390
676
  end
391
677
 
392
678
  private
@@ -398,9 +684,10 @@ module OpenHAB
398
684
  #
399
685
  # @return [Object] QuantityType or the original value
400
686
  #
687
+ # @deprecated OH 4.1 in OH4.2, quantify is no longer needed because it is done inside core
401
688
  def quantify(value)
402
689
  if value.is_a?(DecimalType) && respond_to?(:unit) && unit
403
- logger.trace("Unitizing #{value} with unit #{unit}")
690
+ logger.trace { "Unitizing #{value} with unit #{unit}" }
404
691
  QuantityType.new(value.to_big_decimal, unit)
405
692
  else
406
693
  value
@@ -411,23 +698,23 @@ module OpenHAB
411
698
  # Wrap the result into a more convenient object type depending on the method and result.
412
699
  #
413
700
  # @param [Object] result the raw result type to be wrapped
414
- # @param [Symbol] method the name of the called method
701
+ # @param [true, false] quantify whether to quantify the result
415
702
  #
416
- # @return [HistoricState] a {HistoricState} object if the result was a HistoricItem
703
+ # @return [PersistedState] a {PersistedState} object if the result was a HistoricItem
704
+ # @return [Array<PersistedState>] an array of {PersistedState} objects if the result was an array
705
+ # of HistoricItem
417
706
  # @return [QuantityType] a `QuantityType` object if the result was an average, delta, deviation,
418
- # sum, or variance.
419
- # @return [Array<HistoricState>] an array of {HistoricState} objects if the result was an array
420
- # of HistoricItem
707
+ # sum, or variance.
421
708
  # @return [Object] the original result object otherwise.
422
709
  #
423
- def wrap_result(result, method)
710
+ def wrap_result(result, quantify: false)
424
711
  case result
425
712
  when org.openhab.core.persistence.HistoricItem
426
- HistoricState.new(quantify(result.state), result)
713
+ PersistedState.new(result, quantify ? quantify(result.state) : nil)
427
714
  when java.util.Collection, Array
428
- result.to_a.map { |historic_item| wrap_result(historic_item, method) }
715
+ result.to_a.map { |historic_item| wrap_result(historic_item, quantify: quantify) }
429
716
  else
430
- return quantify(result) if QUANTITY_METHODS.include?(method)
717
+ return quantify(result) if quantify
431
718
 
432
719
  result
433
720
  end
@@ -57,6 +57,126 @@ module OpenHAB
57
57
  uid.to_s
58
58
  end
59
59
 
60
+ # @!attribute item_name [r]
61
+ # Return the name of the item this channel is linked to. If a channel is linked to more than one item,
62
+ # this method only returns the first item.
63
+ #
64
+ # @return [String, nil]
65
+ def item_name
66
+ item_names.first
67
+ end
68
+
69
+ # @!attribute item_names [r]
70
+ # Return the names of all of the items this channel is linked to.
71
+ #
72
+ # @return [Array<String>]
73
+ def item_names
74
+ Things::Links::Provider.registry.get_linked_item_names(uid)
75
+ end
76
+
77
+ # @!attribute item [r]
78
+ # Return the item this channel is linked to. If a channel is linked to more than one item,
79
+ # this method only returns the first item.
80
+ #
81
+ # @return [Items::Item, nil]
82
+ def item
83
+ items.first
84
+ end
85
+
86
+ # @!attribute items [r]
87
+ # Return all of the items this channel is linked to.
88
+ #
89
+ # @return [Array<Items::Item>]
90
+ def items
91
+ Things::Links::Provider.registry.get_linked_items(uid).map { |item| Items::Proxy.new(item) }
92
+ end
93
+
94
+ #
95
+ # @!attribute links [r]
96
+ # Returns all of the channel's links (items and link configurations).
97
+ #
98
+ # @return [Items::ItemChannelLinks] An array of ItemChannelLink or an empty array
99
+ #
100
+ # @example Get the configuration of the first link
101
+ # things["mqtt:topic:livingroom-light"].channel["power"].links.first.configuration
102
+ #
103
+ # @example Remove all managed links
104
+ # things["mqtt:topic:livingroom-light"].channel["power"].links.clear
105
+ #
106
+ # @see link
107
+ # @see unlink
108
+ #
109
+ def links
110
+ Items::ItemChannelLinks.new(uid, Things::Links::Provider.registry.get_links(uid))
111
+ end
112
+
113
+ #
114
+ # @return [ItemChannelLink, nil]
115
+ #
116
+ # @overload link
117
+ # Returns the channel's link. If an channel is linked to more than one item,
118
+ # this method only returns the first link.
119
+ #
120
+ # @return [Things::ItemChannelLink, nil]
121
+ #
122
+ # @overload link(item, config = {})
123
+ #
124
+ # Links the channel to an item.
125
+ #
126
+ # @param [String, Items::Item] channel The channel to link to.
127
+ # @param [Hash] config The configuration for the link.
128
+ #
129
+ # @return [Things::ItemChannelLink] The created link.
130
+ #
131
+ # @example Link a channel to an item
132
+ # things["mqtt:topic:livingroom-light"].channels["power"].link(LivingRoom_Light_Power)
133
+ #
134
+ # @example Specify a link configuration
135
+ # things["mqtt:topic:outdoor-thermometer"].channels["temperature"].link(
136
+ # High_Temperature_Alert,
137
+ # profile: "system:hysteresis",
138
+ # lower: "29 °C",
139
+ # upper: "30 °C")
140
+ #
141
+ # @see links
142
+ # @see unlink
143
+ #
144
+ def link(item = nil, config = nil)
145
+ return Things::Links::Provider.registry.get_links(uid).first if item.nil? && config.nil?
146
+
147
+ config ||= {}
148
+ Core::Things::Links::Provider.create_link(item, self, config).tap do |new_link|
149
+ provider = Core::Things::Links::Provider.current
150
+ if !(current_link = provider.get(new_link.uid))
151
+ provider.add(new_link)
152
+ elsif current_link.configuration != config
153
+ provider.update(new_link)
154
+ end
155
+ end
156
+ end
157
+
158
+ #
159
+ # Removes a link to an item from managed link providers.
160
+ #
161
+ # @param [String, Items::Item] item The item to remove the link to.
162
+ #
163
+ # @return [Things::ItemChannelLink, nil] The removed link, if found.
164
+ # @raise [FrozenError] if the link is not managed by a managed link provider.
165
+ #
166
+ # @see link
167
+ # @see links
168
+ #
169
+ def unlink(item)
170
+ link_to_delete = Things::Links::Provider.create_link(item, self, {})
171
+ provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid)
172
+ unless provider.is_a?(ManagedProvider)
173
+ raise FrozenError,
174
+ "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}"
175
+ end
176
+
177
+ provider.remove(link_to_delete.uid)
178
+ end
179
+
60
180
  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
61
181
  if Core.version < Core::V4_0
62
182
  # @!visibility private
@@ -28,7 +28,8 @@ module OpenHAB
28
28
  config = Configuration.new(config.transform_keys(&:to_s))
29
29
  channel = ChannelUID.new(channel) if channel.is_a?(String)
30
30
  channel = channel.uid if channel.is_a?(Channel)
31
- org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel, config)
31
+ item = item.name if item.is_a?(Item)
32
+ org.openhab.core.thing.link.ItemChannelLink.new(item, channel, config)
32
33
  end
33
34
  end
34
35
 
@@ -992,9 +992,14 @@ module OpenHAB
992
992
  #
993
993
  # Creates a channel linked trigger
994
994
  #
995
+ # @param [Item, String, nil] item The item to create a trigger for. If nil, all items are matched.
996
+ # @param [Core::Things::Channel, Core::Things::ChannelUID, String, nil] channel
997
+ # The channel to create a trigger for. If nil, all channels are matched.
995
998
  # @param [Object] attach object to be attached to the trigger
996
999
  # @return [void]
997
1000
  #
1001
+ # @since openHAB 4.0 Support for filtering with item and channel was added
1002
+ #
998
1003
  # @example
999
1004
  # rule "channel linked" do
1000
1005
  # channel_linked
@@ -1002,9 +1007,10 @@ module OpenHAB
1002
1007
  # logger.info("#{event.link.item.name} linked to #{event.link.channel_uid}.")
1003
1008
  # end
1004
1009
  # end
1005
- def channel_linked(attach: nil)
1006
- @ruby_triggers << [:channel_linked]
1007
- event("openhab/links/*/added", types: "ItemChannelLinkAddedEvent", attach: attach)
1010
+ def channel_linked(item: nil, channel: nil, attach: nil)
1011
+ pattern = (item.nil? && channel.nil?) ? "*" : "#{item || "*"}-#{channel || "*"}"
1012
+ @ruby_triggers << [:channel_linked, pattern]
1013
+ event("openhab/links/#{pattern}/added", types: "ItemChannelLinkAddedEvent", attach: attach)
1008
1014
  end
1009
1015
 
1010
1016
  #
@@ -1013,9 +1019,14 @@ module OpenHAB
1013
1019
  # Note that the item or the thing it's linked to may no longer exist,
1014
1020
  # so if you try to access those objects they'll be nil.
1015
1021
  #
1022
+ # @param [Item, String, nil] item The item to create a trigger for. If nil, all items are matched.
1023
+ # @param [Core::Things::Channel, Core::Things::ChannelUID, String, nil] channel
1024
+ # The channel to create a trigger for. If nil, all channels are matched.
1016
1025
  # @param [Object] attach object to be attached to the trigger
1017
1026
  # @return [void]
1018
1027
  #
1028
+ # @since openHAB 4.0 Support for filtering with item and channel was added
1029
+ #
1019
1030
  # @example
1020
1031
  # rule "channel unlinked" do
1021
1032
  # channel_unlinked
@@ -1023,9 +1034,10 @@ module OpenHAB
1023
1034
  # logger.info("#{event.link.item_name} unlinked from #{event.link.channel_uid}.")
1024
1035
  # end
1025
1036
  # end
1026
- def channel_unlinked(attach: nil)
1027
- @ruby_triggers << [:channel_linked]
1028
- event("openhab/links/*/removed", types: "ItemChannelLinkRemovedEvent", attach: attach)
1037
+ def channel_unlinked(item: nil, channel: nil, attach: nil)
1038
+ pattern = (item.nil? && channel.nil?) ? "*" : "#{item || "*"}-#{channel || "*"}"
1039
+ @ruby_triggers << [:channel_unlinked, pattern]
1040
+ event("openhab/links/#{pattern}/removed", types: "ItemChannelLinkRemovedEvent", attach: attach)
1029
1041
  end
1030
1042
 
1031
1043
  #
@@ -1556,9 +1568,12 @@ module OpenHAB
1556
1568
  #
1557
1569
  # Creates an item added trigger
1558
1570
  #
1571
+ # @param [String, nil] pattern The pattern to match items against
1559
1572
  # @param [Object] attach object to be attached to the trigger
1560
1573
  # @return [void]
1561
1574
  #
1575
+ # @since openHAB 4.0 Support for pattern filter was added
1576
+ #
1562
1577
  # @example
1563
1578
  # rule "item added" do
1564
1579
  # item_added
@@ -1566,17 +1581,20 @@ module OpenHAB
1566
1581
  # logger.info("#{event.item.name} added.")
1567
1582
  # end
1568
1583
  # end
1569
- def item_added(attach: nil)
1570
- @ruby_triggers << [:item_added]
1571
- event("openhab/items/*/added", types: "ItemAddedEvent", attach: attach)
1584
+ def item_added(pattern = "*", attach: nil)
1585
+ @ruby_triggers << [:item_added, pattern]
1586
+ event("openhab/items/#{pattern}/added", types: "ItemAddedEvent", attach: attach)
1572
1587
  end
1573
1588
 
1574
1589
  #
1575
1590
  # Creates an item removed trigger
1576
1591
  #
1592
+ # @param [String, nil] pattern The pattern to match items against
1577
1593
  # @param [Object] attach object to be attached to the trigger
1578
1594
  # @return [void]
1579
1595
  #
1596
+ # @since openHAB 4.0 Support for pattern filter was added
1597
+ #
1580
1598
  # @example
1581
1599
  # rule "item removed" do
1582
1600
  # item_removed
@@ -1584,14 +1602,15 @@ module OpenHAB
1584
1602
  # logger.info("#{event.item.name} removed.")
1585
1603
  # end
1586
1604
  # end
1587
- def item_removed(attach: nil)
1588
- @ruby_triggers << [:item_removed]
1589
- event("openhab/items/*/removed", types: "ItemRemovedEvent", attach: attach)
1605
+ def item_removed(pattern = "*", attach: nil)
1606
+ @ruby_triggers << [:item_removed, pattern]
1607
+ event("openhab/items/#{pattern}/removed", types: "ItemRemovedEvent", attach: attach)
1590
1608
  end
1591
1609
 
1592
1610
  #
1593
1611
  # Creates an item updated trigger
1594
1612
  #
1613
+ # @param [String, nil] pattern The pattern to match items against
1595
1614
  # @param [Object] attach object to be attached to the trigger
1596
1615
  # @return [void]
1597
1616
  #
@@ -1603,17 +1622,20 @@ module OpenHAB
1603
1622
  # end
1604
1623
  # end
1605
1624
  #
1606
- def item_updated(attach: nil)
1607
- @ruby_triggers << [:item_updated]
1608
- event("openhab/items/*/updated", types: "ItemUpdatedEvent", attach: attach)
1625
+ def item_updated(pattern = "*", attach: nil)
1626
+ @ruby_triggers << [:item_updated, pattern]
1627
+ event("openhab/items/#{pattern}/updated", types: "ItemUpdatedEvent", attach: attach)
1609
1628
  end
1610
1629
 
1611
1630
  #
1612
1631
  # Creates a thing added trigger
1613
1632
  #
1633
+ # @param [String, nil] pattern The pattern to match things against
1614
1634
  # @param [Object] attach object to be attached to the trigger
1615
1635
  # @return [void]
1616
1636
  #
1637
+ # @since openHAB 4.0 Support for pattern filter was added
1638
+ #
1617
1639
  # @example
1618
1640
  # rule "thing added" do
1619
1641
  # thing_added
@@ -1621,17 +1643,20 @@ module OpenHAB
1621
1643
  # logger.info("#{event.thing.uid} added.")
1622
1644
  # end
1623
1645
  # end
1624
- def thing_added(attach: nil)
1625
- @ruby_triggers << [:thing_added]
1626
- event("openhab/things/*/added", types: "ThingAddedEvent", attach: attach)
1646
+ def thing_added(pattern = "*", attach: nil)
1647
+ @ruby_triggers << [:thing_added, pattern]
1648
+ event("openhab/things/#{pattern}/added", types: "ThingAddedEvent", attach: attach)
1627
1649
  end
1628
1650
 
1629
1651
  #
1630
1652
  # Creates a thing removed trigger
1631
1653
  #
1654
+ # @param [String, nil] pattern The pattern to match things against
1632
1655
  # @param [Object] attach object to be attached to the trigger
1633
1656
  # @return [void]
1634
1657
  #
1658
+ # @since openHAB 4.0 Support for pattern filter was added
1659
+ #
1635
1660
  # @example
1636
1661
  # rule "thing removed" do
1637
1662
  # thing_removed
@@ -1639,17 +1664,20 @@ module OpenHAB
1639
1664
  # logger.info("#{event.thing.uid} removed.")
1640
1665
  # end
1641
1666
  # end
1642
- def thing_removed(attach: nil)
1643
- @ruby_triggers << [:thing_removed]
1644
- event("openhab/things/*/removed", types: "ThingRemovedEvent", attach: attach)
1667
+ def thing_removed(pattern = "*", attach: nil)
1668
+ @ruby_triggers << [:thing_removed, pattern]
1669
+ event("openhab/things/#{pattern}/removed", types: "ThingRemovedEvent", attach: attach)
1645
1670
  end
1646
1671
 
1647
1672
  #
1648
1673
  # Creates a thing updated trigger
1649
1674
  #
1675
+ # @param [String, nil] pattern The pattern to match things against
1650
1676
  # @param [Object] attach object to be attached to the trigger
1651
1677
  # @return [void]
1652
1678
  #
1679
+ # @since openHAB 4.0 Support for pattern filter was added
1680
+ #
1653
1681
  # @example
1654
1682
  # rule "thing updated" do
1655
1683
  # thing_updated
@@ -1658,9 +1686,9 @@ module OpenHAB
1658
1686
  # end
1659
1687
  # end
1660
1688
  #
1661
- def thing_updated(attach: nil)
1662
- @ruby_triggers << [:thing_updated]
1663
- event("openhab/things/*/updated", types: "ThingUpdatedEvent", attach: attach)
1689
+ def thing_updated(pattern = "*", attach: nil)
1690
+ @ruby_triggers << [:thing_updated, pattern]
1691
+ event("openhab/things/#{pattern}/updated", types: "ThingUpdatedEvent", attach: attach)
1664
1692
  end
1665
1693
 
1666
1694
  #
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of openHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.18.1"
7
+ VERSION = "5.19.0"
8
8
  end
9
9
  end
@@ -28,7 +28,7 @@ module OpenHAB
28
28
  end
29
29
  end
30
30
 
31
- module HistoricState
31
+ module PersistedState
32
32
  def timestamp
33
33
  # PersistenceExtensions uses an anonymous class to wrap the current
34
34
  # state if that happens to be an answer. Except it calls
@@ -41,7 +41,7 @@ module OpenHAB
41
41
  super
42
42
  end
43
43
  end
44
- Core::Items::Persistence::HistoricState.prepend(HistoricState)
44
+ Core::Items::Persistence::PersistedState.prepend(PersistedState)
45
45
 
46
46
  attr_reader :id
47
47
 
@@ -54,14 +54,18 @@ module OpenHAB
54
54
  @data = Hash.new { |h, k| h[k] = [] }
55
55
  end
56
56
 
57
- def store(item, date = nil, state = nil)
58
- date = nil if date.is_a?(String) # alias overload
57
+ def store(item, date = nil, state = nil, item_alias = nil)
58
+ if date.is_a?(String) # alias overload
59
+ item_alias = date
60
+ date = nil
61
+ end
59
62
  state ||= item.state
60
63
  date ||= ZonedDateTime.now
64
+ item_alias ||= item.name
61
65
 
62
66
  new_item = HistoricItem.new(date, state, item.name)
63
67
 
64
- item_history = @data[item.name]
68
+ item_history = @data[item_alias]
65
69
 
66
70
  insert_index = item_history.bsearch_index do |i|
67
71
  i.timestamp.compare_to(date).positive?
@@ -79,6 +83,7 @@ module OpenHAB
79
83
  historic_item = item_history.delete_at(index)
80
84
  @data.delete(historic_item.name) if item_history.empty?
81
85
  end
86
+ true
82
87
  end
83
88
 
84
89
  def query(filter)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.18.1
4
+ version: 5.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-04-06 00:00:00.000000000 Z
13
+ date: 2024-05-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -485,7 +485,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
485
485
  - !ruby/object:Gem::Version
486
486
  version: '0'
487
487
  requirements: []
488
- rubygems_version: 3.5.7
488
+ rubygems_version: 3.5.10
489
489
  signing_key:
490
490
  specification_version: 4
491
491
  summary: JRuby Helper Libraries for openHAB Scripting