openhab-scripting 4.31.0 → 4.32.0

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: d1e47b2bd8ffa13c3736bf92e2007ba10f733f6af21733f75aacb3edf579a100
4
- data.tar.gz: 6ba382d4a2620626a2a58519c71aa17d2dff84748a7e29de991b254d4d3ee5ae
3
+ metadata.gz: 737517891b51326c4c0ab343c448cca3f07a4855ca44222a8564d107d5101cf4
4
+ data.tar.gz: 0cd4aea92b89e152c7bc7bb0b1e759a22609d833f1022510ec348ff4ad634812
5
5
  SHA512:
6
- metadata.gz: 0bd7f2697b7135c4eed32918613be450e52ee257f07a200e5c777e2d04f1872696a58df81e352f0dd82f6f4cebfc03bcc75fa0eefa1a138c796de7ee4b87727a
7
- data.tar.gz: ba920e1fdf93981f749873b28aa271664a9410e1152db9b6071ad6a50e869868098e0d96ae791a667e963d5219c761b1e2017efa600c620efae3274b5eb1b8d5
6
+ metadata.gz: c479724e15cb266f9f797f2e6c5646384f089ff83164890cfd2c6924d1ec0b07011015d4195a4e6de30dfa06da8c2617d2528de4652f8432afd4b2cef4c9bb21
7
+ data.tar.gz: 6e52ab144a7ba4118c39b9d843ba33a16a9f66903b6f9cdd6fd1cb585febdc121b5934bdd59ae813422c2e6325755302a0285f002427258762ae275214f686ce
@@ -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.cover? 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
 
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.31.0'
8
+ VERSION = '4.32.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: 4.31.0
4
+ version: 4.32.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: 2022-01-22 00:00:00.000000000 Z
11
+ date: 2022-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler