openhab-jrubyscripting 5.0.0.rc4 → 5.0.0.rc6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions.rb +21 -9
  3. data/lib/openhab/core/dependency_tracking.rb +34 -0
  4. data/lib/openhab/core/entity_lookup.rb +132 -78
  5. data/lib/openhab/core/events/item_channel_link.rb +2 -2
  6. data/lib/openhab/core/events/item_command_event.rb +1 -1
  7. data/lib/openhab/core/events/item_event.rb +2 -2
  8. data/lib/openhab/core/events/item_state_changed_event.rb +1 -1
  9. data/lib/openhab/core/events/thing.rb +1 -1
  10. data/lib/openhab/core/items/accepted_data_types.rb +2 -2
  11. data/lib/openhab/core/items/contact_item.rb +1 -1
  12. data/lib/openhab/core/items/dimmer_item.rb +2 -2
  13. data/lib/openhab/core/items/generic_item.rb +45 -224
  14. data/lib/openhab/core/items/group_item.rb +5 -3
  15. data/lib/openhab/core/items/image_item.rb +2 -2
  16. data/lib/openhab/core/items/item.rb +219 -0
  17. data/lib/openhab/core/items/metadata/hash.rb +1 -1
  18. data/lib/openhab/core/items/metadata/namespace_hash.rb +1 -1
  19. data/lib/openhab/core/items/persistence.rb +19 -10
  20. data/lib/openhab/core/items/provider.rb +2 -2
  21. data/lib/openhab/core/items/proxy.rb +68 -7
  22. data/lib/openhab/core/items/registry.rb +6 -6
  23. data/lib/openhab/core/items/semantics/enumerable.rb +6 -6
  24. data/lib/openhab/core/items/semantics.rb +8 -7
  25. data/lib/openhab/core/items.rb +3 -2
  26. data/lib/openhab/core/provider.rb +14 -7
  27. data/lib/openhab/core/rules/registry.rb +2 -2
  28. data/lib/openhab/core/rules.rb +1 -1
  29. data/lib/openhab/core/script_handling.rb +6 -6
  30. data/lib/openhab/core/things/channel.rb +1 -1
  31. data/lib/openhab/core/things/channel_uid.rb +2 -2
  32. data/lib/openhab/core/things/item_channel_link.rb +2 -2
  33. data/lib/openhab/core/things/links/provider.rb +2 -2
  34. data/lib/openhab/core/things/profile_callback.rb +1 -1
  35. data/lib/openhab/core/things/registry.rb +1 -1
  36. data/lib/openhab/core/things/thing.rb +1 -1
  37. data/lib/openhab/core/timer.rb +21 -10
  38. data/lib/openhab/core/types/date_time_type.rb +4 -4
  39. data/lib/openhab/core/types/hsb_type.rb +2 -2
  40. data/lib/openhab/core/types/point_type.rb +1 -1
  41. data/lib/openhab/core/types/quantity_type.rb +1 -1
  42. data/lib/openhab/core/types.rb +1 -1
  43. data/lib/openhab/core/uid.rb +1 -1
  44. data/lib/openhab/core/value_cache.rb +188 -0
  45. data/lib/openhab/core.rb +57 -15
  46. data/lib/openhab/core_ext/between.rb +32 -0
  47. data/lib/openhab/core_ext/java/duration.rb +1 -0
  48. data/lib/openhab/core_ext/java/local_date.rb +1 -0
  49. data/lib/openhab/core_ext/java/local_time.rb +1 -0
  50. data/lib/openhab/core_ext/java/month.rb +12 -1
  51. data/lib/openhab/core_ext/java/month_day.rb +2 -0
  52. data/lib/openhab/core_ext/java/zoned_date_time.rb +4 -4
  53. data/lib/openhab/core_ext/ruby/date.rb +3 -1
  54. data/lib/openhab/core_ext/ruby/date_time.rb +1 -0
  55. data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
  56. data/lib/openhab/core_ext/ruby/time.rb +1 -0
  57. data/lib/openhab/dsl/items/builder.rb +17 -10
  58. data/lib/openhab/dsl/items/ensure.rb +5 -5
  59. data/lib/openhab/dsl/items/timed_command.rb +5 -5
  60. data/lib/openhab/dsl/rules/automation_rule.rb +54 -40
  61. data/lib/openhab/dsl/rules/builder.rb +128 -79
  62. data/lib/openhab/dsl/rules/guard.rb +5 -5
  63. data/lib/openhab/dsl/rules/name_inference.rb +21 -2
  64. data/lib/openhab/dsl/rules/rule_triggers.rb +3 -3
  65. data/lib/openhab/dsl/rules/terse.rb +1 -0
  66. data/lib/openhab/dsl/rules/triggers/changed.rb +27 -24
  67. data/lib/openhab/dsl/rules/triggers/command.rb +6 -5
  68. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +3 -3
  69. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +2 -2
  70. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +6 -6
  71. data/lib/openhab/dsl/rules/triggers/updated.rb +5 -5
  72. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +11 -12
  73. data/lib/openhab/dsl/things/builder.rb +73 -14
  74. data/lib/openhab/dsl/version.rb +2 -2
  75. data/lib/openhab/dsl.rb +45 -17
  76. data/lib/openhab/log.rb +5 -5
  77. data/lib/openhab/rspec/configuration.rb +5 -5
  78. data/lib/openhab/rspec/example_group.rb +1 -1
  79. data/lib/openhab/rspec/helpers.rb +5 -5
  80. data/lib/openhab/rspec/hooks.rb +19 -1
  81. data/lib/openhab/rspec/karaf.rb +13 -21
  82. data/lib/openhab/rspec/mocks/persistence_service.rb +15 -0
  83. data/lib/openhab/rspec/mocks/thing_handler.rb +2 -2
  84. data/lib/openhab/rspec/suspend_rules.rb +2 -1
  85. data/lib/openhab/yard/base_helper.rb +46 -0
  86. data/lib/openhab/yard/html_helper.rb +3 -3
  87. data/lib/openhab/yard/markdown_directive.rb +125 -0
  88. data/lib/openhab/yard/markdown_helper.rb +99 -0
  89. metadata +15 -7
