openhab-scripting 2.15.0 → 2.16.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) 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 -56
  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 -140
  64. data/lib/openhab/core/dsl/group.rb +0 -93
  65. data/lib/openhab/core/dsl/items/items.rb +0 -51
  66. data/lib/openhab/core/dsl/items/number_item.rb +0 -318
  67. data/lib/openhab/core/dsl/items/string_item.rb +0 -120
  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 -167
  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 -6
  84. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -24
  85. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  86. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  87. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  88. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -7
  89. data/lib/openhab/core/dsl/persistence.rb +0 -27
  90. data/lib/openhab/core/dsl/property.rb +0 -96
  91. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -348
  92. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  93. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  94. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  95. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  96. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  97. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  98. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  99. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  100. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  101. data/lib/openhab/core/dsl/states.rb +0 -63
  102. data/lib/openhab/core/dsl/things.rb +0 -93
  103. data/lib/openhab/core/dsl/time_of_day.rb +0 -229
  104. data/lib/openhab/core/dsl/timers.rb +0 -79
  105. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  106. data/lib/openhab/core/dsl/units.rb +0 -41
  107. data/lib/openhab/core/log.rb +0 -170
  108. data/lib/openhab/core/patch_load_path.rb +0 -7
  109. data/lib/openhab/core/startup_delay.rb +0 -23
  110. data/lib/openhab/osgi.rb +0 -59
@@ -1,120 +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
- end
79
- end
80
-
81
- #
82
- # Forward missing methods to Openhab String Item or String representation of the item if they are defined
83
- #
84
- # @param [String] meth method name
85
- # @param [Array] args arguments for method
86
- # @param [Proc] block <description>
87
- #
88
- # @return [Object] Value from delegated method in OpenHAB StringItem or Ruby String
89
- #
90
- def method_missing(meth, *args, &block)
91
- if @string_item.respond_to?(meth)
92
- @string_item.__send__(meth, *args, &block)
93
- elsif @string_item.state&.to_full_string&.to_s.respond_to?(meth)
94
- @string_item.state.to_full_string.to_s.__send__(meth, *args, &block)
95
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
96
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
97
- else
98
- super(meth, *args, &block)
99
- end
100
- end
101
-
102
- #
103
- # Checks if this method responds to the missing method
104
- #
105
- # @param [String] method_name Name of the method to check
106
- # @param [Boolean] _include_private boolean if private methods should be checked
107
- #
108
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
109
- #
110
- def respond_to_missing?(method_name, _include_private = false)
111
- @string_item.respond_to?(method_name) ||
112
- @string_item.state&.to_full_string&.to_s.respond_to?(method_name) ||
113
- ::Kernel.method_defined?(method_name) ||
114
- ::Kernel.private_method_defined?(method_name)
115
- end
116
- end
117
- end
118
- end
119
- end
120
- 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,167 +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 DimmerItem, NumberItem
100
- state <=> other.state
101
- when DecimalType
102
- state <=> other
103
- else
104
- to_i <=> other.to_i
105
- end
106
- end
107
-
108
- #
109
- # Compare DimmerItem to supplied object.
110
- # The == operator needs to be overridden because the parent java object
111
- # has .equals which overrides the <=> operator above
112
- #
113
- # @param [Object] other object to compare to
114
- #
115
- # @return [Integer] true if the two objects contain the same value, false otherwise
116
- #
117
- def ==(other)
118
- (self <=> other).zero?
119
- end
120
-
121
- #
122
- # Check if dimmer has a state and state is not zero
123
- #
124
- # @return [Boolean] True if dimmer is not NULL or UNDEF and value is not 0
125
- #
126
- def truthy?
127
- state? && state != DecimalType::ZERO
128
- end
129
-
130
- #
131
- # Value of dimmer
132
- #
133
- # @return [Integer] Value of dimmer or nil if state is UNDEF or NULL
134
- #
135
- def to_i
136
- state&.to_big_decimal&.intValue
137
- end
138
-
139
- alias to_int to_i
140
-
141
- #
142
- # Return the string representation of the dimmer item
143
- #
144
- # @return [String] String version of the dimmer value
145
- #
146
- def to_s
147
- to_i.to_s
148
- end
149
-
150
- #
151
- # Check if dimmer is on
152
- #
153
- # @return [Boolean] True if item is not UNDEF or NULL and has a value greater than 0
154
- #
155
- def on?
156
- state&.to_big_decimal&.intValue&.positive?
157
- end
158
-
159
- #
160
- # Check if dimmer is off
161
- #
162
- # @return [Boolean] True if item is not UNDEF or NULL and has a state of 0
163
- #
164
- def off?
165
- state&.to_big_decimal&.intValue&.zero?
166
- end
167
- end