openhab-scripting 4.32.6 → 4.32.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +0 -4
  3. data/lib/openhab/core/openhab_setup.rb +1 -2
  4. data/lib/openhab/core/thread_local.rb +12 -2
  5. data/lib/openhab/dsl/actions.rb +2 -0
  6. data/lib/openhab/dsl/between.rb +2 -0
  7. data/lib/openhab/dsl/dsl.rb +2 -2
  8. data/lib/openhab/dsl/group.rb +15 -11
  9. data/lib/openhab/dsl/items/ensure.rb +71 -71
  10. data/lib/openhab/dsl/items/generic_item.rb +1 -1
  11. data/lib/openhab/dsl/items/item_registry.rb +11 -6
  12. data/lib/openhab/dsl/items/items.rb +2 -2
  13. data/lib/openhab/dsl/items/metadata.rb +1 -2
  14. data/lib/openhab/dsl/items/timed_command.rb +7 -6
  15. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -2
  16. data/lib/openhab/dsl/openhab.rb +2 -0
  17. data/lib/openhab/dsl/persistence.rb +2 -0
  18. data/lib/openhab/dsl/rules/automation_rule.rb +4 -5
  19. data/lib/openhab/dsl/rules/rule.rb +104 -95
  20. data/lib/openhab/dsl/rules/rule_config.rb +1 -2
  21. data/lib/openhab/dsl/rules/rule_triggers.rb +1 -3
  22. data/lib/openhab/dsl/rules/terse.rb +1 -1
  23. data/lib/openhab/dsl/rules/triggers/changed.rb +6 -5
  24. data/lib/openhab/dsl/rules/triggers/channel.rb +2 -0
  25. data/lib/openhab/dsl/rules/triggers/command.rb +6 -5
  26. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +2 -2
  27. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +2 -2
  28. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +4 -2
  29. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +2 -2
  30. data/lib/openhab/dsl/rules/triggers/generic.rb +3 -2
  31. data/lib/openhab/dsl/rules/triggers/trigger.rb +2 -2
  32. data/lib/openhab/dsl/rules/triggers/updated.rb +9 -8
  33. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +8 -3
  34. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +3 -3
  35. data/lib/openhab/dsl/states.rb +34 -20
  36. data/lib/openhab/dsl/things.rb +2 -0
  37. data/lib/openhab/dsl/time/month_day.rb +2 -2
  38. data/lib/openhab/dsl/timers/manager.rb +4 -3
  39. data/lib/openhab/dsl/timers/reentrant_timer.rb +22 -27
  40. data/lib/openhab/dsl/timers/timer.rb +2 -3
  41. data/lib/openhab/dsl/timers.rb +4 -2
  42. data/lib/openhab/dsl/types/decimal_type.rb +3 -2
  43. data/lib/openhab/dsl/types/point_type.rb +4 -3
  44. data/lib/openhab/dsl/types/quantity_type.rb +0 -4
  45. data/lib/openhab/dsl/types/types.rb +24 -24
  46. data/lib/openhab/dsl/units.rb +2 -0
  47. data/lib/openhab/log/logger.rb +10 -24
  48. data/lib/openhab/version.rb +1 -1
  49. data/lib/openhab.rb +2 -8
  50. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34c74d99bb0771d8c4bfda01a96e59565dd3cd204918c0726b4286350b987b8b
4
- data.tar.gz: 17414fbbbf1ad48e9645f0bfdda0cd09703270695885d4938b5b74a22b9b1e84
3
+ metadata.gz: 1741788eaf0826f7b5e2c69935c96b21399771b3332a89debc77992c4ef687d0
4
+ data.tar.gz: b1200a71458efe21d389631bc29d2ef7d073ae5128706009a12b9a35af559bd3
5
5
  SHA512:
