openhab-scripting 4.1.2 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +1 -57
  3. data/lib/openhab/dsl/dsl.rb +8 -12
  4. data/lib/openhab/dsl/group.rb +1 -5
  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 +174 -0
  10. data/lib/openhab/dsl/items/group_item.rb +56 -92
  11. data/lib/openhab/dsl/items/image_item.rb +5 -41
  12. data/lib/openhab/dsl/items/item_equality.rb +36 -0
  13. data/lib/openhab/dsl/items/item_registry.rb +49 -0
  14. data/lib/openhab/dsl/items/items.rb +71 -35
  15. data/lib/openhab/dsl/items/metadata.rb +325 -0
  16. data/lib/openhab/dsl/items/number_item.rb +6 -312
  17. data/lib/openhab/dsl/items/numeric_item.rb +68 -0
  18. data/lib/openhab/dsl/items/persistence.rb +122 -0
  19. data/lib/openhab/dsl/items/player_item.rb +49 -40
  20. data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
  21. data/lib/openhab/dsl/items/string_item.rb +16 -58
  22. data/lib/openhab/dsl/items/switch_item.rb +62 -0
  23. data/lib/openhab/dsl/lazy_array.rb +8 -6
  24. data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
  25. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
  26. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
  27. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
  28. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
  29. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
  30. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
  31. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
  32. data/lib/openhab/dsl/rules/terse.rb +24 -0
  33. data/lib/openhab/dsl/states.rb +1 -1
  34. data/lib/openhab/dsl/time_of_day.rb +3 -5
  35. data/lib/openhab/dsl/types/comparable_type.rb +21 -0
  36. data/lib/openhab/dsl/types/date_time_type.rb +334 -0
  37. data/lib/openhab/dsl/types/decimal_type.rb +187 -0
  38. data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
  39. data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
  40. data/lib/openhab/dsl/types/numeric_type.rb +39 -0
  41. data/lib/openhab/dsl/types/on_off_type.rb +29 -0
  42. data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
  43. data/lib/openhab/dsl/types/percent_type.rb +68 -0
  44. data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
  45. data/lib/openhab/dsl/types/quantity_type.rb +275 -0
  46. data/lib/openhab/dsl/types/refresh_type.rb +18 -0
  47. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
  48. data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
  49. data/lib/openhab/dsl/types/string_type.rb +88 -0
  50. data/lib/openhab/dsl/types/type.rb +72 -0
  51. data/lib/openhab/dsl/types/types.rb +77 -0
  52. data/lib/openhab/dsl/types/un_def_type.rb +22 -0
  53. data/lib/openhab/dsl/types/up_down_type.rb +32 -0
  54. data/lib/openhab/dsl/units.rb +11 -6
  55. data/lib/openhab/version.rb +1 -1
  56. data/lib/openhab.rb +0 -1
  57. metadata +33 -28
  58. data/lib/openhab/dsl/items/datetime_item.rb +0 -75
  59. data/lib/openhab/dsl/items/item_command.rb +0 -90
  60. data/lib/openhab/dsl/items/item_delegate.rb +0 -125
  61. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
  62. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
  63. data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
  64. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
  65. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
  66. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
  67. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
  68. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
  69. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
  70. data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
  71. data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
  72. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
  73. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
  74. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
  75. data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
  76. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
  77. data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
  78. data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
  79. data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
  80. data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
  81. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
  82. data/lib/openhab/dsl/types/datetime.rb +0 -338
  83. data/lib/openhab/dsl/types/quantity.rb +0 -300
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d752e22ac324370b7c63e7e38455cd919f3f64abece0fc7ab62fd29c72953b7
4
- data.tar.gz: 34c7cd408a075c831e7ea75e55f907bd57203268da81f34e318657d18ad6a90f
3
+ metadata.gz: 34dcff6b869bdb1bbcf1e61d84ffe7ff07d039204ee868d16f0f249c3074e898
4
+ data.tar.gz: 39645a3f3486d9bba7db56a43d4c60883c346eea007242603a2dd2f7c8aa3b6e
5
5
  SHA512:
