openhab-scripting 2.16.2 → 2.16.3

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 (112) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab.rb +12 -16
  3. data/lib/openhab/core/entity_lookup.rb +162 -0
  4. data/lib/openhab/core/openhab_setup.rb +31 -0
  5. data/lib/openhab/core/osgi.rb +61 -0
  6. data/lib/openhab/dsl/actions.rb +105 -0
  7. data/lib/openhab/dsl/dsl.rb +47 -0
  8. data/lib/openhab/{core/dsl → dsl}/gems.rb +0 -1
  9. data/lib/openhab/dsl/group.rb +100 -0
  10. data/lib/openhab/dsl/items/items.rb +46 -0
  11. data/lib/openhab/dsl/items/number_item.rb +352 -0
  12. data/lib/openhab/dsl/items/string_item.rb +120 -0
  13. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
  14. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
  15. data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
  16. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
  17. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
  18. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
  19. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
  20. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
  21. data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
  22. data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
  23. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
  24. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
  25. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
  26. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
  27. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
  28. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +7 -0
  29. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
  30. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
  31. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
  32. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
  33. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
  34. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
  35. data/lib/openhab/dsl/monkey_patch/types/types.rb +8 -0
  36. data/lib/openhab/dsl/persistence.rb +25 -0
  37. data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
  38. data/lib/openhab/dsl/rules/guard.rb +134 -0
  39. data/lib/openhab/dsl/rules/property.rb +102 -0
  40. data/lib/openhab/dsl/rules/rule.rb +116 -0
  41. data/lib/openhab/dsl/rules/rule_config.rb +151 -0
  42. data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
  43. data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
  44. data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
  45. data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
  46. data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
  47. data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
  48. data/lib/openhab/dsl/states.rb +61 -0
  49. data/lib/openhab/dsl/things.rb +91 -0
  50. data/lib/openhab/dsl/time_of_day.rb +228 -0
  51. data/lib/openhab/dsl/timers.rb +77 -0
  52. data/lib/openhab/dsl/types/quantity.rb +290 -0
  53. data/lib/openhab/dsl/units.rb +39 -0
  54. data/lib/openhab/log/configuration.rb +21 -0
  55. data/lib/openhab/log/logger.rb +172 -0
  56. data/lib/openhab/version.rb +1 -1
  57. metadata +55 -58
  58. data/lib/openhab/configuration.rb +0 -16
  59. data/lib/openhab/core/cron.rb +0 -27
  60. data/lib/openhab/core/debug.rb +0 -34
  61. data/lib/openhab/core/dsl.rb +0 -51
  62. data/lib/openhab/core/dsl/actions.rb +0 -107
  63. data/lib/openhab/core/dsl/entities.rb +0 -147
  64. data/lib/openhab/core/dsl/group.rb +0 -102
  65. data/lib/openhab/core/dsl/items/items.rb +0 -51
  66. data/lib/openhab/core/dsl/items/number_item.rb +0 -323
  67. data/lib/openhab/core/dsl/items/string_item.rb +0 -122
  68. data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
  69. data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
  70. data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
  71. data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
  72. data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
  73. data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
  74. data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
  75. data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
  76. data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
  77. data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
  78. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
  79. data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
  80. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
  81. data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
  82. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
  83. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
  84. data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
  85. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
  86. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  87. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  88. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  89. data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
  90. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
  91. data/lib/openhab/core/dsl/persistence.rb +0 -27
  92. data/lib/openhab/core/dsl/property.rb +0 -96
  93. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -345
  94. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  95. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  96. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  97. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  98. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  99. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  100. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  101. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  102. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  103. data/lib/openhab/core/dsl/states.rb +0 -63
  104. data/lib/openhab/core/dsl/things.rb +0 -93
  105. data/lib/openhab/core/dsl/time_of_day.rb +0 -231
  106. data/lib/openhab/core/dsl/timers.rb +0 -79
  107. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  108. data/lib/openhab/core/dsl/units.rb +0 -41
  109. data/lib/openhab/core/log.rb +0 -170
  110. data/lib/openhab/core/patch_load_path.rb +0 -7
  111. data/lib/openhab/core/startup_delay.rb +0 -23
  112. data/lib/openhab/osgi.rb +0 -59
