openhab-scripting 2.20.1 → 2.22.1

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: c8d7de90fee54798d88e51a4ea9f7ac6437852d7c4eb5da2b847700f0dedd5de
4
- data.tar.gz: 9a91e9a2ee0d977f4e2a0dd9b0ba50b1db89c71554596bfd8ca86bd9ec168d68
3
+ metadata.gz: 854569a4ecda01dc3fb8dc2e12f66147248a2aafffdbbdbabe5d0e750b6849cc
4
+ data.tar.gz: 1bf8451a6c8d1dd556472ffa0ceb210e34b9db1b0af6a652bdb336182abb7f20
5
5
  SHA512:
6
- metadata.gz: 1d956f08c8884347de414eb9de04a69184c9d95a39c35bd0ff8685ceefb28efe64945a944a87efd11da16f5dceda77be453693b84cc816a7541e3e923b218479
7
- data.tar.gz: ab3fd69c22c115a03848b112922f9b489d69c35a1bac3040ef40c9a73ffa8540e73318011768647fe0793a197973239e028949126a83ed233340efbeb667f490
6
+ metadata.gz: 4cb862c98031f4d62d1366dbfaf4a9370103687ad06ab930db77dba32af5623337652d9264f09f28622c12b14314902d1a5ea3b58fffec4cfa2fdd086c3bab31
7
+ data.tar.gz: 2044c8d3cee633c59b9368889c7995b4533b29add5d36e97dc3b8400706229c0afd133121b816fd8b4f53886b86e82b281f9dbe659e104c859bec212f6f5e1d8
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module Items
8
+ #
9
+ # Holds methods to automatically generate commands and
10
+ # accessors for items
11
+ module ItemCommand
12
+ #
13
+ # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
14
+ # string representation of the enum value For example, an enum with values of STOP and START
15
+ # would create methods stop() and start() that send the corresponding STOP and START commands to the item
16
+ #
17
+ # @param [Java::JavaLang::Enum] command_enum Enumeration to create commands for
18
+ #
19
+ #
20
+ def item_command(command_enum)
21
+ # rubocop:disable Style/HashEachMethods
22
+ # Disable rule because Java enum does not support each_value
23
+ command_enum.values.each do |command|
24
+ command_method = command.to_s.downcase
25
+ define_method(command_method) do
26
+ self.command(command)
27
+ end
28
+ end
29
+ # rubocop:enable Style/HashEachMethods
30
+ end
31
+
32
+ #
33
+ # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
34
+ # string representation appended with a question mark '?' of the enum value For example,
35
+ # an enum with values of UP and DOWN would create methods up? and down? that check
36
+ # if the current state matches the value of the enum
37
+ #
38
+ # @param [Java::JavaLang::Enum] command_enum Enumeration to create methods for each value
39
+ # to check if current state matches that enum
40
+ # @yield [state] Optional block that can be used to transform state prior to comparison
41
+ #
42
+ #
43
+ def item_state(command_enum)
44
+ # rubocop:disable Style/HashEachMethods
45
+ # Disable rule because Java enum does not support each_value
46
+ command_enum.values.each do |command|
47
+ status_method = "#{command.to_s.downcase}?"
48
+ define_method(status_method) do
49
+ state? && state.as(command_enum) == command
50
+ end
51
+ end
52
+ # rubocop:enable Style/HashEachMethods
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'forwardable'
4
4
  require 'java'
5
+ require 'openhab/dsl/items/item_command'
5
6
 
6
7
  module OpenHAB
7
8
  module DSL
@@ -11,6 +12,7 @@ module OpenHAB
11
12
  #
12
13
  class RollershutterItem < Numeric
13
14
  extend Forwardable
15
+ extend OpenHAB::DSL::Items::ItemCommand
14
16
  include Comparable
15
17
 
16
18
  def_delegator :@rollershutter_item, :to_s
@@ -19,6 +21,10 @@ module OpenHAB
19
21
  java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
20
22
  java_import Java::OrgOpenhabCoreLibraryTypes::StopMoveType
21
23
 
24
+ item_command Java::OrgOpenhabCoreLibraryTypes::StopMoveType
25
+ item_command Java::OrgOpenhabCoreLibraryTypes::UpDownType
26
+ item_state Java::OrgOpenhabCoreLibraryTypes::UpDownType
27
+
22
28
  #
23
29
  # Creates a new RollershutterItem
24
30
  #
@@ -32,24 +38,6 @@ module OpenHAB
32
38
  super()
33
39
  end
34
40
 
35
- #
36
- # Check if the rollershutter is up
37
- #
38
- # @return [Boolean] true if the rollershutter is up, false otherwise
39
- #
40
- def up?
41
- state.as(UpDownType) == UpDownType::UP
42
- end
43
-
44
- #
45
- # Check if the rollershutter is down
46
- #
47
- # @return [Boolean] true if the rollershutter is down, false otherwise
48
- #
49
- def down?
50
- state.as(UpDownType) == UpDownType::DOWN
51
- end
52
-
53
41
  #
54
42
  # Returns the rollershutter's position
55
43
  #
@@ -104,34 +92,6 @@ module OpenHAB
104
92
  state.as(UpDownType).equals(other)
105
93
  end
106
94
 
107
- #
108
- # Sends an UP command to the Item
109
- #
110
- def up
111
- command UpDownType::UP
112
- end
113
-
114
- #
115
- # Sends a DOWN command to the Item
116
- #
117
- def down
118
- command UpDownType::DOWN
119
- end
120
-
121
- #
122
- # Sends a STOP command to the Item
123
- #
124
- def stop
125
- command StopMoveType::STOP
126
- end
127
-
128
- #
129
- # Sends a MOVE command to the Item
130
- #
131
- def move
132
- command StopMoveType::MOVE
133
- end
134
-
135
95
  #
136
96
  # Define math operations
137
97
  #
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'java'
4
+ require 'openhab/dsl/items/item_command'
4
5
 
5
6
  module OpenHAB
6
7
  module DSL
@@ -20,24 +21,11 @@ module OpenHAB
20
21
  # Monkey patch Contact Item with Ruby methods
21
22
  #
22
23
  class ContactItem
23
- java_import org.openhab.core.library.types.OpenClosedType
24
- #
25
- # Check if the contact is open
26
- #
27
- # @return [Boolean] True if contact has state and is open, false otherwise
28
- #
29
- def open?
30
- state? && state == OpenClosedType::OPEN
31
- end
24
+ extend OpenHAB::DSL::Items::ItemCommand
32
25
 
33
- #
34
- # Check if the contact is closed
35
- #
36
- # @return [Boolean] True if contact has state and is closed, false otherwise
37
- #
38
- def closed?
39
- state? && state == OpenClosedType::CLOSED
40
- end
26
+ java_import Java::OrgOpenhabCoreLibraryTypes::OpenClosedType
27
+
28
+ item_state Java::OrgOpenhabCoreLibraryTypes::OpenClosedType
41
29
 
42
30
  #
43
31
  # Compares contacts to OpenClosedTypes
@@ -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
 
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'java'
4
+ require 'openhab/dsl/items/item_command'
5
+
3
6
  module OpenHAB
4
7
  module DSL
5
8
  module MonkeyPatch
@@ -16,43 +19,15 @@ module OpenHAB
16
19
  # Monkeypatching SwitchItem to add Ruby Support methods
17
20
  #
18
21
  class SwitchItem
19
- java_import Java::OrgOpenhabCoreLibraryTypes::OnOffType
20
- #
21
- # Send the OFF command to the switch
22
- #
23
- #
24
- def off
25
- command(OnOffType::OFF)
26
- end
22
+ extend OpenHAB::DSL::Items::ItemCommand
27
23
 
28
- #
29
- # Send the OFF command to the switch
30
- #
31
- #
32
- def on
33
- command(OnOffType::ON)
34
- end
24
+ java_import Java::OrgOpenhabCoreLibraryTypes::OnOffType
35
25
 
36
- #
37
- # Check if a switch is on
38
- #
39
- # @return [Boolean] True if the switch is on, false otherwise
40
- #
41
- def on?
42
- state? && state == OnOffType::ON
43
- end
26
+ item_command Java::OrgOpenhabCoreLibraryTypes::OnOffType
27
+ item_state Java::OrgOpenhabCoreLibraryTypes::OnOffType
44
28
 
45
29
  alias truthy? on?
46
30
 
47
- #
48
- # Check if a switch is off
49
- #
50
- # @return [Boolean] True if the switch is off, false otherwise
51
- #
52
- def off?
53
- state? && state == OnOffType::OFF
54
- end
55
-
56
31
  #
57
32
  # Send a command to invert the state of the switch
58
33
  #
@@ -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.1'
8
+ VERSION = '2.22.1'
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.1
4
+ version: 2.22.1
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
@@ -40,6 +40,7 @@ files:
40
40
  - lib/openhab/dsl/gems.rb
41
41
  - lib/openhab/dsl/group.rb
42
42
  - lib/openhab/dsl/items/datetime_item.rb
43
+ - lib/openhab/dsl/items/item_command.rb
43
44
  - lib/openhab/dsl/items/items.rb
44
45
  - lib/openhab/dsl/items/number_item.rb
45
46
  - lib/openhab/dsl/items/rollershutter_item.rb