openhab-scripting 4.30.5 → 4.32.2

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: 6b08e3b890a03ea51fe482ae4c54f1e69c85f7e6bd1fa2efb3289369906ea6cf
4
- data.tar.gz: 0ffd8091cba81d6e41e1d893433ddee3290bb791f8d09fc21320485fbdec9504
3
+ metadata.gz: 8d988eed567dd4fb75d8f43550f005cc1e09264310157c6ec7f267f82d68f6af
4
+ data.tar.gz: f790a909228f3671204fe6e00c27064ee25ab69154f4b54a21efe233d36d6b2b
5
5
  SHA512:
6
- metadata.gz: cc315816395abcde25be7a0722d1190b001a17758a7dd2f513e6ff1d35b7ce0f5d9d35d817f4bd045d5812492aed2f280b7065ee3797a0eba04ac74ae4b20291
7
- data.tar.gz: 1d1362ef4aff0a0560995322f7b4c52c0255291b2f8d2041418be419fe4a899c163624a7bdee6323b85af67677a89851a8247857e111a228e1fdf19b07049e21
6
+ metadata.gz: bf7f712761e6b051f674c0709abba39f13ca3cbc52db417cb7a5c40f013425f860b52bd8ff840f1bb60a81cbda845899c348a9da2045ad0b0480816e70478f4d
7
+ data.tar.gz: 9c40d8bb624713aba27f93edc5c0dac12d003c14d3db47a37715954b7c788cf0d0f195e32797cd3a454eb984d57a05eb650660e585f476aff7ded57c4e6c5fff
@@ -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
 
@@ -8,7 +8,8 @@ module OpenHAB
8
8
  #
9
9
  # Provides access to the OpenHAB logging using a Ruby logging methods
10
10
  #
11
- module Log
11
+
12
+ module Core
12
13
  #
13
14
  # Ruby Logger that forwards messages at appropriate levels to OpenHAB Logger
14
15
  #
@@ -105,7 +106,9 @@ module OpenHAB
105
106
  #
106
107
  def log_exception(exception, rule_name)
107
108
  exception = clean_backtrace(exception)
108
- error { "#{exception.message} (#{exception.class})\nIn rule: #{rule_name}\n#{exception.backtrace&.join("\n")}" }
109
+ error do
110
+ "#{exception.message} (#{exception.class})\nIn rule: #{rule_name}\n#{exception.backtrace&.join("\n")}"
111
+ end
109
112
  end
110
113
 
111
114
  private
@@ -152,6 +155,12 @@ module OpenHAB
152
155
  end
153
156
  end
154
157
  end
158
+ end
159
+
160
+ module Log
161
+ #
162
+ # Ruby Logger that forwards messages at appropriate levels to OpenHAB Logger
163
+ #
155
164
 
156
165
  # Logger caches
157
166
  @loggers = {}
@@ -180,7 +189,7 @@ module OpenHAB
180
189
  # of logger name requires lots of operations and logger
181
190
  # names for some objects are specific to the class
182
191
  logger_name = logger_name(object)
183
- @loggers[logger_name] ||= Logger.new(logger_name)
192
+ @loggers[logger_name] ||= OpenHAB::Core::Logger.new(logger_name)
184
193
  end
185
194
 
186
195
  private
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.30.5'
8
+ VERSION = '4.32.2'
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.5
4
+ version: 4.32.2
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-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler