openhab-scripting 4.30.4 → 4.32.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: 5836c0c28481307c2e5b7e455f78e6a41db200d6d0479b6510ca920a6b4b1327
4
- data.tar.gz: 3dae55830faebd93761bd1250c7d1a82c1fdb0e086fe152c9c7cb4b3791aa817
3
+ metadata.gz: 89c274fd9901e8ccc5d1890a7cf41dbf17ab6d79eb2b798b4e86dde9101cdd11
4
+ data.tar.gz: dea8df6ebfb5b572745df0f4ab2b33ac251d74f0fe694ca4759e227f0588b4c3
5
5
  SHA512:
6
- metadata.gz: cf42c9413c725a57783cd261c89162186573739676f681e0d0a6749c461262a7dbe22fac2d7692a7c39dd36a525e75facb4345f0f61b2ee5e4ea81b09bd0875f
7
- data.tar.gz: 588986cf6160cf230ffe403381cc55b267a29c9bd36f444bb38d446eddbdee7173c7f42efea39082302506eb50c92b60125a9575e19e9063f2015f42098c2ee1
6
+ metadata.gz: 7ddee7681015930c085afe7fa68a6c6b913e414097d8da32ed9519b2d701ad71699f9bc585186f9cf229fb67633f3781995b04099290b67dd781ba9d5e167e93
7
+ data.tar.gz: a8d606a0b5c1fe02822955ad03fd6affbf111a774581cb1706ab7661af874ef907004c73e21bfa445f16780e27f65090b140ab83487b3dc2b6729ac1fba01b03
@@ -54,29 +54,6 @@ module OpenHAB
54
54
  super
55
55
  end
56
56
 
57
- #
58
- # Convert the ColorItem to a hash
59
- # @param [:hsb, :rgb] format for hash
60
- # @return [Hash] in requested format
61
- def to_h(format = :hsb)
62
- values = to_a(format)
63
- keys = (format == :hsb ? %i[hue saturation brightness] : %i[red green blue])
64
- keys.zip(values).to_h
65
- end
66
-
67
- #
68
- # Convert the ColorItem to an array of values
69
- # @param [:hsb, :rgb] format for elements in the array
70
- # @return [Array] of ColorItem components in requested format
71
- def to_a(format = :hsb)
72
- case format
73
- when :hsb then [hue, saturation, brightness]
74
- when :rgb then [red, green, blue].map(&:to_byte)
75
- else
76
- raise ArgumentError, "Unsupported format #{format}"
77
- end
78
- end
79
-
80
57
  private
81
58
 
82
59
  # Mapping of hash values sets to conversion methods
@@ -29,10 +29,11 @@ module OpenHAB
29
29
  combined_commands = Command.combine_commands(command: command, commands: commands)
30
30
 
31
31
  Command.flatten_items(items).map do |item|
32
- logger.states 'Creating received command trigger', item: item, command: command, commands: commands,
33
- combined_commands: combined_commands
32
+ combined_commands.map do |cmd|
33
+ logger.states 'Creating received command trigger', item: item, command: cmd
34
34
 
35
- command_trigger.trigger(item: item, commands: combined_commands, attach: attach)
35
+ command_trigger.trigger(item: item, command: cmd, attach: attach)
36
+ end
36
37
  end.flatten
37
38
  end
38
39
 
@@ -57,6 +58,47 @@ module OpenHAB
57
58
  combined_commands
58
59
  end
59
60
 
