openhab-scripting 4.1.4 → 4.5.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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +1 -57
  3. data/lib/openhab/dsl/actions.rb +2 -3
  4. data/lib/openhab/dsl/dsl.rb +8 -12
  5. data/lib/openhab/dsl/group.rb +1 -5
  6. data/lib/openhab/dsl/items/color_item.rb +60 -0
  7. data/lib/openhab/dsl/items/comparable_item.rb +49 -0
  8. data/lib/openhab/dsl/items/contact_item.rb +41 -0
  9. data/lib/openhab/dsl/items/date_time_item.rb +64 -0
  10. data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
  11. data/lib/openhab/dsl/items/ensure.rb +93 -0
  12. data/lib/openhab/dsl/items/generic_item.rb +174 -0
  13. data/lib/openhab/dsl/items/group_item.rb +121 -89
  14. data/lib/openhab/dsl/items/image_item.rb +5 -41
  15. data/lib/openhab/dsl/items/item_equality.rb +36 -0
  16. data/lib/openhab/dsl/items/item_registry.rb +49 -0
  17. data/lib/openhab/dsl/items/items.rb +81 -35
  18. data/lib/openhab/dsl/items/metadata.rb +325 -0
  19. data/lib/openhab/dsl/items/number_item.rb +6 -312
  20. data/lib/openhab/dsl/items/numeric_item.rb +68 -0
  21. data/lib/openhab/dsl/items/persistence.rb +122 -0
  22. data/lib/openhab/dsl/items/player_item.rb +49 -40
  23. data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
  24. data/lib/openhab/dsl/items/string_item.rb +16 -58
  25. data/lib/openhab/dsl/items/switch_item.rb +62 -0
  26. data/lib/openhab/dsl/lazy_array.rb +8 -6
  27. data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
  28. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
  29. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
  30. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
  31. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
  32. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
  33. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
  34. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
  35. data/lib/openhab/dsl/rules/terse.rb +24 -0
  36. data/lib/openhab/dsl/states.rb +1 -1
  37. data/lib/openhab/dsl/time_of_day.rb +3 -5
  38. data/lib/openhab/dsl/types/comparable_type.rb +21 -0
  39. data/lib/openhab/dsl/types/date_time_type.rb +334 -0
  40. data/lib/openhab/dsl/types/decimal_type.rb +187 -0
  41. data/lib/openhab/dsl/types/hsb_type.rb +201 -0
  42. data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
  43. data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
  44. data/lib/openhab/dsl/types/numeric_type.rb +39 -0
  45. data/lib/openhab/dsl/types/on_off_type.rb +29 -0
  46. data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
  47. data/lib/openhab/dsl/types/percent_type.rb +70 -0
  48. data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
  49. data/lib/openhab/dsl/types/quantity_type.rb +275 -0
  50. data/lib/openhab/dsl/types/refresh_type.rb +18 -0
  51. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
  52. data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
  53. data/lib/openhab/dsl/types/string_type.rb +88 -0
  54. data/lib/openhab/dsl/types/type.rb +72 -0
  55. data/lib/openhab/dsl/types/types.rb +78 -0
  56. data/lib/openhab/dsl/types/un_def_type.rb +22 -0
  57. data/lib/openhab/dsl/types/up_down_type.rb +32 -0
  58. data/lib/openhab/dsl/units.rb +11 -6
  59. data/lib/openhab/version.rb +1 -1
  60. data/lib/openhab.rb +0 -1
  61. metadata +36 -28
  62. data/lib/openhab/dsl/items/datetime_item.rb +0 -75
  63. data/lib/openhab/dsl/items/item_command.rb +0 -90
  64. data/lib/openhab/dsl/items/item_delegate.rb +0 -125
  65. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
  66. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
  67. data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
  68. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
  69. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
  70. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
  71. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
  72. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
  73. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
  74. data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
  75. data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
  76. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
  77. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
  78. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
  79. data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
  80. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
  81. data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
  82. data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
  83. data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
  84. data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
  85. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
  86. data/lib/openhab/dsl/types/datetime.rb +0 -338
  87. data/lib/openhab/dsl/types/quantity.rb +0 -300
