openhab-scripting 2.18.0 → 2.20.0

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: 44145354fe523ea40a69c6cf2ba81f2684e9982580fe5bc3996f07ab56202e71
4
- data.tar.gz: ac579ab41fe3f005d7e5a87df7d18c7f32efa9f49c40f251e07620f238b9e65f
3
+ metadata.gz: '08f9a551904b3396bb8f7944c5eb239250b00f68309f53c23e98203bd440ec85'
4
+ data.tar.gz: 8089ce9cc9fe7aea8d0b7e6c0f3ec8b7b298cd5584e4a1d2ec0c9ae6deb6583f
5
5
  SHA512:
6
- metadata.gz: 00026266f68f7d1c9b1ba442ffe0933ac90b5dbe80b61aa2db25470bf3c166c3f5f73067fbba4c292ad31eaeb6913e56299fc9c3625b1e7ce3efc8aaeac69665
7
- data.tar.gz: 497fee2e97d90f9e64bd2ba324b9612ed44becb0aec7eb51d4706f1414e3313afeeb3242b5ece884be2fb15525c190a753116548fc2a022c0d4159cff2a8f559
6
+ metadata.gz: 44a0c0387d5e738a33a108448c5b2d4f6bd0b6e09769690e072d6a370e8bd044e4d7dade6773e5afc70c0a78be74b223b70dbb63b13dfc720b0ad9ee15d98546
7
+ data.tar.gz: 7415332d0525749b1d50d53ee8dfa3829a4f9ee51d308f9d2d4808279260c1bc5eda212d2c0714bace5d0cd25e8bf2159f115ba2d2b8382d365b557d2e4a8dbb
@@ -8,6 +8,7 @@ require 'openhab/log/logger'
8
8
  require 'openhab/dsl/items/number_item'
9
9
  require 'openhab/dsl/items/string_item'
10
10
  require 'openhab/dsl/items/datetime_item'
11
+ require 'openhab/dsl/items/rollershutter_item'
11
12
 
12
13
  # Automation lookup and injection of OpenHab entities
13
14
  java_import org.openhab.core.items.GroupItem
@@ -86,7 +87,8 @@ module OpenHAB
86
87
  # @return [Object] the ruby wrapper for the item
87
88
  #
88
89
  # rubocop: disable Metrics/MethodLength
89
- # Method length check disabled - case dispatch pattern
90
+ # rubocop: disable Metrics/AbcSize
91
+ # Disabled line length and branch size - case dispatch pattern
90
92
  private_class_method def self.decorate_item(item)
91
93
  case item
92
94
  when GroupItem
@@ -97,10 +99,13 @@ module OpenHAB
97
99
  OpenHAB::DSL::Items::StringItem.new(item)
98
100
  when Java::Org.openhab.core.library.items::DateTimeItem
99
101
  OpenHAB::DSL::Items::DateTimeItem.new(item)
102
+ when Java::Org.openhab.core.library.items::RollershutterItem
103
+ OpenHAB::DSL::Items::RollershutterItem.new(item)
100
104
  else
101
105
  item
102
106
  end
103
107
  end
108
+ # rubocop: enable Metrics/AbcSize
104
109
  # rubocop: enable Metrics/MethodLength
105
110
 