@@ -1,122 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bigdecimal'
4
- require 'forwardable'
5
- require 'java'
6
-
7
- module OpenHAB
8
- module Core
9
- module DSL
10
- module Items
11
- #
12
- # Delegator to OpenHAB String Item
13
- #
14
- class StringItem
15
- extend Forwardable
16
- include Comparable
17
-
18
- # @return [Regex] Regular expression matching blank strings
19
- BLANK_RE = /\A[[:space:]]*\z/.freeze
20
- private_constant :BLANK_RE
21
-
22
- def_delegator :@string_item, :to_s
23
-
24
- #
25
- # Create a new StringItem
26
- #
27
- # @param [Java::Org::openhab::core::library::items::StringItem] string_item OpenHAB string item to delegate to
28
- #
29
- def initialize(string_item)
30
- @string_item = string_item
31
- super()
32
- end
33
-
34
- #
35
- # Convert the StringItem into a String
36
- #
37
- # @return [String] String representation of the StringItem or
38
- # nil if underlying OpenHAB StringItem does not have a state
39
- #
40
- def to_str
41
- @string_item.state&.to_full_string&.to_s
42
- end
43
-
44
- #
45
- # Detect if the string is blank (not set or only whitespace)
46
- #
47
- # @return [Boolean] True if string item is not set or contains only whitespace, false otherwise
48
- #
49
- def blank?
50
- return true unless @string_item.state?
51
-
52
- @string_item.state.to_full_string.to_s.empty? || BLANK_RE.match?(self)
53
- end
54
-
55
- #
56
- # Check if StringItem is truthy? as per defined by library
57
- #
58
- # @return [Boolean] True if item is not in state UNDEF or NULL and value is not blank
59
- #
60
- def truthy?
61
- @string_item.state? && blank? == false
62
- end
63
-
64
- #
65
- # Compare StringItem to supplied object
66
- #
67
- # @param [Object] other object to compare to
68
- #
69
- # @return [Integer] -1,0,1 or nil depending on value supplied,
70
- # nil comparison to supplied object is not possible.
71
- #
72
- def <=>(other)
73
- case other
74
- when StringItem
75
- @string_item.state <=> other.state
76
- when String
77
- @string_item.state.to_s <=> other
78
- else
79
- @string_item.state <=> other
80
- end
81
- end
82
-
83
- #
84
- # Forward missing methods to Openhab String Item or String representation of the item if they are defined
85
- #
86
- # @param [String] meth method name
87
- # @param [Array] args arguments for method
88
- # @param [Proc] block <description>
89
- #
90
- # @return [Object] Value from delegated method in OpenHAB StringItem or Ruby String
91
- #
92
- def method_missing(meth, *args, &block)
93
- if @string_item.respond_to?(meth)
94
- @string_item.__send__(meth, *args, &block)
95
- elsif @string_item.state&.to_full_string&.to_s.respond_to?(meth)
96
- @string_item.state.to_full_string.to_s.__send__(meth, *args, &block)
97
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
98
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
99
- else
100
- super(meth, *args, &block)
101
- end
102
- end
103
-
104
- #
105
- # Checks if this method responds to the missing method
106
- #
107
- # @param [String] method_name Name of the method to check
108
- # @param [Boolean] _include_private boolean if private methods should be checked
109
- #
110
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
111
- #
112
- def respond_to_missing?(method_name, _include_private = false)
113
- @string_item.respond_to?(method_name) ||
114
- @string_item.state&.to_full_string&.to_s.respond_to?(method_name) ||
115
- ::Kernel.method_defined?(method_name) ||
116
- ::Kernel.private_method_defined?(method_name)
117
- end
118
- end
119
- end
120
- end
121
- end
122
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Monkey patch actions
4
- require 'openhab/core/dsl/monkey_patch/actions/script_thing_actions'
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
-
5
- #
6
- # MonkeyPatching ScriptThingActions
7
- #
8
- # rubocop:disable Style/ClassAndModuleChildren
9
- class Java::OrgOpenhabCoreAutomationModuleScriptInternalDefaultscope::ScriptThingActions
10
- # rubocop:enable Style/ClassAndModuleChildren
11
-
12
- field_reader :THING_ACTIONS_MAP
13
-
14
- #
15
- # Fetch keys for all actions defined in OpenHAB
16
- #
17
- # @return [Set] of keys for defined actions in the form of 'scope-thing_uid'
18
- #
19
- def action_keys
20
- Java::OrgOpenhabCoreAutomationModuleScriptInternalDefaultscope::ScriptThingActions.THING_ACTIONS_MAP.keys
21
- end
22
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'core/dsl/monkey_patch/events/item_state_changed'
4
- require 'core/dsl/monkey_patch/events/thing_status_info'
5
- require 'core/dsl/monkey_patch/events/item_command'
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
-
5
- #
6
- # Monkey patch with ruby style accesors
7
- #
8
- # rubocop:disable Style/ClassAndModuleChildren
9
- class Java::OrgOpenhabCoreItemsEvents::ItemCommandEvent
10
- # rubocop:enable Style/ClassAndModuleChildren
11
-
12
- alias command item_command
13
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
-
5
- #
6
- # MonkeyPatch with ruby style accessors
7
- #
8
- # rubocop:disable Style/ClassAndModuleChildren
9
- class Java::OrgOpenhabCoreItemsEvents::ItemStateChangedEvent
10
- # rubocop:enable Style/ClassAndModuleChildren
11
-
12
- #
13
- # Get the item that caused the state change
14
- #
15
- # @return [Item] Item that caused state change
16
- #
17
- def item
18
- # rubocop:disable Style/GlobalVars
19
- $ir.get(item_name)
20
- # rubocop:enable Style/GlobalVars
21
- end
22
-
23
- alias state item_state
24
- alias last old_item_state
25
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
-
5
- #
6
- # Monkey patch with ruby style accessors
7
- #
8
- # rubocop:disable Style/ClassAndModuleChildren
9
- class Java::OrgOpenhabCoreThingEvents::ThingStatusInfoChangedEvent
10
- # rubocop:enable Style/ClassAndModuleChildren
11
-
12
- alias uid get_thing_uid
13
- alias last get_old_status_info
14
- alias status status_info
15
- end
16
-
17
- #
18
- # Monkey patch with ruby style accessors
19
- #
20
- # rubocop:disable Style/ClassAndModuleChildren
21
- class Java::OrgOpenhabCoreThingEvents::ThingStatusInfoEvent
22
- # rubocop:enable Style/ClassAndModuleChildren
23
-
24
- alias uid get_thing_uid
25
- alias status status_info
26
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
-
5
- # Alias for is_a? testing
6
- java_import org.openhab.core.library.items.ContactItem
7
-
8
- #
9
- # Alias class for ContactItem
10
- #
11
- Contact = ContactItem
12
-
13
- #
14
- # Monkey patch Contact Item with Ruby methods
15
- #
16
- # rubocop:disable Style/ClassAndModuleChildren
17
- class Java::OrgOpenhabCoreLibraryItems::ContactItem
18
- java_import org.openhab.core.library.types.OpenClosedType
19
- # rubocop:enable Style/ClassAndModuleChildren
20
-
21
- #
22
- # Check if the contact is open
23
- #
24
- # @return [Boolean] True if contact has state and is open, false otherwise
25
- #
26
- def open?
27
- state? && state == OpenClosedType::OPEN
28
- end
29
-
30
- #
31
- # Check if the contact is closed
32
- #
33
- # @return [Boolean] True if contact has state and is closed, false otherwise
34
- #
35
- def closed?
36
- state? && state == OpenClosedType::CLOSED
37
- end
38
-
39
- #
40
- # Compares contacts to OpenClosedTypes
41
- #
42
- # @param [Object] other object to compare to
43
- #
44
- # @return [Boolean] True if contact has a state and state equals other, nil if no state,
45
- # result from super if not supplied an OpenClosedType
46
- #
47
- def ==(other)
48
- if other.is_a? OpenClosedType
49
- state? && state == other
50
- else
51
- super
52
- end
53
- end
54
- end
@@ -1,182 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
-
5
- java_import org.openhab.core.library.items.DimmerItem
6
-
7
- #
8
- # Alias class for is_a? testing
9
- #
10
- Dimmer = DimmerItem
11
-
12
- #
13
- # Monkey Patch DimmerItem
14
- #
15
- # rubocop:disable Style/ClassAndModuleChildren
16
- class Java::OrgOpenhabCoreLibraryItems::DimmerItem
17
- include Comparable
18
- # rubocop:enable Style/ClassAndModuleChildren
19
- java_import org.openhab.core.library.types.DecimalType
20
- java_import org.openhab.core.library.types.IncreaseDecreaseType
21
-
22
- #
23
- # Add the current dimmer value to the supplied object
24
- #
25
- # @param [Object] other object to add the dimmer value to
26
- #
27
- # @return [Integer] Current dimmer value plus value of supplied object
28
- #
29
- def +(other)
30
- return unless state?
31
-
32
- state.to_big_decimal.intValue + other
33
- end
34
-
35
- #
36
- # Subtract the supplied object from the current value of the dimmer
37
- #
38
- # @param [Object] other object to subtract from the dimmer value
39
- #
40
- # @return [Integer] Current dimmer value minus value of supplied object
41
- #
42
- def -(other)
43
- return unless state?
44
-
45
- state.to_big_decimal.intValue - other
46
- end
47
-
48
- #
49
- # Dim the dimmer
50
- #
51
- # @param [Integer] amount to dim by
52
- #
53
- # @return [Integer] level target for dimmer
54
- #
55
- def dim(amount = 1)
56
- return unless state?
57
-
58
- target = [state.to_big_decimal.intValue - amount, 0].max
59
-
60
- if amount == 1
61
- command(IncreaseDecreaseType::DECREASE)
62
- else
63
- command(target)
64
- end
65
-
66
- target
67
- end
68
-
69
- #
70
- # Brighten the dimmer
71
- #
72
- # @param [Integer] amount to brighten by
73
- #
74
- # @return [Integer] level target for dimmer
75
- #
76
- def brighten(amount = 1)
77
- return unless state?
78
-
79
- target = state.to_big_decimal.intValue + amount
80
-
81
- if amount == 1
82
- command(IncreaseDecreaseType::INCREASE)
83
- else
84
- command(target)
85
- end
86
- target
87
- end
88
-
89
- #
90
- # Compare DimmerItem to supplied object
91
- #
92
- # @param [Object] other object to compare to
93
- #
94
- # @return [Integer] -1,0,1 or nil depending on value supplied, nil comparison to supplied object is not possible.
95
- #
96
- def <=>(other)
97
- logger.trace("Comparing #{self} to #{other}")
98
- case other
99
- when Java::OrgOpenhabCoreItems::GenericItem, NumberItem then state <=> other.state
100
- when DecimalType then state <=> other
101
- when Numeric then state.to_big_decimal.to_d <=> other.to_d
102
- else compare_to(other)
103
- end
104
- end
105
-
106
- #
107
- # Coerce objects into a DimmerItem
108
- #
109
- # @param [Object] other object to coerce to a DimmerItem if possible
110
- #
111
- # @return [Object] Numeric when applicable
112
- #
113
- def coerce(other)
114
- logger.trace("Coercing #{self} as a request from #{other.class}")
115
- case other
116
- when Numeric
117
- [other, state.to_big_decimal.to_d]
118
- else
119
- [other, state]
120
- end
121
- end
122
-
123
- #
124
- # Compare DimmerItem to supplied object.
125
- # The == operator needs to be overridden because the parent java object
126
- # has .equals which overrides the <=> operator above
127
- #
128
- # @param [Object] other object to compare to
129
- #
130
- # @return [Integer] true if the two objects contain the same value, false otherwise
131
- #
132
- def ==(other)
133
- (self <=> other).zero?
134
- end
135
-
136
- #
137
- # Check if dimmer has a state and state is not zero
138
- #
139
- # @return [Boolean] True if dimmer is not NULL or UNDEF and value is not 0
140
- #
141
- def truthy?
142
- state? && state != DecimalType::ZERO
143
- end
144
-
145
- #
146
- # Value of dimmer
147
- #
148
- # @return [Integer] Value of dimmer or nil if state is UNDEF or NULL
149
- #
150
- def to_i
151
- state&.to_big_decimal&.intValue
152
- end
153
-
154
- alias to_int to_i
155
-
156
- #
157
- # Return the string representation of the dimmer item
158
- #
159
- # @return [String] String version of the dimmer value
160
- #
161
- def to_s
162
- to_i.to_s
163
- end
164
-
165
- #
166
- # Check if dimmer is on
167
- #
168
- # @return [Boolean] True if item is not UNDEF or NULL and has a value greater than 0
169
- #
170
- def on?
171
- state&.to_big_decimal&.intValue&.positive?
172
- end
173
-
174
- #
175
- # Check if dimmer is off
176
- #
177
- # @return [Boolean] True if item is not UNDEF or NULL and has a state of 0
178
- #
179
- def off?
180
- state&.to_big_decimal&.intValue&.zero?
181
- end
182
- end