@@ -1,94 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'forwardable'
4
- require 'java'
5
- require 'openhab/dsl/items/item_command'
6
- require 'openhab/dsl/items/item_delegate'
3
+ require_relative 'numeric_item'
7
4
 
8
5
  module OpenHAB
9
6
  module DSL
10
7
  module Items
11
- #
12
- # Delegator to OpenHAB Rollershutter Item
13
- #
14
- class RollershutterItem < Numeric
15
- extend Forwardable
16
- extend OpenHAB::DSL::Items::ItemCommand
17
- extend OpenHAB::DSL::Items::ItemDelegate
18
- include Comparable
8
+ java_import org.openhab.core.library.items.RollershutterItem
19
9
 
20
- def_item_delegator :@rollershutter_item
10
+ # Adds methods to core OpenHAB RollershutterItem type to make it more natural in
11
+ # Ruby
12
+ class RollershutterItem < GenericItem
13
+ include NumericItem
21
14
 
22
- item_type Java::OrgOpenhabCoreLibraryItems::RollershutterItem
15
+ alias position state
23
16
 
24
- #
25
- # Creates a new RollershutterItem
26
- #
27
- # @param [Java::OrgOpenhabCoreLibraryItems::RollershutterItem] rollershutter_item
28
- # The OpenHAB RollershutterItem to delegate to
29
- #
30
- def initialize(rollershutter_item)
31
- logger.trace("Wrapping #{rollershutter_item}")
32
- @rollershutter_item = rollershutter_item
17
+ # @!method up?
18
+ # Check if the item state == +UP+
19
+ # @return [Boolean]
33
20
 
34
- item_missing_delegate { @rollershutter_item }
35
- item_missing_delegate { position }
21
+ # @!method down?
22
+ # Check if the item state == +DOWN+
23
+ # @return [Boolean]
36
24
 
37
- super()
38
- end
25
+ # @!method up
26
+ # Send the +UP+ command to the item
27
+ # @return [RollershutterItem] +self+
39
28
 
40
- #
41
- # Returns the rollershutter's position
42
- #
43
- # @return [Java::OrgOpenhabCoreLibraryTypes::PercentType] the position of the rollershutter
44
- #
45
- def position
46
- state&.as(PercentType)
47
- end
29
+ # @!method down
30
+ # Send the +DOWN+ command to the item
31
+ # @return [RollershutterItem] +self+
48
32
 
49
- #
50
- # Compare the rollershutter's position against another object
51
- #
52
- # @param [Object] other object to compare against
53
- #
54
- # @return [Integer] -1, 0 or 1 depending on the result of the comparison
55
- #
56
- def <=>(other)
57
- return nil unless state?
33
+ # @!method stop
34
+ # Send the +STOP+ command to the item
35
+ # @return [RollershutterItem] +self+
58
36
 
59
- case other
60
- when PercentType, Java::OrgOpenhabCoreLibraryTypes::DecimalType then position.compare_to(other)
61
- when Numeric then position.int_value <=> other
62
- when RollershutterItem then position.compare_to(other.position)
63
- when UpDownType then state.as(UpDownType) == other
64
- end
65
- end
66
-
67
- #
68
- # Coerce self into other to enable calculations
69
- #
70
- # @param [Numeric] other Other numeric to coerce into
71
- #
72
- # @return [Array<Numeric>] an array of other and self coerced into other's type
73
- #
74
- def coerce(other)
75
- raise ArgumentError, "Cannot coerce to #{other.class}" unless other.is_a? Numeric
76
-
77
- case other
78
- when Integer then [other, position&.int_value]
79
- when Float then [other, position&.float_value]
80
- end
81
- end
82
-
83
- #
84
- # Define math operations
85
- #
86
- %i[+ - * / %].each do |operator|
87
- define_method(operator) do |other|
88
- right, left = coerce(other)
89
- left&.send(operator, right)
90
- end
91
- end
37
+ # @!method move
38
+ # Send the +MOVE+ command to the item
39
+ # @return [RollershutterItem] +self+
92
40
  end