@@ -18,6 +18,7 @@ module OpenHAB
18
18
  "core.ItemCommandTrigger",
19
19
  "core.ItemStateChangeTrigger",
20
20
  "core.ItemStateUpdateTrigger",
21
+ "core.SystemStartlevelTrigger",
21
22
  Triggers::Cron::CRON_TRIGGER_MODULE_ID
22
23
  ].freeze
23
24
  private_constant :KNOWN_TRIGGER_TYPES
@@ -61,6 +62,8 @@ module OpenHAB
61
62
  infer_rule_name_from_channel_link_trigger(trigger)
62
63
  when :thing_added, :thing_removed, :thing_updated
63
64
  infer_rule_name_from_thing_trigger(trigger)
65
+ when :on_start
66
+ infer_rule_name_from_on_start_trigger(items)
64
67
  end
65
68
  end
66
69
 
@@ -110,7 +113,7 @@ module OpenHAB
110
113
 
111
114
  # formulate a readable rule name from a channel link trigger
112
115
  def infer_rule_name_from_channel_link_trigger(trigger)
113
- trigger == :channel_linked ? "Channel linked to item" : "Channel unlinked from item"
116
+ (trigger == :channel_linked) ? "Channel linked to item" : "Channel unlinked from item"
114
117
  end
115
118
 
116
119
  # formulate a readable rule name from a thing added/updated/remove trigger
@@ -122,6 +125,22 @@ module OpenHAB
122
125
  }[trigger]
123
126
  end
124
127
 
128
+ # formulate a readable rule name from an on_start trigger
129
+ def infer_rule_name_from_on_start_trigger(levels)
130
+ levels = levels.map { |level| "#{level} (#{start_level_description(level)})" }
131
+ levels = format_array(levels)
132
+ "System Start Level reached #{levels}"
133
+ end
134
+
135
+ def start_level_description(level)
136
+ klass = org.openhab.core.service.StartLevelService.java_class
137
+ start_level_type = klass.declared_field("STARTLEVEL_OSGI").type
138
+ klass.declared_fields
139
+ .select { |field| field.type == start_level_type && field.name.start_with?("STARTLEVEL_") }
140
+ .find { |field| field.get_int(klass) == level }
141
+ .name.split("_", 2).last.downcase
142
+ end
143
+
125
144
  # format an array of words that will be the beginning of a sentence
126
145
  def format_beginning_of_sentence_array(array)
127
146
  result = format_array(array)
@@ -146,7 +165,7 @@ module OpenHAB
146
165
  return array[0] if array.length == 1
147
166
  return "#{array[0]} or #{array[1]}" if array.length == 2
148
167
 
149
- "any of #{array.join(", ")}"
168
+ "#{array[0..-2].join(", ")}, or #{array[-1]}"
150
169
  end
151
170
  end
152
171
  end
@@ -10,7 +10,7 @@ module OpenHAB
10
10
  module DSL
11
11
  module Rules
12
12
  #
13
- # Rule configuration for OpenHAB Rules engine
13
+ # Rule configuration for openHAB Rules engine
14
14
  #
15
15
  # @!visibility private
16
16
  class RuleTriggers
@@ -39,7 +39,7 @@ module OpenHAB
39
39
  # @param [Map] config map describing trigger configuration
40
40
  # @param [Object] attach object to be attached to the trigger