6
- metadata.gz: bc29460c4a427f81fa6a8ce0760d746d0bf1513574e72f89a17c4d5647c22dc860abe12c60d4a0904f5c32927725bed03ff8ff62212bd45217e05d9b9e3fe01a
7
- data.tar.gz: b29f6d50649f32134815cb71b9c2d42b466f9d0147a613bbc2225f99469f5c5d4194a7a5c47f81c20757b149c2e5bcc7133fc6b099025f43132ecef25bb2cfc2
6
+ metadata.gz: 303f0751aac26ab7f28a4350ff03151a3a03bb1559f51ae0130c830ea5e20d6bb4165929a173ed4e749cf62ee0e4ab66f1f79845e8e851d44a288407e11be5a5
7
+ data.tar.gz: dfee68ab34f41a303886bc8028ae7a30ff4baaee2fd87cc75602bfa7d82f57c2c3a7bf6190f6efecdb2a364c21a4514fe59984e9da8574cf19c5f3fd782abc7e
@@ -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,24 @@
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'
13
+ require 'openhab/dsl/rules/terse'
11
14
  require 'openhab/dsl/actions'
12
15
  require 'openhab/dsl/timers'
13
16
  require 'openhab/dsl/group'
14
17
  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
18
  require 'openhab/dsl/time_of_day'
22
19
  require 'openhab/dsl/gems'
23
20
  require 'openhab/dsl/persistence'
24
21
  require 'openhab/dsl/units'
25
- require 'openhab/dsl/types/datetime'
26
- require 'openhab/dsl/types/quantity'
27
22
  require 'openhab/dsl/states'
28
23
 
29
24
  module OpenHAB
@@ -40,6 +35,7 @@ module OpenHAB
40
35
  base.send :include, OpenHAB::DSL::Items
41
36
  base.send :include, OpenHAB::DSL::Persistence
42
37
  base.send :include, OpenHAB::DSL::Rules
38
+ base.send :include, OpenHAB::DSL::Rules::Terse
43
39
  base.send :include, OpenHAB::DSL::States
44
40
  base.send :include, OpenHAB::DSL::Things
45
41
  base.send :include, OpenHAB::DSL::Timers
@@ -1,10 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
3
  require 'singleton'
5
4
 
6
- require 'openhab/core/entity_lookup'
7
- require 'openhab/dsl/items/group_item'
8
5
  require 'openhab/dsl/lazy_array'
9
6
 
10
7
  module OpenHAB
@@ -35,8 +32,7 @@ module OpenHAB
35
32
 
36
33
  # explicit conversion to array
37
34
  def to_a
38
- items = $ir.items.grep(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
39
- Core::EntityLookup.decorate_items(items)
35
+ $ir.items.grep(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
40
36
  end
41
37
  end
42
38
 
@@ -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,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openhab/dsl/items/metadata'
4
+ require 'openhab/dsl/items/persistence'
5
+
6
+ require_relative 'item_equality'
7
+
8
+ module OpenHAB
9
+ module DSL
10
+ module Items
11
+ java_import org.openhab.core.items.GenericItem
12
+
13
+ # Adds methods to core OpenHAB DimmerItem type to make it more natural in
14
+ # Ruby
15
+ class GenericItem
16
+ include Log
17
+ include ItemEquality
18
+
19
+ prepend Metadata
20
+ prepend Persistence
21
+
22
+ # rubocop:disable Naming/MethodName these mimic Java fields, which are
23
+ # actually methods
24
+ class << self
25
+ # manually define this, since the Java side doesn't
26
+ # @!visibility private
27
+ def ACCEPTED_COMMAND_TYPES
28
+ [org.openhab.core.types.RefreshType.java_class].freeze
29
+ end
30
+
31
+ # manually define this, since the Java side doesn't
32
+ # @!visibility private
33
+ def ACCEPTED_DATA_TYPES
34
+ [org.openhab.core.types.UnDefType.java_class].freeze
35
+ end
36
+ end
37
+ # rubocop:enable Naming/MethodName
38
+
39
+ alias hash hash_code
40
+
41
+ # Get the raw item state.
42
+ #
43
+ # The state of the item, including possibly +NULL+ or +UNDEF+
44
+ #
45
+ # @return [Types::Type]
46
+ #
47
+ alias raw_state state
48
+
49
+ remove_method(:==)
50
+
51
+ #
52
+ # Send a command to this item
53
+ #
54
+ # @param [Types::Type] command to send to object
55
+ #
56
+ #
57
+ def command(command)
58
+ command = format_type_pre(command)
59
+ logger.trace "Sending Command #{command} to #{id}"
60
+ org.openhab.core.model.script.actions.BusEvent.sendCommand(self, command)
61
+ self
62
+ end
63
+ alias << command
64
+
65
+ #
66
+ # Send an update to this item
67
+ #
68
+ # @param [Types::Type] update the item
69
+ #
70
+ #
71
+ def update(update)
72
+ update = format_type_pre(update)
73
+ logger.trace "Sending Update #{update} to #{id}"
74
+ org.openhab.core.model.script.actions.BusEvent.postUpdate(self, update)
75
+ self
76
+ end
77
+
78
+ #
79
+ # Check if the item has a state (not +UNDEF+ or +NULL+)
80
+ #
81
+ # @return [Boolean]
82
+ #
83
+ def state?
84
+ !raw_state.is_a?(Types::UnDefType)
85
+ end
86
+
87
+ #
88
+ # Get the item state
89
+ #
90
+ # @return [Types::Type, nil]
91
+ # OpenHAB item state if state is not +UNDEF+ or +NULL+, nil otherwise
92
+ #
93
+ def state
94
+ raw_state if state?
95
+ end
96
+
97
+ #
98
+ # Get an ID for the item, using the item label if set, otherwise item name
99
+ #
100
+ # @return [String] label if set otherwise name
101
+ #
102
+ def id
103
+ label || name
104
+ end
105
+
106
+ #
107
+ # Get the string representation of the state of the item
108
+ #
109
+ # @return [String] State of the item as a string
110
+ #
111
+ def to_s
112
+ raw_state.to_s # call the super state to include UNDEF/NULL
113
+ end
114
+
115
+ #
116
+ # Inspect the item
117
+ #
118
+ # @return [String] details of the item
119
+ #
120
+ def inspect
121
+ to_string
122
+ end
123
+
124
+ #
125
+ # Return all groups that this item is part of
126
+ #
127
+ # @return [Array<Group>] All groups that this item is part of
128
+ #
129
+ def groups
130
+ group_names.map { |name| Groups.groups[name] }
131
+ end
132
+
133
+ #
134
+ # Check equality without type conversion
135
+ #
136
+ # @return [Boolean] if the same Item is represented, without checking
137
+ # state
138
+ def eql?(other)
139
+ other.instance_of?(self.class) && hash == other.hash
140
+ end
141
+
142
+ # @!method null?
143
+ # Check if the item state == +NULL+
144
+ # @return [Boolean]
145
+
146
+ # @!method undef?
147
+ # Check if the item state == +UNDEF+
148
+ # @return [Boolean]
149
+
150
+ # @!method refresh
151
+ # Send the +REFRESH+ command to the item
152
+ # @return [GenericItem] +self+
153
+
154
+ # formats a {Types::Type} to send to the event bus
155
+ # @!visibility private
156
+ def format_type(command)
157
+ # actual Type types can be sent directly without conversion
158
+ return command if command.is_a?(Types::Type)
159
+
160
+ command.to_s
161
+ end
162
+
163
+ private
164
+
165
+ # convert items to their state before formatting, so that subclasses
166
+ # only have to deal with Types
167
+ def format_type_pre(command)
168
+ command = command.state if command.is_a?(GenericItem)
169
+ format_type(command)
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end