61
+ #
62
+ # Create a received command trigger
63
+ #
64
+ # @param [Object] item item to create trigger for
65
+ # @param [Object] command to check against
66
+ # @param [Object] attach attachment
67
+ #
68
+ # @return [Trigger] OpenHAB triggers
69
+ #
70
+ def trigger(item:, command:, attach:)
71
+ case command
72
+ when Range then range_trigger(item: item, command: command, attach: attach)
73
+ when Proc then proc_trigger(item: item, command: command, attach: attach)
74
+ else command_trigger(item: item, command: command, attach: attach)
75
+ end
76
+ end
77
+
78
+ #
79
+ # Creates a trigger with a range condition on the 'command' field
80
+ # @param [Object] item to create changed trigger on
81
+ # @param [Object] command to restrict trigger to
82
+ # @param [Object] attach to trigger
83
+ # @return [Trigger] OpenHAB trigger
84
+ #
85
+ def range_trigger(item:, command:, attach:)
86
+ command_range, * = Conditions::Proc.range_procs(command)
87
+ proc_trigger(item: item, command: command_range, attach: attach)
88
+ end
89
+
90
+ #
91
+ # Creates a trigger with a proc condition on the 'command' field
92
+ # @param [Object] item to create changed trigger on
93
+ # @param [Object] command to restrict trigger to
94
+ # @param [Object] attach to trigger
95
+ # @return [Trigger] OpenHAB trigger
96
+ #
97
+ def proc_trigger(item:, command:, attach:)
98
+ conditions = Conditions::Proc.new(command: command)
99
+ command_trigger(item: item, command: nil, attach: attach, conditions: conditions)
100
+ end
101
+
60
102
  #
61
103
  # Create a received trigger based on item type
62
104
  #
@@ -64,16 +106,14 @@ module OpenHAB
64
106
  # @param [Object] item to create trigger for
65
107
  #
66
108
  #
67
- def trigger(item:, commands:, attach:)
68
- commands.map do |command|
69
- type, config = if item.is_a? OpenHAB::DSL::Items::GroupItem::GroupMembers
70
- group(group: item)
71
- else
72
- item(item: item)
73
- end
74
- config['command'] = command.to_s unless command.nil?
75
- append_trigger(type: type, config: config, attach: attach)
76
- end
109
+ def command_trigger(item:, command:, attach: nil, conditions: nil)
110
+ type, config = if item.is_a? OpenHAB::DSL::Items::GroupItem::GroupMembers
111
+ group(group: item)
112
+ else
113
+ item(item: item)
114
+ end
115
+ config['command'] = command.to_s unless command.nil?
116
+ append_trigger(type: type, config: config, attach: attach, conditions: conditions)
77
117
  end
78
118
 
79
119
  private
@@ -38,9 +38,9 @@ module OpenHAB
38
38
  #
39
39
  def self.range_proc(range)
40
40
  logger.trace("Creating range proc for #{range}")
41
- lambda do |state|
42
- logger.trace("Range proc checking if #{state} is in #{range}")
43
- range.include? state
41
+ lambda do |val|
42
+ logger.trace("Range proc checking if #{val} is in #{range}")
43
+ range.cover? val
44
44
  end
45
45
  end
46
46
 
@@ -77,9 +77,10 @@ module OpenHAB
77
77
  # @param [Proc] from Proc to check against from value
78
78
  # @param [Proc] to Proc to check against to value
79
79
  #
80
- def initialize(from: nil, to: nil)
80
+ def initialize(from: nil, to: nil, command: nil)
81
81
  @from = from
82
82
  @to = to
83
+ @command = command
83
84
  end
84
85
 
85
86
  #
@@ -88,7 +89,18 @@ module OpenHAB
88
89
  #
89
90
  def process(mod:, inputs:) # rubocop:disable Lint/UnusedMethodArgument - mod is unused here but required
90
91
  logger.trace("Checking #{inputs} against condition trigger #{self}")
91
- yield if check_from(inputs: inputs) && check_to(inputs: inputs)
92
+ yield if check_procs(inputs: inputs)
93
+ end
94
+
95
+ #
96
+ # Check if command condition match the proc
97
+ # @param [Hash] inputs from trigger must be supplied if state is not supplied
98
+ # @return [true/false] depending on if from is set and matches supplied conditions
99
+ #
100
+ def check_command(inputs: nil)
101
+ command = input_state(inputs, 'command')
102
+ logger.trace "Checking command(#{@command}) against command(#{command})"
103
+ check_proc(proc: @command, value: command)
92
104
  end
93
105
 
94
106
  #
@@ -100,7 +112,7 @@ module OpenHAB
100
112
  def check_from(inputs: nil, state: nil)
101
113
  state ||= input_state(inputs, 'oldState')
102
114
  logger.trace "Checking from(#{@from}) against state(#{state})"
103
- check_proc(proc: @from, state: state)
115
+ check_proc(proc: @from, value: state)
104
116
  end
105
117
 
106
118
  #
@@ -112,27 +124,34 @@ module OpenHAB
112
124
  def check_to(inputs: nil, state: nil)