41
41
  #
42
- # @return [Trigger] OpenHAB trigger
42
+ # @return [org.openhab.core.automation.Trigger] openHAB trigger
43
43
  #
44
44
  def append_trigger(type:, config:, attach: nil, conditions: nil)
45
45
  config.transform_keys!(&:to_s)
@@ -57,7 +57,7 @@ module OpenHAB
57
57
  # @param [String] type of trigger
58
58
  # @param [Map] config map
59
59
  #
60
- # @return [OpenHAB Trigger] configured by type and supplied config
60
+ # @return [org.openhab.core.automation.Trigger] configured by type and supplied config
61
61
  #
62
62
  def self.trigger(type:, config:)
63
63
  logger.trace("Creating trigger of type '#{type}' config: #{config}")
@@ -56,6 +56,7 @@ module OpenHAB
56
56
  def_terse_rule(:thing_updated)
57
57
  def_terse_rule(:thing_removed)
58
58
  def_terse_rule(:updated)
59
+ def_terse_rule(:on_start)
59
60
  end
60
61
  end
61
62
  end
@@ -16,13 +16,13 @@ module OpenHAB
16
16
  #
17
17
  # Create the trigger
18
18
  #
19
- # @param [Object] item item to create trigger for
20
- # @param [Item State] from state to restrict trigger to
21
- # @param [Item State] to state to restrict trigger to
22
- # @param [Duration, nil] duration ruration to delay trigger until to state is met
19
+ # @param [Core::Items::Item, Core::Items::GroupItem::Members] item item to create trigger for
20
+ # @param [Core::Types::State, Array<Core::Types::State>, Range, Proc] from state to restrict trigger to
21
+ # @param [Core::Types::State, Array<Core::Types::State>, Range, Proc] to state to restrict trigger to
22
+ # @param [Duration, nil] duration duration to delay trigger until to state is met
23
23
  # @param [Object] attach object to be attached to the trigger
24
24
  #
25
- # @return [Trigger] OpenHAB triggers
25
+ # @return [org.openhab.core.automation.Trigger] openHAB triggers
26
26
  #
27
27
  def trigger(item:, from:, to:, duration:, attach:)
28
28
  if duration
@@ -50,17 +50,19 @@ module OpenHAB
50
50
  #
51
51
  # Create a TriggerDelay for for an item or group that is changed for a specific duration
52
52
  #
53
- # @param [Object] item to create trigger delay for
53
+ # @param [Core::Items::Item, Core::Items::GroupItem::Members] item to create trigger delay for
54
54
  # @param [Duration] duration to delay trigger for until condition is met
55
- # @param [Item State] to OpenHAB Item State item or group needs to change to
56
- # @param [Item State] from OpenHAB Item State item or group needs to be coming from
55
+ # @param [Core::Types::State, Array<Core::Types::State>, Range, Proc] to
56
+ # State item or group needs to change to
57
+ # @param [Core::Types::State, Array<Core::Types::State>, Range, Proc] from
58
+ # State item or group needs to be coming from
57
59
  # @param [Object] attach object to be attached to the trigger
58
60
  #
59
- # @return [Trigger] OpenHAB trigger
61
+ # @return [org.openhab.core.automation.Trigger]
60
62
  #
61
63
  def wait_trigger(item:, duration:, to: nil, from: nil, attach: nil)
62
64
  item_name = item.respond_to?(:name) ? item.name : item.to_s
63
- logger.trace("Creating Changed Wait Change Trigger for Item(#{item_name}) Duration(#{duration}) "\
65
+ logger.trace("Creating Changed Wait Change Trigger for Item(#{item_name}) Duration(#{duration}) " \
64
66
  "To(#{to}) From(#{from}) Attach(#{attach})")
65
67
  conditions = Conditions::Duration.new(to: to, from: from, duration: duration)
66
68
  changed_trigger(item: item, to: nil, from: nil, attach: attach, conditions: conditions)
@@ -68,11 +70,11 @@ module OpenHAB
68
70
 
69
71
  #
70
72
  # Creates a trigger with a range condition on either 'from' or 'to' field
71
- # @param [Object] item to create changed trigger on
72
- # @param [Object] from state to restrict trigger to
73
- # @param [Object] to state restrict trigger to
73
+ # @param [Core::Items::Item, Core::Items::GroupItem::Members] item to create changed trigger on
74
+ # @param [Range] from state to restrict trigger to
75
+ # @param [Range] to state restrict trigger to
74
76
  # @param [Object] attach object to be attached to the trigger
75
- # @return [Trigger] OpenHAB trigger
77
+ # @return [org.openhab.core.automation.Trigger]
76
78
  #
77
79
  def range_trigger(item:, from:, to:, attach:)
78
80
  from, to = Conditions::Proc.range_procs(from, to)
@@ -81,11 +83,11 @@ module OpenHAB
81
83
 
82
84
  #
83
85
  # Creates a trigger with a proc condition on either 'from' or 'to' field
84
- # @param [Object] item to create changed trigger on
85
- # @param [Object] from state to restrict trigger to
86
- # @param [Object] to state restrict trigger to
86
+ # @param [Core::Items::Item, Core::Items::GroupItem::Members] item to create changed trigger on
87
+ # @param [Proc] from state to restrict trigger to
88
+ # @param [Proc] to state restrict trigger to
87
89
  # @param [Object] attach object to be attached to the trigger
88
- # @return [Trigger] OpenHAB trigger
90
+ # @return [org.openhab.core.automation.Trigger]
89
91
  #
90
92
  def proc_trigger(item:, from:, to:, attach:)
91
93
  # swap from/to w/ nil if from/to is a proc
@@ -100,10 +102,11 @@ module OpenHAB
100
102
  #
101
103
  # Create a changed trigger
102
104
  #
103
- # @param [Object] item to create changed trigger on
104
- # @param [Object] from state to restrict trigger to
105
- # @param [Object] to state restrict trigger to
105
+ # @param [Core::Items::Item, Core::Items::GroupItem::Members] item to create changed trigger on
106
+ # @param [Core::Items::State, Array<Core::Items::State>] from state to restrict trigger to
107
+ # @param [Core::Items::State, Array<Core::Items::State>] to state restrict trigger to
106
108
  # @param [Object] attach object to be attached to the trigger
109
+ # @return [org.openhab.core.automation.Trigger]
107
110
  #
108
111
  def changed_trigger(item:, from:, to:, attach: nil, conditions: nil)
109
112
  type, config = case item
@@ -121,7 +124,7 @@ module OpenHAB
121
124
  #
122
125
  # Create a changed trigger for a thing
123
126
  #
124
- # @param [Thing] thing to detected changed states on
127
+ # @param [Core::Things::Thing] thing to detected changed states on
125
128
  # @param [String] from state to restrict trigger to
126
129
  # @param [String] to state to restrict trigger to
127
130
  #
@@ -152,7 +155,7 @@ module OpenHAB
152
155
  #
153
156
  # Create a changed trigger for group items
154
157
  #
155
- # @param [Group] group to detected changed states on
158
+ # @param [GroupItem] group to detected changed states on
156
159
  # @param [String] from state to restrict trigger to
157
160
  # @param [String] to to restrict trigger to
158
161
  #
@@ -160,7 +163,7 @@ module OpenHAB
160
163
  # second element is a Hash configuring trigger
161
164
  #
162
165
  def group(group:, from:, to:)
163
- config = { "groupName" => group.group.name }
166
+ config = { "groupName" => group.name }
164
167
  config["state"] = to.to_s if to
165
168
  config["previousState"] = from.to_s if from
166
169
  [GROUP_STATE_CHANGE, config]
@@ -18,7 +18,7 @@ module OpenHAB
18
18
  # @param [Object] command to check against
19
19
  # @param [Object] attach object to be attached to the trigger
20
20
  #
21
- # @return [Trigger] OpenHAB triggers
21
+ # @return [org.openhab.core.automation.Trigger]
22
22
  #
23
23
  def trigger(item:, command:, attach:)
24
24
  case command
@@ -31,9 +31,9 @@ module OpenHAB
31
31
  #
32
32
  # Creates a trigger with a range condition on the 'command' field
33
33
  # @param [Object] item to create changed trigger on
34
- # @param [Object] command to restrict trigger to
34
+ # @param [Range] command to restrict trigger to
35
35
  # @param [Object] attach object to be attached to the trigger
36
- # @return [Trigger] OpenHAB trigger
36
+ # @return [org.openhab.core.automation.Trigger]
37
37
  #
38
38
  def range_trigger(item:, command:, attach:)
39
39
  command_range, * = Conditions::Proc.range_procs(command)
@@ -45,7 +45,7 @@ module OpenHAB
45
45
  # @param [Object] item to create changed trigger on
46
46
  # @param [Object] command to restrict trigger to
47
47
  # @param [Object] attach object to be attached to the trigger
48
- # @return [Trigger] OpenHAB trigger
48
+ # @return [org.openhab.core.automation.Trigger]
49
49
  #
50
50
  def proc_trigger(item:, command:, attach:)
51
51
  conditions = Conditions::Proc.new(command: command)
@@ -58,6 +58,7 @@ module OpenHAB
58
58
  # @param [Object] item to create trigger for
59
59
  # @param [String] command to create trigger for
60
60
  # @param [Object] attach object to be attached to the trigger
61
+ # @return [org.openhab.core.automation.Trigger]
61
62
  #
62
63
  def command_trigger(item:, command:, attach: nil, conditions: nil)
63
64
  type, config = if item.is_a?(GroupItem::Members)
@@ -92,7 +93,7 @@ module OpenHAB
92
93
  #
93
94
  # Create trigger for group items
94
95
  #
95
- # @param [Group] group to create trigger for
96
+ # @param [GroupItem::Members] group to create trigger for
96
97
  #
97
98
  # @return [Array<Hash,Trigger>] first element is hash of trigger config properties
98
99
  # second element is trigger type
@@ -24,7 +24,7 @@ module OpenHAB
24
24
  @conditions = Conditions::Proc.new(to: to, from: from)
25
25
  @duration = duration
26
26
  @timer = nil
27
- logger.trace "Created Duration Condition To(#{to}) From(#{from}) "\
27
+ logger.trace "Created Duration Condition To(#{to}) From(#{from}) " \
28
28
  "Conditions(#{@conditions}) Duration(#{@duration})"
29
29
  end
30
30
 
@@ -57,7 +57,7 @@ module OpenHAB
57
57
  #
58
58
  # Check if trigger guards prevent rule execution
59
59
  #
60
- # @param [Map] inputs OpenHAB map object describing rule trigger
60
+ # @param [java.util.Map] inputs map object describing rule trigger
61
61
  #
62
62
  # @return [true,false] True if the rule should execute, false if trigger guard prevents execution
63
63
  #
@@ -70,7 +70,7 @@ module OpenHAB
70
70
  # Rerieve the newState and oldState, alternatively newStatus and oldStatus
71
71
  # from the input map
72
72
  #
73
- # @param [Map] inputs OpenHAB map object describing rule trigger
73
+ # @param [java.util.Map] inputs map object describing rule trigger
74
74
  #
75
75
  # @return [Array] An array of the values for [newState, oldState] or [newStatus, oldStatus]
76
76
  #
@@ -33,7 +33,7 @@ module OpenHAB
33
33
  }.freeze
34
34
  private_constant :CRON_EXPRESSION_MAP
35
35
 
36
- # @return [Hash] Map of days of the week from symbols to to OpenHAB cron strings
36
+ # @return [Hash] Map of days of the week from symbols to to openHAB cron strings
37
37
  DAY_OF_WEEK_MAP = {
38
38
  monday: "MON",
39
39
  tuesday: "TUE",
@@ -134,7 +134,7 @@ module OpenHAB
134
134
  #
135
135
  # @param [Map] map of cron expression
136
136
  #
137
- # @return [String] OpenHAB cron string
137
+ # @return [String] openHAB cron string
138
138
  #
139
139
  def self.map_to_cron(map)
140
140
  %i[second minute hour dom month dow year].map { |field| map.fetch(field) }.join(" ")
@@ -12,7 +12,7 @@ module OpenHAB
12
12
  #
13
13
  module CronHandler
14
14
  # Cron Trigger Handler that provides trigger IDs
15
- # Unfortunatly because the CronTriggerHandler in OpenHAB core is marked internal
15
+ # Unfortunatly because the CronTriggerHandler in openHAB core is marked internal
16
16
  # the entire thing must be recreated here
17
17
  class CronTriggerHandler < org.openhab.core.automation.handler.BaseTriggerModuleHandler
18
18
  include org.openhab.core.scheduler.SchedulerRunnable
@@ -22,7 +22,7 @@ module OpenHAB
22
22
  field_accessor :callback
23
23
 
24
24
  # Creates a new CronTriggerHandler
25
- # @param [Trigger] trigger OpenHAB trigger associated with handler
25
+ # @param [org.openhab.core.automation.Trigger] trigger openHAB trigger associated with handler
26
26
  #
27
27
  def initialize(trigger)
28
28
  @trigger = trigger
@@ -79,8 +79,8 @@ module OpenHAB
79
79
  class CronTriggerHandlerFactory
80
80
  include org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory
81
81
 
82
- # Invoked by the OpenHAB core to get a trigger handler for the supllied trigger
83
- # @param [Trigger] trigger OpenHAB trigger
82
+ # Invoked by openHAB core to get a trigger handler for the supllied trigger
83
+ # @param [org.openhab.core.automation.Trigger] trigger
84
84
  #
85
85
  # @return [WatchTriggerHandler] trigger handler for supplied trigger
86
86
  def get(trigger)
@@ -92,7 +92,7 @@ module OpenHAB
92
92
  private
93
93
 
94
94
  #
95
- # Creates trigger types and trigger type factories for OpenHAB
95
+ # Creates trigger types and trigger type factories for openHAB
96
96
  #
97
97
  def add_script_cron_handler
98
98
  Core.automation_manager.add_trigger_handler(
@@ -105,7 +105,7 @@ module OpenHAB
105
105
  end
106
106
 
107
107
  #
108
- # Creates trigger types and trigger type factories for OpenHAB
108
+ # Creates trigger types and trigger type factories for openHAB
109
109
  #
110
110
  def cron_trigger_type
111
111
  org.openhab.core.automation.type.TriggerType.new(
@@ -18,7 +18,7 @@ module OpenHAB
18
18
  # @param [Item State] to state to restrict trigger to
19
19
  # @param [Object] attach object to be attached to the trigger
20
20
  #
21
- # @return [Trigger] OpenHAB triggers
21
+ # @return [org.openhab.core.automation.Trigger]
22
22
  #
23
23
  def trigger(item:, to:, attach:)
24
24
  case to
@@ -44,7 +44,7 @@ module OpenHAB
44
44
  # @param [Object] item to create changed trigger on
45
45
  # @param [Object] to state restrict trigger to
46
46
  # @param [Object] attach object to be attached to the trigger
47
- # @return [Trigger] OpenHAB trigger
47
+ # @return [org.openhab.core.automation.Trigger]
48
48
  #
49
49
  def range_trigger(item:, to:, attach:)
50
50
  to, * = Conditions::Proc.range_procs(to)
@@ -56,7 +56,7 @@ module OpenHAB
56
56
  # @param [Object] item to create changed trigger on
57
57
  # @param [Object] to state restrict trigger to
58
58
  # @param [Object] attach object to be attached to the trigger
59
- # @return [Trigger] OpenHAB trigger
59
+ # @return [org.openhab.core.automation.Trigger]
60
60
  #
61
61
  def proc_trigger(item:, to:, attach:)
62
62
  conditions = Conditions::Proc.new(to: to)
@@ -70,7 +70,7 @@ module OpenHAB
70
70
  # @param [State] to state restriction on trigger
71
71
  # @param [Object] attach object to be attached to the trigger
72
72
  #
73
- # @return [Trigger] OpenHAB triggers
73
+ # @return [org.openhab.core.automation.Trigger]
74
74
  #
75
75
  def update_trigger(item:, to:, attach: nil, conditions: nil)
76
76
  type, config = case item
@@ -103,7 +103,7 @@ module OpenHAB
103
103
  #
104
104
  # Create an update trigger for a group
105
105
  #
106
- # @param [Item] item to create trigger for
106
+ # @param [GroupItem::Members] item to create trigger for
107
107
  # @param [State] to optional state restriction for target
108
108
  #
109
109
  # @return [Array<Hash,String>] first element is a String specifying trigger type
@@ -10,7 +10,7 @@ module OpenHAB
10
10
  #
11
11
  module WatchHandler
12
12
  #
13
- # Creates trigger types and trigger type factories for OpenHAB
13
+ # Creates trigger types and trigger type factories for openHAB
14
14
  #
15
15
  private_class_method def self.watch_trigger_type
16
16
  org.openhab.core.automation.type.TriggerType.new(
@@ -27,7 +27,7 @@ module OpenHAB
27
27
  # Trigger ID for Watch Triggers
28
28
  WATCH_TRIGGER_MODULE_ID = "jsr223.jruby.WatchTrigger"
29
29
 
30
- # Extends the OpenHAB watch service to watch directories
30
+ # Extends the openHAB watch service to watch directories
31
31
  #
32
32
  # Must match java method name style
33
33
  # rubocop:disable Naming/MethodName
@@ -75,12 +75,12 @@ module OpenHAB
75
75
  end
76
76
  # rubocop:enable Naming/MethodName
77
77
 
78
- # Implements the OpenHAB TriggerHandler interface to process Watch Triggers
78
+ # Implements the openHAB TriggerHandler interface to process Watch Triggers
79
79
  class WatchTriggerHandler
80
80
  include org.openhab.core.automation.handler.TriggerHandler
81
81
 
82
82
  # Creates a new WatchTriggerHandler
83
- # @param [Trigger] trigger OpenHAB trigger associated with handler
83
+ # @param [org.openhab.core.automation.Trigger] trigger
84
84
  #
85
85
  def initialize(trigger)
86
86
  @trigger = trigger
@@ -94,21 +94,20 @@ module OpenHAB
94
94
  # Create a lambda to use to invoke rule engine when file watch notification happens
95
95
  # @param [String] glob to match for notification events
96
96
  #
97
- # @return [Lambda] lambda to execute on notification events
97
+ # @return [Proc] lambda to execute on notification events
98
98
  #
99
99
  def watch_event_handler(glob)
100
- lambda { |watch_event|
100
+ lambda do |watch_event|
101
101
  if watch_event.path.fnmatch?(glob)
102
102
  logger.trace("Received event(#{watch_event})")
103
103
  @rule_engine_callback&.triggered(@trigger, { "event" => watch_event })
104
104
  else
105
105
  logger.trace("Event #{watch_event} did not match glob(#{glob})")
106
106
  end
107
- }
107
+ end
108
108
  end
109
109
 
110
- # Called by OpenHAB to set the rule engine to invoke when triggered
111
- # Must match java method name style
110
+ # Called by openHAB to set the rule engine to invoke when triggered
112
111
  def setCallback(callback) # rubocop:disable Naming/MethodName
113
112
  @rule_engine_callback = callback
114
113
  end
@@ -126,8 +125,8 @@ module OpenHAB
126
125
  class WatchTriggerHandlerFactory
127
126
  include org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory
128
127
 
129
- # Invoked by the OpenHAB core to get a trigger handler for the supllied trigger
130
- # @param [Trigger] trigger OpenHAB trigger
128
+ # Invoked by openHAB core to get a trigger handler for the supllied trigger
129
+ # @param [org.openhab.core.automation.Trigger] trigger
131
130
  #
132
131
  # @return [WatchTriggerHandler] trigger handler for supplied trigger
133
132
  def get(trigger)
@@ -136,7 +135,7 @@ module OpenHAB
136
135
  end
137
136
 
138
137
  #
139
- # Creates trigger types and trigger type factories for OpenHAB
138
+ # Creates trigger types and trigger type factories for openHAB
140
139
  #
141
140
  def self.add_watch_handler
142
141
  Core.automation_manager.add_trigger_handler(
@@ -6,14 +6,36 @@ module OpenHAB
6
6
  # Contains extensions to simplify working with {Core::Things::Thing Thing}s.
7
7
  #
8
8
  module Things
9
- # A thing builder allows you to dynamically create OpenHAB thing at runtime.
9
+ # A thing builder allows you to dynamically create openHAB things at runtime.
10
10
  # This can be useful either to create things as soon as the script loads,
11
11
  # or even later based on a rule executing.
12
12
  #
13
- # @example
13
+ # @example Create a Thing from the Astro Binding
14
14
  # things.build do
15
15
  # thing "astro:sun:home", "Astro Sun Data", config: { "geolocation" => "0,0" }
16
16
  # end
17
+ #
18
+ # @example Create a Thing with Channels
19
+ # thing_config = {
20
+ # availabilityTopic: "my-switch/status",
21
+ # payloadAvailable: "online",
22
+ # payloadNotAvailable: "offline"
23
+ # }
24
+ # things.build do
25
+ # thing("mqtt:topic:my-switch", "My Switch", bridge: "mqtt:bridge:mosquitto", config: thing_config) do
26
+ # channel("switch1", "switch", config: {
27
+ # stateTopic: "stat/my-switch/switch1/state", commandTopic="cmnd/my-switch/switch1/command"
28
+ # })
29
+ # channel("button1", "string", config: {
30
+ # stateTopic: "stat/my-switch/button1/state", commandTopic="cmnd/my-switch/button1/command"
31
+ # })
32
+ # end
33
+ # end
34
+ #
35
+ # @see ThingBuilder#initialize ThingBuilder#initialize for #thing's parameters
36
+ # @see ChannelBuilder#initialize ChannelBuilder#initialize for #channel's parameters
37
+ # @see Items::Builder
38
+ #
17
39
  class Builder
18
40
  # @return [org.openhab.core.things.ManagedThingProvider]
19
41
  attr_reader :provider
@@ -96,6 +118,26 @@ module OpenHAB
96
118
  end
97
119
  end
98
120
 
121
+ #
122
+ # Constructor for ThingBuilder
123
+ #
124
+ # @param [String] uid The ThingUID for the created Thing.
125
+ # This can consist one or more segments separated by a colon. When the uid contains:
126
+ # - One segment: When the uid contains one segment, `binding` or `bridge` id must be provided.
127
+ # - Two segments: `typeid:thingid` The `binding` or `bridge` id must be provided.
128
+ # - Three or more segments: `bindingid:typeid:[bridgeid...]:thingid`. The `type` and `bridge` can be omitted
129
+ # @param [String] label The Thing's label.
130
+ # @param [String] binding The binding id. When this argument is not provided,
131
+ # the binding id must be deducible from the `uid`, `type`, or `bridge`.
132
+ # @param [String] type The type id. When this argument is not provided,
133
+ # it will be deducible from the `uid` if it contains two or more segments.
134
+ # To create a Thing with a blank type id, use one segment for `uid` and provide the binding id.
135
+ # @param [String, BridgeBuilder] bridge The bridge uid, if the Thing should belong to a bridge.
136
+ # @param [String, Item] location The location of this Thing.
137
+ # When given an Item, use the item's label as the location.
138
+ # @param [Hash] config The Thing's configuration, as required by the binding. The key can be strings or symbols.
139
+ # @param [true,false] enabled Whether the Thing should be enabled or disabled.
140
+ #
99
141
  def initialize(uid, label = nil, binding: nil, type: nil, bridge: nil, location: nil, config: {}, enabled: nil)
100
142
  @channels = []
101
143
  uid = uid.to_s
@@ -117,13 +159,18 @@ module OpenHAB
117
159
  @bridge_uid = org.openhab.core.thing.ThingUID.new(*bridge_segments)
118
160
  end
119
161
 
120
- @uid = org.openhab.core.thing.ThingUID.new(*[binding, type, @bridge_uid&.id,
121
- uid_segments.last].compact)
162
+ thinguid = if uid_segments.length > 2
163
+ [binding, type, uid_segments.last].compact
164
+ else
165
+ [binding, type, @bridge_uid&.id, uid_segments.last].compact
166
+ end
167
+
168
+ @uid = org.openhab.core.thing.ThingUID.new(*thinguid)
122
169
  @thing_type_uid = org.openhab.core.thing.ThingTypeUID.new(*@uid.all_segments[0..1])
123
170
  @label = label
124
171
  @location = location
125
- @location = location.label if location.is_a?(GenericItem)
126
- @config = config
172
+ @location = location.label if location.is_a?(Item)
173
+ @config = config.transform_keys(&:to_s)
127
174
  @enabled = enabled
128
175
  end
129
176
 
@@ -132,7 +179,7 @@ module OpenHAB
132
179
  def channel(*args, **kwargs, &block)
133
180
  channel = ChannelBuilder.new(*args, thing: self, **kwargs)
134
181
  channel.instance_eval(&block) if block
135
- @channels << channel
182
+ @channels << channel.build
136
183
  end
137
184
 
138
185
  # @!visibility private
@@ -190,9 +237,22 @@ module OpenHAB
190
237
  # The ChannelBuilder DSL allows you to customize a channel
191
238
  class ChannelBuilder
192
239
  attr_accessor :label
193
- attr_reader :uid, :parameters
240
+ attr_reader :uid, :config, :type
194
241
 
195
- def initialize(uid, type, label, thing:, group: nil, **parameters)
242
+ #
243
+ # Constructor for ChannelBuilder
244
+ #
245
+ # This class is instantiated by the {ThingBuilder#channel #channel} method inside a {Builder#thing} block.
246
+ #
247
+ # @param [String] uid The channel's ID.
248
+ # @param [String, ChannelTypeUID, :trigger] type The concrete type of the channel.
249
+ # @param [String] label The channel label.
250
+ # @param [thing] thing The thing associated with this channel.
251
+ # This parameter is not needed for the {ThingBuilder#channel} method.
252
+ # @param [String] group The group name.
253
+ # @param [Hash] config Channel configuration. The keys can be strings or symbols.
254
+ #
255
+ def initialize(uid, type, label = nil, thing:, group: nil, config: {})
196
256
  @thing = thing
197
257
 
198
258
  uid = uid.to_s
@@ -207,13 +267,12 @@ module OpenHAB
207
267
  uid_segments[-1] = group_segments.join(org.openhab.core.thing.ChannelUID::CHANNEL_GROUP_SEPARATOR)
208
268
  end
209
269
  @uid = org.openhab.core.thing.ChannelUID.new(thing.uid, uid_segments.last)
210
- unless type.is_a?(org.openhab.core.thing.ChannelTypeUID)
211
- type = org.openhab.core.thing.ChannelTypeUID.new(thing.uid.binding_id,
212
- type)
270
+ unless type.is_a?(org.openhab.core.thing.type.ChannelTypeUID)
271
+ type = org.openhab.core.thing.type.ChannelTypeUID.new(thing.uid.binding_id, type)
213
272
  end
214
273
  @type = type
215
274
  @label = label
216
- @parameters = parameters
275
+ @config = config.transform_keys(&:to_s)
217
276
  end
218
277
 
219
278
  # @!visibility private
@@ -221,7 +280,7 @@ module OpenHAB
221
280
  org.openhab.core.thing.binding.builder.ChannelBuilder.create(uid)
222
281
  .with_kind(kind)
223
282
  .with_type(type)
224
- .with_configuration(org.openhab.core.config.core.Configuration.new(parameters))
283
+ .with_configuration(org.openhab.core.config.core.Configuration.new(config))
225
284
  .build
226
285
  end
227
286
 
@@ -2,8 +2,8 @@
2
2
 
3
3
  module OpenHAB
4
4
  module DSL
5
- # Version of OpenHAB helper libraries
5
+ # Version of openHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.0.0.rc4"
7
+ VERSION = "5.0.0.rc6"
8
8
  end
9
9
  end