openhab-scripting 4.29.0 → 4.30.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: 346933e8c00b9208f18ce61d4a87911cdc315a7b7ee1698aec1977dc47ba9bea
4
- data.tar.gz: 5a94e6d5a1d43a4fd3e6586bd70a9f3286918bee1c300fe490d2460dbdce3494
3
+ metadata.gz: a4e5de50bc22188eb5350aa71f00738703afab2e75703c9f5c2f4ffce54c49dc
4
+ data.tar.gz: e42484957fcea08aaf5d397af1ec5012b5595bd893eacca7bbc2db3149a1707b
5
5
  SHA512:
6
- metadata.gz: dcd2f7d6aa7b11074c059296b61b0fa22b691184915afe227ecf41d865690b6a81c8ea431c3b83407f05520aedca8238be4636f8da62217a0caf090d63bd8488
7
- data.tar.gz: 1614e96cd679ebab6dfe811e7818e77e9e512603b126223f6a870f7b6550a2101cb86fe79f9856fe6559cc926cecf212afe15eb8a50892863c208b32d00f6386
6
+ metadata.gz: af5435c5804d2a58f1e8cf84647ee1813c80c958e9b1c3c6794d6879adeebd71ac6955454ad424522916375d7314719312b561d7b2a718ad43b48e76edd2801c
7
+ data.tar.gz: 2f3e46d17e305d1169980fdf2aa5ae8f5673e4730d73d2d10dbb445582642086522d3ab43e9bae5a411637ec1e787d465431423fb307a04ed09178be5953c25a
@@ -32,6 +32,13 @@ module OpenHAB
32
32
  group.get_members.to_a
33
33
  end
34
34
 
35
+ # Name of the group
36
+ #
37
+ # @return [String]
38
+ def name
39
+ group.name
40
+ end
41
+
35
42
  # Send a command to each member of the group
36
43
  #
37
44
  # @return [GroupMembers] +self+
@@ -35,7 +35,7 @@ module OpenHAB
35
35
  # Explicit conversion to array
36
36
  # @return [Array]
37
37
  def to_a
