openhab-jrubyscripting 5.0.0.rc1 → 5.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +1 -12
  3. data/lib/openhab/core/items/generic_item.rb +15 -7
  4. data/lib/openhab/core/items/metadata/hash.rb +81 -39
  5. data/lib/openhab/core/items/metadata/namespace_hash.rb +17 -19
  6. data/lib/openhab/core/items/metadata/provider.rb +48 -0
  7. data/lib/openhab/core/items/persistence.rb +2 -0
  8. data/lib/openhab/core/items/provider.rb +40 -0
  9. data/lib/openhab/core/items/proxy.rb +10 -0
  10. data/lib/openhab/core/items/registry.rb +16 -7
  11. data/lib/openhab/core/items/semantics/enumerable.rb +6 -4
  12. data/lib/openhab/core/items/state_storage.rb +3 -3
  13. data/lib/openhab/core/profile_factory.rb +3 -1
  14. data/lib/openhab/core/provider.rb +223 -0
  15. data/lib/openhab/core/registry.rb +30 -0
  16. data/lib/openhab/core/rules/provider.rb +25 -0
  17. data/lib/openhab/core/rules/registry.rb +76 -0
  18. data/lib/openhab/core/rules/rule.rb +150 -0
  19. data/lib/openhab/core/rules.rb +25 -0
  20. data/lib/openhab/core/script_handling.rb +50 -0
  21. data/lib/openhab/core/things/links/provider.rb +40 -0
  22. data/lib/openhab/core/things/provider.rb +25 -0
  23. data/lib/openhab/core/things/proxy.rb +10 -0
  24. data/lib/openhab/core/things/registry.rb +25 -2
  25. data/lib/openhab/core/timer.rb +17 -7
  26. data/lib/openhab/core/types/quantity_type.rb +5 -2
  27. data/lib/openhab/core/types.rb +1 -1
  28. data/lib/openhab/core.rb +3 -30
  29. data/lib/openhab/core_ext/java/class.rb +34 -0
  30. data/lib/openhab/core_ext/java/list.rb +436 -0
  31. data/lib/openhab/core_ext/java/local_time.rb +2 -1
  32. data/lib/openhab/core_ext/java/map.rb +66 -0
  33. data/lib/openhab/core_ext/java/month.rb +2 -1
  34. data/lib/openhab/core_ext/java/zoned_date_time.rb +1 -2
  35. data/lib/openhab/core_ext/ruby/date.rb +2 -0
  36. data/lib/openhab/core_ext/ruby/date_time.rb +53 -0
  37. data/lib/openhab/core_ext/ruby/time.rb +88 -86
  38. data/lib/openhab/dsl/events/watch_event.rb +1 -1
  39. data/lib/openhab/dsl/items/builder.rb +38 -100
  40. data/lib/openhab/dsl/items/ensure.rb +6 -2
  41. data/lib/openhab/dsl/items/timed_command.rb +10 -11
  42. data/lib/openhab/dsl/rules/automation_rule.rb +36 -13
  43. data/lib/openhab/dsl/rules/builder.rb +126 -8
  44. data/lib/openhab/dsl/rules/name_inference.rb +0 -5
  45. data/lib/openhab/dsl/rules/terse.rb +1 -2
  46. data/lib/openhab/dsl/rules/triggers/changed.rb +7 -4
  47. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +17 -53
  48. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +0 -3
  49. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +1 -1
  50. data/lib/openhab/dsl/rules/triggers/trigger.rb +1 -1
  51. data/lib/openhab/dsl/rules/triggers/updated.rb +7 -3
  52. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +1 -1
  53. data/lib/openhab/dsl/rules.rb +0 -21
  54. data/lib/openhab/dsl/script_handling.rb +0 -49
  55. data/lib/openhab/dsl/things/builder.rb +8 -31
  56. data/lib/openhab/dsl/thread_local.rb +3 -2
  57. data/lib/openhab/dsl/timer_manager.rb +16 -8
  58. data/lib/openhab/dsl/version.rb +1 -1
  59. data/lib/openhab/dsl.rb +137 -120
  60. data/lib/openhab/log.rb +3 -3
  61. data/lib/openhab/rspec/example_group.rb +42 -0
  62. data/lib/openhab/rspec/helpers.rb +33 -27
  63. data/lib/openhab/rspec/hooks.rb +17 -23
  64. data/lib/openhab/rspec/karaf.rb +45 -27
  65. data/lib/openhab/rspec/mocks/synchronous_executor.rb +11 -4
  66. data/lib/openhab/rspec/mocks/timer.rb +7 -1
  67. data/lib/openhab/rspec/suspend_rules.rb +4 -2
  68. metadata +30 -3
  69. data/lib/openhab/rspec/mocks/metadata_provider.rb +0 -75
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @!visibility private
4
+ module Java::JavaUtil::Map # rubocop:disable Style/ClassAndModuleChildren
5
+ def compact
6
+ reject { |_k, v| v.nil? }
7
+ end
8
+
9
+ def compact!
10
+ reject! { |_k, v| v.nil? }
11
+ self
12
+ end
13
+
14
+ def deconstruct_keys
15
+ self
16
+ end
17
+
18
+ def except(*keys)
19
+ reject { |k, _v| keys.include?(k) }
20
+ end
21
+
22
+ def slice(*keys)
23
+ select { |k, _v| keys.include?(k) }
24
+ end
25
+
26
+ def transform_keys(hash2 = nil)
27
+ raise NotImplementedError unless hash2 || block_given?
28
+
29
+ map do |k, v| # rubocop:disable Style/MapToHash
30
+ if hash2&.key?(k)
31
+ [hash2[k], v]
32
+ elsif block_given?
33
+ [(yield k), v]
34
+ else
35
+ [k, v]
36
+ end
37
+ end.to_h
38
+ end
39
+
40
+ def transform_keys!(hash2 = nil)
41
+ raise NotImplementedError unless hash2 || block_given?
42
+
43
+ keys.each do |k|
44
+ if hash2&.key?(k)
45
+ self[hash2[k]] = delete(k)
46
+ elsif block_given?
47
+ new_k = yield k
48
+ self[new_k] = delete(k) unless new_k == k
49
+ end
50
+ end
51
+ self
52
+ end
53
+
54
+ def transform_values
55
+ map do |k, v| # rubocop:disable Style/MapToHash, Style/HashTransformValues
56
+ [k, (yield v)]
57
+ end.to_h
58
+ end
59
+
60
+ def transform_values!
61
+ replace_all do |_k, v|
62
+ yield v
63
+ end
64
+ self
65
+ end
66
+ end
@@ -9,7 +9,7 @@ module OpenHAB
9
9
 
10
10
  # Extensions to Month
11
11
  class Month
12
- include Time
12
+ # @!parse include Time
13
13
 
14
14
  #
15
15
  # Returns the next month
@@ -57,3 +57,4 @@ module OpenHAB
57
57
  end
58
58
 
59
59
  Month = OpenHAB::CoreExt::Java::Month unless Object.const_defined?(:Month)
60
+ java.time.Month.include(OpenHAB::CoreExt::Java::Time)
@@ -25,8 +25,7 @@ module OpenHAB
25
25
  # @return [ZonedDateTime] If other is a TemporalAmount
26
26
  def -(other)
27
27
  if other.respond_to?(:to_zoned_date_time)
28
- nanos = other.to_zoned_date_time.until(self, java.time.temporal.ChronoUnit::NANOS)
29
- (nanos.to_f / 1_000_000_000).seconds
28
+ java.time.Duration.between(other.to_zoned_date_time, self)
30
29
  elsif other.is_a?(Numeric)
31
30
  minus(other.seconds)
32
31
  else
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "date"
4
+
3
5
  # Extensions to Date
4
6
  class Date
5
7
  #
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "forwardable"
5
+
6
+ # DateTime inherits from Date, but is more similar to Time semantically.
7
+ # So it avoid alias_method chain bombs, and to ensure the correct end methods
8
+ # exist here, we define the important methods from Time here as well.
9
+
10
+ # Extensions to DateTime
11
+ class DateTime < Date
12
+ extend Forwardable
13
+
14
+ # (see Time#plus_with_temporal)
15
+ def plus_with_temporal(other)
16
+ return to_zoned_date_time + other if other.is_a?(java.time.temporal.TemporalAmount)
17
+
18
+ plus_without_temporal(other)
19
+ end
20
+ # alias_method :plus_without_temporal, :+ # already done by Date
21
+ alias_method :+, :plus_with_temporal
22
+
23
+ # (see Time#minus_with_temporal)
24
+ def minus_with_temporal(other)
25
+ return to_zoned_date_time - other if other.is_a?(java.time.temporal.TemporalAmount)
26
+
27
+ # Exclude subtracting against the same class
28
+ if other.respond_to?(:to_zoned_date_time) && !other.is_a?(self.class)
29
+ return to_zoned_date_time - other.to_zoned_date_time
30
+ end
31
+
32
+ minus_without_temporal(other)
33
+ end
34
+ # alias_method :minus_without_temporal, :- # already done by Date
35
+ alias_method :-, :minus_with_temporal
36
+
37
+ # @!method to_local_time
38
+ # @return [LocalTime]
39
+ def_delegator :to_zoned_date_time, :to_local_time
40
+
41
+ # (see Time#to_zoned_date_time)
42
+ def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
43
+ to_java(ZonedDateTime)
44
+ end
45
+
46
+ # (see Time#coerce)
47
+ def coerce(other)
48
+ return unless other.respond_to?(:to_zoned_date_time)
49
+
50
+ zdt = to_zoned_date_time
51
+ [other.to_zoned_date_time(zdt), zdt]
52
+ end
53
+ end
@@ -2,103 +2,105 @@
2
2
 
3
3
  require "forwardable"
4
4
 
5
- module OpenHAB
6
- module CoreExt
7
- module Ruby
8
- # Extensions to Time and DateTime
9
- module TimeExtensions
10
- extend Forwardable
11
-
12
- # @!visibility private
13
- def self.included(base)
14
- base.send :alias_method, :plus_without_temporal, :+
15
- base.send :alias_method, :+, :plus_with_temporal
16
- base.send :alias_method, :minus_without_temporal, :-
17
- base.send :alias_method, :-, :minus_with_temporal
18
- end
5
+ # Extensions to Time
6
+ class Time
7
+ extend Forwardable
19
8
 
20
- #
21
- # @!method +(other)
22
- #
23
- # Extends {#+} to allow adding a {java.time.temporal.TemporalAmount TemporalAmount}
24
- #
25
- # @param [java.time.temporal.TemporalAmount] other
26
- # @return [ZonedDateTime] If other is a {java.time.temporal.TemporalAmount TemporalAmount}
27
- # @return [Time] If other is a Numeric
28
- #
29
- def plus_with_temporal(other)
30
- return to_zoned_date_time + other if other.is_a?(java.time.temporal.TemporalAmount)
9
+ #
10
+ # @!method +(other)
11
+ #
12
+ # Extends {#+} to allow adding a {java.time.temporal.TemporalAmount TemporalAmount}
13
+ #
14
+ # @param [java.time.temporal.TemporalAmount] other
15
+ # @return [ZonedDateTime] If other is a {java.time.temporal.TemporalAmount TemporalAmount}
16
+ # @return [Time] If other is a Numeric
17
+ #
18
+ def plus_with_temporal(other)
19
+ return to_zoned_date_time + other if other.is_a?(java.time.temporal.TemporalAmount)
31
20
 
32
- plus_without_temporal(other)
33
- end
21
+ plus_without_temporal(other)
22
+ end
23
+ alias_method :plus_without_temporal, :+
24
+ alias_method :+, :plus_with_temporal
34
25
 
35
- #
36
- # @!method -(other)
37
- #
38
- # Extends {#-} to allow subtracting a {java.time.temporal.TemporalAmount TemporalAmount}
39
- #
40
- # @param [java.time.temporal.TemporalAmount] other
41
- # @return [ZonedDateTime] If other is a {java.time.temporal.TemporalAmount TemporalAmount}
42
- # @return [Time] If other is a Numeric
43
- #
44
- def minus_with_temporal(other)
45
- return to_zoned_date_time - other if other.is_a?(java.time.temporal.TemporalAmount)
26
+ #
27
+ # @!method -(other)
28
+ #
29
+ # Extends {#-} to allow subtracting a {java.time.temporal.TemporalAmount TemporalAmount}
30
+ # or any other date/time class that responds to #to_zoned_date_time.
31
+ #
32
+ # Subtractions with another object of the same class (e.g. Time - Other Time, or DateTime - Other DateTime)
33
+ # remains unchanged from its original behavior.
34
+ #
35
+ # @example Time - Duration -> ZonedDateTime
36
+ # zdt_one_hour_ago = Time.now - 1.hour
37
+ #
38
+ # @example Time - ZonedDateTime -> Duration
39
+ # java_duration = Time.now - 1.hour.ago
40
+ #
41
+ # @example Time - Numeric -> Time
42
+ # time_one_hour_ago = Time - 3600
43
+ #
44
+ # @example Time - Time -> Float
45
+ # one_day_in_secs = Time.new(2002, 10, 31) - Time.new(2002, 10, 30)
46
+ #
47
+ # @param [java.time.temporal.TemporalAmount, #to_zoned_date_time] other
48
+ # @return [ZonedDateTime] If other is a {java.time.temporal.TemporalAmount TemporalAmount}
49
+ # @return [Duration] If other responds to #to_zoned_date_time
50
+ # @return [Time] If other is a Numeric
51
+ # @return [Float] If other is a Time
52
+ #
53
+ def minus_with_temporal(other)
54
+ return to_zoned_date_time - other if other.is_a?(java.time.temporal.TemporalAmount)
46
55
 
47
- minus_without_temporal(other)
48
- end
56
+ # Exclude subtracting against the same class
57
+ if other.respond_to?(:to_zoned_date_time) && !other.is_a?(self.class)
58
+ return to_zoned_date_time - other.to_zoned_date_time
59
+ end
49
60
 
50
- # @return [LocalDate]
51
- def to_local_date(_context = nil)
52
- java.time.LocalDate.of(year, month, day)
53
- end
61
+ minus_without_temporal(other)
62
+ end
63
+ alias_method :minus_without_temporal, :-
64
+ alias_method :-, :minus_with_temporal
54
65
 
55
- # @!method to_local_time
56
- # @return [LocalTime]
57
- def_delegator :to_zoned_date_time, :to_local_time
66
+ # @return [LocalDate]
67
+ def to_local_date(_context = nil)
68
+ java.time.LocalDate.of(year, month, day)
69
+ end
58
70
 
59
- # @return [Month]
60
- def to_month
61
- java.time.Month.of(month)
62
- end
71
+ # @!method to_local_time
72
+ # @return [LocalTime]
73
+ def_delegator :to_zoned_date_time, :to_local_time
63
74
 
64
- # @return [MonthDay]
65
- def to_month_day
66
- java.time.MonthDay.of(month, day)
67
- end
75
+ # @return [Month]
76
+ def to_month
77
+ java.time.Month.of(month)
78
+ end
68
79
 
69
- # @param [ZonedDateTime, nil] context
70
- # A {ZonedDateTime} used to fill in missing fields
71
- # during conversion. Not used in this class.
72
- # @return [ZonedDateTime]
73
- def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
74
- to_java(ZonedDateTime)
75
- end
80
+ # @return [MonthDay]
81
+ def to_month_day
82
+ java.time.MonthDay.of(month, day)
83
+ end
76
84
 
77
- #
78
- # Converts to a {ZonedDateTime} if `other`
79
- # is also convertible to a ZonedDateTime.
80
- #
81
- # @param [#to_zoned_date_time] other
82
- # @return [Array, nil]
83
- #
84
- def coerce(other)
85
- [other.to_zoned_date_time(to_zoned_date_time), self] if other.respond_to?(:to_zoned_date_time)
86
- end
87
- end
88
- end
85
+ # @param [ZonedDateTime, nil] context
86
+ # A {ZonedDateTime} used to fill in missing fields
87
+ # during conversion. Not used in this class.
88
+ # @return [ZonedDateTime]
89
+ def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
90
+ to_java(java.time.ZonedDateTime)
89
91
  end
