openhab-scripting 4.32.6 → 4.32.7

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.
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
@@ -8,12 +8,45 @@ module OpenHAB
8
8
  # Manages storing and restoring item state
9
9
  #
10
10
  module States
11
- java_import org.openhab.core.model.script.actions.BusEvent
11
+ module_function
12
12
 
13
+ #
14
+ # Store states of supplied items
15
+ #
16
+ # @param [Array] items to store states of
17
+ #
18
+ # @return [StateStorage] item states
19
+ #
20
+ def store_states(*items)
21
+ items = items.flatten.map do |item|
22
+ item.respond_to?(:__getobj__) ? item.__getobj__ : item
23
+ end
24
+ states = OpenHAB::DSL::Support::StateStorage.from_items(*items)
25
+ if block_given?
26
+ yield
27
+ states.restore
28
+ end
29
+ states
30
+ end
31
+ end
32
+
33
+ module Support
34
+ java_import org.openhab.core.model.script.actions.BusEvent
13
35
  #
14
36
  # Delegates state storage to a Hash providing methods to operate with states
15
37
  #
16
38
  class StateStorage < SimpleDelegator
39
+ #
40
+ # Create a StateStorage object that stores the states of the given items
41
+ #
42
+ # @param [Array<Item>] *items A list of items
43
+ #
44
+ # @return [StateStorage] A state storage object
45
+ #
46
+ def self.from_items(*items)
47
+ StateStorage.new(BusEvent.storeStates(*items).to_h)
48
+ end
49
+
17
50
  #
18
51
  # Restore the stored states of all items
19
52
  #
@@ -39,25 +72,6 @@ module OpenHAB
39
72
  any? { |item, value| item != value }
40
73
  end
41
74
  end
42
-
43
- #
44
- # Store states of supplied items
45
- #
46
- # @param [Array] items to store states of
47
- #
48
- # @return [StateStorage] item states
49
- #
50
- def store_states(*items)
51
- items = items.flatten.map do |item|
52
- item.respond_to?(:__getobj__) ? item.__getobj__ : item
53
- end
54
- states = StateStorage.new(BusEvent.storeStates(*items).to_h)
55
- if block_given?
56
- yield
57
- states.restore
58
- end
59
- states
60
- end
61
75
  end
62
76
  end
63
77
  end
@@ -132,6 +132,8 @@ module OpenHAB
132
132
  end
133
133
  end
134
134
 
135
+ module_function
136
+
135
137
  #
136
138
  # Get all things known to OpenHAB
137
139
  #
@@ -48,8 +48,8 @@ module OpenHAB
48
48
  DAYS_IN_YEAR = 366
49
49
 
50
50
  # Create a new MonthDayRange element
51
- # @param [MonthDay] MonthDay element
52
- # @param [Range] Underlying MonthDay range
51
+ # @param [MonthDay] month_day MonthDay element
52
+ # @param [Range] range Underlying MonthDay range
53
53
  #
54
54
  def initialize(month_day:, range:)
55
55
  @month_day = month_day
@@ -1,14 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
3
4
  require 'openhab/log/logger'
4
5
  require_relative 'reentrant_timer'
5
6
 
6
7
  module OpenHAB
7
8
  module DSL
8
9
  #
9
- # Provides access to and ruby wrappers around OpenHAB timers
10
+ # Provides supporting classes for Timers module
10
11
  #
11
- module Timers
12
+ module Support
12
13
  #
13
14
  # Manages data structures that track timers
14
15
  #
@@ -52,7 +53,7 @@ module OpenHAB
52
53
 
53
54
  # Fetches the reentrant timer that matches the supplied id and block if it exists
54
55
  #
55
- # @param [Object] Object to associate with timer
56
+ # @param [Object] id Object to associate with timer
56
57
  # @param [Block] block to execute, block is passed a Timer object
57
58
  #
58
59
  # @return [RentrantTimer] Timer object if it exists, nil otherwise
@@ -5,38 +5,33 @@ require_relative 'timer'
5
5
 
6
6
  module OpenHAB
7
7
  module DSL
8
+ # A reentrant timer is a timer that is automatically rescheduled
9
+ # when the block it refers to is encountered again
8
10
  #
9
- # Provides access to and ruby wrappers around OpenHAB timers
10
- #
11
- module Timers
12
- # A reentrant timer is a timer that is automatically rescheduled
13
- # when the block it refers to is encountered again
14
- #
15
- # @author Brian O'Connell
16
- # @since 2.0.0
17
- class ReentrantTimer < Timer
18
- include OpenHAB::Log
11
+ # @author Brian O'Connell
12
+ # @since 2.0.0
13
+ class ReentrantTimer < Timers::Timer
14
+ include OpenHAB::Log
19
15
 