93
41
  end
94
42
  end
@@ -1,63 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bigdecimal'
4
3
  require 'forwardable'
5
- require 'java'
6
- require 'openhab/dsl/items/item_delegate'
4
+
5
+ require 'openhab/dsl/items/comparable_item'
7
6
 
8
7
  module OpenHAB
9
8
  module DSL
10
9
  module Items
11
- #
12
- # Delegator to OpenHAB String Item
13
- #
14
- class StringItem
10
+ java_import org.openhab.core.library.items.StringItem
11
+
12
+ # Adds methods to core OpenHAB StringItem type to make it more natural in
13
+ # Ruby
14
+ class StringItem < GenericItem
15
15
  extend Forwardable
16
- extend OpenHAB::DSL::Items::ItemCommand
17
16
  include Comparable
18
- extend OpenHAB::DSL::Items::ItemDelegate
17
+ include ComparableItem
18
+
19
+ remove_method :==
19
20
 
20
21
  # @return [Regex] Regular expression matching blank strings
21
22
  BLANK_RE = /\A[[:space:]]*\z/.freeze
22
23
  private_constant :BLANK_RE
23
24
 
24
- def_item_delegator :@string_item
25
-
26
- item_type Java::OrgOpenhabCoreLibraryItems::StringItem
27
-
28
- #
29
- # Create a new StringItem
30
- #
31
- # @param [Java::Org::openhab::core::library::items::StringItem] string_item OpenHAB string item to delegate to
32
- #
33
- def initialize(string_item)
34
- @string_item = string_item
35
-
36
- item_missing_delegate { @string_item }
37
- item_missing_delegate { @string_item.state&.to_full_string&.to_s }
38
-
39
- super()
40
- end
41
-
42
- #
43
- # Convert the StringItem into a String
44
- #
45
- # @return [String] String representation of the StringItem or
46
- # nil if underlying OpenHAB StringItem does not have a state
47
- #
48
- def to_str
49
- @string_item.state&.to_full_string&.to_s
50
- end
51
-
52
25
  #
53
26
  # Detect if the string is blank (not set or only whitespace)
54
27
  #
55
28
  # @return [Boolean] True if string item is not set or contains only whitespace, false otherwise
56
29
  #
57
30
  def blank?
58
- return true unless @string_item.state?
31
+ return true unless state?
59
32
 
60
- @string_item.state.to_full_string.to_s.empty? || BLANK_RE.match?(self)
33
+ state.empty? || BLANK_RE.match?(self)
61
34
  end
62
35
 
63
36
  #
@@ -66,27 +39,12 @@ module OpenHAB
66
39
  # @return [Boolean] True if item is not in state UNDEF or NULL and value is not blank
67
40
  #
68
41
  def truthy?
69
- @string_item.state? && blank? == false
42
+ state? && !blank?
70
43
  end
71
44
 
72
- #
73
- # Compare StringItem to supplied object
74
- #
75
- # @param [Object] other object to compare to
76
- #
77
- # @return [Integer] -1,0,1 or nil depending on value supplied,
78
- # nil comparison to supplied object is not possible.
79
- #
80
- def <=>(other)
81
- case other
82
- when StringItem
83
- @string_item.state <=> other.state
84
- when String
85
- @string_item.state.to_s <=> other
86
- else
87
- @string_item.state <=> other
88
- end
89
- end
45
+ # any method that exists on String gets forwarded to state (which will forward as
46
+ # necessary)
47
+ delegate (String.instance_methods - instance_methods) => :state
90
48
  end
91
49
  end
