openhab-scripting 2.23.0 → 2.23.1

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: 3b38e635249a2d0e8633e75504eb3e42f79399d1e66b90f5242f3dc8062c711b
4
- data.tar.gz: dcb573a627b8f93d5a4f6e1bbb8e9b8033f4765b33e49ab88d07414e055b5e7f
3
+ metadata.gz: 1af0c9d15b41ac54e58540ccab079a6c084931abf80f5add687511d332d3afa2
4
+ data.tar.gz: 5e3c341ecf96ab4fb11b6ca6cefae01a27dc02b2498e9e3896ef4683baa3f6e5
5
5
  SHA512:
6
- metadata.gz: c6b8e1f865bf8912e0575495c0539e96acd5f7c8dc77582ab355bee6e5def4e45701cf159d77b12fe7975cb8493fafbcc0260706f2bd500fddeecf4a66683e5e
7
- data.tar.gz: fb3c37bf6fdd9f9f95feb26e39e44cd336b7da07a4831e1d3b8079bcbac0f57c005c7324e683dbcb5baa67c60124e947e965edb88b3f8a43a51f03e29fa383a9
6
+ metadata.gz: e0238451b9d78d45c225f98ede7b86580e26c7c3edfc7681f6942b7f5a7c4dbfb67f98c5c76ccf8a437cfa779bfa2eb9b0c8639b1c4a24206b99b8ca80a3803f
7
+ data.tar.gz: 616d30281aef1da66964cba5d1c6365b24939f4fbec3070e4251551e3c8c7552191835da12e4795d5698e9671add7739a411e53e7b347de65a8b4a9c0fb249a0
@@ -4,6 +4,7 @@ require 'forwardable'
4
4
  require 'java'
5
5
  require 'time'
6
6
  require 'openhab/dsl/types/datetime'
7
+ require 'openhab/dsl/items/item_delegate'
7
8
 
8
9
  module OpenHAB
9
10
  module DSL
@@ -16,6 +17,7 @@ module OpenHAB
16
17
  class DateTimeItem
17
18
  extend Forwardable
18
19
  include Comparable
20
+ include OpenHAB::DSL::Items::ItemDelegate
19
21
 
20
22
  def_delegator :@datetime_item, :to_s
21
23
 
@@ -27,6 +29,8 @@ module OpenHAB
27
29
  #
28
30
  def initialize(datetime_item)
29
31
  @datetime_item = datetime_item
32
+ item_delegate { @datetime_item }
33
+ item_delegate { to_dt }
30
34
  end
31
35
 
32
36
  #
@@ -61,36 +65,6 @@ module OpenHAB
61
65
  def zone
62
66
  to_dt.zone if state?
63
67
  end
64
-
65
- #
66
- # Check if missing method can be delegated to other contained objects
67
- #
68
- # @param [String, Symbol] meth The method name to check for
69
- #
70
- # @return [Boolean] true if DateTimeItem or DateTime responds to the method, false otherwise
71
- #
72
- def respond_to_missing?(meth, *)
73
- @datetime_item.respond_to?(meth) || to_dt.respond_to?(meth)
74
- end
75
-
76
- #
77
- # Forward missing methods to the OpenHAB Item, or a DateTime object wrapping its state
78
- #
79
- # @param [String] meth method name
80
- # @param [Array] args arguments for method
81
- # @param [Proc] block <description>
82
- #
83
- # @return [Object] Value from delegated method in OpenHAB NumberItem
84
- #
85
- def method_missing(meth, *args, &block)
86
- if @datetime_item.respond_to?(meth)
87
- @datetime_item.__send__(meth, *args, &block)
88
- elsif state?
89
- to_dt.send(meth, *args, &block)
90
- else
91
- raise NoMethodError, "undefined method `#{meth}' for #{self.class}"
92
- end
93
- end
94
68
  end
95
69
  end
96
70
  end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+ require 'openhab/log/logger'