20
- attr_reader :id, :reentrant_id
16
+ attr_reader :id, :reentrant_id
21
17
 
22
- #
23
- # Create a new Timer Object
24
- #
25
- # @param [Duration] duration Duration until timer should fire
26
- # @param [Block] block Block to execute when timer fires
27
- #
28
- def initialize(duration:, id:, thread_locals: {}, &block)
29
- raise 'Reentrant timers do not work in dynamically generated code' unless block.source_location
18
+ #
19
+ # Create a new Timer Object
20
+ #
21
+ # @param [Duration] duration Duration until timer should fire
22
+ # @param [Block] block Block to execute when timer fires
23
+ #
24
+ def initialize(duration:, id:, thread_locals: {}, &block)
25
+ raise 'Reentrant timers do not work in dynamically generated code' unless block.source_location
30
26
 
31
- @id = id
32
- @reentrant_id = self.class.reentrant_id(id: id, &block)
33
- super(duration: duration, thread_locals: thread_locals, &block)
34
- logger.trace("Created Reentrant Timer #{self} with reentrant Key #{@reentrant_id}")
35
- end
27
+ @id = id
28
+ @reentrant_id = self.class.reentrant_id(id: id, &block)
29
+ super(duration: duration, thread_locals: thread_locals, &block)
30
+ logger.trace("Created Reentrant Timer #{self} with reentrant Key #{@reentrant_id}")
31
+ end
36
32
 
37
- def self.reentrant_id(id:, &block)
38
- [id, block.source_location].flatten
39
- end
33
+ def self.reentrant_id(id:, &block)
34
+ [id, block.source_location].flatten
40
35
  end
41
36
  end
42
37
  end
@@ -12,7 +12,6 @@ module OpenHAB
12
12
  # Provides access to and ruby wrappers around OpenHAB timers
13
13
  #
14
14
  module Timers
15
- include OpenHAB::Log
16
15
  java_import org.openhab.core.model.script.actions.ScriptExecution
17
16
  java_import java.time.ZonedDateTime
18
17
 
@@ -83,7 +82,7 @@ module OpenHAB
83
82
  #
84
83
  # Constructs a block to execute timer within
85
84
  #
86
- # @param [Semaphore] Semaphore to obtain before executing
85
+ # @param [Semaphore] semaphore to obtain before executing
87
86
  #
88
87
  # @return [Proc] Block for timer to execute
89
88
  #
@@ -101,7 +100,7 @@ module OpenHAB
101
100
  #
102
101
  # Convert argument to a duration
103
102
  #
104
- # @params [Java::JavaTimeTemporal::TemporalAmount, #to_f, #to_i, nil] duration Duration
103
+ # @param [Java::JavaTimeTemporal::TemporalAmount, #to_f, #to_i, nil] duration Duration
105
104
  #
106
105
  # @raise if duration cannot be used for a timer
107
106
  #
@@ -13,12 +13,14 @@ module OpenHAB
13
13
  include OpenHAB::Log
14
14
 
15
15
  # Manages timers
16
- @timer_manager = TimerManager.new
16
+ @timer_manager = OpenHAB::DSL::Support::TimerManager.new
17
17
 
18
18
  class << self
19
19
  attr_reader :timer_manager
20
20
  end
21
21
 
22
+ module_function
23
+
22
24
  #
23
25
  # Execute the supplied block after the specified duration
24
26
  #
@@ -66,7 +68,7 @@ module OpenHAB
66
68
  else
67
69
  logger.trace('No reentrant timer found, creating new timer')
68
70
  end
69
- ReentrantTimer.new(duration: duration, id: id, thread_locals: thread_locals, &block)
71
+ OpenHAB::DSL::ReentrantTimer.new(duration: duration, id: id, thread_locals: thread_locals, &block)
70
72
  end
71
73
  end
72
74
  end
@@ -22,9 +22,10 @@ module OpenHAB
22
22
  include ComparableType
23
23
 
24
24
  #
25
- # Create a new instance of DateTimeType
25
+ # Create a new instance of DecimalType
26
+ #
27
+ # @param [java.math.BigDecimal, Items::NumericItem, Numeric] args Create a DecimalType from the given value
26
28
  #
27
- # @param value [java.math.BigDecimal, Items::NumericItem, Numeric]
28
29
  def initialize(*args) # rubocop:disable Metrics
29
30
  unless args.length == 1
30
31
  super
@@ -13,9 +13,10 @@ module OpenHAB
13
13
  class PointType
14
14
  # @!parse include PrimitiveType
15
15
 