92
50
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ #
6
+ # Patches OpenHAB items
7
+ #
8
+ module Items
9
+ java_import org.openhab.core.library.items.SwitchItem
10
+
11
+ # Alias class names for easy is_a? comparisons
12
+ ::Switch = SwitchItem
13
+
14
+ # Adds methods to core OpenHAB SwitchItem type to make it more natural in
15
+ # Ruby
16
+ class SwitchItem < GenericItem
17
+ remove_method :==
18
+
19
+ def truthy?
20
+ on?
21
+ end
22
+
23
+ #
24
+ # Send a command to invert the state of the switch
25
+ #
26
+ # @return [Types::OnOffType] Inverted state
27
+ #
28
+ def toggle
29
+ command(!self)
30
+ end
31
+
32
+ #
33
+ # Return the inverted state of the switch: +ON+ if the switch is +OFF+,
34
+ # +UNDEF+ or +NULL+; +OFF+ if the switch is +ON+
35
+ #
36
+ # @return [Types::OnOffType] Inverted state
37
+ #
38
+ def !
39
+ return !state if state?
40
+
41
+ ON
42
+ end
43
+
44
+ # @!method on?
45
+ # Check if the item state == +ON+
46
+ # @return [Boolean]
47
+
48
+ # @!method off?
49
+ # Check if the item state == +OFF+
50
+ # @return [Boolean]
51
+
52
+ # @!method on
53
+ # Send the +ON+ command to the item
54
+ # @return [SwitchItem] +self+
55
+
56
+ # @!method off
57
+ # Send the +OFF+ command to the item
58
+ # @return [SwitchItem] +self+
59
+ end
60
+ end
61
+ end
62
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module OpenHAB
4
4
  module DSL
5
- # common base class for array-like collections that have lookup
5
+ # Common base class for array-like collections that have lookup
6
6
  # methods to avoid instantiating the elements if you only use
7
7
  # the lookup method
8
8
  #
@@ -11,7 +11,7 @@ module OpenHAB
11
11
  module LazyArray
12
12
  include Enumerable
13
13
 
14
- # Calls the given block once for each Thing, passing that Thing as a
14
+ # Calls the given block once for each object, passing that object as a
15
15
  # parameter. Returns self.
16
16
  #
17
17
  # If no block is given, an Enumerator is returned.
@@ -20,20 +20,22 @@ module OpenHAB
20
20
  self
21
21
  end
22
22
 
23
- # implicitly convertible to array
23
+ # Implicitly convertible to array
24
+ #
25
+ # @return [Array]
26
+ #
24
27
  def to_ary
25
28
  to_a
26
29
  end
27
30
 
28
- # delegate any other methods to the actual array
29
- # exclude mutating methods though
31
+ # Delegate any other methods to the actual array, exclude mutating methods
30
32
  def method_missing(method, *args, &block)
31
33
  return to_a.send(method, *args, &block) if method[-1] != '!' && Array.instance_methods.include?(method)
32
34
 
33
35
  super
34
36
  end
35
37
 
36
- # advertise that methods exist that would be delegated to Array
38
+ # @!visibility private
37
39
  def respond_to_missing?(method, include_private = false)
38
40
  return true if method[-1] != '!' && Array.instance_methods.include?(method.to_sym)
39
41
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'openhab/dsl/monkey_patch/events/item_state'
4
3
  require 'openhab/dsl/monkey_patch/events/item_event'
4
+ require 'openhab/dsl/monkey_patch/events/item_state'
5
5
  require 'openhab/dsl/monkey_patch/events/item_state_changed'
6
- require 'openhab/dsl/monkey_patch/events/thing_status_info'
7
6
  require 'openhab/dsl/monkey_patch/events/item_command'
7
+ require 'openhab/dsl/monkey_patch/events/thing_status_info'
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
3
+ require_relative 'item_event'
4
4
 
5
5
  module OpenHAB
6
6
  module DSL
@@ -9,32 +9,75 @@ module OpenHAB
9
9
  # Patches OpenHAB events
10
10
  #
11
11
  module Events
12
- java_import Java::OrgOpenhabCoreItemsEvents::ItemCommandEvent
13
-
14
- #
15
- # Monkey patch with ruby style accesors
16
- #
17
- class ItemCommandEvent
18
- include Log
12
+ java_import org.openhab.core.items.events.ItemCommandEvent
19
13
 
14
+ # Adds methods to core OpenHAB ItemCommandEvent to make it more natural in Ruby
15
+ class ItemCommandEvent < ItemEvent
16
+ # @return [Type]
20
17
  alias command item_command
21
18
 
22
- #
23
- # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
24
- # string representation of the enum value For example, an enum with values of STOP and START
25
- # would create methods stop? and start? that check if the command is corresponding STOP and START type
26
- #
27
- # @param [Java::JavaLang::Enum] command_enum Enumeration to create commands for
28
- #
29
- def self.def_enum_predicates(command_enum)
30
- command_enum.values.each do |command| # rubocop:disable Style/HashEachMethods : Java enum does not have each_value
31
- command_method = "#{command.to_s.downcase}?"
32
- logger.trace("Creating predicate method (#{command_method}) for #{self}")
33
- define_method(command_method) do
34
- self.command == command
35
- end
36
- end
37
- end
19
+ # @!method refresh?
20
+ # Check if == +REFRESH+
21
+ # @return [Boolean]
22
+
23
+ # @!method on?
24
+ # Check if == +ON+
25
+ # @return [Boolean]
26
+
27
+ # @!method off?
28
+ # Check if == +OFF+
29
+ # @return [Boolean]
30
+
31
+ # @!method up?
32
+ # Check if == +UP+
33
+ # @return [Boolean]
34
+
35
+ # @!method down?
36
+ # Check if == +DOWN+
37
+ # @return [Boolean]
38
+
39
+ # @!method stop?
40
+ # Check if == +STOP+
41
+ # @return [Boolean]
42
+
43
+ # @!method move?
44
+ # Check if == +MOVE+
45
+ # @return [Boolean]
46
+
47
+ # @!method increase?
48
+ # Check if == +INCREASE+
49
+ # @return [Boolean]
50
+
51
+ # @!method decrease?
52
+ # Check if == +DECREASE+
53
+ # @return [Boolean]
54
+
55
+ # @!method play?
56
+ # Check if == +PLAY+
57
+ # @return [Boolean]
58
+
59
+ # @!method pause?
60
+ # Check if == +PAUSE+
61
+ # @return [Boolean]
62
+
63
+ # @!method rewind?
64
+ # Check if == +REWIND+
65
+ # @return [Boolean]
66
+
67
+ # @!method fast_forward?
68
+ # Check if == +FASTFORWARD+
69
+ # @return [Boolean]
70
+
71
+ # @deprecated
72
+ # @!parse alias fastforward? fast_forward?
73
+
74
+ # @!method next?
75
+ # Check if == +NEXT+
76
+ # @return [Boolean]
77
+
78
+ # @!method previous?
79
+ # Check if == +PREVIOUS+
80
+ # @return [Boolean]
38
81
  end
39
82
  end
40
83
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
-
5
3
  module OpenHAB
6
4
  module DSL
7
5
  module MonkeyPatch
@@ -9,14 +7,16 @@ module OpenHAB
9
7
  # Patches OpenHAB events
10
8
  #
11
9
  module Events
12
- java_import Java::OrgOpenhabCoreItemsEvents::ItemEvent
10
+ java_import org.openhab.core.items.events.ItemEvent
13
11
 
14
12
  #
15
- # MonkeyPatch to add item
13
+ # Adds methods to core OpenHAB ItemEvent to make it more natural in Ruby
16
14
  #
17
15
  class ItemEvent
18
16
  #
19
- # Return a decorated item
17
+ # The triggering item
18
+ #
19
+ # @return [GenericItem]
20
20
  #
21
21
  def item
22
22
  OpenHAB::Core::EntityLookup.lookup_item(item_name)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
3
+ require 'openhab/dsl/types/un_def_type'
4
4
 
5
5
  module OpenHAB
6
6
  module DSL
@@ -9,18 +9,17 @@ module OpenHAB
9
9
  # Patches OpenHAB events
10
10
  #
11
11
  module Events
12
- java_import Java::OrgOpenhabCoreItemsEvents::ItemStateEvent
13
- java_import Java::OrgOpenhabCoreTypes::UnDefType
12
+ java_import org.openhab.core.items.events.ItemStateEvent
14
13
 
15
14
  # Helpers common to ItemStateEvent and ItemStateChangedEvent
16
- module ItemStateUnDefTypeHelpers
15
+ module ItemState
17
16
  #
18
17
  # Check if the state == UNDEF
19
18
  #
20
19
  # @return [Boolean] True if the state is UNDEF, false otherwise
21
20
  #
22
21
  def undef?
23
- item_state == UnDefType::UNDEF
22
+ item_state == UNDEF
24
23
  end
25
24
 
26
25
  #
@@ -28,7 +27,7 @@ module OpenHAB
28
27
  #
29
28
  # @return [Boolean] True if the state is NULL, false otherwise
30
29
  def null?
31
- item_state == UnDefType::NULL
30
+ item_state == NULL
32
31
  end
33
32
 
34
33
  #
@@ -37,13 +36,13 @@ module OpenHAB
37
36
  # @return [Boolean] True if state is not UNDEF or NULL
38
37
  #
39
38
  def state?
40
- undef? == false && null? == false
39
+ !item_state.is_a?(Types::UnDefType)
41
40
  end
42
41
 
43
42
  #
44
43
  # Get the item state
45
44
  #
46
- # @return [State] OpenHAB state if state is not UNDEF or NULL, nil otherwise
45
+ # @return [Types::PrimitiveState] OpenHAB state if state is not UNDEF or NULL, nil otherwise
47
46
  #
48
47
  def state
49
48
  item_state if state?
@@ -51,10 +50,10 @@ module OpenHAB
51
50
  end
52
51
 
53
52
  #
54
- # MonkeyPatch with ruby style accessors
53
+ # Adds methods to core OpenHAB ItemStateEvent to make it more natural in Ruby
55
54
  #
56
- class ItemStateEvent
57
- include ItemStateUnDefTypeHelpers
55
+ class ItemStateEvent < ItemEvent
56
+ include ItemState
58
57
  end
59
58
  end
60
59
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
3
+ require 'openhab/dsl/types/un_def_type'
4
4
 
5
5
  module OpenHAB
6
6
  module DSL
@@ -9,14 +9,13 @@ module OpenHAB
9
9
  # Patches OpenHAB events
10
10
  #
11
11
  module Events
12
- java_import Java::OrgOpenhabCoreItemsEvents::ItemStateChangedEvent
13
- java_import Java::OrgOpenhabCoreTypes::UnDefType
12
+ java_import org.openhab.core.items.events.ItemStateChangedEvent
14
13
 
15
14
  #
16
- # MonkeyPatch with ruby style accessors
15
+ # Adds methods to core OpenHAB ItemStateChangedEvent to make it more natural in Ruby
17
16
  #
18
- class ItemStateChangedEvent
19
- include ItemStateUnDefTypeHelpers
17
+ class ItemStateChangedEvent < ItemEvent
18
+ include ItemState
20
19
 
21
20
  #
22
21
  # Check if state was == UNDEF
@@ -24,7 +23,7 @@ module OpenHAB
24
23
  # @return [Boolean] True if the state is UNDEF, false otherwise
25
24
  #
26
25
  def was_undef?
27
- old_item_state == UnDefType::UNDEF
26
+ old_item_state == UNDEF
28
27
  end
