openhab-scripting 2.16.2 → 2.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab.rb +12 -16
  3. data/lib/openhab/core/entity_lookup.rb +173 -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 +49 -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/datetime_item.rb +97 -0
  11. data/lib/openhab/dsl/items/items.rb +46 -0
  12. data/lib/openhab/dsl/items/number_item.rb +352 -0
  13. data/lib/openhab/dsl/items/rollershutter_item.rb +179 -0
  14. data/lib/openhab/dsl/items/string_item.rb +120 -0
  15. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
  16. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
  17. data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
  18. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
  19. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
  20. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
  21. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
  22. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
  23. data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
  24. data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
  25. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
  26. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
  27. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
  28. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
  29. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
  30. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +8 -0
  31. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
  32. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +32 -0
  33. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
  34. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
  35. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
  36. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
  37. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
  38. data/lib/openhab/dsl/monkey_patch/types/types.rb +9 -0
  39. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +33 -0
  40. data/lib/openhab/dsl/persistence.rb +25 -0
  41. data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
  42. data/lib/openhab/dsl/rules/guard.rb +134 -0
  43. data/lib/openhab/dsl/rules/property.rb +102 -0
  44. data/lib/openhab/dsl/rules/rule.rb +116 -0
  45. data/lib/openhab/dsl/rules/rule_config.rb +151 -0
  46. data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
  47. data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
  48. data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
  49. data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
  50. data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
  51. data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
  52. data/lib/openhab/dsl/states.rb +61 -0
  53. data/lib/openhab/dsl/things.rb +91 -0
  54. data/lib/openhab/dsl/time_of_day.rb +232 -0
  55. data/lib/openhab/dsl/timers.rb +77 -0
  56. data/lib/openhab/dsl/types/datetime.rb +326 -0
  57. data/lib/openhab/dsl/types/quantity.rb +290 -0
  58. data/lib/openhab/dsl/units.rb +39 -0
  59. data/lib/openhab/log/configuration.rb +21 -0
  60. data/lib/openhab/log/logger.rb +172 -0
  61. data/lib/openhab/version.rb +1 -1
  62. metadata +60 -58
  63. data/lib/openhab/configuration.rb +0 -16
  64. data/lib/openhab/core/cron.rb +0 -27
  65. data/lib/openhab/core/debug.rb +0 -34
  66. data/lib/openhab/core/dsl.rb +0 -51
  67. data/lib/openhab/core/dsl/actions.rb +0 -107
  68. data/lib/openhab/core/dsl/entities.rb +0 -147
  69. data/lib/openhab/core/dsl/group.rb +0 -102
  70. data/lib/openhab/core/dsl/items/items.rb +0 -51
  71. data/lib/openhab/core/dsl/items/number_item.rb +0 -323
  72. data/lib/openhab/core/dsl/items/string_item.rb +0 -122
  73. data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
  74. data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
  75. data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
  76. data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
  77. data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
  78. data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
  79. data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
  80. data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
  81. data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
  82. data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
  83. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
  84. data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
  85. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
  86. data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
  87. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
  88. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
  89. data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
  90. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
  91. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  92. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  93. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  94. data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
  95. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
  96. data/lib/openhab/core/dsl/persistence.rb +0 -27
  97. data/lib/openhab/core/dsl/property.rb +0 -96
  98. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -345
  99. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  100. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  101. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  102. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  103. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  104. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  105. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  106. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  107. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  108. data/lib/openhab/core/dsl/states.rb +0 -63
  109. data/lib/openhab/core/dsl/things.rb +0 -93
  110. data/lib/openhab/core/dsl/time_of_day.rb +0 -231
  111. data/lib/openhab/core/dsl/timers.rb +0 -79
  112. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  113. data/lib/openhab/core/dsl/units.rb +0 -41
  114. data/lib/openhab/core/log.rb +0 -170
  115. data/lib/openhab/core/patch_load_path.rb +0 -7
  116. data/lib/openhab/core/startup_delay.rb +0 -23
  117. data/lib/openhab/osgi.rb +0 -59
@@ -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
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Monkey patch Group Item
5
- #
6
- # rubocop:disable Style/ClassAndModuleChildren
7
- class Java::OrgOpenhabCoreItems::GroupItem
8
- # rubocop:enable Style/ClassAndModuleChildren
9
-
10
- #
11
- # Get all items in a group
12
- #
13
- # @return [Array] Array of items in the group
14
- #
15
- def items
16
- to_a
17
- end
18
-
19
- #
20
- # Get all items in the group as an Array
21
- #
22
- # @return [Array] All items in the group
23
- #
24
- def to_a
25
- all_members.each_with_object([]) { |item, arr| arr << item }
26
- end
27
- end
@@ -1,132 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'openhab/core/log'
5
- require 'bigdecimal'
6
-
7
- # Monkey patch items
8
- require 'openhab/core/dsl/monkey_patch/items/metadata'
9
- require 'openhab/core/dsl/monkey_patch/items/persistence'
10
- require 'openhab/core/dsl/monkey_patch/items/contact_item'
11
- require 'openhab/core/dsl/monkey_patch/items/dimmer_item'
12
- require 'openhab/core/dsl/monkey_patch/items/switch_item'
13
- require 'openhab/core/dsl/monkey_patch/items/group_item'
14
-
15
- module OpenHAB
16
- module Core
17
- module DSL
18
- module MonkeyPatch
19
- #
20
- # Monkeypatches Items
21
- #
22
- module Items
23
- #
24
- # Extensions for Items
25
- #
26
- module ItemExtensions
27
- include Logging
28
- java_import org.openhab.core.model.script.actions.BusEvent
29
- java_import org.openhab.core.types.UnDefType
30
-
31
- #
32
- # Send a command to this item
33
- #
34
- # @param [Object] command to send to object
35
- #
36
- #
37
- def command(command)
38
- command = command.to_java.strip_trailing_zeros if command.is_a? BigDecimal
39
- logger.trace "Sending Command #{command} to #{id}"
40
- BusEvent.sendCommand(self, command.to_s)
41
- end
42
-
43
- alias << command
44
-
45
- #
46
- # Send an update to this item
47
- #
48
- # @param [Object] update the item
49
- #
50
- #
51
- def update(update)
52
- logger.trace "Sending Update #{update} to #{id}"
53
- BusEvent.postUpdate(self, update.to_s)
54
- end
55
-
56
- #
57
- # Check if the item state == UNDEF
58
- #
59
- # @return [Boolean] True if the state is UNDEF, false otherwise
60
- #
61
- def undef?
62
- # Need to explicitly call the super method version because this state will never return UNDEF
63
- method(:state).super_method.call == UnDefType::UNDEF
64
- end
65
-
66
- #
67
- # Check if the item state == NULL
68
- #
69
- # @return [Boolean] True if the state is NULL, false otherwise
70
- def null?
71
- # Need to explicitly call the super method version because this state will never return NULL
72
- method(:state).super_method.call == UnDefType::NULL
73
- end
74
-
75
- #
76
- # Check if the item has a state (not UNDEF or NULL)
77
- #
78
- # @return [Boolean] True if state is not UNDEF or NULL
79
- #
80
- def state?
81
- undef? == false && null? == false
82
- end
83
-
84
- #
85
- # Get the item state
86
- #
87
- # @return [State] OpenHAB item state if state is not UNDEF or NULL, nil otherwise
88
- #
89
- def state
90
- super if state?
91
- end
92
-
93
- #
94
- # Get an ID for the item, using the item label if set, otherwise item name
95
- #
96
- # @return [String] label if set otherwise name
97
- #
98
- def id
99
- label || name
100
- end
101
-
102
- #
103
- # Get the string representation of the state of the item
104
- #
105
- # @return [String] State of the item as a string if not UNDEF or NULL, nil otherwise
106
- #
107
- def to_s
108
- state&.to_s
109
- end
110
-
111
- #
112
- # Inspect the item
113
- #
114
- # @return [String] details of the item
115
- #
116
- def inspect
117
- toString
118
- end
119
- end
120
- end
121
- end
122
- end
123
- end
124
- end
125
-
126
- # rubocop:disable Style/ClassAndModuleChildren
127
- class Java::OrgOpenhabCoreItems::GenericItem
128
- # rubocop:enable Style/ClassAndModuleChildren
129
- prepend OpenHAB::Core::DSL::MonkeyPatch::Items::ItemExtensions
130
- prepend OpenHAB::Core::DSL::MonkeyPatch::Items::Metadata
131
- prepend OpenHAB::Core::DSL::MonkeyPatch::Items::Persistence
132
- end