5
+
6
+ module OpenHAB
7
+ module DSL
8
+ module Items
9
+ #
10
+ # Holds methods to delegate to items
11
+ #
12
+ module ItemDelegate
13
+ include OpenHAB::Log
14
+
15
+ #
16
+ # Delegates methods to the object returned from the supplied block if
17
+ # they don't exist in the object this is included in. If the supplied block returns nil, no delegation occurs
18
+ # If this item is called more than once delegation occurs in the order of invocation, i.e. the object returned
19
+ # by the first block is delegated to if it responds to the missing method,
20
+ # then the second block is processed, etc.
21
+ #
22
+ # @param [Proc] &delegate delgegate block
23
+ #
24
+ #
25
+ def item_delegate(&delegate)
26
+ @delegates ||= []
27
+ @delegates << delegate
28
+ end
29
+
30
+ #
31
+ # Delegate missing method calls to delegates supplied to item_delgate method
32
+ # if no delegates exist or respond to missing method, super is invoked which will
33
+ # throw the appropriate method missing error
34
+ #
35
+ # @param [String] meth misisng method
36
+ # @param [Array] *args Arguments to the missing method
37
+ # @param [Proc] &block supplied to the missing method
38
+ #
39
+ # @return [Object] Result of missing method invocation
40
+ #
41
+ def method_missing(meth, *args, &block)
42
+ logger.trace("Method (#{meth}) missing for item #{self.class}")
43
+ delegate = delegate_for(meth)
44
+ if delegate
45
+ logger.trace("Delegating #{meth} to #{delegate.class}")
46
+ delegate.__send__(meth, *args, &block)
47
+ else
48
+ super
49
+ end
50
+ end
51
+
52
+ #
53
+ # Checks if any of the supplied delgates respond to a specific method
54
+ #
55
+ # @param [String] meth method to check for
56
+ # @param [Boolean] _include_private if private methods should be checked
57
+ #
58
+ # @return [Boolean] True if any delegates respond to method, false otherwise
59
+ #
60
+ def respond_to_missing?(meth, _include_private = false)
61
+ logger.trace("Checking if (#{self.class}) responds to (#{meth})")
62
+ responds = !delegate_for(meth).nil?
63
+ logger.trace("(#{self.class}) responds to (#{meth}) (#{responds})")
64
+ responds
65
+ end
66
+
67
+ private
68
+
69
+ #
70
+ # Find a delegate for the supplied method
71
+ #
72
+ # @param [String] meth method to find delegate for
73
+ #
74
+ # @return [Boolean] True if any method responds to the supplied delegate, false otherwise
75
+ #
76
+ def delegate_for(meth)
77
+ (@delegates || []).each do |delegate_block|
78
+ delegate = delegate_block.call(meth)
79
+ logger.trace("Checking if delegate (#{delegate.class}) responds to (#{meth})")
80
+ if delegate.respond_to? meth
81
+ logger.trace("Delegate (#{delegate.class}) found for method (#{meth})")
82
+ return delegate
83
+ end
84
+ end
85
+ logger.trace("No delegate found for method (#{meth})")
86
+ nil
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -4,6 +4,7 @@ require 'bigdecimal'
4
4
  require 'forwardable'
5
5
  require 'java'
6
6
  require 'openhab/dsl/types/quantity'
7
+ require 'openhab/dsl/items/item_delegate'
7
8
 
8
9
  module OpenHAB
9
10
  module DSL
@@ -17,6 +18,8 @@ module OpenHAB
17
18
  class NumberItem < Numeric
18
19
  extend Forwardable
19
20
 
21
+ include OpenHAB::DSL::Items::ItemDelegate
22
+
20
23
  def_delegator :@number_item, :to_s
21
24
 
22
25
  java_import org.openhab.core.library.types.DecimalType
@@ -31,6 +34,7 @@ module OpenHAB
31
34
  #
32
35
  def initialize(number_item)
33
36
  @number_item = number_item
37
+ item_delegate { @number_item }
34
38
  super()
35
39
  end
36
40
 
@@ -146,40 +150,6 @@ module OpenHAB
146
150
  @number_item.dimension
147
151
  end
148
152
 
149
- #
150
- # Forward missing methods to Openhab Number Item if they are defined
151
- #
152
- # @param [String] meth method name
153
- # @param [Array] args arguments for method
154
- # @param [Proc] block <description>
155
- #
156
- # @return [Object] Value from delegated method in OpenHAB NumberItem
157
- #
158
- def method_missing(meth, *args, &block)
159
- logger.trace("Method missing, performing dynamic lookup for: #{meth}")
160
- if @number_item.respond_to?(meth)
161
- @number_item.__send__(meth, *args, &block)
162
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
163
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
164
- else
165
- super(meth, *args, &block)
166
- end
167
- end
168
-
169
- #
170
- # Checks if this method responds to the missing method
171
- #
172
- # @param [String] method_name Name of the method to check
173
- # @param [Boolean] _include_private boolean if private methods should be checked
174
- #
175
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
176
- #
177
- def respond_to_missing?(method_name, _include_private = false)
178
- @number_item.respond_to?(method_name) ||
179
- ::Kernel.method_defined?(method_name) ||
180
- ::Kernel.private_method_defined?(method_name)
181
- end
182
-
183
153
  %w[+ - * /].each do |operation|
184
154
  define_method(operation) do |other|
185
155
  logger.trace("Execution math operation '#{operation}' on #{inspect} with #{other.inspect}")
@@ -3,6 +3,7 @@
3
3
  require 'forwardable'
4
4
  require 'java'
5
5
  require 'openhab/dsl/items/item_command'
6
+ require 'openhab/dsl/items/item_delegate'
6
7
 
7
8
  module OpenHAB
8
9
  module DSL
@@ -13,6 +14,7 @@ module OpenHAB
13
14
  class RollershutterItem < Numeric
14
15
  extend Forwardable
15
16
  extend OpenHAB::DSL::Items::ItemCommand
17
+ include OpenHAB::DSL::Items::ItemDelegate
16
18
  include Comparable
17
19
 
18
20
  def_delegator :@rollershutter_item, :to_s
@@ -35,6 +37,9 @@ module OpenHAB
35
37
  logger.trace("Wrapping #{rollershutter_item}")
36
38
  @rollershutter_item = rollershutter_item
37
39
 
40
+ item_delegate { @rollershutter_item }
41
+ item_delegate { position }
42
+
38
43
  super()
39
44
  end
40
45
 
@@ -79,19 +84,6 @@ module OpenHAB
79
84
  end
80
85
  end
81
86
 
82
- #
83
- # Case equality
84
- #
85
- # @param [Java::OrgOpenhabCoreLibraryTypes::UpDownType, Numeric] other Other object to compare against
86
- #
87
- # @return [Boolean] true if self can be defined as other, false otherwise
88
- #
89
- def ===(other)
90
- super unless other.is_a? UpDownType
91
-
92
- state.as(UpDownType).equals(other)
93
- end
94
-
95
87
  #
96
88
  # Define math operations
97
89
  #
@@ -101,38 +93,6 @@ module OpenHAB
101
93
  left.send(operator, right)
102
94
  end
103
95
  end
104
-
105
- #
106
- # Checks if this method responds to the missing method
107
- #
108
- # @param [String] meth Name of the method to check
109
- # @param [Boolean] _include_private boolean if private methods should be checked
110
- #
111
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
112
- #
113
- def respond_to_missing?(meth, _include_private = false)
114
- @rollershutter_item.respond_to?(meth) || position.respond_to?(meth)
115
- end
116
-
117
- #
118
- # Forward missing methods to Openhab RollershutterItem or the PercentType representing it's state
119
- # if they are defined
120
- #
121
- # @param [String] meth method name
122
- # @param [Array] args arguments for method
123
- # @param [Proc] block <description>
124
- #
125
- # @return [Object] Value from delegated method in OpenHAB NumberItem
126
- #
127
- def method_missing(meth, *args, &block)
128
- if @rollershutter_item.respond_to?(meth)
129
- @rollershutter_item.__send__(meth, *args, &block)
130
- elsif position.respond_to?(meth)
131
- position.__send__(meth, *args, &block)
132
- else
133
- raise NoMethodError, "No method `#{meth}' defined for #{self.class}"
134
- end
135
- end
136
96
  end
137
97
  end
138
98
  end
@@ -3,6 +3,7 @@
3
3
  require 'bigdecimal'
4
4
  require 'forwardable'
5
5
  require 'java'
6
+ require 'openhab/dsl/items/item_delegate'
6
7
 
7
8
  module OpenHAB
8
9
  module DSL
@@ -12,7 +13,9 @@ module OpenHAB
12
13
  #
13
14
  class StringItem
14
15
  extend Forwardable
16
+
15
17
  include Comparable
18
+ include OpenHAB::DSL::Items::ItemDelegate
16
19
 
17
20
  # @return [Regex] Regular expression matching blank strings
18
21
  BLANK_RE = /\A[[:space:]]*\z/.freeze
@@ -27,6 +30,10 @@ module OpenHAB
27
30
  #
28
31
  def initialize(string_item)
29
32
  @string_item = string_item
33
+
34
+ item_delegate { @string_item }
35
+ item_delegate { @string_item.state&.to_full_string&.to_s }
36
+
30
37
  super()
31
38
  end
32
39
 
@@ -78,42 +85,6 @@ module OpenHAB
78
85
  @string_item.state <=> other
79
86
  end
80
87
  end
81
-
82
- #
83
- # Forward missing methods to Openhab String Item or String representation of the item if they are defined
84
- #
85
- # @param [String] meth method name
86
- # @param [Array] args arguments for method
87
- # @param [Proc] block <description>
88
- #
89
- # @return [Object] Value from delegated method in OpenHAB StringItem or Ruby String
90
- #
91
- def method_missing(meth, *args, &block)
92
- if @string_item.respond_to?(meth)
93
- @string_item.__send__(meth, *args, &block)
94
- elsif @string_item.state&.to_full_string&.to_s.respond_to?(meth)
95
- @string_item.state.to_full_string.to_s.__send__(meth, *args, &block)
96
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
97
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
98
- else
99
- super(meth, *args, &block)
100
- end
101
- end
102
-
103
- #
104
- # Checks if this method responds to the missing method
105
- #
106
- # @param [String] method_name Name of the method to check
107
- # @param [Boolean] _include_private boolean if private methods should be checked
108
- #
109
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
110
- #
111
- def respond_to_missing?(method_name, _include_private = false)
112
- @string_item.respond_to?(method_name) ||
113
- @string_item.state&.to_full_string&.to_s.respond_to?(method_name) ||
114
- ::Kernel.method_defined?(method_name) ||
115
- ::Kernel.private_method_defined?(method_name)
116
- end
117
88
  end
118
89
  end
119
90
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'java'
4
+ require 'openhab/dsl/items/item_command'
5
+ require 'forwardable'
4
6
 
5
7
  module OpenHAB
6
8
  module DSL
@@ -25,31 +27,13 @@ module OpenHAB
25
27
  java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
26
28
  java_import Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
27
29
 
28
- #
29
- # Add the current dimmer value to the supplied object
30
- #
31
- # @param [Object] other object to add the dimmer value to
32
- #
33
- # @return [Integer] Current dimmer value plus value of supplied object
34
- #
35
- def +(other)
36
- return unless state?
30
+ extend Forwardable
31
+ extend OpenHAB::DSL::Items::ItemCommand
37
32
 
38
- state.to_big_decimal.intValue + other
39
- end
33
+ item_state Java::OrgOpenhabCoreLibraryTypes::OnOffType
34
+ item_command Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
40
35
 
