openhab-jrubyscripting 5.0.0.rc9 → 5.0.0.rc11

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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions/audio.rb +47 -0
  3. data/lib/openhab/core/actions/ephemeris.rb +39 -0
  4. data/lib/openhab/core/actions/exec.rb +51 -0
  5. data/lib/openhab/core/actions/http.rb +80 -0
  6. data/lib/openhab/core/actions/ping.rb +30 -0
  7. data/lib/openhab/core/actions/transformation.rb +32 -0
  8. data/lib/openhab/core/actions/voice.rb +36 -0
  9. data/lib/openhab/core/actions.rb +23 -120
  10. data/lib/openhab/core/{events → dto}/item_channel_link.rb +1 -4
  11. data/lib/openhab/core/{events → dto}/thing.rb +10 -12
  12. data/lib/openhab/core/dto.rb +11 -0
  13. data/lib/openhab/core/entity_lookup.rb +1 -1
  14. data/lib/openhab/core/events/abstract_event.rb +1 -0
  15. data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
  16. data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
  17. data/lib/openhab/core/events/item_command_event.rb +1 -1
  18. data/lib/openhab/core/events/item_state_changed_event.rb +6 -6
  19. data/lib/openhab/core/events/item_state_event.rb +6 -6
  20. data/lib/openhab/core/events/thing_status_info_event.rb +8 -6
  21. data/lib/openhab/core/items/generic_item.rb +2 -1
  22. data/lib/openhab/core/items/persistence.rb +52 -18
  23. data/lib/openhab/core/items/player_item.rb +1 -1
  24. data/lib/openhab/core/items/proxy.rb +20 -14
  25. data/lib/openhab/core/items/registry.rb +2 -0
  26. data/lib/openhab/core/items.rb +3 -3
  27. data/lib/openhab/core/profile_factory.rb +3 -1
  28. data/lib/openhab/core/proxy.rb +125 -0
  29. data/lib/openhab/core/things/links/provider.rb +1 -1
  30. data/lib/openhab/core/things/proxy.rb +8 -0
  31. data/lib/openhab/core/types/date_time_type.rb +2 -1
  32. data/lib/openhab/core/types/decimal_type.rb +1 -1
  33. data/lib/openhab/core/types/un_def_type.rb +2 -2
  34. data/lib/openhab/core/value_cache.rb +1 -1
  35. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  36. data/lib/openhab/core_ext/java/class.rb +1 -1
  37. data/lib/openhab/core_ext/java/duration.rb +25 -1
  38. data/lib/openhab/core_ext/java/local_date.rb +2 -0
  39. data/lib/openhab/core_ext/java/month_day.rb +2 -0
  40. data/lib/openhab/core_ext/java/zoned_date_time.rb +85 -0
  41. data/lib/openhab/core_ext/ruby/date.rb +2 -0
  42. data/lib/openhab/core_ext/ruby/date_time.rb +1 -0
  43. data/lib/openhab/core_ext/ruby/time.rb +1 -0
  44. data/lib/openhab/dsl/debouncer.rb +259 -0
  45. data/lib/openhab/dsl/items/builder.rb +4 -2
  46. data/lib/openhab/dsl/items/timed_command.rb +31 -13
  47. data/lib/openhab/dsl/rules/automation_rule.rb +28 -21
  48. data/lib/openhab/dsl/rules/builder.rb +357 -37
  49. data/lib/openhab/dsl/rules/guard.rb +12 -54
  50. data/lib/openhab/dsl/rules/name_inference.rb +11 -0
  51. data/lib/openhab/dsl/rules/property.rb +3 -4
  52. data/lib/openhab/dsl/rules/terse.rb +4 -1
  53. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +5 -6
  54. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +1 -0
  55. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +19 -31
  56. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +1 -0
  57. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +22 -30
  58. data/lib/openhab/dsl/things/builder.rb +1 -1
  59. data/lib/openhab/dsl/thread_local.rb +1 -0
  60. data/lib/openhab/dsl/version.rb +1 -1
  61. data/lib/openhab/dsl.rb +224 -3
  62. data/lib/openhab/rspec/hooks.rb +5 -2
  63. data/lib/openhab/rspec/karaf.rb +7 -0
  64. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  65. data/lib/openhab/rspec/mocks/space.rb +23 -0
  66. data/lib/openhab/rspec/openhab/core/actions.rb +16 -4
  67. data/lib/openhab/rspec/openhab/core/items/proxy.rb +1 -13
  68. data/lib/openhab/rspec/suspend_rules.rb +1 -14
  69. data/lib/openhab/yard/base_helper.rb +19 -0
  70. data/lib/openhab/yard/code_objects/group_object.rb +9 -3
  71. data/lib/openhab/yard/coderay.rb +17 -0
  72. data/lib/openhab/yard/handlers/jruby/base.rb +10 -1
  73. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +3 -0
  74. data/lib/openhab/yard/html_helper.rb +49 -15
  75. data/lib/openhab/yard/markdown_helper.rb +135 -0
  76. data/lib/openhab/yard.rb +6 -0
  77. metadata +36 -4
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "property"
4
-
5
3
  module OpenHAB