29
28
 
30
29
  #
@@ -32,7 +31,7 @@ module OpenHAB
32
31
  #
33
32
  # @return [Boolean] True if the state is NULL, false otherwise
34
33
  def was_null?
35
- old_item_state == UnDefType::NULL
34
+ old_item_state == NULL
36
35
  end
37
36
 
38
37
  #
@@ -41,18 +40,18 @@ module OpenHAB
41
40
  # @return [Boolean] True if state is not UNDEF or NULL
42
41
  #
43
42
  def was?
44
- was_undef? == false && was_null? == false
43
+ !old_item_state.is_a?(Types::UnDefType)
45
44
  end
46
45
 
47
46
  #
48
47
  # Get the previous item state
49
48
  #
50
- # @return [State] OpenHAB state if state was not UNDEF or NULL, nil otherwise
49
+ # @return [Types::Type] OpenHAB state if state was not UNDEF or NULL, nil otherwise
51
50
  #
52
51
  def was
53
52
  old_item_state if was?
54
53
  end
55
-
54
+ # @deprecated
56
55
  alias last was
57
56
  end
58
57
  end
@@ -16,7 +16,7 @@ module OpenHAB
16
16
  # @return [Java::JavaTime::Duration] Duration with number of units from self
17
17
  #
18
18
  %w[millis seconds minutes hours].each do |unit|
19
- define_method(unit) { Java::JavaTime::Duration.public_send("of_#{unit}", self) }
19
+ define_method(unit) { java.time.Duration.public_send("of_#{unit}", self) }
20
20
  end
21
21
 
22
22
  alias second seconds
@@ -37,7 +37,7 @@ module OpenHAB
37
37
  # @return [Java::JavaTime::Duration] Duration truncated to an integral number of milliseconds from self
38
38
  #
39
39
  def millis
40
- Java::JavaTime::Duration.of_millis(to_i)
40
+ java.time.Duration.of_millis(to_i)
41
41
  end
42
42
 
43
43
  #
@@ -74,6 +74,26 @@ module OpenHAB
74
74
  alias minute minutes
75
75
  alias hour hours
76
76
  end
77
+
78
+ #
79
+ # Extend numeric to create quantity object
80
+ #
81
+ module NumericExtensions
82
+ #
83
+ # Convert Numeric to a QuantityType
84
+ #
85
+ # @param [Object] other String or Unit representing an OpenHAB Unit
86
+ #
87
+ # @return [Types::QuantityType] +self+ as a {Types::QuantityType} of the supplied Unit
88
+ #
89
+ def |(other)
90
+ other = org.openhab.core.types.util.UnitUtils.parse_unit(other.to_str) if other.respond_to?(:to_str)
91
+
92
+ return super unless other.is_a?(javax.measure.Unit)
93
+
94
+ QuantityType.new(to_d.to_java, other)
95
+ end
96
+ end
77
97
  end
78
98
  end
79
99
  end
@@ -81,3 +101,6 @@ end
81
101
 
82
102
  Integer.prepend(OpenHAB::DSL::MonkeyPatch::Ruby::IntegerExtensions)
83
103
  Float.prepend(OpenHAB::DSL::MonkeyPatch::Ruby::FloatExtensions)
104
+ Numeric.include(OpenHAB::DSL::MonkeyPatch::Ruby::NumericExtensions)
105
+ # Integer already has #|, so we have to prepend it here
106
+ Integer.prepend(OpenHAB::DSL::MonkeyPatch::Ruby::NumericExtensions)
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Monkey patch ruby
4
- require 'openhab/dsl/monkey_patch/ruby/range'
5
3
  require 'openhab/dsl/monkey_patch/ruby/number'
6
4
  require 'openhab/dsl/monkey_patch/ruby/string'
7
- require 'openhab/dsl/monkey_patch/ruby/time'
8
5
  require 'bigdecimal/util'