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
@@ -1,91 +1,53 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'delegate'
4
- require 'forwardable'
5
- require 'java'
6
- require 'openhab/dsl/items/item_command'
7
- require 'openhab/dsl/items/item_delegate'
8
- require 'openhab/core/entity_lookup'
3
+ require_relative 'comparable_item'
4
+ require 'openhab/dsl/lazy_array'
9
5
 
10
6
  module OpenHAB
11
7
  module DSL
12
8
  module Items
13
- #
14
- # Class for indicating to triggers that a group trigger should be used
15
- #
16
- class GroupMembers
17
- include Enumerable
18
-
19
- attr_reader :group
9
+ java_import org.openhab.core.items.GroupItem
20
10
 
11
+ # Adds methods to core OpenHAB GroupItem type to make it more natural in
12
+ # Ruby
13
+ class GroupItem < GenericItem
21
14
  #
22
- # Create a new GroupMembers instance from a GroupItem
23
- #
24
- # @param [GroupItem] group_item GroupItem to use as trigger
15
+ # Class for indicating to triggers that a group trigger should be used
25
16
  #
26
- def initialize(group_item)
27
- @group = group_item
28
- end
17
+ class GroupMembers
18
+ include LazyArray
29
19
 
30
- # Calls the given block once for each group member, passing that
31
- # item as a parameter. Returns self.
32
- #
33
- # If no block is given, an Enumerator is returned.
34
- def each(&block)
35
- to_a.each(&block)
36
- self
37
- end
20
+ # @return [GroupItem]
21
+ attr_reader :group
22
+
23
+ # @!visibility private
24
+ def initialize(group_item)
25
+ @group = group_item
26
+ end
38
27
 
39
- # explicit conversion to array
40
- # more efficient than letting Enumerable do it
41
- def to_a
42
- OpenHAB::Core::EntityLookup.decorate_items(group.members.to_a)
28
+ # Explicit conversion to Array
29
+ #
30
+ # @return [Array]
31
+ def to_a
32
+ group.get_members.to_a
33
+ end
43
34
  end
44
- # implicitly convertible to array
45
- alias to_ary to_a
46
- end
47
35
 
48
- #
49
- # Delegator to OpenHAB Group Item
50
- #
51
- class GroupItem
52
- extend OpenHAB::DSL::Items::ItemCommand
53
- extend OpenHAB::DSL::Items::ItemDelegate
54
36
  include Enumerable
55
- include Comparable
37
+ include ComparableItem
56
38
 
57
- def_item_delegator :@group_item
58
-
59
- #
60
- # @return [Hash] A hash of lambdas with default filters for `all_members`
61
- #
62
- DEFAULT_FILTERS = {
63
- groups: ->(item) { item.is_a?(Java::OrgOpenhabCoreItems::GroupItem) },
64
- all: -> { true }
65
- }.freeze
66
-
67
- private_constant :DEFAULT_FILTERS
68
-
69
- #
70
- # Create a new GroupItem
71
- #
72
- # @param [Java::Org::openhab::core::items::GroupItem] group_item OpenHAB GroupItem to delegate to
73
- #
74
- def initialize(group_item)
75
- @group_item = group_item
76
-
77
- item_missing_delegate { @group_item }
78
- item_missing_delegate { OpenHAB::Core::EntityLookup.decorate_item(base_item) }
79
- end
39
+ remove_method :==
80
40
 
81
41
  #
82
- # Create a GroupMembers object for use in triggers
42
+ # Get an Array-like object representing the members of the group
83
43
  #
84
- # @return [GroupMembers] A GroupMembers object
44
+ # @return [GroupMembers]
85
45
  #
86
46
  def members
87
- GroupMembers.new(@group_item)
47
+ GroupMembers.new(self)
88
48
  end
49
+
50
+ # @deprecated
89
51
  alias items members
90
52
 
91
53
  #
@@ -99,51 +61,43 @@ module OpenHAB
99
61
  # Get all members of the group recursively. Optionally filter the items to only return
100
62
  # Groups or regular Items
101
63
  #
102
- # @param [Symbol] filter Either :groups or :items
64
+ # @param [Symbol] filter Either +:groups+ or +:items+
103
65
  #
104
66
  # @return [Array] An Array containing all descendants of the Group, optionally filtered
105
67
  #
106
68
  def all_members(filter = nil, &block)
107
- predicate = DEFAULT_FILTERS[filter] || block
108
-
109
- return OpenHAB::Core::EntityLookup.decorate_items(@group_item.all_members.to_a) unless predicate
69
+ filter = nil if filter == :items
70
+ raise ArgumentError, 'filter must be :groups or :items' unless [:groups, nil].include?(filter)
110
71
 
111
- OpenHAB::Core::EntityLookup.decorate_items(@group_item.get_members(&predicate).to_a)
112
- end
72
+ block = ->(i) { i.is_a?(GroupItem) } if filter
113
73
 
114
- #
115
- # Test for equality
116
- #
117
- # @param [Object] other Other object to compare against
118
- #
119
- # @return [Boolean] true if self and other can be considered equal, false otherwise
120
- #
121
- def ==(other)
122
- if other.respond_to?(:java_class) && accepted_data_types.include?(other.java_class)
123
- get_state_as(other.class) == other
124
- elsif other.respond_to?(:state)
125
- base_item ? OpenHAB::Core::EntityLookup.decorate_item(base_item) == other.state : self == other.state
74
+ if block
75
+ get_members(&block).to_a
126
76
  else
127
- super
77
+ get_all_members.to_a
128
78
  end
129
79
  end
130
80
 
131
- #
132
- # Compare GroupItem to supplied object
133
- #
134
- # @param [Object] other object to compare to
135
- #
136
- # @return [Integer] -1,0,1 or nil depending on value supplied,
137
- # nil comparison to supplied object is not possible.
138
- #
139
- def <=>(other)
140
- if base_item
141
- OpenHAB::Core::EntityLookup.decorate_item(base_item) <=> other
142
- elsif state?
143
- -(other <=> state)
144
- else
145
- super
146
- end
81
+ # Delegate missing methods to +base_item+ if possible
82
+ def method_missing(method, *args, &block)
83
+ return base_item.__send__(method, *args, &block) if base_item.respond_to?(method)
84
+
85
+ super
86
+ end
87
+
88
+ # @!visibility private
89
+ def respond_to_missing?(method, include_private = false)
90
+ return true if base_item.respond_to?(method)
91
+
92
+ super
93
+ end
94
+
95
+ # give the base item type a chance to format commands
96
+ # @!visibility private
97
+ def format_type(command)
98
+ return super unless base_item
99
+
100
+ base_item.format_type(command)
147
101
  end
148
102
  end
149
103
  end
@@ -3,52 +3,16 @@
3
3
  require 'base64'
4
4
  require 'pathname'
5
5
  require 'net/http'
6
- require 'java'
7
6
  require 'marcel'
8
- require 'openhab/dsl/items/item_command'
9
- require 'openhab/dsl/items/item_delegate'
10
7
 
11
8
  module OpenHAB
12
9
  module DSL
13
10
  module Items
14
- #
15
- # Delegator to OpenHAB Player Item
16
- #
17
- class ImageItem
18
- extend OpenHAB::DSL::Items::ItemCommand
19
- extend OpenHAB::DSL::Items::ItemDelegate
20
-
21
- def_item_delegator :@image_item
22
-
23
- item_type Java::OrgOpenhabCoreLibraryItems::ImageItem
24
-
25
- #
26
- # Creates a new ImageItem
27
- #
28
- # @param [Java::OrgOpenhabCoreLibraryItems::ImageItem] image_item
29
- # The OpenHAB ImageItem to delegate to
30
- #
31
- def initialize(image_item)
32
- logger.trace("Wrapping #{image_item}")
33
- @image_item = image_item
34
-
35
- item_missing_delegate { @image_item }
36
-
37
- super()
38
- end
39
-
40
- #
41
- #
42
- # Update image with base64 encoded OpenHAB compatable image
43
- #
44
- # @param [String] base_64 base_64 encoding of an image
45
- #
46
- #
47
- def update(base_64_encoded_image)
48
- logger.trace { "Updating #{self} with Base64 image #{base_64_encoded_image}" }
49
- @image_item.update base_64_encoded_image
50
- end
11
+ java_import org.openhab.core.library.items.ImageItem
51
12
 
13
+ # Adds methods to core OpenHAB ImageItem type to make it more natural in
14
+ # Ruby
15
+ class ImageItem < GenericItem
52
16
  #
53
17
  # Update image from file
54
18
  #
@@ -87,7 +51,7 @@ module OpenHAB
87
51
  def update_from_bytes(bytes, mime_type: nil)
88
52
  mime_type ||= detect_mime_from_bytes(bytes: bytes)
89
53
  base_64_image = encode_image(mime_type: mime_type, bytes: bytes)
90
- update base_64_image
54
+ update(base_64_image)
91
55
  end
92
56
 
93
57
  #
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ require 'openhab/core/entity_lookup'
6
+ require 'openhab/dsl/lazy_array'
7
+
8
+ module OpenHAB
9
+ module DSL
10
+ module Items
11
+ #
12
+ # Provides access to all OpenHAB items, and acts like an array.
13
+ #
14
+ class ItemRegistry
15
+ include LazyArray
16
+ include Singleton
17
+
18
+ # Fetches the named item from the the ItemRegistry
19
+ # @param [String] name
20
+ # @return [GenericItem] Item from registry, nil if item missing or requested item is a Group Type
21
+ def [](name)
22
+ OpenHAB::Core::EntityLookup.lookup_item(name)
23
+ rescue org.openhab.core.items.ItemNotFoundException
24
+ nil
25
+ end
26
+
27
+ # Returns true if the given item name exists
28
+ # @param name [String] Item name to check
29
+ # @return [Boolean] true if the item exists, false otherwise
30
+ def include?(name)
31
+ !$ir.getItems(name).empty? # rubocop: disable Style/GlobalVars
32
+ end
33
+ alias key? include?
34
+
35
+ # Explicit conversion to array
36
+ # @return [Array]
37
+ def to_a
38
+ $ir.items.grep_v(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
39
+ end
40
+ end
41
+
42
+ # Fetches all non-group items from the item registry
43
+ # @return [ItemRegistry]
44
+ def items
45
+ ItemRegistry.instance
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,64 +1,88 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
- require 'openhab/core/entity_lookup'
5
- require 'singleton'
3
+ require 'openhab/dsl/monkey_patch/events/item_command'
4
+
5
+ require_relative 'item_registry'
6
+
7
+ require_relative 'generic_item'
8
+
9
+ require_relative 'switch_item'
10
+ require_relative 'date_time_item'
11
+ require_relative 'dimmer_item'
12
+
13
+ require_relative 'contact_item'
14
+ require_relative 'group_item'
15
+ require_relative 'image_item'
16
+ require_relative 'number_item'
17
+ require_relative 'player_item'
18
+ require_relative 'rollershutter_item'
19
+ require_relative 'string_item'
6
20
 
7
21
  module OpenHAB
8
22
  module DSL
9
- #
10
- # Manages OpenHAB items
11
- #
23
+ # Contains all OpenHAB *Item classes, as well as associated support
24
+ # modules
12
25
  module Items
13
- #
14
- # Delegates to underlying set of all OpenHAB Items, provides convenience methods
15
- #
16
- class Items
17
- include Enumerable
18
- include Singleton
26
+ class << self
27
+ private
19
28
 
20
- # Fetches the named item from the the ItemRegistry
21
- # @param [String] name
22
- # @return Item from registry, nil if item missing or requested item is a Group Type
23
- def [](name)
24
- OpenHAB::Core::EntityLookup.lookup_item(name)
25
- rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
26
- nil
29
+ # takes an array of Type java classes and returns
30
+ # all the Enum values, in a flat array
31
+ def values_for_enums(enums)
32
+ enums.map(&:ruby_class)
33
+ .select { |k| k < java.lang.Enum }
34
+ .flat_map(&:values)
27
35
  end
28
36
 
29
- # Returns true if the given item name exists
30
- # @param name [String] Item name to check
31
- # @return [Boolean] true if the item exists, false otherwise
32
- def include?(name)
33
- !$ir.getItems(name).empty? # rubocop: disable Style/GlobalVars
34
- end
35
- alias key? include?
37
+ # define predicates for checking if an item is in one of the Enum states
38
+ def def_predicate_methods(klass)
39
+ values_for_enums(klass.ACCEPTED_DATA_TYPES).each do |state|
40
+ _command_predicate, state_predicate = Types::PREDICATE_ALIASES[state.to_s]
41
+ next if klass.instance_methods.include?(state_predicate)
36
42
 
37
- # Calls the given block once for each Item, passing that Item as a
38
- # parameter. Returns self.
39
- #
40
- # If no block is given, an Enumerator is returned.
41
- def each(&block)
42
- # ideally we would do this lazily, but until ruby 2.7
43
- # there's no #eager method to convert back to a non-lazy
44
- # enumerator
45
- to_a.each(&block)
43
+ OpenHAB::Core.logger.trace("Defining #{klass}##{state_predicate} for #{state}")
44
+ klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
45
+ def #{state_predicate} # def on?
46
+ raw_state == #{state} # raw_state == ON
47
+ end # end
48
+ RUBY
49
+ end
46
50
  end
47
51
 
48
- # explicit conversion to array
49
- # more efficient than letting Enumerable do it
50
- def to_a
51
- items = $ir.items.grep_v(Java::OrgOpenhabCoreItems::GroupItem) # rubocop:disable Style/GlobalVars
52
- OpenHAB::Core::EntityLookup.decorate_items(items)
52
+ # defined methods for commanding an item to one of the Enum states
53
+ # as well as predicates for if an ItemCommandEvent is one of those commands
54
+ def def_command_methods(klass) # rubocop:disable Metrics/MethodLength method has single purpose
55
+ values_for_enums(klass.ACCEPTED_COMMAND_TYPES).each do |value|
56
+ command = Types::COMMAND_ALIASES[value.to_s]
57
+ next if klass.instance_methods.include?(command)
58
+
59
+ OpenHAB::Core.logger.trace("Defining #{klass}##{command} for #{value}")
60
+ klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
61
+ def #{command} # def on
62
+ command(#{value}) # command(ON)
63
+ end # end
64
+ RUBY
65
+
66
+ OpenHAB::Core.logger.trace("Defining ItemCommandEvent##{command}? for #{value}")
67
+ MonkeyPatch::Events::ItemCommandEvent.class_eval <<~RUBY, __FILE__, __LINE__ + 1
68
+ def #{command}? # def refresh?
69
+ command == #{value} # command == REFRESH
70
+ end # end
71
+ RUBY
72
+ end
53
73
  end
54
- # implicitly convertible to array
55
- alias to_ary to_a
56
74
  end
57
75
 
58
- # Fetches all non-group items from the item registry
59
- # @return [OpenHAB::DSL::Items::Items]
60
- def items
61
- Items.instance
76
+ # sort classes by hierarchy so we define methods on parent classes first
77
+ constants.map { |c| const_get(c) }
78
+ .grep(Module)
79
+ .select { |k| k <= GenericItem && k != GroupItem && k != StringItem }
80
+ .sort { |a, b| a < b ? 1 : -1 }
81
+ .each do |klass|
82
+ klass.field_reader :ACCEPTED_COMMAND_TYPES, :ACCEPTED_DATA_TYPES unless klass == GenericItem
83
+
84
+ def_predicate_methods(klass)
85
+ def_command_methods(klass)
62
86
  end
63
87
  end
64
88
  end