openhab-scripting 5.18.1 → 5.19.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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