6
- metadata.gz: 2d79c2e5349ec783bef23e2b47f279052c1602a271d05d0c28e1f46f651aa5303138a0ee092a521db3d452764c05787e0416e16ac1a36c1cb1afcb823d205009
7
- data.tar.gz: 20f6d14f431456ab6cfafca83a36fdcc7ef1f7c0f2c2b960f819bfe896916e6b676968dd616fd656288d734ac03e733eea31b0345d122d727dbe43b6978117a9
6
+ metadata.gz: 9317316f15b212005f6f9cb4013c6fb80739b8c427a8685b0610d5c6127a1ed676584f62258efe38a757966f4c9e51f61064a70a486ec2bd4d0e98c52dbe9255
7
+ data.tar.gz: d28a7872616e92eefa43e198f8211ee6ccb4408f418bb942ea82b6a8c9af572f9d6adc2e8fe929d8a2ba5bff19138f238be0e7951f75bf9567800e5681c68381
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pp'
4
- require 'java'
5
- require 'set'
6
-
7
3
  require 'openhab/log/logger'
8
4
  require_relative 'item_proxy'
9
5
 
@@ -17,9 +17,8 @@ module OpenHAB
17
17
  #
18
18
  # Wait until OpenHAB engine ready to process
19
19
  #
20
- #
21
20
  def self.wait_till_openhab_ready
22
- logger.trace('Checking readyness of OpenHAB')
21
+ logger.trace('Checking readiness of OpenHAB')
23
22
  until OpenHAB::Core.automation_manager
24
23
  logger.trace("Automation manager not loaded, checking again in #{CHECK_DELAY} seconds.")
25
24
  sleep CHECK_DELAY
@@ -15,9 +15,9 @@ module OpenHAB
15
15
  # Execute the supplied block with the supplied values set for the currently running thread
16
16
  # The previous values for each key are restored after the block is executed
17
17
  #
18
- # @param [Hash] Keys and values to set for running thread, if hash is nil no values are set
18
+ # @param [Hash] values Keys and values to set for running thread, if hash is nil no values are set
19
19
  #
20
- def thread_local(**values)
20
+ def self.thread_local(**values)
21
21
  old_values = values.map { |key, _value| [key, Thread.current[key]] }.to_h
22
22
  values.each { |key, value| Thread.current[key] = value }
23
23
  logger.trace "Executing block with thread local context: #{values} - old context: #{old_values}"
@@ -25,6 +25,16 @@ module OpenHAB
25
25
  ensure
26
26
  old_values.each { |key, value| Thread.current[key] = value }
27
27
  end
28
+
29
+ #
30
+ # Execute the supplied block with the supplied values set for the currently running thread
31
+ # The previous values for each key are restored after the block is executed
32
+ #
33
+ # @param [Hash] values Keys and values to set for running thread, if hash is nil no values are set
34
+ #
35
+ def thread_local(**values, &block)
36
+ ThreadLocal.thread_local(values, &block)
37
+ end
28
38
  end
29
39
  end
30
40
  end
@@ -18,6 +18,8 @@ module OpenHAB
18
18
  # Import common actions
19
19
  %w[Exec HTTP Ping].each { |action| java_import "org.openhab.core.model.script.actions.#{action}" }
20
20
 
21
+ module_function
22
+
21
23
  #
22
24
  # Return an OpenHAB Action object for the given scope and thing
23
25
  #
@@ -7,6 +7,8 @@ module OpenHAB
7
7
  module DSL
8
8
  # Supports between range syntax
9
9
  module Between
10
+ module_function
11
+
10
12
  # Creates a range that can be compared against time of day/month days or strings
11
13
  # to see if they are within the range
12
14
  # @since 2.4.0
@@ -39,8 +39,8 @@ module OpenHAB
39
39
  base.send :include, OpenHAB::DSL::Groups
40
40
  base.send :include, OpenHAB::DSL::Items
41
41
  base.send :include, OpenHAB::DSL::Persistence
42
- base.send :include, OpenHAB::DSL::Rules
43
- base.send :include, OpenHAB::DSL::Rules::Terse
42
+ base.send :include, OpenHAB::DSL::Rules::Rule
43
+ base.send :include, OpenHAB::DSL::Rules::TerseRule
44
44
  base.send :include, OpenHAB::DSL::States