38
- $ir.items.grep_v(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
38
+ $ir.items.to_a # rubocop:disable Style/GlobalVars
39
39
  end
40
40
  end
41
41
 
@@ -10,7 +10,7 @@ require_relative 'triggers/command'
10
10
  require_relative 'triggers/updated'
11
11
  require_relative 'triggers/generic'
12
12
  require_relative 'triggers/watch'
13
- require_relative 'triggers/conditions/none'
13
+ require_relative 'triggers/conditions/proc'
14
14
  require_relative 'guard'
15
15
  require 'openhab/core/entity_lookup'
16
16
  require 'openhab/dsl/between'
@@ -87,7 +87,7 @@ module OpenHAB
87
87
  #
88
88
  def initialize(rule_name, caller_binding)
89
89
  @triggers = []
90
- @trigger_conditions = Hash.new(OpenHAB::DSL::Rules::Triggers::Conditions::None.instance)
90
+ @trigger_conditions = Hash.new(OpenHAB::DSL::Rules::Triggers::Conditions::Proc::ANY)
91
91
  @attachments = {}
92
92
  @caller = caller_binding.eval 'self'
93
93
  name(rule_name)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'openhab/log/logger'
4
4
  require_relative 'conditions/duration'
5
- require_relative 'conditions/range'
5
+ require_relative 'conditions/proc'
6
6
 
7
7
  module OpenHAB
8
8
  module DSL
@@ -74,6 +74,8 @@ module OpenHAB
74
74
  changed_wait(item, from: from, to: to, duration: duration, attach: attach)
75
75
  elsif [to, from].grep(Range).any?
76
76
  create_changed_range_trigger(item, from: from, to: to, attach: attach)
77
+ elsif [to, from].grep(Proc).any?
78
+ create_changed_proc_trigger(item, from: from, to: to, attach: attach)
77
79
  else
78
80
  create_changed_trigger(item, from, to, attach)
79
81
  end
@@ -91,10 +93,12 @@ module OpenHAB
91
93
  # @return [Trigger] OpenHAB trigger
92
94
  #
93
95
  def changed_wait(item, duration:, to: nil, from: nil, attach: nil)
94
- trigger = create_changed_trigger(item, nil, nil, attach)
95
- logger.trace("Creating Changed Wait Change Trigger for #{item}")
96
- @trigger_conditions[trigger.id] = Conditions::Duration.new(to: to, from: from, duration: duration)
97
- trigger
96
+ item_name = item.respond_to?(:name) ? item.name : item.to_s
97
+ logger.trace("Creating Changed Wait Change Trigger for Item(#{item_name}) Duration(#{duration}) "\
98
+ "To(#{to}) From(#{from}) Attach(#{attach})")
99
+ create_changed_trigger(item, nil, nil, attach).tap do |trigger|
100
+ @trigger_conditions[trigger.id] = Conditions::Duration.new(to: to, from: from, duration: duration)
101
+ end
98
102
  end
99
103
 
100
104
  #
@@ -106,11 +110,26 @@ module OpenHAB
106
110
  # @return [Trigger] OpenHAB trigger
107
111
  #
108
112
  def create_changed_range_trigger(item, from:, to:, attach:)
109
- # swap range w/ nil if from or to is a range
110
- from_range, from = from, from_range if from.is_a? Range
111
- to_range, to = to, to_range if to.is_a? Range
113
+ from, to = Conditions::Proc.range_procs(from, to)
114
+ create_changed_proc_trigger(item, from: from, to: to, attach: attach)
115
+ end
116
+
117
+ #
118
+ # Creates a trigger with a proc condition on either 'from' or 'to' field
119
+ # @param [Object] item to create changed trigger on
120
+ # @param [Object] from state to restrict trigger to
121
+ # @param [Object] to state restrict trigger to
122
+ # @param [Object] attach to trigger
123
+ # @return [Trigger] OpenHAB trigger
124
+ #
125
+ def create_changed_proc_trigger(item, from:, to:, attach:)
126
+ # swap from/to w/ nil if from/to is a proc
127
+ # rubocop:disable Style/ParallelAssignment
128
+ from_proc, from = from, nil if from.is_a? Proc
129
+ to_proc, to = to, nil if to.is_a? Proc
130
+ # rubocop:enable Style/ParallelAssignment
112
131
  trigger = create_changed_trigger(item, from, to, attach)
113
- @trigger_conditions[trigger.id] = Conditions::Range.new(to: to_range, from: from_range)
132
+ @trigger_conditions[trigger.id] = Conditions::Proc.new(to: to_proc, from: from_proc)
114
133
  trigger
115
134
  end
116
135
 
@@ -23,12 +23,20 @@ module OpenHAB
23
23
  # timer&.is_active
24
24
  # end
25
25
  # end
26
-
27
26
  class Duration
27
+ #
28
+ # Create a new duration condition
29
+ # @param [Object] to optional condition on to state
30
+ # @param [Object] from optional condition on from state
31
+ # @param [Duration] Duration to state must stay at specific value before triggering
32
+ #
28
33
  def initialize(to:, from:, duration:)
29
- @to = to
30
- @from = from
34
+ to = Conditions::Proc.from_value(to)
35
+ from = Conditions::Proc.from_value(from)
36
+ @conditions = Conditions::Proc.new(to: to, from: from)
31
37
  @duration = duration
38
+ logger.trace "Created Duration Condition To(#{to}) From(#{from}) "\
39
+ "Conditions(#{@conditions}) Duration(#{@duration})"
32
40
  end
33
41
 
34
42
  # Process rule
@@ -56,15 +64,7 @@ module OpenHAB
56
64
  #
57
65
  def check_trigger_guards(inputs)
58
66
  new_state, old_state = retrieve_states(inputs)
59
- if check_from(old_state)
60
- return true if check_to(new_state)
61
-
62
- logger.trace("Skipped execution of rule because to state #{new_state}"\
63
- " does not equal specified state(#{@to})")
64
- else
65
- logger.trace("Skipped execution of rule because old state #{old_state}"\
66
- " does not equal specified state(#{@from})")
67
- end
67
+ @conditions.check_from(state: old_state) && @conditions.check_to(state: new_state)
68
68
  end
69
69
 
70
70
  #
@@ -76,8 +76,8 @@ module OpenHAB
76
76
  # @return [Array] An array of the values for [newState, oldState] or [newStatus, oldStatus]
77
77
  #
78
78
  def retrieve_states(inputs)
79
- old_state = inputs['oldState'] || thing_status_to_sym(inputs['oldStatus'])
80
79
  new_state = inputs['newState'] || thing_status_to_sym(inputs['newStatus'])
80
+ old_state = inputs['oldState'] || thing_status_to_sym(inputs['oldStatus'])
81
81
 
82
82
  [new_state, old_state]
83
83
  end
@@ -93,32 +93,6 @@ module OpenHAB
93
93
  status&.to_s&.downcase&.to_sym
94
94
  end
95
95
 
96
- #
97
- # Check the from state against the trigger delay
98
- #
99
- # @param [Item State] state from state to check
100
- #
101
- # @return [Boolean] true if no from state is defined or defined state equals supplied state
102
- #
103
- def check_from(state)
104
- return @from.include?(state) if @from.is_a?(::Range)
105
-
106
- @from.nil? || @from == state
107
- end
108
-
109
- #
110
- # Check the to state against the trigger delay
111
- #
112
- # @param [Item State] state to-state to check
113
- #
114
- # @return [Boolean] true if no to state is defined or defined state equals supplied state
115
- #
116
- def check_to(state)
117
- return @to.include?(state) if @to.is_a?(::Range)
118
-
119
- @to.nil? || @to == state
120
- end
121
-
122
96
  #
123
97
  # Process any matching trigger delays
124
98
  #
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openhab/log/logger'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module Rules
8
+ module Triggers
9
+ #
10
+ # Module for conditions for triggers
11
+ #
12
+ module Conditions
13
+ include OpenHAB::Log
14
+
15
+ #
16
+ # This creates trigger conditions that work on procs
17
+ # @param [Proc] from Proc
18
+ # @param [Proc] to Proc
19
+ #
20
+ class Proc
21
+ include OpenHAB::Log
22
+
23
+ # Proc that doesn't check any fields
24
+ ANY = Proc.new.freeze
25
+
26
+ #
27
+ # Converts supplied ranges to procs that check range
28
+ # @param [Array] ranges objects to convert to range proc if they are ranges
29
+ # @return [Array] of procs or supplied arguments if argument was not of type Range
30
+ #
31
+ def self.range_procs(*ranges)
32
+ ranges.map { |range| range.is_a?(Range) ? range_proc(range) : range }
33
+ end
34
+
35
+ #
36
+ # Create a range proc for the supplied range object
37
+ # @param [Range] range to build proc for
38
+ #
39
+ def self.range_proc(range)
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
44
+ end
45
+ end
46
+
47
+ #
48
+ # Create a range proc for the supplied range object
49
+ # @param [Range] range to build proc for
50
+ #
51
+ def self.equality_proc(value)
52
+ logger.trace("Creating equality proc for #{value}")
53
+ lambda do |state|
54
+ logger.trace("Equality proc comparing #{value} against #{state}")
55
+ value == state
56
+ end
57
+ end
58
+
59
+ #
60
+ # Constructs a proc for the specific value type
61
+ # if the value is a proc return the proc
62
+ # if the value is a range create a range proc
63
+ # if the value is nil, return nil
64
+ # otherwise create an equality proc
65
+ # @param [Object] value to construct proc from
66
+ def self.from_value(value)
67
+ logger.trace("Creating proc for Value(#{value})")
68
+ return value if value.nil?
69
+ return value if value.is_a? ::Proc
70
+ return range_proc(value) if value.is_a? Range
71
+
72
+ equality_proc(value)
73
+ end
74
+
75
+ #
76
+ # Create a new Proc Condition that executes only if procs return true
77
+ # @param [Proc] from Proc to check against from value
78
+ # @param [Proc] to Proc to check against to value
79
+ #
80
+ def initialize(from: nil, to: nil)
81
+ @from = from
82
+ @to = to
83
+ end
84
+
85
+ #
86
+ # Process rule
87
+ # @param [Hash] inputs inputs from trigger
88
+ #
89
+ def process(mod:, inputs:) # rubocop:disable Lint/UnusedMethodArgument - mod is unused here but required
90
+ logger.trace("Checking #{inputs} against condition trigger #{self}")
91
+ yield if check_from(inputs: inputs) && check_to(inputs: inputs)
92
+ end
93
+
94
+ #
95
+ # Check if from condition match the proc
96
+ # @param [Hash] inputs from trigger must be supplied if state is not supplied
97
+ # @param [String] state if supplied proc will be passed state value for comparision
98
+ # @return [true/false] depending on if from is set and matches supplied conditions
99
+ #
100
+ def check_from(inputs: nil, state: nil)
101
+ state ||= input_state(inputs, 'oldState')
102
+ logger.trace "Checking from(#{@from}) against state(#{state})"
103
+ check_proc(proc: @from, state: state)
104
+ end
105
+
106
+ #
107
+ # Check if to conditions match the proc
108
+ # @param [Hash] inputs from trigger must be supplied if state is not supplied
109
+ # @param [String] state if supplied proc will be passed state value for comparision
110
+ # @return [true/false] depending on if from is set and matches supplied conditions
111
+ #
112
+ def check_to(inputs: nil, state: nil)
113
+ state ||= input_state(inputs, 'newState', 'state')
114
+ logger.trace "Checking to(#{@to}) against state(#{state})"
115
+ check_proc(proc: @to, state: state)
116
+ end
117
+
118
+ # Describe the Proc Condition as a string
119
+ # @return [String] string representation of proc condition
120
+ #
121
+ def to_s
122
+ "From:(#{@from}) To:(#{@to})"
123
+ end
124
+
125
+ private
126
+
127
+ # Check if a field matches the proc condition
128
+ # @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
131
+ # @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)
134
+
135
+ logger.trace("Skipped execution of rule because state #{state} evalulated false for (#{proc})")
136
+ false
137
+ end
138
+
139
+ # Get the first field from supplied fields in inputs
140
+ # @param [Hash] inputs containing fields
141
+ # @param [Array] fields array of fields to extract from inputs, first one found is returned
142
+ def input_state(inputs, *fields)
143
+ fields.map { |f| inputs[f] }.compact.first
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -42,10 +42,10 @@ module OpenHAB
42
42
  # @return [Trigger] OpenHAB triggers
43
43
  #
44
44
  def update_trigger(item:, to:, attach:)
45
- if to.is_a? Range
46
- create_update_range_trigger(item: item, to: to, attach: attach)
47
- else
48
- create_update_trigger(item: item, to: to, attach: attach)
45
+ case to
46
+ when Range then create_update_range_trigger(item: item, to: to, attach: attach)
47
+ when Proc then create_update_proc_trigger(item: item, to: to, attach: attach)
48
+ else create_update_trigger(item: item, to: to, attach: attach)
49
49
  end
50
50
  end
51
51
 
@@ -57,9 +57,21 @@ module OpenHAB
57
57
  # @return [Trigger] OpenHAB trigger
58
58
  #
59
59
  def create_update_range_trigger(item:, to:, attach:)
60
- trigger = create_update_trigger(item: item, to: nil, attach: attach)
61
- @trigger_conditions[trigger.id] = Conditions::Range.new(to: to, from: nil)
62
- trigger
60
+ to, * = Conditions::Proc.range_procs(to)
61
+ create_update_proc_trigger(item: item, to: to, attach: attach)
62
+ end
63
+
64
+ #
65
+ # Creates a trigger with a proc condition on the 'to' field
66
+ # @param [Object] item to create changed trigger on
67
+ # @param [Object] to state restrict trigger to
68
+ # @param [Object] attach to trigger
69
+ # @return [Trigger] OpenHAB trigger
70
+ #
71
+ def create_update_proc_trigger(item:, to:, attach:)
72
+ create_update_trigger(item: item, to: nil, attach: attach).tap do |trigger|
73
+ @trigger_conditions[trigger.id] = Conditions::Proc.new(to: to)
74
+ end
63
75
  end
64
76
 
65
77
  #
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.29.0'
8
+ VERSION = '4.30.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.29.0
4
+ version: 4.30.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-17 00:00:00.000000000 Z
11
+ date: 2022-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -107,8 +107,7 @@ files:
107
107
  - lib/openhab/dsl/rules/triggers/channel.rb
108
108
  - lib/openhab/dsl/rules/triggers/command.rb
109
109
  - lib/openhab/dsl/rules/triggers/conditions/duration.rb
110
- - lib/openhab/dsl/rules/triggers/conditions/none.rb
111
- - lib/openhab/dsl/rules/triggers/conditions/range.rb
110
+ - lib/openhab/dsl/rules/triggers/conditions/proc.rb
112
111
  - lib/openhab/dsl/rules/triggers/cron.rb
113
112
  - lib/openhab/dsl/rules/triggers/generic.rb
114
113
  - lib/openhab/dsl/rules/triggers/trigger.rb
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'openhab/log/logger'
4
- require 'singleton'
5
-
6
- module OpenHAB
7
- module DSL
8
- module Rules
9
- module Triggers
10
- #
11
- # Module for conditions for triggers
12
- #
13
- module Conditions
14
- include OpenHAB::Log
15
- #
16
- # this is a no-op condition which simply executes the provided block
17
- #
18
- class None
19
- include Singleton
20
-
21
- # Process rule
22
- # @param [Hash] inputs inputs from trigger
23
- #
24
- def process(*)
25
- yield
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'openhab/log/logger'
4
-
5
- module OpenHAB
6
- module DSL
7
- module Rules
8
- module Triggers
9
- #
10
- # Module for conditions for triggers
11
- #
12
- module Conditions
13
- include OpenHAB::Log
14
-
15
- #
16
- # This creates trigger conditions that work on ranges
17
- # @param [Range:] From range
18
- # @param [To:] To range
19
- #
20
- class Range
21
- def initialize(from: nil, to: nil)
22
- @from = from
23
- @to = to
24
- end
25
-
26
- #
27
- # Process rule
28
- # @param [Hash] inputs inputs from trigger
29
- #
30
- def process(mod:, inputs:) # rubocop:disable Lint/UnusedMethodArgument - mod is unused here but required
31
- logger.trace("Checking #{inputs} against condition trigger #{self}")
32
- yield if check_from(inputs: inputs) && check_to(inputs: inputs)
33
- end
34
-
35
- #
36
- # Check if from condition match the inputs
37
- # @param [Hash] inputs inputs from trigger
38
- # @return [true/false] depending on if from is set and matches supplied conditions
39
- #
40
- def check_from(inputs:)
41
- old_state = inputs['oldState']
42
- return true if @from.nil? || @from.include?(old_state)
43
-
44
- logger.trace("Skipped execution of rule because old state #{old_state}"\
45
- " does not equal specified range(#{@from})")
46
- false
47
- end
48
-
49
- #
50
- # Check if to conditions match the inputs
51
- # @param [Hash] inputs inputs from trigger
52
- # @return [true/false] depending on if from is set and matches supplied conditions
53
- #
54
- def check_to(inputs:)
55
- new_state = inputs['newState'] || inputs['state'] # Get state for changed or update
56
- return true if @to.nil? || @to.include?(new_state)
57
-
58
- logger.trace("Skipped execution of rule because new state #{new_state}"\
59
- " does not equal specified range(#{@to})")
60
- false
61
- end
62
-
63
- # Describe the Range Condition as a string
64
- # @return [String] string representation of range condition
65
- #
66
- def to_s
67
- "From:(#{@from}) To:(#{@to})"
68
- end
69
- end
70
- end
71
- end
72
- end
73
- end
74
- end