6
4
  module DSL
7
5
  module Rules
@@ -13,8 +11,8 @@ module OpenHAB
13
11
  #
14
12
  # Create a new Guard
15
13
  #
16
- # @param [Object] only_if Item or Proc to use as guard
17
- # @param [Object] not_if Item or Proc to use as guard
14
+ # @param [Array<Proc>] only_if Array of Procs to use as guard
15
+ # @param [Array<Proc>] not_if Array of Procs to use as guard
18
16
  #
19
17
  def initialize(run_context:, only_if: nil, not_if: nil)
20
18
  @run_context = run_context
@@ -40,74 +38,34 @@ module OpenHAB
40
38
  #
41
39
  def should_run?(event)
42
40
  logger.trace("Checking guards #{self}")
43
- check(@only_if, check_type: :only_if,
44
- event: event) && check(@not_if, check_type: :not_if, event: event)
45
- end
46
-
47
- private
48
-
49
- #
50
- # Check if guard is satisfied
51
- #
52
- # @param [Array] conditions to check
53
- # @param [Symbol] check_type type of check to perform (:only_if or :not_if)
54
- # @param [Event] event openHAB event to see if it satisfies the guard
55
- #
56
- # @return [true,false] True if guard is satisfied, false otherwise
57
- #
58
- def check(conditions, check_type:, event:)
59
- return true if conditions.nil? || conditions.empty?
41
+ return false unless check_only_if(event)
42
+ return false unless check_not_if(event)
60
43
 
61
- procs, items = conditions.flatten.partition { |condition| condition.is_a?(Proc) }
62
- logger.trace("Procs: #{procs} Items: #{items}")
63
-
64
- items.each { |item| logger.trace { "#{item} truthy? #{item.truthy?}" } }
65
-
66
- process_check(check_type: check_type, event: event, items: items, procs: procs)
44
+ true
67
45
  end
68
46
 
69
- #
70
- # Execute the guard check
71
- #
72
- # @param [Symbol] check_type :only_if or :not_if to check
73
- # @param [Object] event event to check if meets guard
74
- # @param [Array<Item>] items to check if satisfy criteria
75
- # @param [Array] procs to check if satisfy criteria
76
- #
77
- # @return [true,false] True if criteria are satisfied, false otherwise
78
- #
79
- def process_check(check_type:, event:, items:, procs:)
80
- case check_type
81
- when :only_if then process_only_if(event, items, procs)
82
- when :not_if then process_not_if(event, items, procs)
83
- else raise ArgumentError, "Unexpected check type: #{check_type}"
84
- end
85
- end
47
+ private
86
48
 
87
49
  #
88
- # Check not_if guard
50
+ # Check not_if guards
89
51
  #
90
52
  # @param [Object] event to check if meets guard
91
- # @param [Array<Item>] items to check if satisfy criteria
92
- # @param [Array] procs to check if satisfy criteria
93
53
  #
94
54
  # @return [true,false] True if criteria are satisfied, false otherwise
95
55
  #
96
- def process_not_if(event, items, procs)
97
- items.flatten.none?(&:truthy?) && procs.none? { |proc| @run_context.instance_exec(event, &proc) }
56
+ def check_not_if(event)
57
+ @not_if.nil? || @not_if.none? { |proc| @run_context.instance_exec(event, &proc) }
98
58
  end
99
59
 
