openhab-scripting 2.23.2 → 2.23.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ee0da4d4273752f4a2e7b0239c09f379f74acbc05edc3f3d10690a923c3d90a
4
- data.tar.gz: b807d26604402500b68ebbfc5b4a5afe6a4c65c963a3d1a3f625a703957239f9
3
+ metadata.gz: e2ae7c9c46b7dff785181b7846dc6e6e6b9f27d445d7f1b7cc108814e1e75531
4
+ data.tar.gz: fffb46ad699350c94c580a3cb8ee2545a3c7c5372a81d4c7dc867cb834ee33db
5
5
  SHA512:
6
- metadata.gz: e9770cad933c03a36f70d320b04e43d9f9394e15da250a1be849a1669a7a863371cac83774d48f76f3dd44c97176b5ccac180f62167adf13c0b316ab431ee41e
7
- data.tar.gz: add9dc156271bbec0918744355bf1362e387f3ce4792be28470abc57c03fd0a7e83acf69798bddfb8f142841dd202fa8df65bbb6c85f222bb8f58302f1686062
6
+ metadata.gz: 790a2b3838ce8847bf3dafe567336400c43dae0b9d638318c7c1d70539f60ed45714af3f376f2a93f27265b222dd35474b1fb477eb1c892258cea401ab3ff321
7
+ data.tar.gz: 5085945c207f2b00353ca899fab65a2e0990751c083b433729b22e3d798dac11b1a4e99a3d07fdaf6db439d5787e06b9d9a984661496b8a767d13d668f077b43
@@ -8,7 +8,23 @@ module OpenHAB
8
8
  # Persistence extension for Items
9
9
  #
10
10
  module Persistence
11
- %w[persist last_update].each do |method|
11
+ # All persistence methods that could return a Quantity
12
+ QUANTITY_METHODS = %i[average_since
13
+ delta_since
14
+ deviation_since
15
+ sum_since
16
+ variance_since].freeze
17
+
18
+ # All persistence methods that require a timestamp
19
+ PERSISTENCE_METHODS = (QUANTITY_METHODS +
20
+ %i[changed_since
21
+ evolution_rate
22
+ historic_state
23
+ maximum_since
24
+ minimum_since
25
+ updated_since]).freeze
26
+
27
+ %i[persist last_update].each do |method|
12
28
  define_method(method) do |service = nil|
13
29
  service ||= persistence_service
14
30
  PersistenceExtensions.public_send(method, self, service&.to_s)
@@ -31,35 +47,48 @@ module OpenHAB
31
47
  PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
32
48
  end
33
49
 
34
- %w[
35
- average_since
36
- changed_since
37
- delta_since
38
- deviation_since
39
- evolution_rate
40
- historic_state
41
- maximum_since
42
- minimum_since
43
- sum_since
44
- updated_since
45
- variance_since
46
- ].each do |method|
50
+ PERSISTENCE_METHODS.each do |method|
47
51
  define_method(method) do |timestamp, service = nil|
48
52
  service ||= persistence_service
49
- if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
50
- timestamp = Java::JavaTime::ZonedDateTime.now.minus(timestamp)
51
- end
52
- result = PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
53
- if result.is_a?(Java::OrgOpenhabCoreLibraryTypes::DecimalType) && respond_to?(:unit) && unit
54
- Quantity.new(Java::OrgOpenhabCoreLibraryTypes::QuantityType.new(result.to_big_decimal, unit))
55
- else
56
- result
57
- end
53
+ result = PersistenceExtensions.public_send(method, self, to_zdt(timestamp), service&.to_s)
54
+ QUANTITY_METHODS.include?(method) ? quantify(result) : result
58
55
  end
59
56
  end
60
57
 
61
58
  private
62
59
 
60
+ #
61
+ # Convert timestamp to ZonedDateTime if it's a TemporalAmount
62
+ #
63
+ # @param [Object] timestamp to convert
64
+ #
65
+ # @return [ZonedDateTime]
66
+ #
67
+ def to_zdt(timestamp)
68
+ if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
69
+ logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
70
+ Java::JavaTime::ZonedDateTime.now.minus(timestamp)
71
+ else
72
+ timestamp
73
+ end
74
+ end
75
+
76
+ #
77
+ # Convert value to Quantity if it is a DecimalType and a unit is defined
78
+ #
79
+ # @param [Object] value The value to convert
80
+ #
81
+ # @return [Object] Quantity or the original value
82
+ #
83
+ def quantify(value)
84
+ if value.is_a?(Java::OrgOpenhabCoreLibraryTypes::DecimalType) && respond_to?(:unit) && unit
85
+ logger.trace("Unitizing #{value} with unit #{unit}")
86
+ Quantity.new(Java::OrgOpenhabCoreLibraryTypes::QuantityType.new(value.to_big_decimal, unit))
87
+ else
88
+ value
89
+ end
90
+ end
91
+
63
92
  #
64
93
  # Get the specified persistence service from the current thread local variable
65
94
  #
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.23.2'
8
+ VERSION = '2.23.3'
9
9
  end
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: 2.23.2
4
+ version: 2.23.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell