openhab-scripting 4.1.1 → 4.2.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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +1 -57
  3. data/lib/openhab/dsl/dsl.rb +6 -12
  4. data/lib/openhab/dsl/group.rb +18 -11
  5. data/lib/openhab/dsl/items/comparable_item.rb +49 -0
  6. data/lib/openhab/dsl/items/contact_item.rb +41 -0
  7. data/lib/openhab/dsl/items/date_time_item.rb +64 -0
  8. data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
  9. data/lib/openhab/dsl/items/generic_item.rb +197 -0
  10. data/lib/openhab/dsl/items/group_item.rb +56 -102
  11. data/lib/openhab/dsl/items/image_item.rb +5 -41
  12. data/lib/openhab/dsl/items/item_registry.rb +49 -0
  13. data/lib/openhab/dsl/items/items.rb +70 -46
  14. data/lib/openhab/dsl/items/metadata.rb +325 -0
  15. data/lib/openhab/dsl/items/number_item.rb +6 -312
  16. data/lib/openhab/dsl/items/numeric_item.rb +66 -0
  17. data/lib/openhab/dsl/items/persistence.rb +122 -0
  18. data/lib/openhab/dsl/items/player_item.rb +49 -40
  19. data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
  20. data/lib/openhab/dsl/items/string_item.rb +16 -58
  21. data/lib/openhab/dsl/items/switch_item.rb +62 -0
  22. data/lib/openhab/dsl/lazy_array.rb +46 -0
  23. data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
  24. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
  25. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
  26. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
  27. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
  28. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
  29. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
  30. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
  31. data/lib/openhab/dsl/states.rb +1 -1
  32. data/lib/openhab/dsl/things.rb +6 -22
  33. data/lib/openhab/dsl/time_of_day.rb +3 -5
  34. data/lib/openhab/dsl/types/comparable_type.rb +21 -0
  35. data/lib/openhab/dsl/types/date_time_type.rb +334 -0
  36. data/lib/openhab/dsl/types/decimal_type.rb +187 -0
  37. data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
  38. data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
  39. data/lib/openhab/dsl/types/numeric_type.rb +39 -0
  40. data/lib/openhab/dsl/types/on_off_type.rb +29 -0
  41. data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
  42. data/lib/openhab/dsl/types/percent_type.rb +68 -0
  43. data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
  44. data/lib/openhab/dsl/types/quantity_type.rb +275 -0
  45. data/lib/openhab/dsl/types/refresh_type.rb +18 -0
  46. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
  47. data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
  48. data/lib/openhab/dsl/types/string_type.rb +88 -0
  49. data/lib/openhab/dsl/types/type.rb +72 -0
  50. data/lib/openhab/dsl/types/types.rb +77 -0
  51. data/lib/openhab/dsl/types/un_def_type.rb +22 -0
  52. data/lib/openhab/dsl/types/up_down_type.rb +32 -0
  53. data/lib/openhab/dsl/units.rb +11 -6
  54. data/lib/openhab/version.rb +1 -1
  55. data/lib/openhab.rb +0 -1
  56. metadata +32 -28
  57. data/lib/openhab/dsl/items/datetime_item.rb +0 -75
  58. data/lib/openhab/dsl/items/item_command.rb +0 -90
  59. data/lib/openhab/dsl/items/item_delegate.rb +0 -125
  60. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
  61. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
  62. data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
  63. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
  64. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
  65. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
  66. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
  67. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
  68. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
  69. data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
  70. data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
  71. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
  72. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
  73. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
  74. data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
  75. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
  76. data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
  77. data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
  78. data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
  79. data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
  80. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
  81. data/lib/openhab/dsl/types/datetime.rb +0 -338
  82. data/lib/openhab/dsl/types/quantity.rb +0 -300
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2210f9eb88b639bd2ca594cce368ec87ee7746595f7b2f9c04e7bd1ebdf10fc3
4
- data.tar.gz: 9dae7f3f53574ba089c37c52e35e110c596a09deb3005ced5a267d18d64b1f3b
3
+ metadata.gz: 4ebe32d36d3366345d28b81bc71d9cd315d1d84feb4a4f1745c882209b1fdbe2
4
+ data.tar.gz: 95331e97286e86bc993eb979531c5e3f80bc203e752e97aa9f3a60698fd56274
5
5
  SHA512:
6
- metadata.gz: be659fbac0f529674ffb4a38944e1f2365fa82a08bbc832edb0ad3a5f074ec683454980050758ba6810b52abaf42d8ab8f442d73276de4fe38d51a8984e7b61a
7
- data.tar.gz: a58d4df593b831c3daf979b2d0094691e22d2be849a375c9bf7c0a30d78e9e4d71d36adfa85d918debd1a8e948ec9b6d7b57e933f9eb791cf9d769ee5ce02193
6
+ metadata.gz: e406fa7d011a733376bb5977293f44d20b893f1f990486f342c541f8d287c453736b75007ba586bfc5e6801638ac71bc6d294ad1b4a436a83247bee157150eb0
7
+ data.tar.gz: d4fad57e6ad4f71a969b8f8c6791c4240854a74800647a4bed770123221eb2491c251c2a28bcbcd1649225d348aadd91b0efc188adba16a56aa7115620224028
@@ -3,13 +3,6 @@
3
3
  require 'pp'
4
4
  require 'java'
5
5
  require 'set'
6
- require 'openhab/dsl/group'
7
- require 'openhab/log/logger'
8
- require 'openhab/dsl/items/number_item'
9
- require 'openhab/dsl/items/string_item'
10
- require 'openhab/dsl/items/datetime_item'
11
- require 'openhab/dsl/items/rollershutter_item'
12
- require 'openhab/dsl/items/group_item'
13
6
 
14
7
  # Automation lookup and injection of OpenHab entities
15
8
 
@@ -68,52 +61,6 @@ module OpenHAB
68
61
  lookup_item(name) || lookup_thing(name)
69
62
  end
70
63
 
71
- #
72
- # Decorate items with Ruby wrappers
73
- #
74
- # @param [Array] items Array of items to decorate
75
- #
76
- # @return [Array] Array of decorated items
77
- #
78
- def self.decorate_items(*items)
79
- items.flatten.map { |item| decorate_item(item) }
80
- end
81
-
82
- #
83
- # Decorate item with Ruby wrappers
84
- #
85
- # @param [Object] item the item object to decorate
86
- #
87
- # @return [Object] the ruby wrapper for the item
88
- #
89
- # rubocop: disable Metrics/MethodLength
90
- # rubocop: disable Metrics/CyclomaticComplexity
91
- # Disabled line length and branch size - case dispatch pattern
92
- def self.decorate_item(item)
93
- logger.trace("Decorating #{item.class}")
94
- case item
95
- when Java::OrgOpenhabCoreItems::GroupItem
96
- OpenHAB::DSL::Items::GroupItem.new(item)
97
- when Java::OrgOpenhabCoreLibraryItems::NumberItem
98
- OpenHAB::DSL::Items::NumberItem.new(item)
99
- when Java::OrgOpenhabCoreLibraryItems::StringItem
100
- OpenHAB::DSL::Items::StringItem.new(item)
101
- when Java::OrgOpenhabCoreLibraryItems::DateTimeItem
102
- OpenHAB::DSL::Items::DateTimeItem.new(item)
103
- when Java::OrgOpenhabCoreLibraryItems::RollershutterItem
104
- OpenHAB::DSL::Items::RollershutterItem.new(item)
105
- when Java::OrgOpenhabCoreLibraryItems::PlayerItem
106
- OpenHAB::DSL::Items::PlayerItem.new(item)
107
- when Java::OrgOpenhabCoreLibraryItems::ImageItem
108
- OpenHAB::DSL::Items::ImageItem.new(item)
109
- else
110
- logger.trace("Returning undecorated item #{item.class}")
111
- item
112
- end
113
- end
114
- # rubocop: enable Metrics/MethodLength
115
- # rubocop: enable Metrics/CyclomaticComplexity
116
-
117
64
  #
118
65
  # Looks up a Thing in the OpenHAB registry replacing '_' with ':'
119
66
  #
@@ -145,10 +92,7 @@ module OpenHAB
145
92
  def self.lookup_item(name)
146
93
  logger.trace("Looking up item(#{name})")
147
94
  name = name.to_s if name.is_a? Symbol
148
- # rubocop: disable Style/GlobalVars
149
- item = $ir.get(name)
150
- # rubocop: enable Style/GlobalVars
151
- decorate_item(item)
95
+ $ir.get(name) # rubocop: disable Style/GlobalVars
152
96
  end
153
97
  end
154
98
  end
@@ -1,29 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
3
  require 'openhab/log/logger'
5
- require 'openhab/dsl/monkey_patch/events/events'
4
+
5
+ # the order of these is important
6
+ require 'openhab/dsl/types/types'
7
+ require 'openhab/dsl/items/items'
6
8
  require 'openhab/dsl/monkey_patch/ruby/ruby'
7
- require 'openhab/dsl/monkey_patch/items/items'
8
- require 'openhab/dsl/monkey_patch/types/types'
9
+
10
+ require 'openhab/dsl/monkey_patch/events/events'
9
11
  require 'openhab/dsl/monkey_patch/actions/actions'
10
12
  require 'openhab/dsl/rules/rule'
11
13
  require 'openhab/dsl/actions'
12
14
  require 'openhab/dsl/timers'
13
15
  require 'openhab/dsl/group'
14
16
  require 'openhab/dsl/things'
15
- require 'openhab/dsl/items/items'
16
- require 'openhab/dsl/items/datetime_item'
17
- require 'openhab/dsl/items/image_item'
18
- require 'openhab/dsl/items/number_item'
19
- require 'openhab/dsl/items/player_item'
20
- require 'openhab/dsl/items/group_item'
21
17
  require 'openhab/dsl/time_of_day'
22
18
  require 'openhab/dsl/gems'
23
19
  require 'openhab/dsl/persistence'
24
20
  require 'openhab/dsl/units'
25
- require 'openhab/dsl/types/datetime'
26
- require 'openhab/dsl/types/quantity'
27
21
  require 'openhab/dsl/states'
28
22
 
29
23
  module OpenHAB
@@ -1,9 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'delegate'
4
- require 'forwardable'
5
- require 'openhab/core/entity_lookup'
6
- require 'openhab/dsl/items/group_item'
3
+ require 'singleton'
4
+
5
+ require 'openhab/dsl/lazy_array'
7
6
 
8
7
  module OpenHAB
9
8
  module DSL
@@ -14,16 +13,26 @@ module OpenHAB
14
13
  #
15
14
  # Provide access to groups as a set
16
15
  #
17
- class Groups < SimpleDelegator
16
+ class Groups
17
+ include LazyArray
18
+ include Singleton
19
+
18
20
  #
19
21
  # Get a OpenHAB Group by name
20
22
  # @param [String] name of the group to retrieve
21
23
  #
22
24
  # @return [Set] of OpenHAB Groups
23
25
  #
24
- def[](name)
25
- group = OpenHAB::Core::EntityLookup.lookup_item(name)
26
- group.is_a?(OpenHAB::DSL::Items::GroupItem) ? group : nil
26
+ def [](name)
27
+ group = Core::EntityLookup.lookup_item(name)
28
+ group.is_a?(Items::GroupItem) ? group : nil
29
+ end
30
+ alias include? []
31
+ alias key? include?
32
+
33
+ # explicit conversion to array
34
+ def to_a
35
+ $ir.items.grep(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
27
36
  end
28
37
  end
29
38
 
@@ -33,9 +42,7 @@ module OpenHAB
33
42
  # @return [Set] of OpenHAB Groups
34
43
  #
35
44
  def groups
36
- # rubocop: disable Style/GlobalVars
37
- Groups.new(OpenHAB::Core::EntityLookup.decorate_items($ir.items.grep(Java::OrgOpenhabCoreItems::GroupItem)))
38
- # rubocop: enable Style/GlobalVars
45
+ Groups.instance
39
46
  end
40
47
  end
41
48
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ module Items
6
+ # Mixin for implementing the comparison operator for Item types that
7
+ # support it
8
+ module ComparableItem
9
+ #
10
+ # Comparison
11
+ #
12
+ # @param [GenericItem, Types::Type, Object] other object to
13
+ # compare to
14
+ #
15
+ # If this item is +NULL+ or +UNDEF+, and +other+ is nil, they are
16
+ # considered equal
17
+ #
18
+ # If this item is +NULL+ or +UNDEF+, and other is a {GenericItem}, they
19
+ # are only considered equal if the other item is in the exact same
20
+ # state (i.e. +NULL+ != +UNDEF+)
21
+ #
22
+ # Otherwise, the state of this item is compared with +other+
23
+ #
24
+ # @return [Integer, nil] -1, 0, +1 depending on whether +other+ is
25
+ # less than, equal to, or greater than self
26
+ #
27
+ # nil is returned if the two values are incomparable
28
+ #
29
+ def <=>(other)
30
+ logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
31
+ # if we're NULL or UNDEF, implement special logic
32
+ unless state?
33
+ # if comparing to nil, consider ourselves equal
34
+ return 0 if other.nil?
35
+ # if the other object is an Item, only consider equal if we're
36
+ # in the same _kind_ of UnDefType state
37
+ return raw_state == other.raw_state if other.is_a?(GenericItem) && !other.state?
38
+
39
+ # otherwise, it's a non-nil thing comparing to nil, which is undefined
40
+ return nil
41
+ end
42
+
43
+ # delegate to how the state compares to the other object
44
+ state <=> other
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,41 @@
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.ContactItem
10
+
11
+ # Alias class for ContactItem
12
+ ::Contact = ContactItem
13
+
14
+ # Adds methods to core OpenHAB ContactItem type to make it more natural
15
+ # in Ruby
16
+ class ContactItem < GenericItem
17
+ remove_method :==
18
+
19
+ #
20
+ # Return the inverted state of the contact: +CLOSED+ if the contact is
21
+ # +OPEN+, +UNDEF+ or +NULL+; +OPEN+ if the contact is +CLOSED+
22
+ #
23
+ # @return [Types::OpenClosedType] Inverted state
24
+ #
25
+ def !
26
+ return !state if state?
27
+
28
+ CLOSED
29
+ end
30
+
31
+ # @!method open?
32
+ # Check if the item state == +OPEN+
33
+ # @return [Boolean]
34
+
35
+ # @!method closed?
36
+ # Check if the item state == +CLOSED+
37
+ # @return [Boolean]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'time'
5
+
6
+ require 'openhab/dsl/items/comparable_item'
7
+
8
+ module OpenHAB
9
+ module DSL
10
+ module Items
11
+ java_import org.openhab.core.library.items.DateTimeItem
12
+
13
+ # Adds methods to core OpenHAB DateTimeItem type to make it more natural
14
+ # in Ruby
15
+ class DateTimeItem < GenericItem
16
+ extend Forwardable
17
+ include Comparable
18
+ include ComparableItem
19
+
20
+ # !@visibility private
21
+ def ==(other)
22
+ # need to check if we're referring to the same item before
23
+ # forwarding to <=> (and thus checking equality with state)
24
+ return true if equal?(other) || eql?(other)
25
+
26
+ super
27
+ end
28
+
29
+ #
30
+ # Type Coercion
31
+ #
32
+ # Coerce object to a DateTimeType
33
+ #
34
+ # @param [Types::DateTimeType, Time] other object to coerce to a
35
+ # DateTimeType
36
+ #
37
+ # @return [[Types::DateTimeType, Types::DateTimeType]]
38
+ #
39
+ def coerce(other)
40
+ logger.trace("Coercing #{self} as a request from #{other.class}")
41
+ return [other, nil] unless state?
42
+ return [other, state] if other.is_a?(Types::DateTimeType) || other.respond_to?(:to_time)
43
+
44
+ raise TypeError, "can't convert #{other.class} into #{self.class}"
45
+ end
46
+
47
+ # any method that exists on DateTimeType, Java's ZonedDateTime, or
48
+ # Ruby's Time class gets forwarded to state (which will forward as
49
+ # necessary)
50
+ delegate ((Types::DateTimeType.instance_methods +
51
+ java.time.ZonedDateTime.instance_methods +
52
+ Time.instance_methods) - instance_methods) => :state
53
+
54
+ # Time types need formatted as ISO8601
55
+ # @!visibility private
56
+ def format_type(command)
57
+ return command.iso8601 if command.respond_to?(:iso8601)
58
+
59
+ super
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openhab/dsl/time_of_day'
4
+
5
+ require_relative 'numeric_item'
6
+
7
+ module OpenHAB
8
+ module DSL
9
+ #
10
+ # Patches OpenHAB items
11
+ #
12
+ module Items
13
+ java_import org.openhab.core.library.items.DimmerItem
14
+
15
+ # Alias class
16
+ ::Dimmer = DimmerItem
17
+
18
+ # Adds methods to core OpenHAB DimmerItem type to make it more natural in
19
+ # Ruby
20
+ class DimmerItem < SwitchItem
21
+ include NumericItem
22
+
23
+ #
24
+ # Dim the dimmer
25
+ #
26
+ # @param [Integer] amount to dim by
27
+ #
28
+ # @return [Integer] level target for dimmer
29
+ #
30
+ def dim(amount = 1)
31
+ target = [state&.-(amount), 0].compact.max
32
+ command(target)
33
+ target
34
+ end
35
+
36
+ #
37
+ # Brighten the dimmer
38
+ #
39
+ # @param [Integer] amount to brighten by
40
+ #
41
+ # @return [Integer] level target for dimmer
42
+ #
43
+ def brighten(amount = 1)
44
+ target = [state&.+(amount), 100].compact.min
45
+ command(target)
46
+ target
47
+ end
48
+
49
+ # @!method increase
50
+ # Send the +INCREASE+ command to the item
51
+ # @return [DimmerItem] +self+
52
+
53
+ # @!method decrease
54
+ # Send the +DECREASE+ command to the item
55
+ # @return [DimmerItem] +self+
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openhab/dsl/items/metadata'
4
+ require 'openhab/dsl/items/persistence'
5
+
6
+ module OpenHAB
7
+ module DSL
8
+ module Items
9
+ java_import org.openhab.core.items.GenericItem
10
+
11
+ # Adds methods to core OpenHAB DimmerItem type to make it more natural in
12
+ # Ruby
13
+ class GenericItem
14
+ include Log
15
+ prepend Metadata
16
+ prepend Persistence
17
+
18
+ # rubocop:disable Naming/MethodName these mimic Java fields, which are
19
+ # actually methods
20
+ class << self
21
+ # manually define this, since the Java side doesn't
22
+ # @!visibility private
23
+ def ACCEPTED_COMMAND_TYPES
24
+ [org.openhab.core.types.RefreshType.java_class].freeze
25
+ end
26
+
27
+ # manually define this, since the Java side doesn't
28
+ # @!visibility private
29
+ def ACCEPTED_DATA_TYPES
30
+ [org.openhab.core.types.UnDefType.java_class].freeze
31
+ end
32
+ end
33
+ # rubocop:enable Naming/MethodName
34
+
35
+ alias hash hash_code
36
+
37
+ # Get the raw item state.
38
+ #
39
+ # The state of the item, including possibly +NULL+ or +UNDEF+
40
+ #
41
+ # @return [Types::Type]
42
+ #
43
+ alias raw_state state
44
+
45
+ remove_method(:==)
46
+
47
+ #
48
+ # Send a command to this item
49
+ #
50
+ # @param [Types::Type] command to send to object
51
+ #
52
+ #
53
+ def command(command)
54
+ command = format_type_pre(command)
55
+ logger.trace "Sending Command #{command} to #{id}"
56
+ org.openhab.core.model.script.actions.BusEvent.sendCommand(self, command)
57
+ self
58
+ end
59
+ alias << command
60
+
61
+ #
62
+ # Send an update to this item
63
+ #
64
+ # @param [Types::Type] update the item
65
+ #
66
+ #
67
+ def update(update)
68
+ update = format_type_pre(update)
69
+ logger.trace "Sending Update #{update} to #{id}"
70
+ org.openhab.core.model.script.actions.BusEvent.postUpdate(self, update)
71
+ self
72
+ end
73
+
74
+ #
75
+ # Check if the item has a state (not +UNDEF+ or +NULL+)
76
+ #
77
+ # @return [Boolean]
78
+ #
79
+ def state?
80
+ !raw_state.is_a?(Types::UnDefType)
81
+ end
82
+
83
+ #
84
+ # Get the item state
85
+ #
86
+ # @return [Types::Type, nil]
87
+ # OpenHAB item state if state is not +UNDEF+ or +NULL+, nil otherwise
88
+ #
89
+ def state
90
+ raw_state if state?
91
+ end
92
+
93
+ #
94
+ # Get an ID for the item, using the item label if set, otherwise item name
95
+ #
96
+ # @return [String] label if set otherwise name
97
+ #
98
+ def id
99
+ label || name
100
+ end
101
+
102
+ #
103
+ # Get the string representation of the state of the item
104
+ #
105
+ # @return [String] State of the item as a string
106
+ #
107
+ def to_s
108
+ raw_state.to_s # call the super state to include UNDEF/NULL
109
+ end
110
+
111
+ #
112
+ # Inspect the item
113
+ #
114
+ # @return [String] details of the item
115
+ #
116
+ def inspect
117
+ to_string
118
+ end
119
+
120
+ #
121
+ # Return all groups that this item is part of
122
+ #
123
+ # @return [Array<Group>] All groups that this item is part of
124
+ #
125
+ def groups
126
+ group_names.map { |name| Groups.groups[name] }
127
+ end
128
+
129
+ #
130
+ # Check equality without type conversion
131
+ #
132
+ # @return [Boolean] if the same Item is represented, without checking
133
+ # state
134
+ def eql?(other)
135
+ other.instance_of?(self.class) && hash == other.hash
136
+ end
137
+
138
+ #
139
+ # Check equality, with type conversions
140
+ #
141
+ # @param [GenericItem, Types::Type, Object] other object to
142
+ # compare to
143
+ #
144
+ # If this item is +NULL+ or +UNDEF+, and +other+ is nil, they are
145
+ # considered equal
146
+ #
147
+ # If this item is +NULL+ or +UNDEF+, and other is a {GenericItem}, they
148
+ # are only considered equal if the other item is in the exact same
149
+ # state (i.e. +NULL+ != +UNDEF+)
150
+ #
151
+ # Otherwise, the state of this item is compared with +other+
152
+ #
153
+ # @return [Boolean]
154
+ #
155
+ def ==(other)
156
+ logger.trace("(#{self.class}) #{self} == #{other} (#{other.class})")
157
+ return true if equal?(other) || eql?(other)
158
+ return true if !state? && other.nil?
159
+
160
+ return raw_state == other.raw_state if other.is_a?(GenericItem)
161
+
162
+ state == other
163
+ end
164
+
165
+ # @!method null?
166
+ # Check if the item state == +NULL+
167
+ # @return [Boolean]
168
+
169
+ # @!method undef?
170
+ # Check if the item state == +UNDEF+
171
+ # @return [Boolean]
172
+
173
+ # @!method refresh
174
+ # Send the +REFRESH+ command to the item
175
+ # @return [GenericItem] +self+
176
+
177
+ # formats a {Types::Type} to send to the event bus
178
+ # @!visibility private
179
+ def format_type(command)
180
+ # actual Type types can be sent directly without conversion
181
+ return command if command.is_a?(Types::Type)
182
+
183
+ command.to_s
184
+ end
185
+
186
+ private
187
+
188
+ # convert items to their state before formatting, so that subclasses
189
+ # only have to deal with Types
190
+ def format_type_pre(command)
191
+ command = command.state if command.is_a?(GenericItem)
192
+ format_type(command)
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end