openhab-jrubyscripting 5.0.0.rc12 → 5.0.0.rc.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38b109c413cbb8b06c2c43e57ad12e6278139b1cf56d887289ada297c8991272
4
- data.tar.gz: 24e6235ce553d4e2ec9447c94407054b9c1c075c6a190a3ce5ed5ebb2421753c
3
+ metadata.gz: 8ef6b7b058463c4bbcbc500b23baf44e1ef8f3872c39f88f3819875523b9d350
4
+ data.tar.gz: f2c5511e9c61eb79a1464161d35ba239570e45172be44415784994688a789c67
5
5
  SHA512:
6
- metadata.gz: a3e0f9f2f58d906ff72f5923817a2b5651a96f4710bbd29ad6ecffc3991722db5f8b1702de90e05ef1854ffe49c3d4214a699f271aaf0ac26a2dd03ad20c71f5
7
- data.tar.gz: a51e80b23b82e7c38db1ac8d263b0a2776a70ad7fe9f401b1ae035f26a06d3660534e4c64c4d1c88a06fb9ee47ef64422b02bdfd26446e8edb148461c1403eca
6
+ metadata.gz: 7b4aca0a4cb592c7bcb84c4c50772d359cfc704f4bccb31e3aa58a1e76be0e15de9276c0184bcd696aed1c05cb7361e0f4d74f892eb9347f0a43f54ac596ecdb
7
+ data.tar.gz: 1053d9bd85f8013bcfa37242f1354e333587884179f8600c4f48ad19c5d1aa8cfdbb7795a32b37147d83d1b11334cb993663e761c4a0c2cf9574b5ec1355586e
@@ -7,7 +7,7 @@ module OpenHAB
7
7
  class Transformation
8
8
  class << self
9
9
  # @!visibility private
10
- alias_method :transform_raw, :transform if instance_methods.include?(:say)
10
+ alias_method :raw_transform, :transform if instance_methods.include?(:transform)
11
11
 
12
12
  #
13
13
  # Applies a transformation of a given type with some function to a value.
@@ -20,10 +20,10 @@ module OpenHAB
20
20
  # @return [String] the transformed value, or the original value if an error occurred
21
21
  #
22
22
  # @example Run a transformation
23
- # Transformation.transform(:map, "myfan.map", 0)
23
+ # transform(:map, "myfan.map", 0)
24
24
  #
25
25
  def transform(type, function, value)
26
- transform_raw(type.to_s.upcase, function.to_s, value.to_s)
26
+ raw_transform(type.to_s.upcase, function.to_s, value.to_s)
27
27
  end
28
28
  end
29
29
  end
@@ -43,7 +43,7 @@ module OpenHAB
43
43
  end
44
44
 
45
45
  # Import common actions
46
- %w[Exec HTTP Ping].each do |action|
46
+ %w[Exec HTTP Ping Transformation].each do |action|
47
47
  klass = (java_import "org.openhab.core.model.script.actions.#{action}").first
48
48
  Object.const_set(action, klass)
49
49
  end
@@ -7,71 +7,71 @@ module OpenHAB
7
7
  module Events
8
8
  java_import org.openhab.core.items.events.ItemCommandEvent
9
9
 
10
- # Adds methods to core openHAB ItemCommandEvent to make it more natural in Ruby
10
+ # {AbstractEvent} sent when an item receives a command.
11
11
  class ItemCommandEvent < ItemEvent
12
12
  # @!attribute [r] command
13
13
  # @return [Command] The command sent to the item.
14
14
  alias_method :command, :item_command
15
15
 
16
16
  # @!method refresh?
17
- # Check if `self == REFRESH`
18
- # @return [true,false]
17
+ # Check if {#command} is {REFRESH}
18
+ # @return [true, false]
19
19
 
20
20
  # @!method on?
21
- # Check if `self == ON`
22
- # @return [true,false]
21
+ # Check if {#command} is (implicitly convertible to) {ON}
22
+ # @return [true, false]
23
23
 
24
24
  # @!method off?
25
- # Check if `self == OFF`
26
- # @return [true,false]
25
+ # Check if {#command} is (implicitly convertible to) {OFF}
26
+ # @return [true, false]
27
27
 
28
28
  # @!method up?
29
- # Check if `self == UP`
30
- # @return [true,false]
29
+ # Check if {#command} is (implicitly convertible to) {UP}
30
+ # @return [true, false]
31
31
 
32
32
  # @!method down?
33
- # Check if `self == DOWN`
34
- # @return [true,false]
33
+ # Check if {#command} is (implicitly convertible to) {DOWN}
34
+ # @return [true, false]
35
35
 
36
36
  # @!method stop?
37
- # Check if `self == STOP`
38
- # @return [true,false]
37
+ # Check if {#command} is {STOP}
38
+ # @return [true, false]
39
39
 
40
40
  # @!method move?
41
- # Check if `self == MOVE`
42
- # @return [true,false]
41
+ # Check if {#command} is {MOVE}
42
+ # @return [true, false]
43
43
 
44
44
  # @!method increase?
45
- # Check if `self == INCREASE`
46
- # @return [true,false]
45
+ # Check if {#command} is {INCREASE}
46
+ # @return [true, false]
47
47
 
48
48
  # @!method decrease?
49
- # Check if `self == DECREASE`
50
- # @return [true,false]
49
+ # Check if {#command} is {DECREASE}
50
+ # @return [true, false]
51
51
 
52
52
  # @!method play?
53
- # Check if `self == PLAY`
54
- # @return [true,false]
53
+ # Check if {#command} is {PLAY}
54
+ # @return [true, false]
55
55
 
56
56
  # @!method pause?
57
- # Check if `self == PAUSE`
58
- # @return [true,false]
57
+ # Check if {#command} is {PAUSE}
58
+ # @return [true, false]
59
59
 
60
60
  # @!method rewind?
61
- # Check if `self == REWIND`
62
- # @return [true,false]
61
+ # Check if {#command} is {REWIND}
62
+ # @return [true, false]
63
63
 
64
64
  # @!method fast_forward?
65
- # Check if `self == FASTFORWARD`
66
- # @return [true,false]
65
+ # Check if {#command} is {FASTFORWARD}
66
+ # @return [true, false]
67
67
 
68
68
  # @!method next?
69
- # Check if `self == NEXT`
70
- # @return [true,false]
69
+ # Check if {#command} is {NEXT}
70
+ # @return [true, false]
71
71
 
72
72
  # @!method previous?
73
- # Check if `self == PREVIOUS`
74
- # @return [true,false]
73
+ # Check if {#command} is {PREVIOUS}
74
+ # @return [true, false]
75
75
  end
76
76
  end
77
77
  end
@@ -8,32 +8,55 @@ module OpenHAB
8
8
  java_import org.openhab.core.items.events.ItemStateChangedEvent
9
9
 
10
10
  #
11
- # Adds methods to core openHAB ItemStateChangedEvent to make it more natural in Ruby
11
+ # {AbstractEvent} sent when an item's state has changed.
12
12
  #
13
13
  class ItemStateChangedEvent < ItemEvent
14
14
  include ItemState
15
15
 
16
- #
17
- # Check if state was == {UNDEF}
18
- #
19
- # @return [true,false] True if the state is {UNDEF}, false otherwise
20
- #
21
- def was_undef?
22
- old_item_state == UNDEF
23
- end
16
+ # @!method was_undef?
17
+ # Check if {#was} is {UNDEF}
18
+ # @return [true, false]
24
19
 
25
- #
26
- # Check if state was == {NULL}
27
- #
28
- # @return [true,false] True if the state is {NULL}, false otherwise
29
- def was_null?
30
- old_item_state == NULL
31
- end
20
+ # @!method was_null?
21
+ # Check if {#was} is {NULL}
22
+ # @return [true, false]
23
+
24
+ # @!method was_on?
25
+ # Check if {#was} is (implicitly convertible to) {ON}
26
+ # @return [true, false]
27
+
28
+ # @!method was_off?
29
+ # Check if {#was} is (implicitly convertible to) {OFF}
30
+ # @return [true, false]
31
+
32
+ # @!method was_up?
33
+ # Check if {#was} is (implicitly convertible to) {UP}
34
+ # @return [true, false]
35
+
36
+ # @!method was_down?
37
+ # Check if {#was} is (implicitly convertible to) {DOWN}
38
+ # @return [true, false]
39
+
40
+ # @!method was_open?
41
+ # Check if {#was} is (implicitly convertible to) {OPEN}
42
+ # @return [true, false]
43
+
44
+ # @!method was_closed?
45
+ # Check if {#was} is (implicitly convertible to) {CLOSED}
46
+ # @return [true, false]
47
+
48
+ # @!method was_playing?
49
+ # Check if {#was} is {PLAY}
50
+ # @return [true, false]
51
+
52
+ # @!method was_paused?
53
+ # Check if {#was} is {PAUSE}
54
+ # @return [true, false]
32
55
 
33
56
  #
34
57
  # Check if state was defined (not {UNDEF} or {NULL})
35
58
  #
36
- # @return [true,false] True if state is not {UNDEF} or {NULL}
59
+ # @return [true,false]
37
60
  #
38
61
  def was?
39
62
  !old_item_state.is_a?(UnDefType)
@@ -41,7 +64,7 @@ module OpenHAB
41
64
 
42
65
  #
43
66
  # @!attribute [r] was
44
- # @return [State, nil] The state of the item if it was not {UNDEF} or {NULL}, `nil` otherwise.
67
+ # @return [State, nil] the prior state of the item if it was not {UNDEF} or {NULL}, `nil` otherwise.
45
68
  #
46
69
  def was
47
70
  old_item_state if was?
@@ -5,29 +5,57 @@ module OpenHAB
5
5
  module Events
6
6
  java_import org.openhab.core.items.events.ItemStateEvent
7
7
 
8
+ #
8
9
  # Helpers common to {ItemStateEvent} and {ItemStateChangedEvent}.
10
+ #
11
+ # Methods that refer to implicit conversion mean that for example
12
+ # a PercentType of 100% will be `true` for {#on?}, etc.
13
+ #
9
14
  module ItemState
10
- #
11
- # Check if the state == {UNDEF}
12
- #
13
- # @return [true,false] True if the state is {UNDEF}, false otherwise
14
- #
15
- def undef?
16
- item_state == UNDEF
17
- end
15
+ # @!method undef?
16
+ # Check if {#state} is {UNDEF}
17
+ # @return [true, false]
18
18
 
19
- #
20
- # Check if the state == {NULL}
21
- #
22
- # @return [true,false] True if the state is {NULL}, false otherwise
23
- def null?
24
- item_state == NULL
25
- end
19
+ # @!method null?
20
+ # Check if {#state} is {NULL}
21
+ # @return [true, false]
22
+
23
+ # @!method on?
24
+ # Check if {#state} is (implicitly convertible to) {ON}
25
+ # @return [true, false]
26
+
27
+ # @!method off?
28
+ # Check if {#state} is (implicitly convertible to) {OFF}
29
+ # @return [true, false]
30
+
31
+ # @!method up?
32
+ # Check if {#state} is (implicitly convertible to) {UP}
33
+ # @return [true, false]
34
+
35
+ # @!method down?
36
+ # Check if {#state} is (implicitly convertible to) {DOWN}
37
+ # @return [true, false]
38
+
39
+ # @!method open?
40
+ # Check if {#state} is (implicitly convertible to) {OPEN}
41
+ # @return [true, false]
42
+
43
+ # @!method closed?
44
+ # Check if {#state} is (implicitly convertible to) {CLOSED}
45
+ # @return [true, false]
46
+
47
+ # @!method playing?
48
+ # Check if {#state} is {PLAY}
49
+ # @return [true, false]
50
+
51
+ # @!method paused?
52
+ # Check if {#state} is {PAUSE}
53
+ # @return [true, false]
26
54
 
27
55
  #
28
- # Check if the state is defined (not {UNDEF} or {NULL})
56
+ # Check if {#state} is defined (not {UNDEF} or {NULL})
29
57
  #
30
- # @return [true,false] True if state is not {UNDEF} or {NULL}
58
+ # @return [true, false]
31
59
  #
32
60
  def state?
33
61
  !item_state.is_a?(UnDefType)
@@ -35,7 +63,7 @@ module OpenHAB
35
63
 
36
64
  #
37
65
  # @!attribute [r] state
38
- # @return [State, nil] The state of the item if it is not {UNDEF} or {NULL}, `nil` otherwise.
66
+ # @return [State, nil] the state of the item if it is not {UNDEF} or {NULL}, `nil` otherwise.
39
67
  #
40
68
  def state
41
69
  item_state if state?
@@ -71,6 +71,34 @@ module OpenHAB
71
71
  !raw_state.is_a?(Types::UnDefType)
72
72
  end
73
73
 
74
+ # @!attribute [r] formatted_state
75
+ #
76
+ # Format the item's state according to its state description
77
+ #
78
+ # This may include running a transformation.
79
+ #
80
+ # @return [String]
81
+ #
82
+ # @example
83
+ # logger.info(Exterior_WindDirection.formatted_state) # => "NE (36°)"
84
+ #
85
+ def formatted_state
86
+ # use to_string, not to_s, to get the original openHAB toString(), instead of any overrides
87
+ # the JRuby library has defined
88
+ raw_state_string = raw_state.to_string
89
+
90
+ return raw_state_string unless (pattern = state_description&.pattern)
91
+
92
+ transformed_state_string = org.openhab.core.transform.TransformationHelper.transform(OSGi.bundle_context,
93
+ pattern,
94
+ raw_state_string)
95
+ return state.format(pattern) if transformed_state_string.nil? || transformed_state_string == raw_state_string
96
+
97
+ transformed_state_string
98
+ rescue org.openhab.core.transform.TransformationException
99
+ raw_state_string
100
+ end
101
+
74
102
  #
75
103
  # @!attribute [r] state
76
104
  # @return [State, nil]
@@ -153,7 +181,7 @@ module OpenHAB
153
181
  return state if state.is_a?(Types::State)
154
182
 
155
183
  state = state.to_s
156
- org.openhab.core.types.TypeParser.parse_state(getAcceptedDataTypes, state) || state
184
+ org.openhab.core.types.TypeParser.parse_state(getAcceptedDataTypes, state) || StringType.new(state)
157
185
  end
158
186
 
159
187
  # formats a {Types::Type} to send to the event bus
@@ -44,12 +44,49 @@ module OpenHAB
44
44
  #
45
45
  # Returns all groups that this item is part of
46
46
  #
47
- # @return [Array<Group>] All groups that this item is part of
47
+ # @return [Array<GroupItem>] All groups that this item is part of
48
48
  #
49
49
  def groups
50
50
  group_names.map { |name| EntityLookup.lookup_item(name) }.compact
51
51
  end
52
52
 
53
+ #
54
+ # Checks if this item is a member of at least one of the given groups.
55
+ #
56
+ # @param groups [String, GroupItem] the group to check membership in
57
+ # @return [true, false]
58
+ #
59
+ # @example
60
+ # event.item.member_of?(gFullOn)
61
+ #
62
+ def member_of?(*groups)
63
+ groups = groups.map! do |group|
64
+ group.is_a?(GroupItem) ? group.name : group
65
+ end
66
+ !(group_names & groups).empty?
67
+ end
68
+
69
+ #
70
+ # @!attribute [r] all_groups
71
+ #
72
+ # Returns all groups that this item is a part of, as well as those groups' groups, recursively
73
+ #
74
+ # @return [Array<GroupItem>]
75
+ #
76
+ def all_groups
77
+ result = []
78
+ new_groups = Set.new(groups)
79
+
80
+ until new_groups.empty?
81
+ result.concat(new_groups.to_a)
82
+ new_groups.replace(new_groups.flat_map(&:groups))
83
+ # remove any groups we already have in the result to avoid loops
84
+ new_groups.subtract(result)
85
+ end
86
+
87
+ result
88
+ end
89
+
53
90
  # rubocop:disable Layout/LineLength
54
91
 
55
92
  # @!attribute [r] metadata
@@ -172,6 +209,22 @@ module OpenHAB
172
209
  end
173
210
  # rubocop:enable Layout/LineLength
174
211
 
212
+ #
213
+ # Checks if this item has at least one of the given tags.
214
+ #
215
+ # @param tags [String, Module] the tag(s) to check
216
+ # @return [true, false]
217
+ #
218
+ # @example
219
+ # event.item.tagged?("Setpoint")
220
+ #
221
+ def tagged?(*tags)
222
+ tags = tags.map! do |tag|
223
+ tag.is_a?(Module) ? tag.simple_name : tag
224
+ end
225
+ !(self.tags.to_a & tags).empty?
226
+ end
227
+
175
228
  # Return the item's thing if this item is linked with a thing. If an item is linked to more than one thing,
176
229
  # this method only returns the first thing.
177
230
  #
@@ -34,6 +34,10 @@ module OpenHAB
34
34
  item.members.each { |member| remove(member.name, true) } if recursive && item.is_a?(GroupItem)
35
35
  item
36
36
  end
37
+
38
+ def initialize
39
+ super(unload_priority: 50)
40
+ end
37
41
  end
38
42
  end
39
43
  end
@@ -40,33 +40,56 @@ module Enumerable
40
40
  #
41
41
 
42
42
  # Returns a new array of items that have at least one of the given tags
43
+ #
44
+ # @param tags [String, Module]
43
45
  # @return [Array<Item>]
44
46
  def tagged(*tags)
45
- reject { |i| (tags & i.tags.to_a).empty? }
47
+ select { |i| i.tagged?(*tags) }
46
48
  end
47
49
 
48
50
  # Returns a new array of items that do not have any of the given tags
51
+ # @param tags [String, Module]
49
52
  # @return [Array<Item>]
50
53
  def not_tagged(*tags)
51
- select { |i| (tags & i.tags.to_a).empty? }
54
+ reject { |i| i.tagged?(*tags) }
52
55
  end
53
56
 
54
57
  # Returns a new array of items that are a member of at least one of the given groups
58
+ # @param groups [String, GroupItem]
55
59
  # @return [Array<Item>]
56
60
  def member_of(*groups)
57
- reject { |i| (groups.map(&:name) & i.group_names).empty? }
61
+ select { |i| i.member_of?(*groups) }
58
62
  end
59
63
 
60
64
  # Returns a new array of items that are not a member of any of the given groups
65
+ # @param groups [String, GroupItem]
61
66
  # @return [Array<Item>]
62
67
  def not_member_of(*groups)
63
- select { |i| (groups.map(&:name) & i.group_names).empty? }
68
+ reject { |i| i.member_of?(*groups) }
64
69
  end
65
70
 
66
- # Returns the group members the elements
71
+ # Returns the group members of all group elements
67
72
  # @return [Array<Item>]
68
73
  def members
69
- grep(OpenHAB::Core::Items::GroupItem).flat_map(&:members)
74
+ grep(OpenHAB::Core::Items::GroupItem).flat_map(&:members).uniq
75
+ end
76
+
77
+ # Returns all non-group members of all group elements, recursively
78
+ # @return [Array<Item>]
79
+ def all_members
80
+ grep(OpenHAB::Core::Items::GroupItem).flat_map(&:all_members).uniq
81
+ end
82
+
83
+ # Returns the groups of all elements
84
+ # @return [Array<GroupItem>]
85
+ def groups
86
+ flat_map(&:groups).uniq
87
+ end
88
+
89
+ # Returns all groups all elements are a part of, recursively
90
+ # @return [Array<GroupItem>]
91
+ def all_groups
92
+ flat_map(&:all_groups).uniq
70
93
  end
71
94
 
72
95
  # @!group Items State and Command Methods
@@ -46,9 +46,9 @@ module OpenHAB
46
46
 
47
47
  logger.trace("Defining #{klass}##{state_predicate} for #{state}")
48
48
  klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
49
- def #{state_predicate} # def on?
50
- raw_state == #{state} # raw_state == ON
51
- end # end
49
+ def #{state_predicate} # def on?
50
+ raw_state.as(#{state.class.java_class.simple_name}).equal?(#{state}) # raw_state.as(OnOffType) == ON
51
+ end # end
52
52
  RUBY
53
53
  end
54
54
  end
@@ -74,18 +74,11 @@ module OpenHAB
74
74
  end # end
75
75
  RUBY
76
76
 
77
- # Override the inherited methods from Enumerable and send it to the base_item
78
- GroupItem.class_eval <<~RUBY, __FILE__, __LINE__ + 1
79
- def #{command} # def on
80
- method_missing(:#{command}) # method_missing(:on)
81
- end # end
82
- RUBY
83
-
84
77
  logger.trace("Defining ItemCommandEvent##{command}? for #{value}")
85
78
  Events::ItemCommandEvent.class_eval <<~RUBY, __FILE__, __LINE__ + 1
86
- def #{command}? # def refresh?
87
- command == #{value} # command == REFRESH
88
- end # end
79
+ def #{command}? # def refresh?
80
+ command.as(#{value.class.java_class.simple_name}).equal?(#{value}) # command.as(RefreshType).equal?(REFRESH)
81
+ end # end
89
82
  RUBY
90
83
  end
91
84
  end
@@ -219,11 +219,11 @@ module OpenHAB
219
219
 
220
220
  private
221
221
 
222
- def initialize(script_unloaded_before: nil)
222
+ def initialize(unload_priority: nil)
223
223
  super()
224
224
  @elements = java.util.concurrent.ConcurrentHashMap.new
225
225
  self.class.registry.add_provider(self)
226
- ScriptHandling.script_unloaded(before: script_unloaded_before) { unregister }
226
+ ScriptHandling.script_unloaded(priority: unload_priority) { unregister }
227
227
  end
228
228
  end
229
229
  end
@@ -19,21 +19,6 @@ module OpenHAB
19
19
  $rules
20
20
  end
21
21
  end
22
-
23
- def initialize
24
- super(script_unloaded_before: lambda do |callbacks|
25
- callbacks.index do |cb|
26
- case cb.binding.receiver
27
- when Items::Provider,
28
- Things::Provider,
29
- DSL::TimerManager
30
- true
31
- else
32
- false
33
- end
34
- end
35
- end)
36
- end
37
22
  end
38
23
  end
39
24
  end
@@ -32,8 +32,6 @@ module OpenHAB
32
32
  ScriptHandlingCallbacks.script_loaded_hooks << block
33
33
  end
34
34
 
35
- #
36
- # @!method script_unloaded(&block)
37
35
  #
38
36
  # Add a block of code to be executed when the script is unloaded.
39
37
  #
@@ -42,6 +40,10 @@ module OpenHAB
42
40
  # Multiple hooks can be added by calling {#script_unloaded} multiple times.
43
41
  # They can be used to perform final cleanup.
44
42
  #
43
+ # @param [Integer, nil] priority The order at which the the given hook will be executed.
44
+ # The higher the number, the lower the priority. Higher priority hooks will be executed
45
+ # first, before the lower priority hooks. When nil, the default priority of zero will
46
+ # be used.
45
47
  # @return [void]
46
48
  #
47
49
  # @example
@@ -49,10 +51,8 @@ module OpenHAB
49
51
  # logger.info 'Hi, this script has been unloaded'
50
52
  # end
51
53
  #
52
- def script_unloaded(before: nil, &block)
53
- # `before` is as yet undocumented, because I'm not set on its interface
54
- index = before.call(ScriptHandlingCallbacks.script_unloaded_hooks) if before
55
- ScriptHandlingCallbacks.script_unloaded_hooks.insert(index || -1, block)
54
+ def script_unloaded(priority: nil, &block)
55
+ ScriptHandlingCallbacks.script_unloaded_hooks[priority || 0] << block
56
56
  end
57
57
  end
58
58
 
@@ -82,7 +82,7 @@ module OpenHAB
82
82
  #
83
83
  # @!visibility private
84
84
  def script_unloaded_hooks
85
- @script_unloaded_hooks ||= []
85
+ @script_unloaded_hooks ||= Hash.new { |hash, key| hash[key] = [] }
86
86
  end
87
87
  end
88
88
  self.script_loaded = false
@@ -92,7 +92,7 @@ module OpenHAB
92
92
  #
93
93
  def scriptUnloaded # rubocop:disable Naming/MethodName method name dictated by openHAB
94
94
  logger.trace("Script unloaded")
95
- ScriptHandlingCallbacks.script_unloaded_hooks.each do |hook|
95
+ ScriptHandlingCallbacks.script_unloaded_hooks.sort_by(&:first).flat_map(&:last).each do |hook|
96
96
  hook.call
97
97
  rescue => e
98
98
  logger.error("Failed to call script_unloaded hook #{hook}: #{e}")
@@ -19,6 +19,10 @@ module OpenHAB
19
19
  $things
20
20
  end
21
21
  end
22
+
23
+ def initialize
24
+ super(unload_priority: 60)
25
+ end
22
26
  end
23
27
  end
24
28
  end
@@ -110,20 +110,15 @@ module OpenHAB
110
110
  #
111
111
  # Coerce object to a {DecimalType DecimalType}
112
112
  #
113
- # @param [Numeric, Type] other object to coerce to a {DecimalType DecimalType}
114
- #
115
- # If `other` is a {Type}, `self` will instead be coerced
116
- # to that type to accomodate comparison with things such as {OnOffType}.
113
+ # @param [Numeric] other object to coerce to a {DecimalType DecimalType}
117
114
  #
118
115
  # @return [Array<(DecimalType, DecimalType)>, nil]
119
116
  #
120
117
  def coerce(other)
121
118
  logger.trace("Coercing #{self} as a request from #{other.class}")
122
- if other.is_a?(Type)
123
- [other, as(other.class)]
124
- elsif other.respond_to?(:to_d)
125
- [self.class.new(other.to_d), self]
126
- end
119
+ return unless other.respond_to?(:to_d)
120
+
121
+ [self.class.new(other.to_d), self]
127
122
  end
128
123
 
129
124
  #
@@ -142,7 +142,7 @@ module OpenHAB
142
142
  end
143
143
 
144
144
  # Convert to an HTML-style string of 6 hex characters in the default sRGB color model.
145
- # @return [String] +'#xxxxxx'+
145
+ # @return [String] `"#xxxxxx"`
146
146
  def to_hex
147
147
  Kernel.format("#%06x", rgb)
148
148
  end
@@ -174,7 +174,7 @@ module OpenHAB
174
174
 
175
175
  # @!method to_xy
176
176
  # Convert to the xyY values representing this object's color in CIE XY color model
177
- # @return [[PercentType, PercentType]]
177
+ # @return [[PercentType, PercentType, PercentType]]
178
178
  end
179
179
  end
180
180
  end
@@ -157,19 +157,14 @@ module OpenHAB
157
157
  #
158
158
  # Coerce object to a {QuantityType}
159
159
  #
160
- # @param [Numeric, Type] other object to coerce to a {QuantityType}
161
- #
162
- # if `other` is a {Type}, `self` will instead be coerced
163
- # to that type to accomodate comparison with things such as {OnOffType}
160
+ # @param [Numeric] other object to coerce to a {QuantityType}
164
161
  #
165
162
  # @return [Array<(QuantityType, QuantityType)>, nil]
166
163
  def coerce(other)
167
164
  logger.trace("Coercing #{self} as a request from #{other.class}")
168
- if other.is_a?(Type)
169
- [other, as(other.class)]
170
- elsif other.respond_to?(:to_d)
171
- [QuantityType.new(other.to_d.to_java, Units::ONE), self]
172
- end
165
+ return unless other.respond_to?(:to_d)
166
+
167
+ [QuantityType.new(other.to_d.to_java, Units::ONE), self]
173
168
  end
174
169
 
175
170
  # arithmetic operators
@@ -63,7 +63,7 @@ module OpenHAB
63
63
  #
64
64
  def coerce(other)
65
65
  logger.trace("Coercing #{self} as a request from #{other.class}")
66
- return [String.new(other.to_str), self] if other.respond_to?(:to_str)
66
+ return [other.to_str, self] if other.respond_to?(:to_str)
67
67
  end
68
68
 
69
69
  # any method that exists on String gets forwarded to to_s
@@ -25,21 +25,6 @@ module OpenHAB
25
25
  to_s
26
26
  end
27
27
 
28
- #
29
- # Type Coercion
30
- #
31
- # Coerce object to the same Type
32
- #
33
- # @param [Type] other object to coerce to the same
34
- # Type as this one
35
- #
36
- # @return [[Type, Type], nil]
37
- #
38
- def coerce(other)
39
- logger.trace("Coercing #{self} (#{self.class}) as a request from #{other.class}")
40
- return [other.as(self.class), self] if other.is_a?(Type) && other.respond_to?(:as)
41
- end
42
-
43
28
  #
44
29
  # Check equality without type conversion
45
30
  #
@@ -51,19 +36,6 @@ module OpenHAB
51
36
  equals(other)
52
37
  end
53
38
 
54
- #
55
- # Case equality
56
- #
57
- # @return [true,false] if the values are of the same Type
58
- # or item state of the same type
59
- #
60
- def ===(other)
61
- logger.trace { "Type (#{self.class}) #{self} === #{other} (#{other.class})" }
62
- return false unless instance_of?(other.class)
63
-
64
- eql?(other)
65
- end
66
-
67
39
  #
68
40
  # Check equality, including type conversion
69
41
  #
@@ -95,6 +67,14 @@ module OpenHAB
95
67
 
96
68
  super
97
69
  end
70
+
71
+ # @!visibility private
72
+ #
73
+ # some openHAB Types don't implement as; do it for them
74
+ #
75
+ def as(klass)
76
+ self if klass == self.class
77
+ end
98
78
  end
99
79
 
100
80
  #
@@ -57,11 +57,24 @@ module OpenHAB
57
57
  ([command] | states).each do |method|
58
58
  logger.trace("Defining #{klass}##{method} for #{value}")
59
59
  klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
60
- def #{method} # def on?
61
- self == #{value} # self == ON
62
- end # end
60
+ def #{method} # def on?
61
+ as(#{value.class.java_class.simple_name}).equal?(#{value}) # as(OnOffType).equal?(ON)
62
+ end # end
63
63
  RUBY
64
64
  end
65
+
66
+ method = states.last
67
+ Events::ItemState.class_eval <<~RUBY, __FILE__, __LINE__ + 1
68
+ def #{method} # def on?
69
+ item_state.as(#{value.class.java_class.simple_name}).equal?(#{value}) # item_state.as(OnOffType).equal?(ON)
70
+ end # end
71
+ RUBY
72
+
73
+ Events::ItemStateChangedEvent.class_eval <<~RUBY, __FILE__, __LINE__ + 1
74
+ def was_#{method} # def was_on?
75
+ old_item_state.as(#{value.class.java_class.simple_name}).equal?(#{value}) # old_item_state.as(OnOffType).equal?(ON)
76
+ end # end
77
+ RUBY
65
78
  end
66
79
  end
67
80
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Extensions to Class
4
- class Class
3
+ # Extensions to Module
4
+ class Module
5
5
  #
6
- # Returns the name of the class, without any containing module or package.
6
+ # Returns the name of the class or module, without any containing module or package.
7
7
  #
8
8
  # @return [String, nil]
9
9
  #
@@ -38,23 +38,25 @@ module OpenHAB
38
38
  # def command(state)
39
39
  # return super(state) unless Thread.current[:openhab_ensure_states]
40
40
  #
41
+ # formatted_state = format_command(state)
41
42
  # logger.trace do
42
- # "#{name} ensure #{state}, format_command: #{format_command(state)}, current state: #{self.state}"
43
+ # "#{name} ensure #{state}, format_command: #{formatted_state}, current state: #{raw_state}"
43
44
  # end
44
- # return if self.state == format_command(state)
45
+ # return if raw_state == formatted_state
45
46
  #
46
- # super(state)
47
+ # super(formatted_state)
47
48
  # end
48
49
  class_eval <<~RUBY, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
49
50
  def #{ensured_method}(state)
50
51
  return super(state) unless Thread.current[:openhab_ensure_states]
51
52
 
53
+ formatted_state = format_#{ensured_method}(state)
52
54
  logger.trace do
53
- "\#{name} ensure \#{state}, format_#{ensured_method}: \#{format_#{ensured_method}(state)}, current state: \#{self.state}"
55
+ "\#{name} ensure \#{state}, format_#{ensured_method}: \#{formatted_state}, current state: \#{raw_state}"
54
56
  end
55
- return if self.state == format_#{ensured_method}(state)
57
+ return if raw_state.as(formatted_state.class) == formatted_state
56
58
 
57
- super(state)
59
+ super(formatted_state)
58
60
  end
59
61
  RUBY
60
62
  end
@@ -20,27 +20,29 @@ module OpenHAB
20
20
  class << self
21
21
  # @!visibility private
22
22
  # @!macro def_terse_rule
23
- # @!method $1(*args, name :nil, id: nil, **kwargs, &block)
23
+ # @!method $1(*args, name :nil, id: nil, on_load: false, **kwargs, &block)
24
24
  # Create a new rule with a $1 trigger.
25
25
  # @param name [String] The name for the rule.
26
26
  # @param id [String] The ID for the rule.
27
+ # @param on_load [true, false] If the rule should _also_ trigger immediately when the script loads.
27
28
  # @yield The execution block for the rule.
28
29
  # @return [void]
29
30
  # @see BuilderDSL#$1
30
31
  def def_terse_rule(trigger)
31
32
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
32
- def #{trigger}(*args, name: nil, id: nil, **kwargs, &block) # def changed(*args, name: nil, id: nil, **kwargs, &block)
33
- raise ArgumentError, "Block is required" unless block # raise ArgumentError, "Block is required" unless block
34
- #
35
- id ||= NameInference.infer_rule_id_from_block(block) # id ||= NameInference.infer_rule_id_from_block(block)
36
- script = block.source rescue nil # script = block.source rescue nil
37
- caller_binding = block.binding # caller_binding = block.binding
38
- rule name, id: id, script: script, binding: caller_binding do # rule name, id: id, script: script, binding: caller_binding do
39
- #{trigger}(*args, **kwargs) # changed(*args, **kwargs)
40
- run(&block) # run(&block)
41
- end # end
42
- end # end
43
- module_function #{trigger.inspect} # module_function :changed
33
+ def #{trigger}(*args, name: nil, id: nil, on_load: false, **kwargs, &block) # def changed(*args, name: nil, id: nil, on_load: false, **kwargs, &block)
34
+ raise ArgumentError, "Block is required" unless block # raise ArgumentError, "Block is required" unless block
35
+ #
36
+ id ||= NameInference.infer_rule_id_from_block(block) # id ||= NameInference.infer_rule_id_from_block(block)
37
+ script = block.source rescue nil # script = block.source rescue nil
38
+ caller_binding = block.binding # caller_binding = block.binding
39
+ rule name, id: id, script: script, binding: caller_binding do # rule name, id: id, script: script, binding: caller_binding do
40
+ self.on_load if on_load # self.on_load if on_load
41
+ #{trigger}(*args, **kwargs) # changed(*args, **kwargs)
42
+ run(&block) # run(&block)
43
+ end # end
44
+ end # end
45
+ module_function #{trigger.inspect} # module_function :changed
44
46
  RUBY
45
47
  end
46
48
  end
@@ -11,7 +11,7 @@ module OpenHAB
11
11
  class TimerManager
12
12
  include Singleton
13
13
 
14
- Core::ScriptHandling.script_unloaded { instance.cancel_all }
14
+ Core::ScriptHandling.script_unloaded(priority: 40) { instance.cancel_all }
15
15
 
16
16
  # @!visibility private
17
17
  def initialize
@@ -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.rc12"
7
+ VERSION = "5.0.0.rc.13"
8
8
  end
9
9
  end
data/lib/openhab/dsl.rb CHANGED
@@ -30,6 +30,12 @@ module OpenHAB
30
30
  # include this before Core::Actions so that Core::Action's method_missing
31
31
  # takes priority
32
32
  include Core::EntityLookup
33
+ #
34
+ # @!parse
35
+ # include Core::Actions
36
+ # include Core::ScriptHandling
37
+ # include Rules::Terse
38
+ #
33
39
  [Core::Actions, Core::ScriptHandling, Rules::Terse].each do |mod|
34
40
  # make these available both as regular and class methods
35
41
  include mod
@@ -473,6 +479,11 @@ module OpenHAB
473
479
  debounce(for: duration, id: id, &block)
474
480
  end
475
481
 
482
+ # (see Core::Actions::Transformation.transform)
483
+ def transform(type, function, value)
484
+ Transformation.transform(type, function, value)
485
+ end
486
+
476
487
  #
477
488
  # Limit how often the given block executes to the specified interval.
478
489
  #
data/lib/openhab/osgi.rb CHANGED
@@ -52,10 +52,8 @@ module OpenHAB
52
52
  )
53
53
  end
54
54
 
55
- private
56
-
57
55
  # @!attribute [r] bundle_context
58
- # @return [org.osgi.framework.BundleContext] OSGi bundle context
56
+ # @return [org.osgi.framework.BundleContext] OSGi bundle context for ScriptExtension Class
59
57
  def bundle_context
60
58
  @bundle_context ||= bundle.bundle_context
61
59
  end
@@ -12,9 +12,6 @@ module OpenHAB
12
12
  # @example Corresponds to `transform/compass.script`
13
13
  # OpenHAB::Transform.compass("30", param: "7")
14
14
  #
15
- # @example Corresponds to `transform/ruby/compass.script`
16
- # OpenHAB::Transform::Ruby.compass("59 °")
17
-
18
15
  module Transform
19
16
  class << self
20
17
  # @!visibility private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-jrubyscripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.rc12
4
+ version: 5.0.0.rc.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-26 00:00:00.000000000 Z
11
+ date: 2023-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: gem-release
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.2'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: guard-rubocop
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -453,9 +467,9 @@ files:
453
467
  - lib/openhab/core_ext/java/unit.rb
454
468
  - lib/openhab/core_ext/java/zoned_date_time.rb
455
469
  - lib/openhab/core_ext/ruby/array.rb
456
- - lib/openhab/core_ext/ruby/class.rb
457
470
  - lib/openhab/core_ext/ruby/date.rb
458
471
  - lib/openhab/core_ext/ruby/date_time.rb
472
+ - lib/openhab/core_ext/ruby/module.rb
459
473
  - lib/openhab/core_ext/ruby/numeric.rb
460
474
  - lib/openhab/core_ext/ruby/range.rb
461
475
  - lib/openhab/core_ext/ruby/symbol.rb
@@ -537,14 +551,14 @@ files:
537
551
  - lib/openhab/yard/tags/constant_directive.rb
538
552
  - lib/openhab/yard/tags/group_directive.rb
539
553
  - lib/openhab/yard/tags/library.rb
540
- homepage: https://ccutrer.github.io/openhab-jrubyscripting/
554
+ homepage: https://openhab.github.io/openhab-jruby/
541
555
  licenses:
542
556
  - EPL-2.0
543
557
  metadata:
544
- homepage_uri: https://ccutrer.github.io/openhab-jrubyscripting/
545
- source_code_uri: https://github.com/ccutrer/openhab-jrubyscripting
546
- documentation_uri: https://ccutrer.github.io/openhab-jrubyscripting/
547
- changelog_uri: https://ccutrer.github.io/openhab-jrubyscripting/file.CHANGELOG.html
558
+ homepage_uri: https://openhab.github.io/openhab-jruby/
559
+ source_code_uri: https://github.com/openhab/openhab-jruby
560
+ documentation_uri: https://openhab.github.io/openhab-jruby/
561
+ changelog_uri: https://openhab.github.io/openhab-jruby/file.CHANGELOG.html
548
562
  rubygems_mfa_required: 'true'
549
563
  post_install_message:
550
564
  rdoc_options: []
@@ -561,7 +575,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
561
575
  - !ruby/object:Gem::Version
562
576
  version: 1.3.1
563
577
  requirements: []
564
- rubygems_version: 3.3.7
578
+ rubygems_version: 3.3.26
565
579
  signing_key:
566
580
  specification_version: 4
567
581
  summary: JRuby Helper Libraries for openHAB Scripting