16
- # @param latitude [DecimalType, QuantityType, StringType, Numeric]
17
- # @param longitude [DecimalType, QuantityType, StringType, Numeric]
18
- # @param altitude [DecimalType, QuantityType, StringType, Numeric]
16
+ # @overload initialize(latitude, longitude, altitude)
17
+ # @param [DecimalType, QuantityType, StringType, Numeric] latitude
18
+ # @param [DecimalType, QuantityType, StringType, Numeric] longitude
19
+ # @param [DecimalType, QuantityType, StringType, Numeric] altitude
19
20
  def initialize(*args) # rubocop:disable Metrics
20
21
  args = from_hash(args.first.to_hash) if args.first.respond_to? :to_hash
21
22
  if (2..3).cover?(args.length)
@@ -26,10 +26,6 @@ module OpenHAB
26
26
  #
27
27
  # Convert this quantity into a another unit
28
28
  #
29
- # @param [Object] other String or Unit to convert to
30
- #
31
- # @return [QuantityType] This quantity converted to another unit
32
- #
33
29
  alias | to_unit
34
30
 
35
31
  #
@@ -24,6 +24,28 @@ require_relative 'un_def_type'
24
24
 
25
25
  module OpenHAB
26
26
  module DSL
27
+ # Hash taking a Enum value, and returning two symbols of
28
+ # predicates to be defined for it. the first is the "command" form,
29
+ # which should be defined on ItemCommandEvent, and on the Type itself.
30
+ # the second is "state" form, which should be defined on the applicable
31
+ # Item, and on the Type itself.
32
+ # @!visibility private
33
+ PREDICATE_ALIASES = Hash.new { |_h, k| [:"#{k.downcase}?"] * 2 }
34
+ .merge({
35
+ 'PLAY' => %i[play? playing?],
36
+ 'PAUSE' => %i[pause? paused?],
37
+ 'REWIND' => %i[rewind? rewinding?],
38
+ 'FASTFORWARD' => %i[fast_forward? fast_forwarding?]
39
+ }).freeze
40
+
41
+ # Hash taking a Enum value, and returning an array of symbols
42
+ # of the command to define for it
43
+ # @!visibility private
44
+ COMMAND_ALIASES = Hash.new { |_h, k| k.downcase.to_sym }
45
+ .merge({
46
+ 'FASTFORWARD' => :fast_forward
47
+ }).freeze
48
+
27
49
  #
28
50
  # Contains all OpenHAB *Type classes, as well as associated support
29
51
  # modules
@@ -31,28 +53,6 @@ module OpenHAB
31
53
  module Types
32
54
  include OpenHAB::Log
33
55
 
34
- # Hash taking a Enum value, and returning two symbols of
35
- # predicates to be defined for it. the first is the "command" form,
36
- # which should be defined on ItemCommandEvent, and on the Type itself.
37
- # the second is "state" form, which should be defined on the applicable
38
- # Item, and on the Type itself.
39
- # @!visibility private
40
- PREDICATE_ALIASES = Hash.new { |_h, k| [:"#{k.downcase}?"] * 2 }
41
- .merge({
42
- 'PLAY' => %i[play? playing?],
43
- 'PAUSE' => %i[pause? paused?],
44
- 'REWIND' => %i[rewind? rewinding?],
45
- 'FASTFORWARD' => %i[fast_forward? fast_forwarding?]
46
- }).freeze
47
-
48
- # Hash taking a Enum value, and returning an array of symbols
49
- # of the command to define for it
50
- # @!visibility private
51
- COMMAND_ALIASES = Hash.new { |_h, k| k.downcase.to_sym }
52
- .merge({
53
- 'FASTFORWARD' => :fast_forward
54
- }).freeze
55
-
56
56
  constants.map { |c| const_get(c) }
57
57
  .grep(Module)
58
58
  .select { |k| k < java.lang.Enum }
@@ -65,8 +65,8 @@ module OpenHAB
65
65
  # include all the aliases that we define for items both command and
66
66
  # state aliases (since types can be interrogated as an incoming
67
67
  # command, or as the state of an item)
68
- command = :"#{COMMAND_ALIASES[value.to_s]}?"
69
- states = PREDICATE_ALIASES[value.to_s]
68
+ command = :"#{OpenHAB::DSL::COMMAND_ALIASES[value.to_s]}?"
69
+ states = OpenHAB::DSL::PREDICATE_ALIASES[value.to_s]
70
70
 
71
71
  ([command] | states).each do |method|
72
72
  logger.trace("Defining #{klass}##{method} for #{value}")
@@ -17,6 +17,8 @@ module OpenHAB
17
17
  # Provides support for interacting with OpenHAB Units of Measurement
18
18
  #
19
19
  module Units
20
+ module_function
21
+
20
22
  # @return The default unit for the current thread
21
23
  def unit(unit = nil, &block)
22
24
  return with_unit(unit, &block) if unit || block # back-compat
@@ -2,13 +2,8 @@
2
2
 
3
3
  require 'openhab/log/configuration'
4
4
  require 'java'
5
- require 'pp'
6
5
 
7
6
  module OpenHAB
8
- #
9
- # Provides access to the OpenHAB logging using a Ruby logging methods
10
- #
11
-
12
7
  module Core
13
8
  #
14
9
  # Ruby Logger that forwards messages at appropriate levels to OpenHAB Logger
@@ -65,7 +60,7 @@ module OpenHAB
65
60
  #
66
61
  # Logs a map of key(value) with an optional preamble at trace level
67
62
  # @param [String] preamble to put at start of log message
68
- # @param [Hash] key and values to log
63
+ # @param [Hash] kwargs key and values to log
69
64
  def state(preamble = 'State:', **kwargs)
70
65
  return unless trace_enabled?
71
66
 
@@ -102,7 +97,8 @@ module OpenHAB
102
97
  #
103
98
  # Print error and stack trace without calls to internal classes
104
99
  #
105
- # @param [Exception] error A rescued error
100
+ # @param [Exception] exception A rescued error
101
+ # @param [String] rule_name The name of the rule where the exception occurred
106
102
  #
107
103
  def log_exception(exception, rule_name)
108
104
  exception = clean_backtrace(exception)
@@ -157,10 +153,11 @@ module OpenHAB
157
153
  end
158
154
  end
159
155
 
156
+ #
157
+ # Provides access to the OpenHAB logging using a Ruby logging methods
158
+ #
160
159
  module Log
161
- #
162
- # Ruby Logger that forwards messages at appropriate levels to OpenHAB Logger
163
- #
160
+ module_function
164
161
 
165
162
  # Logger caches
166
163
  @loggers = {}
@@ -180,7 +177,7 @@ module OpenHAB
180
177
  #
181
178
  # Injects a logger into the base class
182
179
  #
183
- # @param [Class] class the logger is for
180
+ # @param [Object] object the logger is for
184
181
  #
185
182
  # @return [Logger] for the supplied name
186
183
  #
@@ -189,7 +186,7 @@ module OpenHAB
189
186
  # of logger name requires lots of operations and logger
190
187
  # names for some objects are specific to the class
191
188
  logger_name = logger_name(object)
192
- @loggers[logger_name] ||= OpenHAB::Core::Logger.new(logger_name)
189
+ @loggers[logger_name] ||= Core::Logger.new(logger_name)
193
190
  end
194
191
 
195
192
  private
@@ -230,11 +227,7 @@ module OpenHAB
230
227
  end
231
228
 
232
229
  #
233
- # Configure a logger for the supplied classname
234
- #
235
- # @param [String] classname to create logger for
236
- #
237
- # @return [Logger] Logger for the supplied classname
230
+ # Returns the rules file name
238
231
  #
239
232
  def rules_file
240
233
  # Each rules file gets its own context
@@ -256,13 +249,6 @@ module OpenHAB
256
249
  klass_name
257
250
  end
258
251
 
259
- # "#{rule_name.downcase}.#{klass_name}"
260
- # if klass_name == 'Object'
261
- # "rules.#{rules_file_name.downcase}"
262
- # else
263
- # "rules.#{rules_file_name.downcase}.#{klass_name}"
264
- # end
265
-
266
252
  #
267
253
  # Figure out the log prefix
268
254
  #
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.32.6'
8
+ VERSION = '4.32.7'
9
9
  end
data/lib/openhab.rb CHANGED
@@ -19,16 +19,13 @@ module OpenHAB
19
19
  #
20
20
  # @param [Object] base Object to decorate with DSL and helper methods
21
21
  #
22
- #
23
- # Number of extensions and includes requires more lines
24
22
  def self.extended(base)
25
23
  OpenHAB::Core.wait_till_openhab_ready
26
- base.extend Log
27
24
  base.extend OpenHAB::Core::ScriptHandling
25
+ base.extend OpenHAB::Core::ScriptHandlingCallbacks
28
26
  base.extend OpenHAB::Core::EntityLookup
29
27
  base.extend OpenHAB::DSL
30
28
 
31
- base.send :include, OpenHAB::Core::ScriptHandlingCallbacks
32
29
  logger.debug "OpenHAB JRuby Scripting Library Version #{OpenHAB::VERSION} Loaded"
33
30
 
34
31
  OpenHAB::Core.add_rubylib_to_load_path
@@ -36,7 +33,4 @@ module OpenHAB
36
33
  end
37
34
 
38
35
  # Extend caller with OpenHAB methods
39
-
40
- # rubocop: disable Style/MixinUsage
41
- extend OpenHAB
42
- # rubocop: enable Style/MixinUsage
36
+ extend OpenHAB # rubocop:disable Style/MixinUsage
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.32.6
4
+ version: 4.32.7
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-03-04 00:00:00.000000000 Z
11
+ date: 2022-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler