openhab-scripting 2.22.0 → 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 +4 -4
- data/lib/openhab/dsl/items/datetime_item.rb +4 -30
- data/lib/openhab/dsl/items/item_delegate.rb +91 -0
- data/lib/openhab/dsl/items/number_item.rb +4 -34
- data/lib/openhab/dsl/items/rollershutter_item.rb +5 -45
- data/lib/openhab/dsl/items/string_item.rb +7 -36
- data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -0
- data/lib/openhab/dsl/monkey_patch/events/item_event.rb +28 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state.rb +23 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -11
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +23 -75
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +3 -5
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +52 -23
- data/lib/openhab/dsl/rules/automation_rule.rb +33 -24
- data/lib/openhab/dsl/rules/triggers/changed.rb +2 -10
- data/lib/openhab/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e2ae7c9c46b7dff785181b7846dc6e6e6b9f27d445d7f1b7cc108814e1e75531
|
|
4
|
+
data.tar.gz: fffb46ad699350c94c580a3cb8ee2545a3c7c5372a81d4c7dc867cb834ee33db
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 790a2b3838ce8847bf3dafe567336400c43dae0b9d638318c7c1d70539f60ed45714af3f376f2a93f27265b222dd35474b1fb477eb1c892258cea401ab3ff321
|
|
7
|
+
data.tar.gz: 5085945c207f2b00353ca899fab65a2e0990751c083b433729b22e3d798dac11b1a4e99a3d07fdaf6db439d5787e06b9d9a984661496b8a767d13d668f077b43
|
|
@@ -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,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'openhab/dsl/monkey_patch/events/item_state'
|
|
4
|
+
require 'openhab/dsl/monkey_patch/events/item_event'
|
|
3
5
|
require 'openhab/dsl/monkey_patch/events/item_state_changed'
|
|
4
6
|
require 'openhab/dsl/monkey_patch/events/thing_status_info'
|
|
5
7
|
require 'openhab/dsl/monkey_patch/events/item_command'
|
|
@@ -0,0 +1,28 @@
|
|
|
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::ItemEvent
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# MonkeyPatch to add item
|
|
16
|
+
#
|
|
17
|
+
class ItemEvent
|
|
18
|
+
#
|
|
19
|
+
# Return a decorated item
|
|
20
|
+
#
|
|
21
|
+
def item
|
|
22
|
+
OpenHAB::Core::EntityLookup.lookup_item(item_name)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -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
|
|
@@ -15,17 +15,6 @@ module OpenHAB
|
|
|
15
15
|
# MonkeyPatch with ruby style accessors
|
|
16
16
|
#
|
|
17
17
|
class ItemStateChangedEvent
|
|
18
|
-
#
|
|
19
|
-
# Get the item that caused the state change
|
|
20
|
-
#
|
|
21
|
-
# @return [Item] Item that caused state change
|
|
22
|
-
#
|
|
23
|
-
def item
|
|
24
|
-
# rubocop:disable Style/GlobalVars
|
|
25
|
-
$ir.get(item_name)
|
|
26
|
-
# rubocop:enable Style/GlobalVars
|
|
27
|
-
end
|
|
28
|
-
|
|
29
18
|
alias state item_state
|
|
30
19
|
alias last old_item_state
|
|
31
20
|
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
|
-
|
|
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
|
-
|
|
39
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -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
|
-
|
|
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)
|
|
@@ -8,7 +8,23 @@ module OpenHAB
|
|
|
8
8
|
# Persistence extension for Items
|
|
9
9
|
#
|
|
10
10
|
module Persistence
|
|
11
|
-
|
|
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
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
#
|
|
@@ -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
|
|
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
|
#
|
|
@@ -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
|
|
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
|
|
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))
|
|
@@ -218,21 +243,6 @@ module OpenHAB
|
|
|
218
243
|
false
|
|
219
244
|
end
|
|
220
245
|
|
|
221
|
-
#
|
|
222
|
-
# Patch event to decorate event.item with our item wrapper
|
|
223
|
-
#
|
|
224
|
-
# @param [OpenHAB Event] event patch
|
|
225
|
-
#
|
|
226
|
-
def decorate_event_item(event)
|
|
227
|
-
return if event.nil?
|
|
228
|
-
|
|
229
|
-
class << event
|
|
230
|
-
def item
|
|
231
|
-
OpenHAB::Core::EntityLookup.lookup_item(item_name)
|
|
232
|
-
end
|
|
233
|
-
end
|
|
234
|
-
end
|
|
235
|
-
|
|
236
246
|
#
|
|
237
247
|
# Process the run queue
|
|
238
248
|
#
|
|
@@ -262,7 +272,6 @@ module OpenHAB
|
|
|
262
272
|
#
|
|
263
273
|
#
|
|
264
274
|
def process_otherwise_task(event, task)
|
|
265
|
-
decorate_event_item(event)
|
|
266
275
|
logger.trace { "Executing rule '#{name}' otherwise block with event(#{event})" }
|
|
267
276
|
task.block.call(event)
|
|
268
277
|
end
|
|
@@ -289,9 +298,10 @@ module OpenHAB
|
|
|
289
298
|
#
|
|
290
299
|
#
|
|
291
300
|
def process_trigger_task(event, task)
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
301
|
+
return unless event&.item
|
|
302
|
+
|
|
303
|
+
logger.trace { "Executing rule '#{name}' trigger block with item (#{event.item})" }
|
|
304
|
+
task.block.call(event.item)
|
|
295
305
|
end
|
|
296
306
|
|
|
297
307
|
#
|
|
@@ -302,7 +312,6 @@ module OpenHAB
|
|
|
302
312
|
#
|
|
303
313
|
#
|
|
304
314
|
def process_run_task(event, task)
|
|
305
|
-
decorate_event_item(event)
|
|
306
315
|
logger.trace { "Executing rule '#{name}' run block with event(#{event})" }
|
|
307
316
|
task.block.call(event)
|
|
308
317
|
end
|
|
@@ -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
|
-
|
|
62
|
-
|
|
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
|
|
data/lib/openhab/version.rb
CHANGED
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.
|
|
4
|
+
version: 2.23.3
|
|
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-
|
|
11
|
+
date: 2021-02-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -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
|
|
@@ -49,6 +50,8 @@ files:
|
|
|
49
50
|
- lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
|
|
50
51
|
- lib/openhab/dsl/monkey_patch/events/events.rb
|
|
51
52
|
- lib/openhab/dsl/monkey_patch/events/item_command.rb
|
|
53
|
+
- lib/openhab/dsl/monkey_patch/events/item_event.rb
|
|
54
|
+
- lib/openhab/dsl/monkey_patch/events/item_state.rb
|
|
52
55
|
- lib/openhab/dsl/monkey_patch/events/item_state_changed.rb
|
|
53
56
|
- lib/openhab/dsl/monkey_patch/events/thing_status_info.rb
|
|
54
57
|
- lib/openhab/dsl/monkey_patch/items/contact_item.rb
|