45
45
  base.send :include, OpenHAB::DSL::Things
46
46
  base.send :include, OpenHAB::DSL::Timers
@@ -2,8 +2,8 @@
2
2
 
3
3
  require 'singleton'
4
4
 
5
- require 'openhab/dsl/lazy_array'
6
5
  require 'openhab/core/entity_lookup'
6
+ require 'openhab/dsl/lazy_array'
7
7
 
8
8
  module OpenHAB
9
9
  module DSL
@@ -11,6 +11,19 @@ module OpenHAB
11
11
  # Provides access to OpenHAB Groups
12
12
  #
13
13
  module Groups
14
+ module_function
15
+
16
+ #
17
+ # Retrieve all OpenHAB groups
18
+ #
19
+ # @return [Set] of OpenHAB Groups
20
+ #
21
+ def groups
22
+ OpenHAB::DSL::Support::Groups.instance
23
+ end
24
+ end
25
+
26
+ module Support
14
27
  #
15
28
  # Provide access to groups as a set
16
29
  #
@@ -26,7 +39,7 @@ module OpenHAB
26
39
  #
27
40
  def [](name)
28
41
  group = OpenHAB::Core::EntityLookup.lookup_item(name)
29
- group.is_a?(Items::GroupItem) ? group : nil
42
+ group.is_a?(OpenHAB::DSL::Items::GroupItem) ? group : nil
30
43
  end
31
44
  alias include? []
32
45
  alias key? include?
