openhab-scripting 2.20.2 → 2.23.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: e99181d5d68e3b8deab1440bfc457890ea512a1f25a4da4dc048555f3e296f2a
4
- data.tar.gz: 2b07f929d309bda53b78270b9d7274b2373132de08a075af634ca9237a79cea7
3
+ metadata.gz: 3b38e635249a2d0e8633e75504eb3e42f79399d1e66b90f5242f3dc8062c711b
4
+ data.tar.gz: dcb573a627b8f93d5a4f6e1bbb8e9b8033f4765b33e49ab88d07414e055b5e7f
5
5
  SHA512:
6
- metadata.gz: 375b6c769ee121a9dfb88d6a93338de1bd1040b1cb885f8668cdf07c0f0c0dce6d2a34cd744ebe3837a06739477f296bea0e907599b3c5446ebd400745a7a92b
7
- data.tar.gz: 64396208c64ddb457a873b7829ee024d01f598c32348c9717229af106e754a545aad10dde6a85dd27045a0ac73844251b4f0bc0f56f8c72ae2b88a6a0c589878
6
+ metadata.gz: c6b8e1f865bf8912e0575495c0539e96acd5f7c8dc77582ab355bee6e5def4e45701cf159d77b12fe7975cb8493fafbcc0260706f2bd500fddeecf4a66683e5e
7
+ data.tar.gz: fb3c37bf6fdd9f9f95feb26e39e44cd336b7da07a4831e1d3b8079bcbac0f57c005c7324e683dbcb5baa67c60124e947e965edb88b3f8a43a51f03e29fa383a9
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'openhab/dsl/monkey_patch/events/item_state'
3
4
  require 'openhab/dsl/monkey_patch/events/item_state_changed'
4
5
  require 'openhab/dsl/monkey_patch/events/thing_status_info'
5
6
  require 'openhab/dsl/monkey_patch/events/item_command'
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB events
10
+ #
11
+ module Events
12
+ java_import Java::OrgOpenhabCoreItemsEvents::ItemStateEvent
13
+
14
+ #
15
+ # MonkeyPatch with ruby style accessors
16
+ #
17
+ class ItemStateEvent
18
+ alias state item_state
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -29,7 +29,7 @@ module OpenHAB
29
29
  def_delegator :@metadata, :value
30
30
 
31
31
  def initialize(metadata: nil, key: nil, value: nil, config: nil)
32
- @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value, config)
32
+ @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value&.to_s, config)
33
33
  super(@metadata&.configuration)
34
34
  end
35
35
 
@@ -60,9 +60,7 @@ module OpenHAB
60
60
  # @return [Java::Org::openhab::core::items::Metadata] the old metadata
61
61
  #
62
62
  def value=(value)
63
- raise ArgumentError, 'Value must be a string' unless value.is_a? String
64
-
65
- metadata = Metadata.new(@metadata&.uID, value, @metadata&.configuration)
63
+ metadata = Metadata.new(@metadata&.uID, value&.to_s, @metadata&.configuration)
66
64
  NamespaceAccessor.registry.update(metadata) if @metadata&.uID
67
65
  end
68
66
 
@@ -125,7 +123,7 @@ module OpenHAB
125
123
  meta_value, configuration = update_from_value(value)
126
124
 
127
125
  key = MetadataKey.new(namespace, @item_name)
128
- metadata = Metadata.new(key, meta_value, configuration)
126
+ metadata = Metadata.new(key, meta_value&.to_s, configuration)
129
127
  # registry.get can be omitted, but registry.update will log a warning for nonexistent metadata
130
128
  if NamespaceAccessor.registry.get(key)
131
129
  NamespaceAccessor.registry.update(metadata)
@@ -49,7 +49,12 @@ module OpenHAB
49
49
  if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
50
50
  timestamp = Java::JavaTime::ZonedDateTime.now.minus(timestamp)
51
51
  end
52
- PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
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
58
  end
54
59
  end
55
60
 
@@ -21,7 +21,7 @@ module OpenHAB
21
21
  #
22
22
  def ==(other)
23
23
  case other
24
- when OpenHAB::DSL::Types::Quantity, QuantityType
24
+ when OpenHAB::DSL::Types::Quantity, QuantityType, Java::OrgOpenhabCoreLibraryTypes::StringType
25
25
  other == self
26
26
  else
27
27
  super
@@ -63,6 +63,27 @@ module OpenHAB
63
63
  logger.trace("#{self.class} #{self} == #{other} (#{other.class})")
64
64
  (self <=> other).zero?
65
65
  end
66
+
67
+ #
68
+ # Convert DecimalType to a Quantity
69
+ #
70
+ # @param [Object] other String or Unit representing an OpenHAB Unit
71
+ #
72
+ # @return [OpenHAB::Core::DSL::Types::Quantity] NumberItem converted to supplied Unit
73
+ #
74
+ def |(other)
75
+ other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
76
+ Quantity.new(QuantityType.new(to_big_decimal, other))
77
+ end
78
+
79
+ #
80
+ # Provide details about DecimalType object
81
+ #
82
+ # @return [String] Representing details about the DecimalType object
83
+ #
84
+ def inspect
85
+ to_string
86
+ end
66
87
  end
67
88
  end
68
89
  end
@@ -100,8 +100,7 @@ module OpenHAB
100
100
  # @return [Boolean] True if the rule should execute, false if trigger guard prevents execution
101
101
  #
102
102
  def check_trigger_guards(trigger_delay, inputs)
103
- old_state = inputs['oldState']
104
- new_state = inputs['newState']
103
+ new_state, old_state = retrieve_states(inputs)
105
104
  if check_from(trigger_delay, old_state)
106
105
  return true if check_to(trigger_delay, new_state)
107
106
 
@@ -113,6 +112,32 @@ module OpenHAB
113
112
  end
114
113
  end
115
114
 
115
+ #
116
+ # Rerieve the newState and oldState, alternatively newStatus and oldStatus
117
+ # from the input map
118
+ #
119
+ # @param [Map] inputs OpenHAB map object describing rule trigger
120
+ #
121
+ # @return [Array] An array of the values for [newState, oldState] or [newStatus, oldStatus]
122
+ #
123
+ def retrieve_states(inputs)
124
+ old_state = inputs['oldState'] || thing_status_to_sym(inputs['oldStatus'])
125
+ new_state = inputs['newState'] || thing_status_to_sym(inputs['newStatus'])
126
+
127
+ [new_state, old_state]
128
+ end
129
+
130
+ #
131
+ # Converts a ThingStatus object to a ruby Symbol
132
+ #
133
+ # @param [Java::OrgOpenhabCoreThing::ThingStatus] status A ThingStatus instance
134
+ #
135
+ # @return [Symbol] A corresponding symbol, in lower case
136
+ #
137
+ def thing_status_to_sym(status)
138
+ status&.to_s&.downcase&.to_sym
139
+ end
140
+
116
141
  #
117
142
  # Check the from state against the trigger delay
118
143
  #
@@ -122,7 +147,7 @@ module OpenHAB
122
147
  # @return [Boolean] true if no from state is defined or defined state equals supplied state
123
148
  #
124
149
  def check_from(trigger_delay, state)
125
- trigger_delay.from.nil? || trigger_delay.from == state
150
+ trigger_delay.from.nil? || state == trigger_delay.from
126
151
  end
127
152
 
128
153
  #
@@ -134,7 +159,7 @@ module OpenHAB
134
159
  # @return [Boolean] true if no to state is defined or defined state equals supplied state
135
160
  #
136
161
  def check_to(trigger_delay, state)
137
- trigger_delay.to.nil? || trigger_delay.to == state
162
+ trigger_delay.to.nil? || state == trigger_delay.to
138
163
  end
139
164
 
140
165
  #
@@ -174,7 +199,7 @@ module OpenHAB
174
199
  queue = create_queue(inputs)
175
200
  process_queue(queue, mod, inputs)
176
201
  end
177
- trigger_delay.tracking_to = inputs['newState']
202
+ trigger_delay.tracking_to, = retrieve_states(inputs)
178
203
  end
179
204
 
180
205
  #
@@ -186,7 +211,7 @@ module OpenHAB
186
211
  #
187
212
  #
188
213
  def process_active_timer(inputs, mod, trigger_delay)
189
- state = inputs['newState']
214
+ state, = retrieve_states(inputs)
190
215
  if state == trigger_delay.tracking_to
191
216
  logger.trace("Item changed to #{state} for #{trigger_delay}, rescheduling timer.")
192
217
  trigger_delay.timer.reschedule(ZonedDateTime.now.plus(trigger_delay.duration))
@@ -58,16 +58,8 @@ module OpenHAB
58
58
  # @return [Array] Array of current TriggerDelay objects
59
59
  #
60
60
  def changed_wait(item, duration:, to: nil, from: nil)
61
- # If GroupItems specified, use the group state trigger instead
62
- if item.is_a? GroupItems
63
- config = { 'groupName' => item.group.name }
64
- trigger = Trigger::GROUP_STATE_CHANGE
65
- else
66
- config = { 'itemName' => item.name }
67
- trigger = Trigger::ITEM_STATE_CHANGE
68
- end
69
- logger.trace("Creating Changed Wait Change Trigger for #{config}")
70
- trigger = append_trigger(trigger, config)
61
+ trigger = create_changed_trigger(item, nil, nil)
62
+ logger.trace("Creating Changed Wait Change Trigger for #{item}")
71
63
  @trigger_delays[trigger.id] = TriggerDelay.new(to: to, from: from, duration: duration)
72
64
  end
73
65
 
@@ -22,6 +22,7 @@ module OpenHAB
22
22
  def_delegator :@quantity, :to_s
23
23
 
24
24
  java_import org.openhab.core.library.types.QuantityType
25
+ java_import org.openhab.core.library.types.DecimalType
25
26
  java_import 'tec.uom.se.format.SimpleUnitFormat'
26
27
  java_import 'tec.uom.se.AbstractUnit'
27
28
 
@@ -92,6 +93,7 @@ module OpenHAB
92
93
  case other
93
94
  when Quantity then [other.quantity, quantity]
94
95
  when QuantityType then [other, quantity]
96
+ when DecimalType then [Quantity.new(other.to_big_decimal.to_d), quantity]
95
97
  when NumberItem then [other.to_qt.quantity, quantity]
96
98
  when Numeric, String then [Quantity.new(other), self]
97
99
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.20.2'
8
+ VERSION = '2.23.0'
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.20.2
4
+ version: 2.23.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-18 00:00:00.000000000 Z
11
+ date: 2021-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -49,6 +49,7 @@ files:
49
49
  - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
50
50
  - lib/openhab/dsl/monkey_patch/events/events.rb
51
51
  - lib/openhab/dsl/monkey_patch/events/item_command.rb
52
+ - lib/openhab/dsl/monkey_patch/events/item_state.rb
52
53
  - lib/openhab/dsl/monkey_patch/events/item_state_changed.rb
53
54
  - lib/openhab/dsl/monkey_patch/events/thing_status_info.rb
54
55
  - lib/openhab/dsl/monkey_patch/items/contact_item.rb