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
data/lib/openhab.rb CHANGED
@@ -4,7 +4,6 @@ require 'openhab/core/load_path'
4
4
  require 'openhab/core/openhab_setup'
5
5
  require 'openhab/log/logger'
6
6
  require 'openhab/dsl/dsl'
7
-
8
7
  require 'openhab/version'
9
8
 
10
9
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.4
4
+ version: 4.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-11 00:00:00.000000000 Z
11
+ date: 2021-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,16 +54,26 @@ files:
54
54
  - lib/openhab/dsl/dsl.rb
55
55
  - lib/openhab/dsl/gems.rb
56
56
  - lib/openhab/dsl/group.rb
57
- - lib/openhab/dsl/items/datetime_item.rb
57
+ - lib/openhab/dsl/items/color_item.rb
58
+ - lib/openhab/dsl/items/comparable_item.rb
59
+ - lib/openhab/dsl/items/contact_item.rb
60
+ - lib/openhab/dsl/items/date_time_item.rb
61
+ - lib/openhab/dsl/items/dimmer_item.rb
62
+ - lib/openhab/dsl/items/ensure.rb
63
+ - lib/openhab/dsl/items/generic_item.rb
58
64
  - lib/openhab/dsl/items/group_item.rb
59
65
  - lib/openhab/dsl/items/image_item.rb
60
- - lib/openhab/dsl/items/item_command.rb
61
- - lib/openhab/dsl/items/item_delegate.rb
66
+ - lib/openhab/dsl/items/item_equality.rb
67
+ - lib/openhab/dsl/items/item_registry.rb
62
68
  - lib/openhab/dsl/items/items.rb
69
+ - lib/openhab/dsl/items/metadata.rb
63
70
  - lib/openhab/dsl/items/number_item.rb
71
+ - lib/openhab/dsl/items/numeric_item.rb
72
+ - lib/openhab/dsl/items/persistence.rb
64
73
  - lib/openhab/dsl/items/player_item.rb
65
74
  - lib/openhab/dsl/items/rollershutter_item.rb
66
75
  - lib/openhab/dsl/items/string_item.rb
76
+ - lib/openhab/dsl/items/switch_item.rb
67
77
  - lib/openhab/dsl/lazy_array.rb
68
78
  - lib/openhab/dsl/monkey_patch/actions/actions.rb
69
79
  - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
@@ -73,36 +83,16 @@ files:
73
83
  - lib/openhab/dsl/monkey_patch/events/item_state.rb
74
84
  - lib/openhab/dsl/monkey_patch/events/item_state_changed.rb
75
85
  - lib/openhab/dsl/monkey_patch/events/thing_status_info.rb
76
- - lib/openhab/dsl/monkey_patch/items/contact_item.rb
77
- - lib/openhab/dsl/monkey_patch/items/dimmer_item.rb
78
- - lib/openhab/dsl/monkey_patch/items/items.rb
79
- - lib/openhab/dsl/monkey_patch/items/metadata.rb
80
- - lib/openhab/dsl/monkey_patch/items/persistence.rb
81
- - lib/openhab/dsl/monkey_patch/items/switch_item.rb
82
86
  - lib/openhab/dsl/monkey_patch/ruby/number.rb
83
- - lib/openhab/dsl/monkey_patch/ruby/range.rb
84
87
  - lib/openhab/dsl/monkey_patch/ruby/ruby.rb
85
88
  - lib/openhab/dsl/monkey_patch/ruby/string.rb
86
- - lib/openhab/dsl/monkey_patch/ruby/time.rb
87
- - lib/openhab/dsl/monkey_patch/types/decimal_type.rb
88
- - lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb
89
- - lib/openhab/dsl/monkey_patch/types/next_previous_type.rb
90
- - lib/openhab/dsl/monkey_patch/types/on_off_type.rb
91
- - lib/openhab/dsl/monkey_patch/types/open_closed_type.rb
92
- - lib/openhab/dsl/monkey_patch/types/percent_type.rb
93
- - lib/openhab/dsl/monkey_patch/types/play_pause_type.rb
94
- - lib/openhab/dsl/monkey_patch/types/quantity_type.rb
95
- - lib/openhab/dsl/monkey_patch/types/refresh_type.rb
96
- - lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb
97
- - lib/openhab/dsl/monkey_patch/types/stop_move_type.rb
98
- - lib/openhab/dsl/monkey_patch/types/types.rb
99
- - lib/openhab/dsl/monkey_patch/types/up_down_type.rb
100
89
  - lib/openhab/dsl/persistence.rb
101
90
  - lib/openhab/dsl/rules/automation_rule.rb