@@ -36,15 +49,6 @@ module OpenHAB
36
49
  $ir.items.grep(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
37
50
  end
38
51
  end
39
-
40
- #
41
- # Retreive all OpenHAB groups
42
- #
43
- # @return [Set] of OpenHAB Groups
44
- #
45
- def groups
46
- Groups.instance
47
- end
48
52
  end
49
53
  end
50
54
  end
@@ -4,94 +4,94 @@ require_relative 'generic_item'
4
4
 
5
5
  module OpenHAB
6
6
  module DSL
7
- module Items
8
- # Functionality to implement +ensure+/+ensure_states+
9
- module Ensure
10
- # Contains the global +ensure_states+ method
11
- module EnsureStates
12
- # Global method that takes a block and for the duration of the block
13
- # all commands sent will check if the item is in the command's state
14
- # before sending the command.
15
- #
16
- # @example Turn on several switches only if they're not already on
17
- # ensure_states do
18
- # Switch1.on
19
- # Switch2.on
20
- # end
21
- def ensure_states
22
- old = Thread.current[:ensure_states]
23
- Thread.current[:ensure_states] = true
24
- yield
25
- ensure
26
- Thread.current[:ensure_states] = old
27
- end
28
- module_function :ensure_states
7
+ # Functionality to implement +ensure+/+ensure_states+
8
+ module Ensure
9
+ # Contains the global +ensure_states+ method
10
+ module EnsureStates
11
+ # Global method that takes a block and for the duration of the block
12
+ # all commands sent will check if the item is in the command's state
13
+ # before sending the command.
14
+ #
15
+ # @example Turn on several switches only if they're not already on
16
+ # ensure_states do
17
+ # Switch1.on
18
+ # Switch2.on
19
+ # end
20
+ def ensure_states
21
+ old = Thread.current[:ensure_states]
22
+ Thread.current[:ensure_states] = true
23
+ yield
24
+ ensure
25
+ Thread.current[:ensure_states] = old
29
26
  end
27
+ module_function :ensure_states
28
+ end
30
29
 
31
- # Contains the +ensure+ method mixed into {GenericItem} and {GroupItem::GroupMembers}
32
- module Ensurable
33
- # Fluent method call that you can chain commands on to, that will
34
- # then automatically ensure that the item is not in the command's
35
- # state before sending the command.
36
- #
37
- # @example Turn switch on only if it's not on
38
- # MySwitch.ensure.on
39
- # @example Turn on all switches in a group that aren't already on
40
- # MySwitchGroup.members.ensure.on
41
- def ensure
42
- GenericItemDelegate.new(self)
43
- end
30
+ # Contains the +ensure+ method mixed into {GenericItem} and {GroupItem::GroupMembers}
31
+ module Ensurable
32
+ # Fluent method call that you can chain commands on to, that will
33
+ # then automatically ensure that the item is not in the command's
34
+ # state before sending the command.
35
+ #
36
+ # @example Turn switch on only if it's not on
37
+ # MySwitch.ensure.on
38
+ # @example Turn on all switches in a group that aren't already on
39
+ # MySwitchGroup.members.ensure.on
40
+ def ensure
41
+ GenericItemDelegate.new(self)
44
42
  end
43
+ end
45
44
 
46
- # Extensions for {Items::GenericItem} to implement {Ensure}'s
47
- # functionality
48
- module GenericItem
49
- include Ensurable
50
-
51
- # If +ensure_states+ is active (by block or chained method), then
52
- # check if this item is in the command's state before actually
53
- # sending the command
54
- def command(command)
55
- return super unless Thread.current[:ensure_states]
45
+ # Extensions for {Items::GenericItem} to implement {Ensure}'s
46
+ # functionality
47
+ module GenericItem
48
+ include Ensurable
56
49
 
57
- logger.trace do
58
- "#{name} ensure #{command}, format_type_pre: #{format_type_pre(command)}, current state: #{state}"
59
- end
60
- return if state == format_type_pre(command)
50
+ # If +ensure_states+ is active (by block or chained method), then
51
+ # check if this item is in the command's state before actually
52
+ # sending the command
53
+ def command(command)
54
+ return super unless Thread.current[:ensure_states]
61
55
 
62
- super
56
+ logger.trace do
57
+ "#{name} ensure #{command}, format_type_pre: #{format_type_pre(command)}, current state: #{state}"
63
58
  end
64
- alias << command
59
+ return if state == format_type_pre(command)
60
+
61
+ super
65
62
  end
63
+ alias << command
64
+ end
66
65
 
67
- # "anonymous" class that wraps any method call in +ensure_states+
68
- # before forwarding to the wrapped object
69
- # @!visibility private
70
- class GenericItemDelegate
71
- def initialize(item)
72
- @item = item
73
- end
66
+ # "anonymous" class that wraps any method call in +ensure_states+
67
+ # before forwarding to the wrapped object
68
+ # @!visibility private
69
+ class GenericItemDelegate
70
+ def initialize(item)
71
+ @item = item
72
+ end
74
73
 
75
- # activate +ensure_states+ before forwarding to the wrapped object
76
- def method_missing(method, *args, &block)
77
- return super unless @item.respond_to?(method)
74
+ # activate +ensure_states+ before forwarding to the wrapped object
75
+ def method_missing(method, *args, &block)
76
+ return super unless @item.respond_to?(method)
78
77
 
79
- ensure_states do
80
- @item.__send__(method, *args, &block)
81
- end
78
+ ensure_states do
79
+ @item.__send__(method, *args, &block)
82
80
  end
81
+ end
83
82
 
84
- # .
85
- def respond_to_missing?(method, include_private = false)
86
- @item.respond_to?(method, include_private) || super
87
- end
83
+ # .
84
+ def respond_to_missing?(method, include_private = false)
85
+ @item.respond_to?(method, include_private) || super
88
86
  end
89
87
  end
88
+ end
90
89
 
91
- GenericItem.prepend(Ensure::GenericItem)
92
- GroupItem::GroupMembers.include(Ensure::Ensurable)
90
+ module Items
91
+ GenericItem.prepend(OpenHAB::DSL::Ensure::GenericItem)
92
+ GroupItem::GroupMembers.include(OpenHAB::DSL::Ensure::Ensurable)
93
93
  end
94
94
  end
95
95
  end
96
96
 
97
- Object.include OpenHAB::DSL::Items::Ensure::EnsureStates
97
+ Object.include OpenHAB::DSL::Ensure::EnsureStates
@@ -153,7 +153,7 @@ module OpenHAB
153
153
  def things
154
154
  registry = OpenHAB::Core::OSGI.service('org.openhab.core.thing.link.ItemChannelLinkRegistry')
155
155
  channels = registry.get_bound_channels(name).to_a
156
- channels.map(&:thing_uid).uniq.map { |tuid| Object.things[tuid] }
156
+ channels.map(&:thing_uid).uniq.map { |tuid| OpenHAB::DSL::Things.things[tuid] }
157
157
  end
158
158
  alias all_linked_things things
159
159
 
@@ -8,6 +8,17 @@ require 'openhab/dsl/lazy_array'
8
8
  module OpenHAB
9
9
  module DSL
10
10
  module Items
11
+ module_function
12
+
13
+ # Fetches all non-group items from the item registry
14
+ # @return [ItemRegistry]
15
+ def items
16
+ OpenHAB::DSL::Support::ItemRegistry.instance
17
+ end
18
+ end
19
+
20
+ # Provide supporting objects for DSL functions
21
+ module Support
11
22
  #
12
23
  # Provides access to all OpenHAB items, and acts like an array.
13
24
  #
@@ -38,12 +49,6 @@ module OpenHAB
38
49
  $ir.items.to_a # rubocop:disable Style/GlobalVars
39
50
  end
40
51
  end
41
-
42
- # Fetches all non-group items from the item registry
43
- # @return [ItemRegistry]
44
- def items
45
- ItemRegistry.instance
46
- end
47
52
  end
48
53
  end
49
54
  end
@@ -43,7 +43,7 @@ module OpenHAB
43
43
  # define predicates for checking if an item is in one of the Enum states
44
44
  def def_predicate_methods(klass)
45
45
  values_for_enums(klass.ACCEPTED_DATA_TYPES).each do |state|
46
- _command_predicate, state_predicate = Types::PREDICATE_ALIASES[state.to_s]
46
+ _command_predicate, state_predicate = OpenHAB::DSL::PREDICATE_ALIASES[state.to_s]
47
47
  next if klass.instance_methods.include?(state_predicate)
48
48
 
49
49
  logger.trace("Defining #{klass}##{state_predicate} for #{state}")
@@ -59,7 +59,7 @@ module OpenHAB
59
59
  # as well as predicates for if an ItemCommandEvent is one of those commands
60
60
  def def_command_methods(klass) # rubocop:disable Metrics method has single purpose
61
61
  values_for_enums(klass.ACCEPTED_COMMAND_TYPES).each do |value|
62
- command = Types::COMMAND_ALIASES[value.to_s]
62
+ command = OpenHAB::DSL::COMMAND_ALIASES[value.to_s]
63
63
  next if klass.instance_methods.include?(command)
64
64
 
65
65
  logger.trace("Defining #{klass}##{command} for #{value}")
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'delegate'
4
- require 'pp'
5
4
  require 'forwardable'
6
5
  require 'openhab/core/osgi'
7
6
  require 'openhab/log/logger'
@@ -89,7 +88,7 @@ module OpenHAB
89
88
  #
90
89
  # Recursively convert the supplied Hash object into a Ruby Hash and recreate the keys and values
91
90
  #
92
- # @param [Hash] Hash to convert
91
+ # @param [Hash] hash to convert
93
92
  #
94
93
  # @return [Hash] The converted hash
95
94
  #
@@ -43,7 +43,7 @@ module OpenHAB
43
43
  #
44
44
  #
45
45
  # rubocop: disable Metrics/MethodLength
46
- # The mutex makes this over 10 lines, but there is usable way to break thsi method up
46
+ # The mutex makes this over 10 lines, but there is usable way to break this method up
47
47
  def command(command, for: nil, on_expire: nil, &block)
48
48
  duration = binding.local_variable_get(:for)
49
49
  return super(command) unless duration
@@ -86,8 +86,9 @@ module OpenHAB
86
86
  timed_command_details.timer = timed_command_timer(timed_command_details, semaphore, &block)
87
87
  timed_command_details.cancel_rule = TimedCommandCancelRule.new(timed_command_details, semaphore,
88
88
  &block)
89
- timed_command_details.rule_uid = OpenHAB::DSL::Rules.automation_manager
90
- .addRule(timed_command_details.cancel_rule).getUID
89
+ timed_command_details.rule_uid = OpenHAB::DSL::Rules::Rule.automation_manager
90
+ .addRule(timed_command_details.cancel_rule)
91
+ .getUID
91
92
  logger.trace "Created Timed Command #{timed_command_details}"
92
93
  TimedCommand.timed_commands[self] = timed_command_details
93
94
  end
@@ -120,10 +121,10 @@ module OpenHAB
120
121
  # rubocop: enable Metrics/MethodLength
121
122
 
122
123
  # Cancels timed command rule
123
- # @param [TimedCommandDetailed] timed_command details about the timed command
124
+ # @param [TimedCommandDetailed] timed_command_details details about the timed command
124
125
  def cancel_timed_command_rule(timed_command_details)
125
126
  logger.trace "Removing rule: #{timed_command_details.rule_uid}"
126
- OpenHAB::DSL::Rules.registry.remove(timed_command_details.rule_uid)
127
+ OpenHAB::DSL::Rules::Rule.registry.remove(timed_command_details.rule_uid)
127
128
  end
128
129
 
129
130
  #
@@ -162,7 +163,7 @@ module OpenHAB
162
163
  #
163
164
  # Execute the rule
164
165
  #
165
- # @param [Map] mod map provided by OpenHAB rules engine
166
+ # @param [Map] _mod map provided by OpenHAB rules engine
166
167
  # @param [Map] inputs map provided by OpenHAB rules engine containing event and other information
167
168
  #
168
169
  #
@@ -8,8 +8,6 @@ module OpenHAB
8
8
  # Extend integer to create duration object
9
9
  #
10
10
  module IntegerExtensions
11
- include OpenHAB::Core
12
-
13
11
  #
14
12
  # Create Duration with the specified unit
15
13
  #
@@ -10,6 +10,8 @@ module OpenHAB
10
10
  module Core
11
11
  include OpenHAB::Log
12
12
 
13
+ module_function
14
+
13
15
  #
14
16
  # Return the OpenHAB conf directory as a ruby pathname
15
17
  #
@@ -6,6 +6,8 @@ module OpenHAB
6
6
  # Provides support for interacting with OpenHAB Persistence service
7
7
  #
8
8
  module Persistence
9
+ module_function
10
+
9
11
  #
10
12
  # Sets a thread local variable to set the default persistence service
11
13
  # for method calls inside the block
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'java'
4
- require 'set'
5
4
  require 'openhab/core/thread_local'
6
5
  require 'openhab/log/logger'
7
6
  require 'openhab/dsl/between'
@@ -57,8 +56,8 @@ module OpenHAB
57
56
  #
58
57
  def execute(mod = nil, inputs = nil)
59
58
  thread_local(RULE_NAME: name) do
60
- logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs&.pretty_inspect})" }
61
- logger.trace { "Event details #{inputs['event'].pretty_inspect}" } if inputs&.key?('event')
59
+ logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs.inspect})" }
60
+ logger.trace { "Event details #{inputs['event'].inspect}" } if inputs&.key?('event')
62
61
  trigger_conditions(inputs).process(mod: mod, inputs: inputs) do
63
62
  process_queue(create_queue(inputs), mod, inputs)
64
63
  end
@@ -131,8 +130,8 @@ module OpenHAB
131
130
  end
132
131
 
133
132
  # If an attachment exists for the trigger for this event add it to the event object
134
- # @param [Object] Event
135
- # @param [Hash] Inputs into event
133
+ # @param [Object] event Event
134
+ # @param [Hash] inputs Inputs into event
136
135
  # @return [Object] Event with attachment added
137
136
  #
138
137
  def add_attachment(event, inputs)