41
- #
42
- # Subtract the supplied object from the current value of the dimmer
43
- #
44
- # @param [Object] other object to subtract from the dimmer value
45
- #
46
- # @return [Integer] Current dimmer value minus value of supplied object
47
- #
48
- def -(other)
49
- return unless state?
50
-
51
- state.to_big_decimal.intValue - other
52
- end
36
+ def_delegator :state, :to_s
53
37
 
54
38
  #
55
39
  # Dim the dimmer
@@ -59,17 +43,9 @@ module OpenHAB
59
43
  # @return [Integer] level target for dimmer
60
44
  #
61
45
  def dim(amount = 1)
62
- return unless state?
63
-
64
- target = [state.to_big_decimal.intValue - amount, 0].max
65
-
66
- if amount == 1
67
- command(IncreaseDecreaseType::DECREASE)
68
- else
69
- command(target)
70
- end
71
-
72
- target
46
+ [state&.to_big_decimal&.intValue&.-(amount), 0].compact
47
+ .max
48
+ .tap { |target| command(target) }
73
49
  end
74
50
 
75
51
  #
@@ -80,16 +56,7 @@ module OpenHAB
80
56
  # @return [Integer] level target for dimmer
81
57
  #
82
58
  def brighten(amount = 1)
83
- return unless state?
84
-
85
- target = state.to_big_decimal.intValue + amount
86
-
87
- if amount == 1
88
- command(IncreaseDecreaseType::INCREASE)
89
- else
90
- command(target)
91
- end
92
- target
59
+ state&.to_big_decimal&.intValue&.+(amount)&.tap { |target| command(target) }
93
60
  end
94
61
 
95
62
  #
@@ -120,10 +87,8 @@ module OpenHAB
120
87
  def coerce(other)
121
88
  logger.trace("Coercing #{self} as a request from #{other.class}")
122
89
  case other
123
- when Numeric
124
- [other, state.to_big_decimal.to_d]
125
- else
126
- [other, state]
90
+ when Numeric then [other, state.to_big_decimal.to_d]
91
+ else [other, state]
127
92
  end
128
93
  end
129
94
 
@@ -140,6 +105,16 @@ module OpenHAB
140
105
  (self <=> other).zero?
141
106
  end
142
107
 
108
+ #
109
+ # Define math operations
110
+ #
111
+ %i[+ - / *].each do |operator|
112
+ define_method(operator) do |other|
113
+ right, left = coerce(other)
114
+ left.send(operator, right)
115
+ end
116
+ end
117
+
143
118
  #
144
119
  # Check if dimmer has a state and state is not zero
145
120
  #
@@ -159,33 +134,6 @@ module OpenHAB
159
134
  end
160
135
 
161
136
  alias to_int to_i
162
-
163
- #
164
- # Return the string representation of the dimmer item
165
- #
166
- # @return [String] String version of the dimmer value
167
- #
168
- def to_s
169
- to_i.to_s
170
- end
171
-
172
- #
173
- # Check if dimmer is on
174
- #
175
- # @return [Boolean] True if item is not UNDEF or NULL and has a value greater than 0
176
- #
177
- def on?
178
- state&.to_big_decimal&.intValue&.positive?
179
- end
180
-
181
- #
182
- # Check if dimmer is off
183
- #
184
- # @return [Boolean] True if item is not UNDEF or NULL and has a state of 0
185
- #
186
- def off?
187
- state&.to_big_decimal&.intValue&.zero?
188
- end
189
137
  end
190
138
  end
191
139
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.23.0'
8
+ VERSION = '2.23.1'
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.0
4
+ version: 2.23.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -41,6 +41,7 @@ files:
41
41
  - lib/openhab/dsl/group.rb
42
42
  - lib/openhab/dsl/items/datetime_item.rb
43
43
  - lib/openhab/dsl/items/item_command.rb
44
+ - lib/openhab/dsl/items/item_delegate.rb
44
45
  - lib/openhab/dsl/items/items.rb
45
46
  - lib/openhab/dsl/items/number_item.rb
46
47
  - lib/openhab/dsl/items/rollershutter_item.rb