100
60
  #
101
- # Check only_if guard
61
+ # Check only_if guards
102
62
  #
103
63
  # @param [Object] event to check if meets guard
104
- # @param [Array<Item>] items to check if satisfy criteria
105
- # @param [Array] procs to check if satisfy criteria
106
64
  #
107
65
  # @return [true,false] True if criteria are satisfied, false otherwise
108
66
  #
109
- def process_only_if(event, items, procs)
110
- items.flatten.all?(&:truthy?) && procs.all? { |proc| @run_context.instance_exec(event, &proc) }
67
+ def check_only_if(event)
68
+ @only_if.nil? || @only_if.all? { |proc| @run_context.instance_exec(event, &proc) }
111
69
  end
112
70
  end
113
71
  end
@@ -60,6 +60,8 @@ module OpenHAB
60
60
  infer_rule_name_from_item_trigger(trigger, items, kwargs)
61
61
  when :channel_linked, :channel_unlinked
62
62
  infer_rule_name_from_channel_link_trigger(trigger)
63
+ when :item_added, :item_removed, :item_updated
64
+ infer_rule_name_from_item_registry_trigger(trigger)
63
65
  when :thing_added, :thing_removed, :thing_updated
64
66
  infer_rule_name_from_thing_trigger(trigger)
65
67
  when :on_start
@@ -116,6 +118,15 @@ module OpenHAB
116
118
  (trigger == :channel_linked) ? "Channel linked to item" : "Channel unlinked from item"
117
119
  end
118
120
 
121
+ # formulate a readable rule name from an item added/updated/remove trigger
122
+ def infer_rule_name_from_item_registry_trigger(trigger)
123
+ {
124
+ item_added: "Item added",
125
+ item_updated: "Item updated",
126
+ item_removed: "Item removed"
127
+ }[trigger]
128
+ end
129
+
119
130
  # formulate a readable rule name from a thing added/updated/remove trigger
120
131
  def infer_rule_name_from_thing_trigger(trigger)
121
132
  {
@@ -9,12 +9,11 @@ module OpenHAB
9
9
  # @visibility private
10
10
  module Property
11
11
  #
12
- # Dynamically creates a property that acts and an accessor with no arguments
12
+ # Dynamically creates a property that acts as an accessor with no arguments
13
13
  # and a setter with any number of arguments or a block.
14
14
  #
15
15
  # @param [String] name of the property
16
16
  #
17
- #
18
17
  def prop(name)
19
18
  # rubocop rules are disabled because this method is dynamically defined on the calling
20
19
  # object making calls to other methods in this module impossible, or if done on methods
@@ -36,8 +35,8 @@ module OpenHAB
36
35
  end
37
36
 
38
37
  #
39
- # Dynamically creates a property array acts and an accessor with no arguments
40
- # and a pushes any number of arguments or a block onto they property array
38
+ # Dynamically creates a property array that acts as an accessor with no arguments
39
+ # and pushes any number of arguments or a block onto the property array
41
40
  # You can provide a block to this method which can be used to check if the provided value is acceptable.
42
41
  #
43
42
  # @param [String] name of the property
@@ -51,12 +51,15 @@ module OpenHAB
51
51
  def_terse_rule(:channel_unlinked)
52
52
  def_terse_rule(:cron)
53
53
  def_terse_rule(:every)
54
+ def_terse_rule(:item_added)
55
+ def_terse_rule(:item_updated)
56
+ def_terse_rule(:item_removed)
57
+ def_terse_rule(:on_start)
54
58
  def_terse_rule(:received_command)
55
59
  def_terse_rule(:thing_added)
56
60
  def_terse_rule(:thing_updated)
57
61
  def_terse_rule(:thing_removed)
58
62
  def_terse_rule(:updated)
59
- def_terse_rule(:on_start)
60
63
  end
61
64
  end
62
65
  end
@@ -96,7 +96,7 @@ module OpenHAB
96
96
  yield
97
97
  end
98
98
  rule.on_removal(self)
99
- @tracking_to, = retrieve_states(inputs)
99
+ _, @tracking_from = retrieve_states(inputs)
100
100
  end
101
101
 
102
102
  #
@@ -106,12 +106,11 @@ module OpenHAB
106
106
  # @param [Hash] mod rule trigger mods
107
107
  #
108
108
  def process_active_timer(inputs, mod, &block)
109
- state, = retrieve_states(inputs)
110
- if state == @tracking_to
111
- logger.trace("Item changed to #{state} for #{self}, rescheduling timer.")
112
- @timer.reschedule(@duration)
109
+ new_state, old_state = retrieve_states(inputs)
110
+ if new_state != @tracking_from && @conditions.check_to(inputs: {})
111
+ logger.trace("Item changed from #{old_state} to #{new_state} for #{self}, keep waiting.")
113
112
  else
114
- logger.trace("Item changed to #{state} for #{self}, canceling timer.")
113
+ logger.trace("Item changed from #{old_state} to #{new_state} for #{self}, canceling timer.")
115
114
  @timer.cancel
116
115
  # Reprocess trigger delay after canceling to track new state (if guards matched, etc)
117
116
  process(mod: mod, inputs: inputs, &block)
@@ -186,6 +186,7 @@ module OpenHAB
186
186
  #
187
187
  #
188
188
  def trigger(config:, attach:)
189
+ CronHandler::CronTriggerHandlerFactory.instance # ensure it's registered
189
190
  append_trigger(type: CRON_TRIGGER_MODULE_ID, config: config, attach: attach)
190
191
  end
191
192
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "singleton"
4
+
3
5
  require_relative "cron"
4
6
 
5
7
  module OpenHAB
@@ -77,49 +79,35 @@ module OpenHAB
77
79
 
78
80
  # Implements the ScriptedTriggerHandlerFactory interface to create a new Cron Trigger Handler
79
81
  class CronTriggerHandlerFactory
82
+ include Singleton
80
83
  include org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory
81
84
 
82
- # Invoked by openHAB core to get a trigger handler for the supllied trigger
83
- # @param [org.openhab.core.automation.Trigger] trigger
84
- #
85
- # @return [WatchTriggerHandler] trigger handler for supplied trigger
86
- def get(trigger)
87
- CronTriggerHandler.new(trigger)
88
- end
89
- end
90
-
91
- class << self
92
- private
93
-
94
- #
95
- # Creates trigger types and trigger type factories for openHAB
96
- #
97
- def add_script_cron_handler
85
+ def initialize
98
86
  Core.automation_manager.add_trigger_handler(
99
87
  Cron::CRON_TRIGGER_MODULE_ID,
100
- CronTriggerHandlerFactory.new
88
+ self
101
89
  )
102
90
 
103
- Core.automation_manager.add_trigger_type(cron_trigger_type)
91
+ Core.automation_manager.add_trigger_type(org.openhab.core.automation.type.TriggerType.new(
92
+ Cron::CRON_TRIGGER_MODULE_ID,
93
+ nil,
94
+ "A specific instant occurs",
95
+ "Triggers when the specified instant occurs",
96
+ nil,
97
+ org.openhab.core.automation.Visibility::VISIBLE,
98
+ nil
99
+ ))
104
100
  logger.trace("Added script cron trigger handler")
105
101
  end
106
102
 
103
+ # Invoked by openHAB core to get a trigger handler for the supplied trigger
104
+ # @param [org.openhab.core.automation.Trigger] trigger
107
105
  #
108
- # Creates trigger types and trigger type factories for openHAB
109
- #
110
- def cron_trigger_type
111
- org.openhab.core.automation.type.TriggerType.new(
112
- Cron::CRON_TRIGGER_MODULE_ID,
113
- nil,
114
- "A specific instant occurs",
115
- "Triggers when the specified instant occurs",
116
- nil,
117
- org.openhab.core.automation.Visibility::VISIBLE,
118
- nil
119
- )
106
+ # @return [WatchTriggerHandler] trigger handler for supplied trigger
107
+ def get(trigger)
108
+ CronTriggerHandler.new(trigger)
120
109
  end
121
110
  end
122
- add_script_cron_handler
123
111
  end
124
112
  end
125
113
  end
@@ -44,6 +44,7 @@ module OpenHAB
44
44
  #
45
45
  #
46
46
  def trigger(config:, attach:)
47
+ WatchHandler::WatchTriggerHandlerFactory.instance # ensure it's registered
47
48
  append_trigger(type: WatchHandler::WATCH_TRIGGER_MODULE_ID,
48
49
  config: config,
49
50
  attach: attach)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "singleton"
4
+
3
5
  module OpenHAB
4
6
  module DSL
5
7
  module Rules
@@ -9,21 +11,6 @@ module OpenHAB
9
11
  # Module for watching directories/files
10
12
  #
11
13
  module WatchHandler
12
- #
13
- # Creates trigger types and trigger type factories for openHAB
14
- #
15
- private_class_method def self.watch_trigger_type
16
- org.openhab.core.automation.type.TriggerType.new(
17
- WATCH_TRIGGER_MODULE_ID,
18
- nil,
19
- "A path change event is detected",
20
- "Triggers when a path change event is detected",
21
- nil,
22
- org.openhab.core.automation.Visibility::VISIBLE,
23
- nil
24
- )
25
- end
26
-
27
14
  # Trigger ID for Watch Triggers
28
15
  WATCH_TRIGGER_MODULE_ID = "jsr223.jruby.WatchTrigger"
29
16
 
@@ -68,7 +55,7 @@ module OpenHAB
68
55
  # Invoked by java super class when an watch event occurs
69
56
  # @param [String] _event ignored
70
57
  # @param [StandardWatchEventKind] kind NIO watch event kind
71
- # @param [java.nio.Path] path that had an event
58
+ # @param [java.nio.file.Path] path that had an event
72
59
  def processWatchEvent(_event, kind, path)
73
60
  @block.call(Events::WatchEvent.new(EVENT_TO_SYMBOL[kind], Pathname.new(path.to_s)))
74
61
  end
@@ -123,8 +110,27 @@ module OpenHAB
123
110
 
124
111
  # Implements the ScriptedTriggerHandlerFactory interface to create a new Trigger Handler
125
112
  class WatchTriggerHandlerFactory
113
+ include Singleton
126
114
  include org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory
127
115
 
116
+ def initialize
117
+ Core.automation_manager.add_trigger_handler(
118
+ WATCH_TRIGGER_MODULE_ID,
119
+ self
120
+ )
121
+
122
+ Core.automation_manager.add_trigger_type(org.openhab.core.automation.type.TriggerType.new(
123
+ WATCH_TRIGGER_MODULE_ID,
124
+ nil,
125
+ "A path change event is detected",
126
+ "Triggers when a path change event is detected",
127
+ nil,
128
+ org.openhab.core.automation.Visibility::VISIBLE,
129
+ nil
130
+ ))
131
+ logger.trace("Added watch trigger handler")
132
+ end
133
+
128
134
  # Invoked by openHAB core to get a trigger handler for the supllied trigger
129
135
  # @param [org.openhab.core.automation.Trigger] trigger
130
136
  #
@@ -133,20 +139,6 @@ module OpenHAB
133
139
  WatchTriggerHandler.new(trigger)
134
140
  end
135
141
  end
136
-
137
- #
138
- # Creates trigger types and trigger type factories for openHAB
139
- #
140
- def self.add_watch_handler
141
- Core.automation_manager.add_trigger_handler(
142
- WATCH_TRIGGER_MODULE_ID,
143
- WatchTriggerHandlerFactory.new
144
- )
145
-
146
- Core.automation_manager.add_trigger_type(watch_trigger_type)
147
- logger.trace("Added watch trigger handler")
148
- end
149
- add_watch_handler
150
142
  end
151
143
  end
152
144
  end
@@ -37,7 +37,7 @@ module OpenHAB
37
37
  # @see Items::Builder
38
38
  #
39
39
  class Builder
40
- # @return [org.openhab.core.things.ManagedThingProvider]
40
+ # @return [org.openhab.core.thing.ManagedThingProvider]
41
41
  attr_reader :provider
42
42
 
43
43
  def initialize(provider)
@@ -12,6 +12,7 @@ module OpenHAB
12
12
  # Keys to persist
13
13
  KNOWN_KEYS = %i[
14
14
  openhab_ensure_states
15
+ openhab_holiday_file
15
16
  openhab_persistence_service
16
17
  openhab_providers
17
18
  openhab_rule_uid
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of openHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.0.0.rc9"
7
+ VERSION = "5.0.0.rc11"
8
8
  end
9
9
  end