106
111
  #
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'java'
5
+
6
+ module OpenHAB
7
+ module DSL
8
+ module Items
9
+ #
10
+ # Delegator to OpenHAB Rollershutter Item
11
+ #
12
+ class RollershutterItem < Numeric
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegator :@rollershutter_item, :to_s
17
+
18
+ java_import Java::OrgOpenhabCoreLibraryTypes::PercentType
19
+ java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
20
+ java_import Java::OrgOpenhabCoreLibraryTypes::StopMoveType
21
+
22
+ #
23
+ # Creates a new RollershutterItem
24
+ #
25
+ # @param [Java::OrgOpenhabCoreLibraryItems::RollershutterItem] rollershutter_item
26
+ # The OpenHAB RollershutterItem to delegate to
27
+ #
28
+ def initialize(rollershutter_item)
29
+ logger.trace("Wrapping #{rollershutter_item}")
30
+ @rollershutter_item = rollershutter_item
31
+
32
+ super()
33
+ end
34
+
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
+ #
54
+ # Returns the rollershutter's position
55
+ #
56
+ # @return [Java::OrgOpenhabCoreLibraryTypes::PercentType] the position of the rollershutter
57
+ #
58
+ def position
59
+ state.as(PercentType)
60
+ end
61
+
62
+ #
63
+ # Compare the rollershutter's position against another object
64
+ #
65
+ # @param [Object] other object to compare against
66
+ #
67
+ # @return [Integer] -1, 0 or 1 depending on the result of the comparison
68
+ #
69
+ def <=>(other)
70
+ case other
71
+ when PercentType, Java::OrgOpenhabCoreLibraryTypes::DecimalType then position.compare_to(other)
72
+ when Numeric then position.int_value <=> other
73
+ when RollershutterItem then position.compare_to(other.position)
74
+ when UpDownType then state.as(UpDownType) == other
75
+ end
76
+ end
77
+
78
+ #
79
+ # Coerce self into other to enable calculations
80
+ #
81
+ # @param [Numeric] other Other numeric to coerce into
82
+ #
83
+ # @return [Array<Numeric>] an array of other and self coerced into other's type
84
+ #
85
+ def coerce(other)
86
+ raise ArgumentError, "Cannot coerce to #{other.class}" unless other.is_a? Numeric
87
+
88
+ case other
89
+ when Integer then [other, position.int_value]
90
+ when Float then [other, position.float_value]
91
+ end
92
+ end
93
+
94
+ #
95
+ # Case equality
96
+ #
97
+ # @param [Java::OrgOpenhabCoreLibraryTypes::UpDownType, Numeric] other Other object to compare against
98
+ #
99
+ # @return [Boolean] true if self can be defined as other, false otherwise
100
+ #
101
+ def ===(other)
102
+ super unless other.is_a? UpDownType
103
+
104
+ state.as(UpDownType).equals(other)
105
+ end
106
+
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
+ #
136
+ # Define math operations
137
+ #
138
+ %i[+ - * / %].each do |operator|
139
+ define_method(operator) do |other|
140
+ right, left = coerce(other)
141
+ left.send(operator, right)
142
+ end
143
+ end
144
+
145
+ #
146
+ # Checks if this method responds to the missing method
147
+ #
148
+ # @param [String] meth Name of the method to check
149
+ # @param [Boolean] _include_private boolean if private methods should be checked
150
+ #
151
+ # @return [Boolean] true if this object will respond to the supplied method, false otherwise
152
+ #
153
+ def respond_to_missing?(meth, _include_private = false)
154
+ @rollershutter_item.respond_to?(meth) || position.respond_to?(meth)
155
+ end
156
+
157
+ #
158
+ # Forward missing methods to Openhab RollershutterItem or the PercentType representing it's state
159
+ # if they are defined
160
+ #
161
+ # @param [String] meth method name
162
+ # @param [Array] args arguments for method
163
+ # @param [Proc] block <description>
164
+ #
165
+ # @return [Object] Value from delegated method in OpenHAB NumberItem
166
+ #
167
+ def method_missing(meth, *args, &block)
168
+ if @rollershutter_item.respond_to?(meth)
169
+ @rollershutter_item.__send__(meth, *args, &block)
170
+ elsif position.respond_to?(meth)
171
+ position.__send__(meth, *args, &block)
172
+ else
173
+ raise NoMethodError, "No method `#{meth}' defined for #{self.class}"
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -134,6 +134,18 @@ module OpenHAB
134
134
  end
135
135
  end
136
136
 
137
+ #
138
+ # Implements Hash#dig-like functionaity to metadata
139
+ #
140
+ # @param [String] key The first key
141
+ # @param [Array<String, Symbol>] keys More keys to dig deeper
142
+ #
143
+ # @return [OpenHAB::DSL::MonkeyPatch::Items::MetadataItem], or nil if the namespace doesn't exist
144
+ #
145
+ def dig(key, *keys)
146
+ keys.empty? ? self[key]&.value : self[key]&.dig(*keys)
147
+ end
148
+
137
149
  #
138
150
  # Enumerates through all the namespaces
139
151
  #
@@ -6,3 +6,4 @@ require 'openhab/dsl/monkey_patch/types/on_off_type'
6
6
  require 'openhab/dsl/monkey_patch/types/decimal_type'
