openhab-scripting 4.1.4 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) 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 +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 +197 -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_registry.rb +49 -0
  13. data/lib/openhab/dsl/items/items.rb +71 -35
  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 +8 -6
  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/time_of_day.rb +3 -5
  33. data/lib/openhab/dsl/types/comparable_type.rb +21 -0
  34. data/lib/openhab/dsl/types/date_time_type.rb +334 -0
  35. data/lib/openhab/dsl/types/decimal_type.rb +187 -0
  36. data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
  37. data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
  38. data/lib/openhab/dsl/types/numeric_type.rb +39 -0
  39. data/lib/openhab/dsl/types/on_off_type.rb +29 -0
  40. data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
  41. data/lib/openhab/dsl/types/percent_type.rb +68 -0
  42. data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
  43. data/lib/openhab/dsl/types/quantity_type.rb +275 -0
  44. data/lib/openhab/dsl/types/refresh_type.rb +18 -0
  45. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
  46. data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
  47. data/lib/openhab/dsl/types/string_type.rb +88 -0
  48. data/lib/openhab/dsl/types/type.rb +72 -0
  49. data/lib/openhab/dsl/types/types.rb +77 -0
  50. data/lib/openhab/dsl/types/un_def_type.rb +22 -0
  51. data/lib/openhab/dsl/types/up_down_type.rb +32 -0
  52. data/lib/openhab/dsl/units.rb +11 -6
  53. data/lib/openhab/version.rb +1 -1
  54. data/lib/openhab.rb +0 -1
  55. metadata +31 -28
  56. data/lib/openhab/dsl/items/datetime_item.rb +0 -75
  57. data/lib/openhab/dsl/items/item_command.rb +0 -90
  58. data/lib/openhab/dsl/items/item_delegate.rb +0 -125
  59. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
  60. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
  61. data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
  62. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
  63. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
  64. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
  65. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
  66. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
  67. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
  68. data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
  69. data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
  70. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
  71. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
  72. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
  73. data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
  74. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
  75. data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
  76. data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
  77. data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
  78. data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
  79. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
  80. data/lib/openhab/dsl/types/datetime.rb +0 -338
  81. data/lib/openhab/dsl/types/quantity.rb +0 -300
@@ -1,81 +1,53 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'delegate'
4
- require 'forwardable'
5
- require 'java'
6
-
7
- require 'openhab/core/entity_lookup'
8
- require 'openhab/dsl/items/item_command'
9
- require 'openhab/dsl/items/item_delegate'
3
+ require_relative 'comparable_item'
10
4
  require 'openhab/dsl/lazy_array'
11
5
 
12
6
  module OpenHAB
13
7
  module DSL
14
8
  module Items
15
- #
16
- # Class for indicating to triggers that a group trigger should be used
17
- #
18
- class GroupMembers
19
- include LazyArray
20
-
21
- attr_reader :group
9
+ java_import org.openhab.core.items.GroupItem
22
10
 
11
+ # Adds methods to core OpenHAB GroupItem type to make it more natural in
12
+ # Ruby
13
+ class GroupItem < GenericItem
23
14
  #
24
- # Create a new GroupMembers instance from a GroupItem
25
- #
26
- # @param [GroupItem] group_item GroupItem to use as trigger
15
+ # Class for indicating to triggers that a group trigger should be used
27
16
  #
28
- def initialize(group_item)
29
- @group = group_item
30
- end
31
-
32
- # explicit conversion to array
33
- def to_a
34
- OpenHAB::Core::EntityLookup.decorate_items(group.members.to_a)
35
- end
36
- end
17
+ class GroupMembers
18
+ include LazyArray
37
19
 
38
- #
39
- # Delegator to OpenHAB Group Item
40
- #
41
- class GroupItem
42
- extend OpenHAB::DSL::Items::ItemCommand
43
- extend OpenHAB::DSL::Items::ItemDelegate
44
- include Enumerable
45
- include Comparable
20
+ # @return [GroupItem]
21
+ attr_reader :group
46
22
 
47
- def_item_delegator :@group_item
48
-
49
- #
50
- # @return [Hash] A hash of lambdas with default filters for `all_members`
51
- #
52
- DEFAULT_FILTERS = {
53
- groups: ->(item) { item.is_a?(Java::OrgOpenhabCoreItems::GroupItem) },
54
- all: -> { true }
55
- }.freeze
23
+ # @!visibility private
24
+ def initialize(group_item)
25
+ @group = group_item
26
+ end
56
27
 
57
- private_constant :DEFAULT_FILTERS
28
+ # Explicit conversion to Array
29
+ #
30
+ # @return [Array]
31
+ def to_a
32
+ group.get_members.to_a
33
+ end
34
+ end
58
35
 
59
- #
60
- # Create a new GroupItem
61
- #
62
- # @param [Java::Org::openhab::core::items::GroupItem] group_item OpenHAB GroupItem to delegate to
63
- #
64
- def initialize(group_item)
65
- @group_item = group_item
36
+ include Enumerable
37
+ include ComparableItem
66
38
 
67
- item_missing_delegate { @group_item }
68
- item_missing_delegate { OpenHAB::Core::EntityLookup.decorate_item(base_item) }
69
- end
39
+ remove_method :==
70
40
 
71
41
  #
72
- # Create a GroupMembers object for use in triggers
42
+ # Get an Array-like object representing the members of the group
73
43
  #
74
- # @return [GroupMembers] A GroupMembers object
44
+ # @return [GroupMembers]
75
45
  #
76
46
  def members
77
- GroupMembers.new(@group_item)
47
+ GroupMembers.new(self)
78
48
  end
49
+
50
+ # @deprecated
79
51
  alias items members
80
52
 
81
53
  #
@@ -89,51 +61,43 @@ module OpenHAB
89
61
  # Get all members of the group recursively. Optionally filter the items to only return
90
62
  # Groups or regular Items
91
63
  #
92
- # @param [Symbol] filter Either :groups or :items
64
+ # @param [Symbol] filter Either +:groups+ or +:items+
93
65
  #
94
66
  # @return [Array] An Array containing all descendants of the Group, optionally filtered
95
67
  #
96
68
  def all_members(filter = nil, &block)
97
- predicate = DEFAULT_FILTERS[filter] || block
69
+ filter = nil if filter == :items
70
+ raise ArgumentError, 'filter must be :groups or :items' unless [:groups, nil].include?(filter)
98
71
 
99
- return OpenHAB::Core::EntityLookup.decorate_items(@group_item.all_members.to_a) unless predicate
72
+ block = ->(i) { i.is_a?(GroupItem) } if filter
100
73
 
101
- OpenHAB::Core::EntityLookup.decorate_items(@group_item.get_members(&predicate).to_a)
102
- end
103
-
104
- #
105
- # Test for equality
106
- #
107
- # @param [Object] other Other object to compare against
108
- #
109
- # @return [Boolean] true if self and other can be considered equal, false otherwise
110
- #
111
- def ==(other)
112
- if other.respond_to?(:java_class) && accepted_data_types.include?(other.java_class)
113
- get_state_as(other.class) == other
114
- elsif other.respond_to?(:state)
115
- base_item ? OpenHAB::Core::EntityLookup.decorate_item(base_item) == other.state : self == other.state
74
+ if block
75
+ get_members(&block).to_a
116
76
  else
117
- super
77
+ get_all_members.to_a
118
78
  end
119
79
  end
120
80
 
121
- #
122
- # Compare GroupItem to supplied object
123
- #
124
- # @param [Object] other object to compare to
125
- #
126
- # @return [Integer] -1,0,1 or nil depending on value supplied,
127
- # nil comparison to supplied object is not possible.
128
- #
129
- def <=>(other)
130
- if base_item
131
- OpenHAB::Core::EntityLookup.decorate_item(base_item) <=> other
132
- elsif state?
133
- -(other <=> state)
134
- else
135
- super
136
- 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)
137
101
  end
138
102
  end
139
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,52 +1,88 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
- require 'singleton'
3
+ require 'openhab/dsl/monkey_patch/events/item_command'
5
4
 
6
- require 'openhab/core/entity_lookup'
7
- require 'openhab/dsl/lazy_array'
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'
8
20
 
9
21
  module OpenHAB
10
22
  module DSL
11
- #
12
- # Manages OpenHAB items
13
- #
23
+ # Contains all OpenHAB *Item classes, as well as associated support
24
+ # modules
14
25
  module Items
15
- #
16
- # Delegates to underlying set of all OpenHAB Items, provides convenience methods
17
- #
18
- class Items
19
- include LazyArray
20
- include Singleton
21
-
22
- # Fetches the named item from the the ItemRegistry
23
- # @param [String] name
24
- # @return Item from registry, nil if item missing or requested item is a Group Type
25
- def [](name)
26
- OpenHAB::Core::EntityLookup.lookup_item(name)
27
- rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
28
- nil
26
+ class << self
27
+ private
28
+
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)
29
35
  end
30
36
 
31
- # Returns true if the given item name exists
32
- # @param name [String] Item name to check
33
- # @return [Boolean] true if the item exists, false otherwise
34
- def include?(name)
35
- !$ir.getItems(name).empty? # rubocop: disable Style/GlobalVars
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)
42
+
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
36
50
  end
37
- alias key? []
38
51
 
39
- # explicit conversion to array
40
- def to_a
41
- items = $ir.items.grep_v(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
42
- 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
43
73
  end
44
74
  end
45
75
 
46
- # Fetches all non-group items from the item registry
47
- # @return [OpenHAB::DSL::Items::Items]
48
- def items
49
- 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)
50
86
  end
51
87
  end
52
88
  end