102
91
  - lib/openhab/dsl/rules/guard.rb
103
92
  - lib/openhab/dsl/rules/property.rb
104
93
  - lib/openhab/dsl/rules/rule.rb
105
94
  - lib/openhab/dsl/rules/rule_config.rb
95
+ - lib/openhab/dsl/rules/terse.rb
106
96
  - lib/openhab/dsl/rules/triggers/changed.rb
107
97
  - lib/openhab/dsl/rules/triggers/channel.rb
108
98
  - lib/openhab/dsl/rules/triggers/command.rb
@@ -113,8 +103,26 @@ files:
113
103
  - lib/openhab/dsl/things.rb
114
104
  - lib/openhab/dsl/time_of_day.rb
115
105
  - lib/openhab/dsl/timers.rb
116
- - lib/openhab/dsl/types/datetime.rb
117
- - lib/openhab/dsl/types/quantity.rb
106
+ - lib/openhab/dsl/types/comparable_type.rb
107
+ - lib/openhab/dsl/types/date_time_type.rb
108
+ - lib/openhab/dsl/types/decimal_type.rb
109
+ - lib/openhab/dsl/types/hsb_type.rb
110
+ - lib/openhab/dsl/types/increase_decrease_type.rb
111
+ - lib/openhab/dsl/types/next_previous_type.rb
112
+ - lib/openhab/dsl/types/numeric_type.rb
113
+ - lib/openhab/dsl/types/on_off_type.rb
114
+ - lib/openhab/dsl/types/open_closed_type.rb
115
+ - lib/openhab/dsl/types/percent_type.rb
116
+ - lib/openhab/dsl/types/play_pause_type.rb
117
+ - lib/openhab/dsl/types/quantity_type.rb
118
+ - lib/openhab/dsl/types/refresh_type.rb
119
+ - lib/openhab/dsl/types/rewind_fastforward_type.rb
120
+ - lib/openhab/dsl/types/stop_move_type.rb
121
+ - lib/openhab/dsl/types/string_type.rb
122
+ - lib/openhab/dsl/types/type.rb
123
+ - lib/openhab/dsl/types/types.rb
124
+ - lib/openhab/dsl/types/un_def_type.rb
125
+ - lib/openhab/dsl/types/up_down_type.rb
118
126
  - lib/openhab/dsl/units.rb
119
127
  - lib/openhab/log/configuration.rb
120
128
  - lib/openhab/log/logger.rb
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
- require 'java'
5
- require 'time'
6
- require 'openhab/dsl/types/datetime'
7
- require 'openhab/dsl/items/item_delegate'
8
- require 'openhab/dsl/items/item_command'
9
-
10
- module OpenHAB
11
- module DSL
12
- module Items
13
- #
14
- # Delegation to OpenHAB DateTime Item
15
- #
16
- # @author Anders Alfredsson
17
- #
18
- class DateTimeItem
19
- extend Forwardable
20
- extend OpenHAB::DSL::Items::ItemDelegate
21
- extend OpenHAB::DSL::Items::ItemCommand
22
- include Comparable
23
-
24
- def_item_delegator :@datetime_item
25
-
26
- item_type Java::OrgOpenhabCoreLibraryItems::DateTimeItem
27
-
28
- #
29
- # Create a new DateTimeItem
30
- #
31
- # @param [Java::org::openhab::core::libarary::items::DateTimeItem] datetime_item Openhab DateTimeItem to
32
- # delegate to
33
- #
34
- def initialize(datetime_item)
35
- @datetime_item = datetime_item
36
- item_missing_delegate { @datetime_item }
37
- item_missing_delegate { to_dt }
38
- end
39
-
40
- #
41
- # Return an instance of DateTime that wraps the DateTimeItem's state
42
- #
43
- # @return [OpenHAB::DSL::Types::DateTime] Wrapper for the Item's state, or nil if it has no state
44
- #
45
- def to_dt
46
- OpenHAB::DSL::Types::DateTime.new(@datetime_item.state) if state?
47
- end
48
-
49
- #
50
- # Compare the Item's state to another Item or object that can be compared
51
- #
52
- # @param [Object] other Other objet to compare against
53
- #
54
- # @return [Integer] -1, 0 or 1 depending on the result of the comparison
55
- #
56
- def <=>(other)
57
- return unless state?
58
-
59
- logger.trace("Comparing self (#{self.class}) to #{other} (#{other.class})")
60
- other = other.to_dt if other.is_a? DateTimeItem
61
- to_dt <=> other
62
- end
63
-
64
- #
65
- # Get the time zone of the Item
66
- #
67
- # @return [String] The timezone in `[+-]hh:mm(:ss)` format or nil if the Item has no state
68
- #
69
- def zone
70
- to_dt.zone if state?
71
- end
72
- end
73
- end
74
- end
75
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/log/logger'
5
- require 'openhab/dsl/monkey_patch/events/item_command'
6
-
7
- module OpenHAB
8
- module DSL
9
- module Items
10
- #
11
- # Holds methods to automatically generate commands and
12
- # accessors for items
13
- module ItemCommand
14
- include OpenHAB::Log
15
-
16
- #
17
- # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
18
- # string representation of the enum value For example, an enum with values of STOP and START
19
- # would create methods stop() and start() that send the corresponding STOP and START commands to the item
20
- #
21
- # @param [Java::JavaLang::Enum] command_enum Enumeration to create commands for
22
- # @param [Hash] optional hash in which if a generated method name mactches a key, the value of that key
23
- # will be used as the method name instead, for example `:play? => :playing?`
24
- #
25
- #
26
- def item_command(command_enum, methods = {})
27
- # rubocop:disable Style/HashEachMethods
28
- # Disable rule because Java enum does not support each_value
29
- command_enum.values.each do |command|
30
- command_method = command.to_s.downcase
31
- command_method = methods.transform_keys(&:to_sym).fetch(command_method.to_sym, command_method)
32
- logger.trace("Creating command method (#{command_method}) for #{self}")
33
- define_method(command_method) do
34
- self.command(command)
35
- end
36
- end
37
- # rubocop:enable Style/HashEachMethods
38
- end
39
-
40
- #
41
- # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
42
- # string representation appended with a question mark '?' of the enum value For example,
43
- # an enum with values of UP and DOWN would create methods up? and down? that check
44
- # if the current state matches the value of the enum
45
- #
46
- # @param [Java::JavaLang::Enum] command_enum Enumeration to create methods for each value
47
- # to check if current state matches that enum
48
- # @param [Hash] optional hash in which if a generated method name mactches a key, the value of that key
49
- # will be used as the method name instead, for example `:play? => :playing?`
50
- #
51
- #
52
- def item_state(command_enum, methods = {})
53
- # rubocop:disable Style/HashEachMethods
54
- # Disable rule because Java enum does not support each_value
55
- command_enum.values.each do |command|
56
- status_method = "#{command.to_s.downcase}?"
57
- status_method = methods.transform_keys(&:to_sym).fetch(status_method.to_sym, status_method)
58
- logger.trace("Creating status method (#{status_method}) for #{self}")
59
- define_method(status_method) do
60
- state? && state.as(command_enum) == command
61
- end
62
- end
63
- # rubocop:enable Style/HashEachMethods
64
- end
65
-
66
- #
67
- # Extract the accepted state and command types from the specified OpenHAB
68
- # Item class and pass them to item_state/item_command
69
- #
70
- # @param [Java::JavaLang::Class] item_class a Class that implements Java::OrgOpenhabCoreItems::Item
71
- # @param [Hash] optional hash in which if a generated method name mactches a key, the value of that key
72
- # will be used as the method name instead, for example `:play? => :playing?`
73
- #
74
- def item_type(item_class, methods = {}) # rubocop:disable Metrics/MethodLength
75
- item_class.field_reader(:ACCEPTED_DATA_TYPES)
76
- item_class.field_reader(:ACCEPTED_COMMAND_TYPES)
77
- item_class.ACCEPTED_DATA_TYPES.select(&:is_enum)
78
- .grep_v(UnDefType)
79
- .each { |type| item_state(type.ruby_class, methods) }
80
- item_class.ACCEPTED_COMMAND_TYPES.select(&:is_enum)
81
- .grep_v(UnDefType)
82
- .each do |type|
83
- item_command(type.ruby_class, methods)
84
- MonkeyPatch::Events::ItemCommandEvent.def_enum_predicates(type.ruby_class)
85
- end
86
- end
87
- end
88
- end
89
- end
90
- end
@@ -1,125 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/log/logger'
5
- require 'forwardable'
6
-
7
- module OpenHAB
8
- module DSL
9
- module Items
10
- #
11
- # Holds methods to delegate to items
12
- #
13
- module ItemDelegate
14
- include OpenHAB::Log
15
-
16
- # @return [Array<Symbol>] Array of methods that by default will be forwarded to an OpenHAB Item.
17
- DEFAULT_DELEGATION_METHODS = %i[to_s groups].freeze
18
- private_constant :DEFAULT_DELEGATION_METHODS
19
-
20
- #
21
- # Extends calling object with DSL and helper methods
22
- #
23
- # @param [Object] base Object to decorate with DSL and helper methods
24
- #
25
- #
26
- def self.extended(base)
27
- base.extend Forwardable
28
- base.include OpenHAB::DSL::Items::ItemDelegate
29
- end
30
-
31
- #
32
- # Delegate a set of methods to the supplied delegator
33
- #
34
- # @param [Java::OrgOpenhabCoreItems::GenericItem] item to delegate methods to
35
- # @param [Symbol] methods to delegate, if not supplied default methods are used
36
- #
37
- #
38
- def def_item_delegator(item, *methods)
39
- methods = DEFAULT_DELEGATION_METHODS if methods.size.zero?
40
- methods.each do |method|
41
- logger.trace("Creating delegate for (#{method}) to #{item}) for #{self.class}")
42
- def_delegator item, method
43
- end
44
- define_method(:oh_item) { instance_variable_get(item) }
45
- define_method(:hash) { oh_item.hash_code }
46
- define_method(:eql?) { |other| hash == other.hash }
47
- end
48
-
49
- #
50
- # Delegates methods to the object returned from the supplied block if
51
- # they don't exist in the object this is included in. If the supplied block returns nil, no delegation occurs
52
- # If this item is called more than once delegation occurs in the order of invocation, i.e. the object returned
53
- # by the first block is delegated to if it responds to the missing method,
54
- # then the second block is processed, etc.
55
- #
56
- # @param [Proc] &delegate delgegate block
57
- #
58
- #
59
- def item_missing_delegate(&delegate)
60
- @delegates ||= []
61
- @delegates << delegate
62
- end
63
-
64
- #
65
- # Delegate missing method calls to delegates supplied to item_delgate method
66
- # if no delegates exist or respond to missing method, super is invoked which will
67
- # throw the appropriate method missing error
68
- #
69
- # @param [String] meth misisng method
70
- # @param [Array] *args Arguments to the missing method
71
- # @param [Proc] &block supplied to the missing method
72
- #
73
- # @return [Object] Result of missing method invocation
74
- #
75
- def method_missing(meth, *args, &block)
76
- logger.trace("Method (#{meth}) missing for item #{self.class}")
77
- delegate = delegate_for(meth)
78
- if delegate
79
- logger.trace("Delegating #{meth} to #{delegate.class}")
80
- delegate.__send__(meth, *args, &block)
81
- else
82
- super
83
- end
84
- end
85
-
86
- #
87
- # Checks if any of the supplied delgates respond to a specific method
88
- #
89
- # @param [String] meth method to check for
90
- # @param [Boolean] _include_private if private methods should be checked
91
- #
92
- # @return [Boolean] True if any delegates respond to method, false otherwise
93
- #
94
- def respond_to_missing?(meth, _include_private = false)
95
- logger.trace("Checking if (#{self.class}) responds to (#{meth})")
96
- responds = !delegate_for(meth).nil?
97
- logger.trace("(#{self.class}) responds to (#{meth}) (#{responds})")
98
- responds
99
- end
100
-
101
- private
102
-
103
- #
104
- # Find a delegate for the supplied method
105
- #
106
- # @param [String] meth method to find delegate for
107
- #
108
- # @return [Boolean] True if any method responds to the supplied delegate, false otherwise
109
- #
110
- def delegate_for(meth)
111
- (@delegates || []).each do |delegate_block|
112
- delegate = delegate_block.call(meth)
113
- logger.trace("Checking if delegate (#{delegate.class}) responds to (#{meth})")
114
- if delegate.respond_to? meth
115
- logger.trace("Delegate (#{delegate.class}) found for method (#{meth})")
116
- return delegate
117
- end
118
- end
119
- logger.trace("No delegate found for method (#{meth})")
120
- nil
121
- end
122
- end
123
- end
124
- end
125
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/dsl/items/item_command'
5
-
6
- module OpenHAB
7
- module DSL
8
- module MonkeyPatch
9
- #
10
- # Patches OpenHAB items
11
- #
12
- module Items
13
- java_import Java::OrgOpenhabCoreLibraryItems::ContactItem
14
-
15
- #
16
- # Alias class for ContactItem
17
- #
18
- ::Contact = ContactItem
19
-
20
- #
21
- # Monkey patch Contact Item with Ruby methods
22
- #
23
- class ContactItem
24
- extend OpenHAB::DSL::Items::ItemCommand
25
-
26
- java_import Java::OrgOpenhabCoreLibraryTypes::OpenClosedType
27
-
28
- item_type Java::OrgOpenhabCoreLibraryItems::ContactItem
29
-
30
- #
31
- # Compares contacts to OpenClosedTypes
32
- #
33
- # @param [Object] other object to compare to
34
- #
35
- # @return [Boolean] True if contact has a state and state equals other, nil if no state,
36
- # result from super if not supplied an OpenClosedType
37
- #
38
- def ==(other)
39
- other = other.get_state_as(OpenClosedType) if other.respond_to?(:get_state_as)
40
-
41
- if other.is_a? OpenClosedType
42
- state? && state == other
43
- else
44
- super
45
- end
46
- end
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,140 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/dsl/items/item_command'
5
- require 'forwardable'
6
-
7
- module OpenHAB
8
- module DSL
9
- module MonkeyPatch
10
- #
11
- # Patches OpenHAB items
12
- #
13
- module Items
14
- java_import Java::OrgOpenhabCoreLibraryItems::DimmerItem
15
- java_import Java::OrgOpenhabCoreItems::GenericItem
16
-
17
- #
18
- # Alias class for is_a? testing
19
- #
20
- ::Dimmer = DimmerItem
21
-
22
- #
23
- # Monkey Patch DimmerItem
24
- #
25
- class DimmerItem
26
- include Comparable
27
- java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
28
- java_import Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
29
-
30
- extend Forwardable
31
- extend OpenHAB::DSL::Items::ItemCommand
32
-
33
- item_type Java::OrgOpenhabCoreLibraryItems::DimmerItem
34
-
35
- def_delegator :state, :to_s
36
-
37
- #
38
- # Dim the dimmer
39
- #
40
- # @param [Integer] amount to dim by
41
- #
42
- # @return [Integer] level target for dimmer
43
- #
44
- def dim(amount = 1)
45
- [state&.to_big_decimal&.intValue&.-(amount), 0].compact
46
- .max
47
- .tap { |target| command(target) }
48
- end
49
-
50
- #
51
- # Brighten the dimmer
52
- #
53
- # @param [Integer] amount to brighten by
54
- #
55
- # @return [Integer] level target for dimmer
56
- #
57
- def brighten(amount = 1)
58
- state&.to_big_decimal&.intValue&.+(amount)&.tap { |target| command(target) }
59
- end
60
-
61
- #
62
- # Compare DimmerItem to supplied object
63
- #
64
- # @param [Object] other object to compare to
65
- #
66
- # @return [Integer] -1,0,1 or nil depending on value supplied,
67
- # nil comparison to supplied object is not possible.
68
- #
69
- def <=>(other)
70
- logger.trace("Comparing #{self} to #{other}")
71
- case other
72
- when GenericItem, NumberItem then state <=> other.state
73
- when DecimalType then state <=> other
74
- when Numeric then state.to_big_decimal.to_d <=> other.to_d
75
- else compare_to(other)
76
- end
77
- end
78
-
79
- #
80
- # Coerce objects into a DimmerItem
81
- #
82
- # @param [Object] other object to coerce to a DimmerItem if possible
83
- #
84
- # @return [Object] Numeric when applicable
85
- #
86
- def coerce(other)
87
- logger.trace("Coercing #{self} as a request from #{other.class}")
88
- case other
89
- when Numeric then [other, state.to_big_decimal.to_d]
90
- else [other, state]
91
- end
92
- end
93
-
94
- #
95
- # Compare DimmerItem to supplied object.
96
- # The == operator needs to be overridden because the parent java object
97
- # has .equals which overrides the <=> operator above
98
- #
99
- # @param [Object] other object to compare to
100
- #
101
- # @return [Integer] true if the two objects contain the same value, false otherwise
102
- #
103
- def ==(other)
104
- (self <=> other).zero?
105
- end
106
-
107
- #
108
- # Define math operations
109
- #
110
- %i[+ - / *].each do |operator|
111
- define_method(operator) do |other|
112
- right, left = coerce(other)
113
- left.send(operator, right)
114
- end
115
- end
116
-
117
- #
118
- # Check if dimmer has a state and state is not zero
119
- #
120
- # @return [Boolean] True if dimmer is not NULL or UNDEF and value is not 0
121
- #
122
- def truthy?
123
- state? && state != DecimalType::ZERO
124
- end
125
-
126
- #
127
- # Value of dimmer
128
- #
129
- # @return [Integer] Value of dimmer or nil if state is UNDEF or NULL
130
- #
131
- def to_i
132
- state&.to_big_decimal&.intValue
133
- end
134
-
135
- alias to_int to_i
136
- end
137
- end
138
- end
139
- end
140
- end