113
125
  state ||= input_state(inputs, 'newState', 'state')
114
126
  logger.trace "Checking to(#{@to}) against state(#{state})"
115
- check_proc(proc: @to, state: state)
127
+ check_proc(proc: @to, value: state)
116
128
  end
117
129
 
118
130
  # Describe the Proc Condition as a string
119
131
  # @return [String] string representation of proc condition
120
132
  #
121
133
  def to_s
122
- "From:(#{@from}) To:(#{@to})"
134
+ "From:(#{@from}) To:(#{@to}) Command:(#{@command})"
123
135
  end
124
136
 
125
137
  private
126
138
 
139
+ #
140
+ # Check all procs
141
+ # @param [Hash] inputs from event
142
+ # @return [true/false] true if all procs return true, false otherwise
143
+ def check_procs(inputs:)
144
+ check_from(inputs: inputs) && check_to(inputs: inputs) && check_command(inputs: inputs)
145
+ end
146
+
127
147
  # Check if a field matches the proc condition
128
148
  # @param [Proc] proc to call
129
- # @param [Hash] inputs containing fields
130
- # @param [Array] fields array of fields to extract from inputs, first one found is passed to proc
149
+ # @param [Hash] value to check
131
150
  # @return [true,false] true if proc is nil or proc.call returns true, false otherwise
132
- def check_proc(proc:, state:)
133
- return true if proc.nil? || proc.call(state)
151
+ def check_proc(proc:, value:)
152
+ return true if proc.nil? || proc.call(value)
134
153
 
135
- logger.trace("Skipped execution of rule because state #{state} evalulated false for (#{proc})")
154
+ logger.trace("Skipped execution of rule because value #{value} evaluated false for (#{proc})")
136
155
  false
137
156
  end
138
157
 
@@ -105,13 +105,15 @@ module OpenHAB
105
105
  #
106
106
  # nil is returned if the two values are incomparable
107
107
  #
108
- def <=>(other)
108
+ def <=>(other) # rubocop:disable Metrics
109
109
  logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
110
110
  if other.is_a?(Items::ColorItem) ||
111
111
  (other.is_a?(Items::GroupItem) && other.base_item.is_a?(ColorItem))
112
- return false unless other.state?
112
+ return nil unless other.state?
113
113
 
114
114
  self <=> other.state
115
+ elsif other.is_a?(HSBType)
116
+ to_a <=> other.to_a
115
117
  elsif other.respond_to?(:to_str)
116
118
  self <=> HSBType.new(other)
117
119
  else
@@ -178,6 +180,29 @@ module OpenHAB
178
180
  "#{hue},#{saturation},#{brightness}"
179
181
  end
180
182
 
183
+ #
184
+ # Convert the ColorItem to a hash
185
+ # @param [:hsb, :rgb] format for hash
186
+ # @return [Hash] in requested format
187
+ def to_h(format = :hsb)
188
+ values = to_a(format)
189
+ keys = (format == :hsb ? %i[hue saturation brightness] : %i[red green blue])
190
+ keys.zip(values).to_h
191
+ end
192
+
193
+ #
194
+ # Convert the ColorItem to an array of values
195
+ # @param [:hsb, :rgb] format for elements in the array
196
+ # @return [Array] of ColorItem components in requested format
197
+ def to_a(format = :hsb)
198
+ case format
199
+ when :hsb then [hue, saturation, brightness]
200
+ when :rgb then [red, green, blue].map(&:to_byte)
201
+ else
202
+ raise ArgumentError, "Unsupported format #{format}"
203
+ end
204
+ end
205
+
181
206
  # @!attribute [r] saturation
182
207
  # @return [PercentType]
183
208
 
@@ -80,7 +80,7 @@ module OpenHAB
80
80
  end
81
81
 
82
82
  # any method that exists on String gets forwarded to to_s
83
- delegate (String.instance_methods - instance_methods) => :to_s
83
+ delegate (String.instance_methods - instance_methods + %w[=~ inspect]) => :to_s
84
84
  end
85
85
  end
86
86
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.30.4'
8
+ VERSION = '4.32.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: 4.30.4
4
+ version: 4.32.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: 2022-01-21 00:00:00.000000000 Z
11
+ date: 2022-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler