openhab-scripting 5.16.0 → 5.18.0

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 (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +5 -5
  3. data/lib/openhab/core/events/item_state_updated_event.rb +11 -11
  4. data/lib/openhab/core/events/item_time_series_updated_event.rb +23 -0
  5. data/lib/openhab/core/events/timer_event.rb +32 -32
  6. data/lib/openhab/core/items/generic_item.rb +12 -0
  7. data/lib/openhab/core/items/numeric_item.rb +1 -1
  8. data/lib/openhab/core/items/persistence.rb +2 -4
  9. data/lib/openhab/core/items/registry.rb +1 -3
  10. data/lib/openhab/core/items/semantics/enumerable.rb +2 -2
  11. data/lib/openhab/core/items/semantics.rb +1 -4
  12. data/lib/openhab/core/profile_factory.rb +16 -1
  13. data/lib/openhab/core/rules/registry.rb +2 -1
  14. data/lib/openhab/core/rules/rule.rb +1 -0
  15. data/lib/openhab/core/sitemaps/provider.rb +3 -1
  16. data/lib/openhab/core/things/channel.rb +1 -1
  17. data/lib/openhab/core/things/profile_callback.rb +5 -0
  18. data/lib/openhab/core/things/thing.rb +6 -0
  19. data/lib/openhab/core/types/string_type.rb +1 -1
  20. data/lib/openhab/core/types/time_series.rb +131 -0
  21. data/lib/openhab/core.rb +14 -1
  22. data/lib/openhab/core_ext/java/instant.rb +14 -0
  23. data/lib/openhab/dsl/items/builder.rb +0 -1
  24. data/lib/openhab/dsl/items/timed_command.rb +1 -0
  25. data/lib/openhab/dsl/rules/builder.rb +140 -39
  26. data/lib/openhab/dsl/rules/name_inference.rb +16 -8
  27. data/lib/openhab/dsl/rules/property.rb +4 -1
  28. data/lib/openhab/dsl/rules/rule_triggers.rb +5 -3
  29. data/lib/openhab/dsl/rules/terse.rb +32 -15
  30. data/lib/openhab/dsl/rules/triggers/changed.rb +15 -7
  31. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +3 -2
  32. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +98 -100
  33. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +2 -2
  34. data/lib/openhab/dsl/sitemaps/builder.rb +1 -1
  35. data/lib/openhab/dsl/version.rb +1 -1
  36. data/lib/openhab/dsl.rb +30 -4
  37. data/lib/openhab/rspec/mocks/persistence_service.rb +10 -3
  38. data/lib/openhab/rspec/mocks/synchronous_executor.rb +2 -1
  39. data/lib/openhab/rspec/mocks/thing_handler.rb +4 -0
  40. metadata +6 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66607fb79de6b8b718ddd8cc5cc10662809001e67915074b44360ba9f6408d31
4
- data.tar.gz: e21976073a7323b4e8bfe05a09e8959472d950ad28df26534371c857fe64699f
3
+ metadata.gz: b1a3e61eb7613c8235e61cf99dc8d97d4c8403d8c2af21f873434ebf89ecff49
4
+ data.tar.gz: '09330d12003c08cd0af744d14c52287e9588c9b440975839e3cbd18d4914e027'
5
5
  SHA512:
6
- metadata.gz: 815f05142db0071b36cd45b9f9e81c1c033cf759823150066f2c65ec4f5eb61fdf9b1bf4dbc0e8125adf3535eb9e83b71fbbf401c2e6f1a716838fe6f37cdd29
7
- data.tar.gz: 4fe2bd7469d2d4489bd41ee9c62c6b4e7d53e6350e095c895b936813582d89e088b772bb0a44fde82aaecbfe5ff1c3fac320505001e24d0ac4519646abac5889
6
+ metadata.gz: 2ee2f0a8eb080aaed28d4484f23828d3e926f957a574e67ab5231ab44cf2dcb46897e841a9a481cdadfa30e1d026b4f4375755ee85f75954707208001d3442d5
7
+ data.tar.gz: 95ff1eb5298403900d9fb3370efaf9c3ffa7f356c40d5176a5d9ccf64fce3b6f701b8ddbb2f1103cb3102f3628cf9f27b0e9209cc44e97537eb0e832cc64061d
@@ -74,7 +74,7 @@ module OpenHAB
74
74
  ruby2_keywords def method_missing(method, *args)
75
75
  return super unless args.empty? && !block_given?
76
76
 
77
- logger.trace("method missing, performing openHAB Lookup for: #{method}")
77
+ logger.trace { "method missing, performing openHAB Lookup for: #{method}" }
78
78
  EntityLookup.lookup_entity(method,
79
79
  create_dummy_items: self.class.respond_to?(:create_dummy_items?) &&
80
80
  self.class.create_dummy_items?) || super
@@ -82,7 +82,7 @@ module OpenHAB
82
82
 
83
83
  # @!visibility private
84
84
  def respond_to_missing?(method, *)
85
- logger.trace("Checking if openHAB entities exist for #{method}")
85
+ logger.trace { "Checking if openHAB entities exist for #{method}" }
86
86
  EntityLookup.lookup_entity(method) || super
87
87
  end
88
88
 
@@ -121,14 +121,14 @@ module OpenHAB
121
121
  # @return [Things::Thing, nil]
122
122
  #
123
123
  def lookup_thing(uid)
124
- logger.trace("Looking up thing '#{uid}'")
124
+ logger.trace { "Looking up thing '#{uid}'" }
125
125
  uid = uid.to_s if uid.is_a?(Symbol)
126
126
 
127
127
  uid = Things::ThingUID.new(uid) unless uid.is_a?(Things::ThingUID)
128
128
  thing = $things.get(uid)
129
129
  return unless thing
130
130
 
131
- logger.trace("Retrieved Thing(#{thing}) from registry for uid: #{uid}")
131
+ logger.trace { "Retrieved Thing(#{thing}) from registry for uid: #{uid}" }
132
132
  Things::Proxy.new(thing)
133
133
  end
134
134
 
@@ -164,7 +164,7 @@ module OpenHAB
164
164
  # @return [Item, nil]
165
165
  #
166
166
  def lookup_item(name)
167
- logger.trace("Looking up item '#{name}'")
167
+ logger.trace { "Looking up item '#{name}'" }
168
168
  name = name.to_s if name.is_a?(Symbol)
169
169
  item = $ir.get(name)
170
170
  Items::Proxy.new(item) unless item.nil?
@@ -2,21 +2,21 @@
2
2
 
3
3
  require_relative "item_state_event"
4
4
 
5
+ # @deprecated OH3.4 guard only needed in OH 3.4
6
+ return unless OpenHAB::Core.version >= OpenHAB::Core::V4_0
7
+
5
8
  module OpenHAB
6
9
  module Core
7
10
  module Events
8
- # @deprecated OH3.4 if guard only needed in OH 3.4
9
- if Gem::Version.new(OpenHAB::Core::VERSION) >= Gem::Version.new("4.0.0")
10
- java_import org.openhab.core.items.events.ItemStateUpdatedEvent
11
+ java_import org.openhab.core.items.events.ItemStateUpdatedEvent
11
12
 
12
- #
13
- # {AbstractEvent} sent when an item's state has updated.
14
- #
15
- # @since openHAB 4.0
16
- #
17
- class ItemStateUpdatedEvent < ItemEvent
18
- include ItemState
19
- end
13
+ #
14
+ # {AbstractEvent} sent when an item's state has updated.
15
+ #
16
+ # @since openHAB 4.0
17
+ #
18
+ class ItemStateUpdatedEvent < ItemEvent
19
+ include ItemState
20
20
  end
21
21
  end
22
22
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @deprecated OH4.0 guard not needed in OH 4.1
4
+ return unless OpenHAB::Core.version >= OpenHAB::Core::V4_1
5
+
6
+ module OpenHAB
7
+ module Core
8
+ module Events
9
+ java_import org.openhab.core.items.events.ItemTimeSeriesUpdatedEvent
10
+
11
+ #
12
+ # {AbstractEvent} sent when an item received a time series update.
13
+ #
14
+ # @!attribute [r] time_series
15
+ # @return [TimeSeries] The updated time series.
16
+ #
17
+ # @since openHAB 4.1
18
+ # @see DSL::Rules::BuilderDSL#time_series_updated #time_series_updated rule trigger
19
+ #
20
+ class ItemTimeSeriesUpdatedEvent < ItemEvent; end
21
+ end
22
+ end
23
+ end
@@ -1,46 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # @deprecated OH3.4 this guard is not needed on OH4
4
+ return unless OpenHAB::Core.version >= OpenHAB::Core::V4_0
5
+
3
6
  module OpenHAB
4
7
  module Core
5
8
  module Events
6
- # @deprecated OH3.4 this guard is not needed on OH4
7
- if Gem::Version.new(OpenHAB::Core::VERSION) >= Gem::Version.new("4.0.0")
8
- java_import org.openhab.core.automation.events.TimerEvent
9
+ java_import org.openhab.core.automation.events.TimerEvent
9
10
 
11
+ #
12
+ # Adds methods to core openHAB TimerEvent to make it more natural in Ruby
13
+ #
14
+ # This event can be triggered by a `DateTimeTrigger`, `cron`, or `TimeOfDay` trigger.
15
+ #
16
+ # @since openHAB 4.0
17
+ #
18
+ class TimerEvent < AbstractEvent
10
19
  #
11
- # Adds methods to core openHAB TimerEvent to make it more natural in Ruby
20
+ # @!attribute [r] cron_expression
21
+ # @return [String, nil] The cron expression that triggered this event.
22
+ # `nil` when this event wasn't triggered by a cron trigger.
12
23
  #
13
- # This event can be triggered by a `DateTimeTrigger`, `cron`, or `TimeOfDay` trigger.
24
+ def cron_expression
25
+ payload&.[](:cronExpression)
26
+ end
27
+
14
28
  #
15
- # @since openHAB 4.0
29
+ # @!attribute [r] item
30
+ # @return [Item, nil] The DateTime item that triggered this event.
31
+ # `nil` when this event wasn't triggered by a DateTimeItem trigger.
16
32
  #
17
- class TimerEvent < AbstractEvent
18
- #
19
- # @!attribute [r] cron_expression
20
- # @return [String, nil] The cron expression that triggered this event.
21
- # `nil` when this event wasn't triggered by a cron trigger.
22
- #
23
- def cron_expression
24
- payload&.[](:cronExpression)
25
- end
26
-
27
- #
28
- # @!attribute [r] item
29
- # @return [Item, nil] The DateTime item that triggered this event.
30
- # `nil` when this event wasn't triggered by a DateTimeItem trigger.
31
- #
32
- def item
33
- payload&.[](:itemName)&.then { |item_name| EntityLookup.lookup_item(item_name) }
34
- end
33
+ def item
34
+ payload&.[](:itemName)&.then { |item_name| EntityLookup.lookup_item(item_name) }
35
+ end
35
36
 
36
- #
37
- # @!attribute [r] time
38
- # @return [LocalTime, nil] The configured time for this TimeOfDay trigger event.
39
- # `nil` when this event wasn't triggered by a TimeOfDay trigger.
40
- #
41
- def time
42
- payload&.[](:time)&.then { |time| LocalTime.parse(time) }
43
- end
37
+ #
38
+ # @!attribute [r] time
39
+ # @return [LocalTime, nil] The configured time for this TimeOfDay trigger event.
40
+ # `nil` when this event wasn't triggered by a TimeOfDay trigger.
41
+ #
42
+ def time
43
+ payload&.[](:time)&.then { |time| LocalTime.parse(time) }
44
44
  end
45
45
  end
46
46
  end
@@ -207,6 +207,18 @@ module OpenHAB
207
207
  type.to_s
208
208
  end
209
209
 
210
+ #
211
+ # @method time_series=(time_series)
212
+ # Set a new time series.
213
+ #
214
+ # This will trigger a {DSL::Rules::BuilderDSL#time_series_updated time_series_updated} event.
215
+ #
216
+ # @param [Core::Types::TimeSeries] time_series New time series to set.
217
+ # @return [void]
218
+ #
219
+ # @since openHAB 4.1
220
+ #
221
+
210
222
  #
211
223
  # Defers notifying openHAB of modifications to multiple attributes until the block is complete.
212
224
  #
@@ -7,7 +7,7 @@ module OpenHAB
7
7
  module Items
8
8
  # Mixin for implementing type coercion for number-like items
9
9
  module NumericItem
10
- %i[positive? negative? zero?].each do |predicate|
10
+ %i[positive? negative? zero? nonzero?].each do |predicate|
11
11
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
12
12
  def #{predicate} # def positive?
13
13
  return false unless state? # return false unless state?
@@ -83,9 +83,7 @@ module OpenHAB
83
83
  updated_since?])