7
7
  require 'openhab/dsl/monkey_patch/types/percent_type'
8
8
  require 'openhab/dsl/monkey_patch/types/quantity_type'
9
+ require 'openhab/dsl/monkey_patch/types/up_down_type'
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+ module OpenHAB
5
+ module DSL
6
+ module MonkeyPatch
7
+ #
8
+ # Patches OpenHAB types
9
+ #
10
+ module Types
11
+ java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
12
+
13
+ #
14
+ # MonkeyPatching UpDownType
15
+ #
16
+ class UpDownType
17
+ #
18
+ # Check if the supplied object is case equals to self
19
+ #
20
+ # @param [Object] other object to compare
21
+ #
22
+ # @return [Boolean] True if the other object is a RollershutterItem and has the same state
23
+ #
24
+ def ===(other)
25
+ super unless other.is_a? OpenHAB::DSL::Items::RollershutterItem
26
+
27
+ equals(other.state.as(UpDownType))
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -70,7 +70,7 @@ module OpenHAB
70
70
  def create_queue(inputs)
71
71
  case check_guards(event: inputs&.dig('event'))
72
72
  when true
73
- @run_queue.dup
73
+ @run_queue.dup.grep_v(RuleConfig::Otherwise)
74
74
  when false
75
75
  @run_queue.dup.grep(RuleConfig::Otherwise)
76
76
  end
@@ -145,16 +145,14 @@ module OpenHAB
145
145
  #
146
146
  #
147
147
  def process_trigger_delay(trigger_delay, mod, inputs)
148
- if check_trigger_guards(trigger_delay, inputs)
148
+ if trigger_delay.timer_active?
149
+ process_active_timer(inputs, mod, trigger_delay)
150
+ elsif check_trigger_guards(trigger_delay, inputs)
149
151
  logger.trace("Trigger Guards Matched for #{trigger_delay}, delaying rule execution")
150
152
  # Add timer and attach timer to delay object, and also state being tracked to so timer can be cancelled if
151
153
  # state changes
152
154
  # Also another timer should not be created if changed to same value again but instead rescheduled
153
- if trigger_delay.timer_active?
154
- process_active_timer(inputs, mod, trigger_delay)
155
- else
156
- create_trigger_delay_timer(inputs, mod, trigger_delay)
157
- end
155
+ create_trigger_delay_timer(inputs, mod, trigger_delay)
158
156
  else
159
157
  logger.trace("Trigger Guards did not match for #{trigger_delay}, ignoring trigger.")
160
158
  end
@@ -173,7 +171,8 @@ module OpenHAB
173
171
  trigger_delay.timer = after(trigger_delay.duration) do
174
172
  logger.trace("Delay Complete for #{trigger_delay}, executing rule")
175
173
  trigger_delay.timer = nil
176
- process_queue(@run_queue.dup, mod, inputs)
174
+ queue = create_queue(inputs)
175
+ process_queue(queue, mod, inputs)
177
176
  end
178
177
  trigger_delay.tracking_to = inputs['newState']
179
178
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.18.0'
8
+ VERSION = '2.20.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.18.0
4
+ version: 2.20.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-14 00:00:00.000000000 Z
11
+ date: 2021-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,6 +42,7 @@ files:
42
42
  - lib/openhab/dsl/items/datetime_item.rb
43
43
  - lib/openhab/dsl/items/items.rb
44
44
  - lib/openhab/dsl/items/number_item.rb
45
+ - lib/openhab/dsl/items/rollershutter_item.rb
45
46
  - lib/openhab/dsl/items/string_item.rb
46
47
  - lib/openhab/dsl/monkey_patch/actions/actions.rb
47
48
  - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
@@ -67,6 +68,7 @@ files:
67
68
  - lib/openhab/dsl/monkey_patch/types/percent_type.rb
68
69
  - lib/openhab/dsl/monkey_patch/types/quantity_type.rb
69
70
  - lib/openhab/dsl/monkey_patch/types/types.rb
71
+ - lib/openhab/dsl/monkey_patch/types/up_down_type.rb
70
72
  - lib/openhab/dsl/persistence.rb
71
73
  - lib/openhab/dsl/rules/automation_rule.rb
72
74
  - lib/openhab/dsl/rules/guard.rb