openhab-scripting 2.16.1 → 2.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab.rb +12 -16
  3. data/lib/openhab/core/entity_lookup.rb +168 -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/string_item.rb +120 -0
  14. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
  15. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
  16. data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
  17. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
  18. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
  19. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
  20. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
  21. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
  22. data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
  23. data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
  24. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
  25. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
  26. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
  27. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
  28. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
  29. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +8 -0
  30. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
  31. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +32 -0
  32. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
  33. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
  34. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
  35. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
  36. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
  37. data/lib/openhab/dsl/monkey_patch/types/types.rb +8 -0
  38. data/lib/openhab/dsl/persistence.rb +25 -0
  39. data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
  40. data/lib/openhab/dsl/rules/guard.rb +134 -0
  41. data/lib/openhab/dsl/rules/property.rb +102 -0
  42. data/lib/openhab/dsl/rules/rule.rb +116 -0
  43. data/lib/openhab/dsl/rules/rule_config.rb +151 -0
  44. data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
  45. data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
  46. data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
  47. data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
  48. data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
  49. data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
  50. data/lib/openhab/dsl/states.rb +61 -0
  51. data/lib/openhab/dsl/things.rb +91 -0
  52. data/lib/openhab/dsl/time_of_day.rb +232 -0
  53. data/lib/openhab/dsl/timers.rb +77 -0
  54. data/lib/openhab/dsl/types/datetime.rb +326 -0
  55. data/lib/openhab/dsl/types/quantity.rb +290 -0
  56. data/lib/openhab/dsl/units.rb +39 -0
  57. data/lib/openhab/log/configuration.rb +21 -0
  58. data/lib/openhab/log/logger.rb +172 -0
  59. data/lib/openhab/version.rb +1 -1
  60. metadata +58 -58
  61. data/lib/openhab/configuration.rb +0 -16
  62. data/lib/openhab/core/cron.rb +0 -27
  63. data/lib/openhab/core/debug.rb +0 -34
  64. data/lib/openhab/core/dsl.rb +0 -51
  65. data/lib/openhab/core/dsl/actions.rb +0 -107
  66. data/lib/openhab/core/dsl/entities.rb +0 -140
  67. data/lib/openhab/core/dsl/group.rb +0 -93
  68. data/lib/openhab/core/dsl/items/items.rb +0 -51
  69. data/lib/openhab/core/dsl/items/number_item.rb +0 -323
  70. data/lib/openhab/core/dsl/items/string_item.rb +0 -122
  71. data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
  72. data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
  73. data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
  74. data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
  75. data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
  76. data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
  77. data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
  78. data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
  79. data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
  80. data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
  81. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
  82. data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
  83. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
  84. data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
  85. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
  86. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
  87. data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
  88. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
  89. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  90. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  91. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  92. data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
  93. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
  94. data/lib/openhab/core/dsl/persistence.rb +0 -27
  95. data/lib/openhab/core/dsl/property.rb +0 -96
  96. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -348
  97. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  98. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  99. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  100. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  101. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  102. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  103. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  104. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  105. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  106. data/lib/openhab/core/dsl/states.rb +0 -63
  107. data/lib/openhab/core/dsl/things.rb +0 -93
  108. data/lib/openhab/core/dsl/time_of_day.rb +0 -231
  109. data/lib/openhab/core/dsl/timers.rb +0 -79
  110. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  111. data/lib/openhab/core/dsl/units.rb +0 -41
  112. data/lib/openhab/core/log.rb +0 -170
  113. data/lib/openhab/core/patch_load_path.rb +0 -7
  114. data/lib/openhab/core/startup_delay.rb +0 -23
  115. data/lib/openhab/osgi.rb +0 -59
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'core/log'
4
- require 'openhab/core/dsl/rule/triggers/trigger'
5
-
6
- module OpenHAB
7
- module Core
8
- module DSL
9
- module Rule
10
- #
11
- # Channel triggers
12
- #
13
- module Triggers
14
- include Logging
15
-
16
- #
17
- # Creates a channel trigger
18
- #
19
- # @param [Array] channels array to create triggers for on form of 'binding_id:type_id:thing_id#channel_id'
20
- # or 'channel_id' if thing is provided
21
- # @param [thing] thing to create trigger for if not specified with the channel
22
- # @param [String] triggered specific triggering condition to match for trigger
23
- #
24
- #
25
- def channel(*channels, thing: nil, triggered: nil)
26
- channels.flatten.each do |channel|
27
- channel = [thing, channel].join(':') if thing
28
- logger.trace("Creating channel trigger for channel(#{channel}), thing(#{thing}), trigger(#{triggered})")
29
- [triggered].flatten.each do |trigger|
30
- create_channel_trigger(channel, trigger)
31
- end
32
- end
33
- end
34
-
35
- private
36
-
37
- #
38
- # Create a trigger for a channel
39
- #
40
- # @param [Channel] channel to look for triggers
41
- # @param [Trigger] trigger specific channel trigger to match
42
- #
43
- #
44
- def create_channel_trigger(channel, trigger)
45
- config = { 'channelUID' => channel }
46
- config['event'] = trigger.to_s unless trigger.nil?
47
- config['channelUID'] = channel
48
- logger.trace("Creating Change Trigger for #{config}")
49
- @triggers << Trigger.trigger(type: Trigger::CHANNEL_EVENT, config: config)
50
- end
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,106 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'core/log'
4
-
5
- module OpenHAB
6
- module Core
7
- module DSL
8
- module Rule
9
- #
10
- # Module holds rule triggers
11
- #
12
- module Triggers
13
- include Logging
14
-
15
- #
16
- # Create a trigger for when an item or group receives a command
17
- #
18
- # The commands/commands parameters are replicated for DSL fluency
19
- #
20
- # @param [Array] items Array of items to create trigger for
21
- # @param [Array] command commands to match for trigger
22
- # @param [Array] commands commands to match for trigger
23
- #
24
- #
25
- def received_command(*items, command: nil, commands: nil)
26
- items.flatten.each do |item|
27
- logger.trace("Creating received command trigger for item(#{item})"\
28
- "command(#{command}) commands(#{commands})")
29
-
30
- # Combine command and commands, doing union so only a single nil will be in the combined array.
31
- combined_commands = combine_commands(command, commands)
32
- create_received_trigger(combined_commands, item)
33
- end
34
- end
35
-
36
- private
37
-
38
- #
39
- # Create a received trigger based on item type
40
- #
41
- # @param [Array] commands to create trigger for
42
- # @param [Object] item to create trigger for
43
- #
44
- #
45
- def create_received_trigger(commands, item)
46
- commands.each do |command|
47
- if item.is_a? GroupItems
48
- config, trigger = create_group_command_trigger(item)
49
- else
50
- config, trigger = create_item_command_trigger(item)
51
- end
52
- config['command'] = command.to_s unless command.nil?
53
- append_trigger(trigger, config)
54
- end
55
- end
56
-
57
- #
58
- # Create trigger for item commands
59
- #
60
- # @param [Item] item to create trigger for
61
- #
62
- # @return [Array<Hash,Trigger>] first element is hash of trigger config properties
63
- # second element is trigger type
64
- #
65
- def create_item_command_trigger(item)
66
- config = { 'itemName' => item.name }
67
- trigger = Trigger::ITEM_COMMAND
68
- [config, trigger]
69
- end
70
-
71
- #
72
- # Create trigger for group items
73
- #
74
- # @param [Group] group to create trigger for
75
- #
76
- # @return [Array<Hash,Trigger>] first element is hash of trigger config properties
77
- # second element is trigger type
78
- #
79
- def create_group_command_trigger(group)
80
- config = { 'groupName' => group.group.name }
81
- trigger = Trigger::GROUP_COMMAND
82
- [config, trigger]
83
- end
84
-
85
- #
86
- # Combine command and commands into a single array
87
- #
88
- # @param [Array] command list of commands to trigger on
89
- # @param [Array] commands list of commands to trigger on
90
- #
91
- # @return [Array] Combined flattened and compacted list of commands
92
- #
93
- def combine_commands(command, commands)
94
- combined_commands = ([command] | [commands]).flatten
95
-
96
- # If either command or commands has a value and one is nil, we need to remove nil from the array.
97
- # If it is only now a single nil value, we leave the nil in place, so that we create a trigger
98
- # That isn't looking for a specific command.
99
- combined_commands = combined_commands.compact unless combined_commands.all?(&:nil?)
100
- combined_commands
101
- end
102
- end
103
- end
104
- end
105
- end
106
- end
@@ -1,160 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'core/dsl/time_of_day'
5
- require 'core/cron'
6
-
7
- module OpenHAB
8
- module Core
9
- module DSL
10
- module Rule
11
- #
12
- # Cron type rules
13
- #
14
- module Triggers
15
- java_import org.openhab.core.automation.util.TriggerBuilder
16
- java_import org.openhab.core.config.core.Configuration
17
-
18
- include OpenHAB::Core::DSL::Rule
19
- extend OpenHAB::Core::Cron
20
-
21
- # @return [Map] Map of days of the week from symbols to to OpenHAB cron strings
22
- DAY_OF_WEEK_MAP = {
23
- monday: 'MON',
24
- tuesday: 'TUE',
25
- wednesday: 'WED',
26
- thursday: 'THU',
27
- friday: 'FRI',
28
- saturday: 'SAT',
29
- sunday: 'SUN'
30
- }.freeze
31
-
32
- private_constant :DAY_OF_WEEK_MAP
33
-
34
- # @return [MAP] Converts the DAY_OF_WEEK_MAP to map used by Cron Expression
35
- DAY_OF_WEEK_EXPRESSION_MAP = DAY_OF_WEEK_MAP.transform_values { |v| cron_expression_map.merge(dow: v) }
36
-
37
- private_constant :DAY_OF_WEEK_EXPRESSION_MAP
38
-
39
- # @return [Map] Create a set of cron expressions based on different time intervals
40
- EXPRESSION_MAP = {
41
- second: cron_expression_map,
42
- minute: cron_expression_map.merge(second: '0'),
43
- hour: cron_expression_map.merge(second: '0', minute: '0'),
44
- day: cron_expression_map.merge(second: '0', minute: '0', hour: '0'),
45
- week: cron_expression_map.merge(second: '0', minute: '0', hour: '0', dow: 'MON'),
46
- month: cron_expression_map.merge(second: '0', minute: '0', hour: '0', dom: '1'),
47
- year: cron_expression_map.merge(second: '0', minute: '0', hour: '0', dom: '1', month: '1')
48
- }.merge(DAY_OF_WEEK_EXPRESSION_MAP)
49
- .freeze
50
-
51
- private_constant :EXPRESSION_MAP
52
-
53
- #
54
- # Create a rule that executes at the specified interval
55
- #
56
- # @param [Object] value Symbol or Duration to execute this rule
57
- # @param [Object] at TimeOfDay or String representing TimeOfDay in which to execute rule
58
- #
59
- #
60
- def every(value, at: nil)
61
- cron_expression = case value
62
- when Symbol then cron_from_symbol(value, at)
63
- when Java::JavaTime::Duration then cron_from_duration(value, at)
64
- else raise ArgumentExpression, 'Unknown interval'
65
- end
66
- cron(cron_expression)
67
- end
68
-
69
- #
70
- # Create a OpenHAB Cron trigger
71
- #
72
- # @param [String] expression OpenHAB style cron expression
73
- #
74
- def cron(expression)
75
- @triggers << Trigger.trigger(type: Trigger::CRON, config: { 'cronExpression' => expression })
76
- end
77
-
78
- private
79
-
80
- #
81
- # Create a cron map from a duration
82
- #
83
- # @param [java::time::Duration] duration
84
- # @param [Object] at TimeOfDay or String representing time of day
85
- #
86
- # @return [Hash] map describing cron expression
87
- #
88
- def cron_from_duration(duration, at)
89
- raise ArgumentError, '"at" cannot be used with duration' if at
90
-
91
- map_to_cron(duration_to_map(duration))
92
- end
93
-
94
- #
95
- # Create a cron map from a symbol
96
- #
97
- # @param [Symbol] symbol
98
- # @param [Object] at TimeOfDay or String representing time of day
99
- #
100
- # @return [Hash] map describing cron expression created from symbol
101
- #
102
- def cron_from_symbol(symbol, at)
103
- expression_map = EXPRESSION_MAP[symbol]
104
- expression_map = at_condition(expression_map, at) if at
105
- map_to_cron(expression_map)
106
- end
107
-
108
- #
109
- # Map cron expression to to cron string
110
- #
111
- # @param [Map] map of cron expression
112
- #
113
- # @return [String] OpenHAB cron string
114
- #
115
- def map_to_cron(map)
116
- %i[second minute hour dom month dow].map { |field| map.fetch(field) }.join(' ')
117
- end
118
-
119
- #
120
- # Convert a Java Duration to a map for the map_to_cron method
121
- #
122
- # @param duration [Java::JavaTime::Duration] The duration object
123
- #
124
- # @return [Hash] a map suitable for map_to_cron
125
- #
126
- def duration_to_map(duration)
127
- if duration.to_millis_part.zero? && duration.to_nanos_part.zero? && duration.to_days.zero?
128
- %i[second minute hour].each do |unit|
129
- to_unit_part = duration.public_send("to_#{unit}s_part")
130
- next unless to_unit_part.positive?
131
-
132
- to_unit = duration.public_send("to_#{unit}s")
133
- break unless to_unit_part == to_unit
134
-
135
- return EXPRESSION_MAP[unit].merge(unit => "*/#{to_unit}")
136
- end
137
- end
138
- raise ArgumentError, "Cron Expression not supported for duration: #{duration}"
139
- end
140
-
141
- #
142
- # If an at time is provided, parse that and merge the new fields into the expression.
143
- #
144
- # @param [<Type>] expression_map <description>
145
- # @param [<Type>] at_time <description>
146
- #
147
- # @return [<Type>] <description>
148
- #
149
- def at_condition(expression_map, at_time)
150
- if at_time
151
- tod = at_time.is_a?(TimeOfDay) ? at_time : TimeOfDay.parse(at_time)
152
- expression_map = expression_map.merge(hour: tod.hour, minute: tod.minute, second: tod.second)
153
- end
154
- expression_map
155
- end
156
- end
157
- end
158
- end
159
- end
160
- end
@@ -1,126 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'securerandom'
4
- require 'java'
5
-
6
- module OpenHAB
7
- module Core
8
- module DSL
9
- module Rule
10
- #
11
- # Module holds rule triggers
12
- #
13
- module Triggers
14
- #
15
- # Create a trigger for a thing
16
- #
17
- # @param [Thing] thing to create trigger for
18
- # @param [Trigger] trigger to map with thing
19
- # @param [State] to for thing
20
- # @param [State] from state of thing
21
- #
22
- # @return [Array] Trigger and config for thing
23
- #
24
- def trigger_for_thing(thing, trigger, to = nil, from = nil)
25
- config = { 'thingUID' => thing.uid.to_s }
26
- config['status'] = trigger_state_from_symbol(to).to_s if to
27
- config['previousStatus'] = trigger_state_from_symbol(from).to_s if from
28
- [trigger, config]
29
- end
30
-
31
- #
32
- # converts object to upcase string if its a symbol
33
- #
34
- # @param [sym] sym potential symbol to convert
35
- #
36
- # @return [String] Upcased symbol as string
37
- #
38
- def trigger_state_from_symbol(sym)
39
- sym.to_s.upcase if (sym.is_a? Symbol) || sym
40
- end
41
-
42
- #
43
- # Append a trigger to the list of triggeres
44
- #
45
- # @param [String] type of trigger to create
46
- # @param [Map] config map describing trigger configuration
47
- #
48
- # @return [Trigger] OpenHAB trigger
49
- #
50
- def append_trigger(type, config)
51
- logger.trace("Creating trigger of type #{type} for #{config}")
52
- trigger = Trigger.trigger(type: type, config: config)
53
- @triggers << trigger
54
- trigger
55
- end
56
-
57
- #
58
- # Class for creating and managing triggers
59
- #
60
- class Trigger
61
- java_import org.openhab.core.automation.util.TriggerBuilder
62
- java_import org.openhab.core.config.core.Configuration
63
-
64
- # @return [String] A channel event trigger
65
- CHANNEL_EVENT = 'core.ChannelEventTrigger'
66
-
67
- # @return [String] A thing status Change trigger
68
- THING_CHANGE = 'core.ThingStatusChangeTrigger'
69
-
70
- # @return [String] A thing status update trigger
71
- THING_UPDATE = 'core.ThingStatusUpdateTrigger'
72
-
73
- # @return [String] An item command trigger
74
- ITEM_COMMAND = 'core.ItemCommandTrigger'
75
-
76
- # @return [String] An item state update trigger
77
- ITEM_STATE_UPDATE = 'core.ItemStateUpdateTrigger'
78
-
79
- # @return [String] An item state change trigger
80
- ITEM_STATE_CHANGE = 'core.ItemStateChangeTrigger'
81
-
82
- # @return [String] A group state change trigger for items in the group
83
- GROUP_STATE_CHANGE = 'core.GroupStateChangeTrigger'
84
-
85
- # @return [String] A group state update trigger for items in the group
86
- GROUP_STATE_UPDATE = 'core.GroupStateUpdateTrigger'
87
-
88
- # @return [String] A group command trigger for items in the group
89
- GROUP_COMMAND = 'core.GroupCommandTrigger'
90
-
91
- # @return [String] A time of day trigger
92
- TIME_OF_DAY = 'timer.TimeOfDayTrigger'
93
-
94
- # @return [String] A cron trigger
95
- CRON = 'timer.GenericCronTrigger'
96
-
97
- #
98
- # Create a trigger
99
- #
100
- # @param [String] type of trigger
101
- # @param [Map] config map
102
- #
103
- # @return [OpenHAB Trigger] configured by type and supplied config
104
- #
105
- def self.trigger(type:, config:)
106
- TriggerBuilder.create
107
- .with_id(uuid)
108
- .with_type_uid(type)
109
- .with_configuration(Configuration.new(config))
110
- .build
111
- end
112
-
113
- #
114
- # Generate a UUID for triggers
115
- #
116
- # @return [String] UUID
117
- #
118
- def self.uuid
119
- SecureRandom.uuid
120
- end
121
- end
122
- end
123
- end
124
- end
125
- end
126
- end