90
- end
91
92
 
92
- #
93
- # Extensions to Ruby Time
94
- #
95
- class Time
96
- include(OpenHAB::CoreExt::Ruby::TimeExtensions)
97
- end
93
+ #
94
+ # Converts to a {ZonedDateTime} if `other`
95
+ # is also convertible to a ZonedDateTime.
96
+ #
97
+ # @param [#to_zoned_date_time] other
98
+ # @return [Array, nil]
99
+ #
100
+ def coerce(other)
101
+ return unless other.respond_to?(:to_zoned_date_time)
98
102
 
99
- #
100
- # Extensions to Ruby DateTime
101
- #
102
- class DateTime
103
- include(OpenHAB::CoreExt::Ruby::TimeExtensions)
103
+ zdt = to_zoned_date_time
104
+ [other.to_zoned_date_time(zdt), zdt]
105
+ end
104
106
  end
@@ -4,7 +4,7 @@ module OpenHAB
4
4
  module DSL
5
5
  module Events
6
6
  #
7
- # Event object passed by a {Rules::Builder#watch} trigger.
7
+ # Event object passed by a {Rules::BuilderDSL#watch} trigger.
8
8
  #
9
9
  # @!attribute [r] path
10
10
  # @return [Pathname] The path that had an event
@@ -6,100 +6,6 @@ module OpenHAB
6
6
  # Contains extensions to simplify working with [GenericItem]s.
7
7
  #
8
8
  module Items
9
- # Stores all items created in scripts, and notifies the ItemRegistry
10
- # of their existence
11
- # @!visibility private
12
- class ItemProvider < org.openhab.core.common.registry.AbstractProvider
13
- include org.openhab.core.items.ItemProvider
14
- include Singleton
15
-
16
- def initialize
17
- super
18
-
19
- @items = {}
20
-
21
- $ir.add_provider(self)
22
- ScriptHandling.script_unloaded { $ir.remove_provider(self) }
23
- end
24
-
25
- # Add an item to this provider
26
- def add(builder)
27
- item = builder.build
28
- raise "Item #{item.name} already exists" if @items.key?(item.name)
29
-
30
- @items[item.name] = item
31
- notify_listeners_about_added_element(item)
32
-
33
- # make sure to add the item to the registry before linking it
34
- builder.channels.each do |(channel, config)|
35
- if !channel.include?(":") &&
36
- (group = builder.groups.find { |g| g.is_a?(GroupItemBuilder) && g.thing })
37
- thing = group.thing
38
- thing = thing.uid if thing.is_a?(Core::Things::Thing)
39
- channel = "#{thing}:#{channel}"
40
- end
41
- ItemChannelLinkProvider.instance.link(item, channel, config)
42
- end
43
-
44
- item
45
- end
46
-
47
- # Remove an item from this provider
48
- #
49
- # @return [GenericItem, nil] The removed item, if found.
50
- def remove(item_name, recursive: false)
51
- return nil unless @items.key?(item_name)
52
-
53
- item = @items.delete(item_name)
54
- if recursive && item.is_a?(Core::Items::GroupItem)
55
- item.members.each { |member| remove(member.__getobj__, recursive: true) }
56
- end
57
-
58
- notify_listeners_about_removed_element(item)
59
- item
60
- end
61
-
62
- # Get all items in this provider
63
- def getAll # rubocop:disable Naming/MethodName required by java interface
64
- @items.values
65
- end
66
- end
67
-
68
- # @!visibility private
69
- class ItemChannelLinkProvider < org.openhab.core.common.registry.AbstractProvider
70
- include org.openhab.core.thing.link.ItemChannelLinkProvider
71
- include Singleton
72
-
73
- def initialize
74
- super
75
-
76
- @links = Hash.new { |h, k| h[k] = Set.new }
77
- registry = OSGi.service("org.openhab.core.thing.link.ItemChannelLinkRegistry")
78
- registry.add_provider(self)
79
- ScriptHandling.script_unloaded { registry.remove_provider(self) }
80
- end
81
-
82
- # @!visibility private
83
- def link(item, channel, config = {})
84
- config = org.openhab.core.config.core.Configuration.new(config.transform_keys(&:to_s))
85
- channel = org.openhab.core.thing.ChannelUID.new(channel) if channel.is_a?(String)
86
- channel = channel.uid if channel.is_a?(org.openhab.core.thing.Channel)
87
- link = org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel, config)
88
-
89
- item_links = @links[item.name]
90
- if item_links.include?(link)
91
- notify_listeners_about_updated_element(link, link)
92
- else
93
- item_links << link
94
- notify_listeners_about_added_element(link)
95
- end
96
- end
97
-
98
- def getAll # rubocop:disable Naming/MethodName required by java interface
99
- @links.values.flatten
100
- end
101
- end
102
-
103
9
  # An item builder allows you to dynamically create OpenHAB items at runtime.
104
10
  # This can be useful either to create items as soon as the script loads,
105
11
  # or even later based on a rule executing.
@@ -134,7 +40,7 @@ module OpenHAB
134
40
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
135
41
  def #{method}_item(*args, **kwargs, &block) # def dimmer_item(*args, **kwargs, &block)
136
42
  item(#{method.inspect}, *args, **kwargs, &block) # item(:dimmer, *args, **kwargs, &block)
137
- end
43
+ end # end
138
44
  RUBY
139
45
  end
140
46
  end
@@ -196,10 +102,37 @@ module OpenHAB
196
102
  class BaseBuilderDSL
197
103
  include Builder
198
104
 
199
- private
105
+ # @!visibility private
106
+ class ProviderWrapper
107
+ attr_reader :provider
200
108
 
201
- def provider
202
- ItemProvider.instance
109
+ def initialize(provider)
110
+ @provider = provider
111
+ end
112
+
113
+ # @!visibility private
114
+ def add(builder)
115
+ item = builder.build
116
+ provider.add(item)
117
+ # make sure to add the item to the registry before linking it
118
+ builder.channels.each do |(channel, config)|
119
+ if !channel.include?(":") &&
120
+ (group = builder.groups.find { |g| g.is_a?(GroupItemBuilder) && g.thing })
121
+ thing = group.thing
122
+ channel = "#{thing}:#{channel}"
123
+ end
124
+ Core::Things::Links::Provider.link(item, channel, config)
125
+ end
126
+ item
127
+ end
128
+ end
129
+ private_constant :ProviderWrapper
130
+
131
+ # @return [org.openhab.core.items.ItemProvider]
132
+ attr_reader :provider
133
+
134
+ def initialize(provider)
135
+ @provider = ProviderWrapper.new(Core::Items::Provider.current(provider))
203
136
  end
204
137
  end
205
138
 
@@ -318,7 +251,12 @@ module OpenHAB
318
251
  @autoupdate = autoupdate
319
252
  @channels = []
320
253
  @expire = nil
321
- self.expire(*Array(expire)) if expire
254
+ if expire
255
+ expire = Array(expire)
256
+ expire_config = expire.pop if expire.last.is_a?(Hash)
257
+ expire_config ||= {}
258
+ self.expire(*expire, **expire_config)
259
+ end
322
260
  self.alexa(alexa) if alexa
323
261
  self.ga(ga) if ga
324
262
  self.homekit(homekit) if homekit
@@ -432,7 +370,7 @@ module OpenHAB
432
370
  # end
433
371
  # end
434
372
  #
435
- def channel(channel, **config)
373
+ def channel(channel, config = {})
436
374
  @channels << [channel, config]
437
375
  end
438
376
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "ruby2_keywords"
4
+
3
5
  module OpenHAB
4
6
  module DSL
5
7
  module Items
@@ -21,6 +23,9 @@ module OpenHAB
21
23
  end
22
24
 
23
25
  # Extensions for {::GenericItem} to implement {Ensure}'s functionality
26
+ #
27
+ # @see OpenHAB::DSL.ensure ensure
28
+ # @see OpenHAB::DSL.ensure_states ensure_states
24
29
  module GenericItem
25
30
  include Ensurable
26
31
 
@@ -71,14 +76,13 @@ module OpenHAB
71
76
  end
72
77
 
73
78
  # activate `ensure_states` before forwarding to the wrapped object
74
- def method_missing(method, *args, &block)
79
+ ruby2_keywords def method_missing(method, *args, &block)
75
80
  return super unless @item.respond_to?(method)
76
81
 
77
82
  DSL.ensure_states do
78
83
  @item.__send__(method, *args, &block)
79
84
  end
80
85
  end
81
- ruby2_keywords :method_missing if respond_to? :ruby2_keywords
82
86
 
83
87
  # .
84
88
  def respond_to_missing?(method, include_private = false)
@@ -123,9 +123,9 @@ module OpenHAB
123
123
  timed_command_details.on_expire = on_expire unless on_expire.nil?
124
124
  timed_command_details.timer.reschedule(duration)
125
125
  # disable the cancel rule while we send the new command
126
- Core.rule_manager.set_enabled(timed_command_details.rule_uid, false)
126
+ DSL.rules[timed_command_details.rule_uid].disable
127
127
  super(command)
128
- Core.rule_manager.set_enabled(timed_command_details.rule_uid, true)
128
+ DSL.rules[timed_command_details.rule_uid].enable
129
129
  timed_command_details
130
130
  end
131
131
  end
@@ -145,10 +145,9 @@ module OpenHAB
145
145
 
146
146
  timed_command_details.timer = timed_command_timer(timed_command_details, duration)
147
147
  cancel_rule = TimedCommandCancelRule.new(timed_command_details)
148
- timed_command_details.rule_uid = Core.automation_manager
149
- .add_rule(cancel_rule)
150
- .uid
151
- Rules.script_rules[timed_command_details.rule_uid] = cancel_rule
148
+ unmanaged_rule = Core.automation_manager.add_unmanaged_rule(cancel_rule)
149
+ timed_command_details.rule_uid = unmanaged_rule.uid
150
+ Core::Rules::Provider.current.add(unmanaged_rule)
152
151
  logger.trace "Created Timed Command #{timed_command_details}"
153
152
  timed_command_details
154
153
  end
@@ -160,7 +159,7 @@ module OpenHAB
160
159
  DSL.after(duration) do
161
160
  timed_command_details.mutex.synchronize do
162
161
  logger.trace "Timed command expired - #{timed_command_details}"
163
- DSL.remove_rule(timed_command_details.rule_uid)
162
+ DSL.rules.remove(timed_command_details.rule_uid)
164
163
  timed_command_details.resolution = :expired
165
164
  case timed_command_details.on_expire
166
165
  when Proc
@@ -200,11 +199,9 @@ module OpenHAB
200
199
  type: Rules::Triggers::Changed::ITEM_STATE_CHANGE,
201
200
  config: { "itemName" => timed_command_details.item.name }
202
201
  )]
202
+ self.visibility = Core::Rules::Visibility::HIDDEN
203
203
  end
204
204
 
205
- # Cleanup the rule; nothing to do here.
206
- def cleanup; end
207
-
208
205
  #
209
206
  # Execute the rule
210
207
  #
@@ -217,7 +214,7 @@ module OpenHAB
217
214
  logger.trace "Canceling implicit timer #{@timed_command_details.timer} for "\
218
215
  "#{@timed_command_details.item.name} because received event #{inputs}"
219
216
  @timed_command_details.timer.cancel
220
- DSL.remove_rule(@timed_command_details.rule_uid)
217
+ DSL.rules.remove(@timed_command_details.rule_uid)
221
218
  @timed_command_details.resolution = :cancelled
222
219
  if @timed_command_details.on_expire.is_a?(Proc)
223
220
  logger.trace "Executing user supplied block on timed command cancelation"
@@ -226,6 +223,8 @@ module OpenHAB
226
223
  end
227
224
  TimedCommand.timed_commands.delete(@timed_command_details.item)
228
225
  rescue Exception => e
226
+ raise if defined?(::RSpec)
227
+
229
228
  logger.log_exception(e)
230
229
  end
231
230
  end