84
84
 
85
85
  # @deprecated OH3.4 - in openHAB 4, just add :get_all_states_since and freeze the list above
86
- if Gem::Version.new(OpenHAB::Core::VERSION) >= Gem::Version.new("4.0.0")
87
- PERSISTENCE_METHODS << :get_all_states_since
88
- end
86
+ PERSISTENCE_METHODS << :get_all_states_since if OpenHAB::Core.version >= OpenHAB::Core::V4_0
89
87
  PERSISTENCE_METHODS.freeze
90
88
 
91
89
  private_constant :QUANTITY_METHODS, :PERSISTENCE_METHODS
@@ -386,7 +384,7 @@ module OpenHAB
386
384
  alias_method :state_changes_since, :count_state_changes_since
387
385
  alias_method :state_changes_between, :count_state_changes_between
388
386
  # @deprecated OH 3.4 - if guard is unnecessary in OH4
389
- if Gem::Version.new(OpenHAB::Core::VERSION) >= Gem::Version.new("4.0.0")
387
+ if OpenHAB::Core.version >= OpenHAB::Core::V4_0
390
388
  alias_method :all_states_since, :get_all_states_since
391
389
  alias_method :all_states_between, :get_all_states_between
392
390
  end
@@ -21,15 +21,13 @@ module OpenHAB
21
21
  # @return [Item] Item from registry, nil if item missing or requested item is a Group Type
22
22
  def [](name)
23
23
  EntityLookup.lookup_item(name)
24
- rescue org.openhab.core.items.ItemNotFoundException
25
- nil
26
24
  end
27
25
 
28
26
  # Returns true if the given item name exists
29
27
  # @param name [String] Item name to check
30
28
  # @return [true,false] true if the item exists, false otherwise
31
29
  def key?(name)
32
- !$ir.getItems(name).empty?
30
+ !$ir.get(name).nil?
33
31
  end
34
32
  alias_method :include?, :key?
35
33
  # @deprecated
@@ -97,8 +97,8 @@ module Enumerable
97
97
  # Send a command to every item in the collection
98
98
  # @return [self, nil] nil when `ensure` is in effect and all the items were already in the same state,
99
99
  # otherwise self
100
- def command(command)
101
- self if count { |i| i.command(command) }.positive?
100
+ def command(command, **kwargs)
101
+ self if count { |i| i.command(command, **kwargs) }.positive?
102
102
  end
103
103
 
104
104
  # Send a command to every item in the collection, even when {OpenHAB::DSL.ensure_states! ensure_states!} is in effect.
@@ -226,10 +226,7 @@ module OpenHAB
226
226
 
227
227
  # @deprecated OH3.4 - the Property tag had an ID of "MeasurementProperty" in OH3.4.
228
228
  # This was corrected in OH4.
229
- # Make sure we compare against pre-release versions
230
- if id == "Property" && Gem::Version.new(Core::VERSION) < Gem::Version.new("4.0.0.M1")
231
- id = "MeasurementProperty"
232
- end
229
+ id = "MeasurementProperty" if id == "Property" && Core.version < Core::V4_0
233
230
 
234
231
  # @deprecated OH3.4 missing registry
235
232
  if Provider.registry
@@ -15,7 +15,12 @@ module OpenHAB
15
15
  include Singleton
16
16
 
17
17
  class Profile
18
- include org.openhab.core.thing.profiles.StateProfile
18
+ # @deprecated OH 4.0 only include TimeSeriesProfile in OH 4.1, because it extends StateProfile
19
+ if OpenHAB::Core.version >= OpenHAB::Core::V4_1
20
+ include org.openhab.core.thing.profiles.TimeSeriesProfile
21
+ else
22
+ include org.openhab.core.thing.profiles.StateProfile
23
+ end
19
24
 
20
25
  def initialize(callback, context, uid, thread_locals, block)
21
26
  unless callback.class.ancestors.include?(Things::ProfileCallback)
@@ -64,6 +69,14 @@ module OpenHAB
64
69
  process_event(:state_from_item, state: state)
65
70
  end
66
71
 
72
+ # @deprecated OH 4.0 guard is only needed for < OH 4.1
73
+ if OpenHAB::Core.version >= OpenHAB::Core::V4_1
74
+ # @!visibility private
75
+ def onTimeSeriesFromHandler(time_series)
76
+ process_event(:time_series_from_handler, time_series: time_series)
77
+ end
78
+ end
79
+
67
80
  private
68
81
 
69
82
  def process_event(event, **params)
@@ -77,6 +90,8 @@ module OpenHAB
77
90
  params[:channel_uid] = @callback.link.linked_uid
78
91
  params[:state] ||= nil
79
92
  params[:command] ||= nil
93
+ # @deprecated OH 4.0 guard is only needed for < OH 4.1
94
+ params[:time_series] ||= nil if OpenHAB::Core.version >= OpenHAB::Core::V4_1
80
95
 
81
96
  kwargs = {}
82
97
  @block.parameters.each do |(param_type, name)|
@@ -63,7 +63,8 @@ module OpenHAB
63
63
  #
64
64
  def remove(rule_uid)
65
65
  rule_uid = rule_uid.uid if rule_uid.is_a?(Rule)
66
- provider = Provider.registry.provider_for(rule_uid)
66
+ return nil unless (provider = Provider.registry.provider_for(rule_uid))
67
+
67
68
  unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
68
69
  raise "Cannot remove rule #{rule_uid} from non-managed provider #{provider.inspect}"
69
70
  end
@@ -149,6 +149,7 @@ module OpenHAB
149
149
  r += " #{visibility}" unless visible?
150
150
  r += " #{status || "<detached>"}"
151
151
  r += " (#{status_info.status_detail})" if status_info && status_info.status_detail != RuleStatusDetail::NONE
152
+ r += " description=#{description.inspect}" if description
152
153
  r += " tags=#{tags.to_a.inspect}" unless tags.empty?
153
154
  r += " configuration=#{configuration.properties.to_h}" if configuration && !configuration.properties.empty?
154
155
  "#{r}>"
@@ -132,7 +132,9 @@ module OpenHAB
132
132
  builder = DSL::Sitemaps::Builder.new(self, builder_proxy, update: update)
133
133
  if block.arity == 1
134
134
  builder_proxy.__setobj__(builder)
135
- yield builder_proxy
135
+ DSL::ThreadLocal.thread_local(openhab_create_dummy_items: true) do
136
+ yield builder_proxy
137
+ end
136
138
  else
137
139
  builder.instance_eval(&block)
138
140
  end
@@ -58,7 +58,7 @@ module OpenHAB
58
58
  end
59
59
 
60
60
  # @deprecated OH3.4 this whole section is not needed in OH4+. Also see Thing#config_eql?
61
- if Gem::Version.new(Core::VERSION) < Gem::Version.new("4.0.0")
61
+ if Core.version < Core::V4_0
62
62
  # @!visibility private
63
63
  module ChannelComparable
64
64
  # @!visibility private
@@ -40,6 +40,11 @@ module OpenHAB
40
40
  state = link.item.format_update(state)
41
41
  super(state)
42
42
  end
43
+
44
+ # @!method send_time_series(time_series)
45
+ # Send a time series to the framework.
46
+ # @param [TimeSeries] time_series
47
+ # @since openHAB 4.1
43
48
  end
44
49
  end
45
50
  end
@@ -165,6 +165,12 @@ module OpenHAB
165
165
  ThingType.registry.get_thing_type(thing_type_uid)
166
166
  end
167
167
 
168
+ # @!attribute [r] bridge
169
+ # @return [Thing, nil]
170
+ def bridge
171
+ bridge_uid && EntityLookup.lookup_thing(bridge_uid)
172
+ end
173
+
168
174
  # @return [String]
169
175
  def inspect
170
176
  r = "#<OpenHAB::Core::Things::Thing #{uid}"
@@ -26,7 +26,7 @@ module OpenHAB
26
26
  def eql?(other)
27
27
  return false unless other.instance_of?(self.class)
28
28
 
29
- to_s.compare_to(other.to_s).zero?
29
+ to_s == other.to_s
30
30
  end
31
31
 
32
32
  #
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @deprecated OH4.0 this guard is not needed on OH4.1
4
+ return unless OpenHAB::Core.version >= OpenHAB::Core::V4_1
5
+
6
+ require "forwardable"
7
+
8
+ module OpenHAB
9
+ module Core
10
+ module Types
11
+ TimeSeries = org.openhab.core.types.TimeSeries
12
+
13
+ #
14
+ # {TimeSeries} is used to transport a set of states together with their timestamp.
15
+ #
16
+ # The states are sorted chronologically. The entries can be accessed like an array.
17
+ #
18
+ # @since openHAB 4.1
19
+ #
20
+ # @example
21
+ # time_series = TimeSeries.new # defaults to :add policy
22
+ # .add(Time.at(2), DecimalType.new(2))
23
+ # .add(Time.at(1), DecimalType.new(1))
24
+ # .add(Time.at(3), DecimalType.new(3))
25
+ # logger.info "first entry: #{time_series.first.state}" # => 1
26
+ # logger.info "last entry: #{time_series.last.state}" # => 3
27
+ # logger.info "second entry: #{time_series[1].state}" # => 2
28
+ # logger.info "sum: #{time_series.sum(&:state)}" # => 6
29
+ #
30
+ # @see DSL::Rules::BuilderDSL#time_series_updated #time_series_updated rule trigger
31
+ #
32
+ class TimeSeries
33
+ extend Forwardable
34
+
35
+ # @!attribute [r] policy
36
+ # Returns the persistence policy of this series.
37
+ # @see org.openhab.core.types.TimeSeries#getPolicy()
38
+ # @return [org.openhab.core.types.TimeSeries.Policy]
39
+
40
+ # @!attribute [r] begin
41
+ # Returns the timestamp of the first element in this series.
42
+ # @return [Instant]
43
+
44
+ # @!attribute [r] end
45
+ # Returns the timestamp of the last element in this series.
46
+ # @return [Instant]
47
+
48
+ # @!attribute [r] size
49
+ # Returns the number of elements in this series.
50
+ # @return [Integer]
51
+
52
+ #
53
+ # Create a new instance of TimeSeries
54
+ #
55
+ # @param [:add, :replace, org.openhab.core.types.TimeSeries.Policy] policy
56
+ # The persistence policy of this series.
57
+ #
58
+ def initialize(policy = :replace)
59
+ policy = Policy.value_of(policy.to_s.upcase) if policy.is_a?(Symbol)
60
+ super
61
+ end
62
+
63
+ # Returns true if the series' policy is `ADD`.
64
+ # @return [true,false]
65
+ def add?
66
+ policy == Policy::ADD
67
+ end
68
+
69
+ # Returns true if the series' policy is `REPLACE`.
70
+ # @return [true,false]
71
+ def replace?
72
+ policy == Policy::REPLACE
73
+ end
74
+
75
+ # @!visibility private
76
+ def inspect
77
+ "#<OpenHAB::Core::Types::TimeSeries " \
78
+ "policy=#{policy} " \
79
+ "begin=#{self.begin} " \
80
+ "end=#{self.end} " \
81
+ "size=#{size}>"
82
+ end
83
+
84
+ #
85
+ # Returns the content of this series.
86
+ # @return [Array<org.openhab.core.types.TimeSeries.Entry>]
87
+ #
88
+ def states
89
+ get_states.to_array.to_a.freeze
90
+ end
91
+
92
+ # rename raw methods so we can overwrite them
93
+ # @!visibility private
94
+ alias_method :add_instant, :add
95
+
96
+ #
97
+ # Adds a new element to this series.
98
+ #
99
+ # Elements can be added in an arbitrary order and are sorted chronologically.
100
+ #
101
+ # @note This method returns self so it can be chained, unlike the Java version.
102
+ #
103
+ # @param [Instant, #to_zoned_date_time, #to_instant] instant An instant for the given state.
104
+ # @param [State, String, Numeric] state The State at the given timestamp.
105
+ # If a String is given, it will be converted to {StringType}.
106
+ # If a {Numeric} is given, it will be converted to {DecimalType}.
107
+ # @return [self]
108
+ # @raise [ArgumentError] if state is not a {State}, String or {Numeric}
109
+ #
110
+ def add(instant, state)
111
+ instant = instant.to_zoned_date_time if instant.respond_to?(:to_zoned_date_time)
112
+ instant = instant.to_instant if instant.respond_to?(:to_instant)
113
+ state = case state
114
+ when State then state
115
+ when String then StringType.new(state)
116
+ when Numeric then DecimalType.new(state)
117
+ else
118
+ raise ArgumentError, "state must be a State, String or Numeric, but was #{state.class}"
119
+ end
120
+ add_instant(instant, state)
121
+ self
122
+ end
123
+
124
+ # any method that exists on Array gets forwarded to states
125
+ delegate (Array.instance_methods - instance_methods) => :states
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ TimeSeries = OpenHAB::Core::Types::TimeSeries unless Object.const_defined?(:TimeSeries)
data/lib/openhab/core.rb CHANGED
@@ -7,7 +7,20 @@ module OpenHAB
7
7
  # @return [String]
8
8
  VERSION = org.openhab.core.OpenHAB.version.freeze
9
9
 
10
- raise "`openhab-scripting` requires openHAB >= 3.4.0" unless Gem::Version.new(VERSION) >= Gem::Version.new("3.4.0")
10
+ # @!visibility private
11
+ V4_0 = Gem::Version.new("4.0.0").freeze
12
+ # @!visibility private
13
+ V4_1 = Gem::Version.new("4.1.0").freeze
14
+ # @!visibility private
15
+ V4_2 = Gem::Version.new("4.2.0").freeze
16
+
17
+ # @return [Gem::Version] Returns the current openHAB version as a Gem::Version object
18
+ # @!visibility private
19
+ def self.version
20
+ @version ||= Gem::Version.new(VERSION).freeze
21
+ end
22
+
23
+ raise "`openhab-scripting` requires openHAB >= 3.4.0" unless version >= Gem::Version.new("3.4.0")
11
24
 
12
25
  # @return [Integer] Number of seconds to wait between checks for automation manager
13
26
  CHECK_DELAY = 10
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module CoreExt
5
+ module Java
6
+ java_import java.time.Instant
7
+
8
+ # Extensions to {java.time.Instant}
9
+ class Instant < java.lang.Object; end
10
+ end
11
+ end
12
+ end
13
+
14
+ Instant = OpenHAB::CoreExt::Java::Instant unless Object.const_defined?(:Instant)
@@ -672,7 +672,6 @@ module OpenHAB
672
672
  # @param (see ItemBuilder#initialize)
673
673
  def initialize(*args, type: nil, function: nil, thing: nil, **kwargs)
674
674
  raise ArgumentError, "invalid function #{function}" if function && !function.match?(FUNCTION_REGEX)
675
- raise ArgumentError, "state cannot be set on GroupItems" if kwargs[:state]
676
675
 
677
676
  super(type, *args, **kwargs)
678
677
  @function = function
@@ -74,6 +74,7 @@ module OpenHAB
74
74
  end
75
75
 
76
76
  Core::Items::GenericItem.prepend(self)
77
+ Core::Items::GroupItem.prepend(self)
77
78
 
78
79
  #
79
80
  # Sends command to an item for specified duration, then on timer expiration sends