openhab-scripting 4.47.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions/audio.rb +47 -0
  3. data/lib/openhab/core/actions/ephemeris.rb +39 -0
  4. data/lib/openhab/core/actions/exec.rb +51 -0
  5. data/lib/openhab/core/actions/http.rb +80 -0
  6. data/lib/openhab/core/actions/ping.rb +30 -0
  7. data/lib/openhab/core/actions/transformation.rb +32 -0
  8. data/lib/openhab/core/actions/voice.rb +36 -0
  9. data/lib/openhab/core/actions.rb +82 -0
  10. data/lib/openhab/core/dependency_tracking.rb +34 -0
  11. data/lib/openhab/core/dto/item_channel_link.rb +33 -0
  12. data/lib/openhab/core/dto/thing.rb +27 -0
  13. data/lib/openhab/core/dto.rb +11 -0
  14. data/lib/openhab/core/entity_lookup.rb +152 -70
  15. data/lib/openhab/core/events/abstract_event.rb +18 -0
  16. data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
  17. data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
  18. data/lib/openhab/core/events/item_command_event.rb +78 -0
  19. data/lib/openhab/core/events/item_event.rb +22 -0
  20. data/lib/openhab/core/events/item_state_changed_event.rb +75 -0
  21. data/lib/openhab/core/events/item_state_event.rb +79 -0
  22. data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
  23. data/lib/openhab/core/events.rb +10 -0
  24. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  25. data/lib/openhab/core/items/color_item.rb +52 -0
  26. data/lib/openhab/core/items/contact_item.rb +52 -0
  27. data/lib/openhab/core/items/date_time_item.rb +59 -0
  28. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  29. data/lib/openhab/core/items/generic_item.rb +292 -0
  30. data/lib/openhab/core/items/group_item.rb +176 -0
  31. data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
  32. data/lib/openhab/core/items/item.rb +273 -0
  33. data/lib/openhab/core/items/location_item.rb +34 -0
  34. data/lib/openhab/core/items/metadata/hash.rb +433 -0
  35. data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
  36. data/lib/openhab/core/items/metadata/provider.rb +48 -0
  37. data/lib/openhab/core/items/metadata.rb +11 -0
  38. data/lib/openhab/core/items/number_item.rb +62 -0
  39. data/lib/openhab/core/items/numeric_item.rb +22 -0
  40. data/lib/openhab/core/items/persistence.rb +416 -0
  41. data/lib/openhab/core/items/player_item.rb +66 -0
  42. data/lib/openhab/core/items/provider.rb +44 -0
  43. data/lib/openhab/core/items/proxy.rb +136 -0
  44. data/lib/openhab/core/items/registry.rb +86 -0
  45. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  46. data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
  47. data/lib/openhab/core/items/semantics.rb +473 -0
  48. data/lib/openhab/core/items/state_storage.rb +53 -0
  49. data/lib/openhab/core/items/string_item.rb +28 -0
  50. data/lib/openhab/core/items/switch_item.rb +78 -0
  51. data/lib/openhab/core/items.rb +108 -0
  52. data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
  53. data/lib/openhab/core/profile_factory.rb +132 -0
  54. data/lib/openhab/core/provider.rb +230 -0
  55. data/lib/openhab/core/proxy.rb +130 -0
  56. data/lib/openhab/core/registry.rb +40 -0
  57. data/lib/openhab/core/rules/module.rb +26 -0
  58. data/lib/openhab/core/rules/provider.rb +25 -0
  59. data/lib/openhab/core/rules/registry.rb +76 -0
  60. data/lib/openhab/core/rules/rule.rb +150 -0
  61. data/lib/openhab/core/rules.rb +25 -0
  62. data/lib/openhab/core/script_handling.rb +78 -20
  63. data/lib/openhab/core/things/channel.rb +48 -0
  64. data/lib/openhab/core/things/channel_uid.rb +51 -0
  65. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  66. data/lib/openhab/core/things/links/provider.rb +78 -0
  67. data/lib/openhab/core/things/profile_callback.rb +52 -0
  68. data/lib/openhab/core/things/provider.rb +29 -0
  69. data/lib/openhab/core/things/proxy.rb +87 -0
  70. data/lib/openhab/core/things/registry.rb +73 -0
  71. data/lib/openhab/core/things/thing.rb +194 -0
  72. data/lib/openhab/core/things.rb +22 -0
  73. data/lib/openhab/core/timer.rb +148 -0
  74. data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
  75. data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
  76. data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
  77. data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
  78. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  79. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  80. data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
  81. data/lib/openhab/core/types/on_off_type.rb +46 -0
  82. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  83. data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
  84. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  85. data/lib/openhab/core/types/point_type.rb +117 -0
  86. data/lib/openhab/core/types/quantity_type.rb +325 -0
  87. data/lib/openhab/core/types/raw_type.rb +26 -0
  88. data/lib/openhab/core/types/refresh_type.rb +27 -0
  89. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  90. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  91. data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
  92. data/lib/openhab/{dsl → core}/types/type.rb +42 -40
  93. data/lib/openhab/core/types/un_def_type.rb +38 -0
  94. data/lib/openhab/core/types/up_down_type.rb +50 -0
  95. data/lib/openhab/core/types.rb +82 -0
  96. data/lib/openhab/{dsl → core}/uid.rb +4 -23
  97. data/lib/openhab/core/value_cache.rb +188 -0
  98. data/lib/openhab/core.rb +98 -0
  99. data/lib/openhab/core_ext/between.rb +32 -0
  100. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  101. data/lib/openhab/core_ext/java/class.rb +34 -0
  102. data/lib/openhab/core_ext/java/duration.rb +142 -0
  103. data/lib/openhab/core_ext/java/list.rb +436 -0
  104. data/lib/openhab/core_ext/java/local_date.rb +104 -0
  105. data/lib/openhab/core_ext/java/local_time.rb +118 -0
  106. data/lib/openhab/core_ext/java/map.rb +66 -0
  107. data/lib/openhab/core_ext/java/month.rb +71 -0
  108. data/lib/openhab/core_ext/java/month_day.rb +119 -0
  109. data/lib/openhab/core_ext/java/period.rb +103 -0
  110. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  111. data/lib/openhab/core_ext/java/time.rb +62 -0
  112. data/lib/openhab/core_ext/java/unit.rb +15 -0
  113. data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
  114. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  115. data/lib/openhab/core_ext/ruby/date.rb +96 -0
  116. data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
  117. data/lib/openhab/core_ext/ruby/module.rb +15 -0
  118. data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
  119. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  120. data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
  121. data/lib/openhab/core_ext/ruby/time.rb +108 -0
  122. data/lib/openhab/core_ext.rb +18 -0
  123. data/lib/openhab/dsl/debouncer.rb +259 -0
  124. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  125. data/lib/openhab/dsl/events.rb +9 -0
  126. data/lib/openhab/dsl/gems.rb +1 -1
  127. data/lib/openhab/dsl/items/builder.rb +578 -0
  128. data/lib/openhab/dsl/items/ensure.rb +73 -82
  129. data/lib/openhab/dsl/items/timed_command.rb +214 -159
  130. data/lib/openhab/dsl/rules/automation_rule.rb +126 -115
  131. data/lib/openhab/dsl/rules/builder.rb +1935 -0
  132. data/lib/openhab/dsl/rules/guard.rb +51 -114
  133. data/lib/openhab/dsl/rules/name_inference.rb +66 -25
  134. data/lib/openhab/dsl/rules/property.rb +48 -75
  135. data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
  136. data/lib/openhab/dsl/rules/terse.rb +58 -14
  137. data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
  138. data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
  139. data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
  140. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
  141. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
  142. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
  143. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
  144. data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
  145. data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
  146. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
  147. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  148. data/lib/openhab/dsl/rules.rb +8 -0
  149. data/lib/openhab/dsl/things/builder.rb +299 -0
  150. data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
  151. data/lib/openhab/dsl/timer_manager.rb +204 -0
  152. data/lib/openhab/dsl/version.rb +9 -0
  153. data/lib/openhab/dsl.rb +979 -0
  154. data/lib/openhab/log.rb +355 -0
  155. data/lib/openhab/osgi.rb +68 -0
  156. data/lib/openhab/rspec/configuration.rb +56 -0
  157. data/lib/openhab/rspec/example_group.rb +132 -0
  158. data/lib/openhab/rspec/helpers.rb +458 -0
  159. data/lib/openhab/rspec/hooks.rb +113 -0
  160. data/lib/openhab/rspec/jruby.rb +46 -0
  161. data/lib/openhab/rspec/karaf.rb +851 -0
  162. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  163. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  164. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  165. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  166. data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
  167. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  168. data/lib/openhab/rspec/mocks/space.rb +23 -0
  169. data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
  170. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  171. data/lib/openhab/rspec/mocks/timer.rb +134 -0
  172. data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
  173. data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
  174. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  175. data/lib/openhab/rspec/shell.rb +31 -0
  176. data/lib/openhab/rspec/suspend_rules.rb +50 -0
  177. data/lib/openhab/rspec.rb +26 -0
  178. data/lib/openhab/yard/base_helper.rb +19 -0
  179. data/lib/openhab/yard/cli/stats.rb +23 -0
  180. data/lib/openhab/yard/code_objects/group_object.rb +23 -0
  181. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  182. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  183. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  184. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  185. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  186. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  187. data/lib/openhab/yard/coderay.rb +17 -0
  188. data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
  189. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  190. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  191. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
  192. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  193. data/lib/openhab/yard/html_helper.rb +78 -0
  194. data/lib/openhab/yard/markdown_helper.rb +148 -0
  195. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  196. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  197. data/lib/openhab/yard/tags/library.rb +3 -0
  198. data/lib/openhab/yard.rb +38 -0
  199. metadata +475 -106
  200. data/lib/openhab/core/item_proxy.rb +0 -29
  201. data/lib/openhab/core/load_path.rb +0 -19
  202. data/lib/openhab/core/openhab_setup.rb +0 -29
  203. data/lib/openhab/core/osgi.rb +0 -58
  204. data/lib/openhab/core/services.rb +0 -24
  205. data/lib/openhab/dsl/actions.rb +0 -114
  206. data/lib/openhab/dsl/between.rb +0 -25
  207. data/lib/openhab/dsl/channel.rb +0 -43
  208. data/lib/openhab/dsl/dsl.rb +0 -59
  209. data/lib/openhab/dsl/group.rb +0 -54
  210. data/lib/openhab/dsl/imports.rb +0 -21
  211. data/lib/openhab/dsl/items/color_item.rb +0 -76
  212. data/lib/openhab/dsl/items/comparable_item.rb +0 -62
  213. data/lib/openhab/dsl/items/contact_item.rb +0 -41
  214. data/lib/openhab/dsl/items/date_time_item.rb +0 -65
  215. data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
  216. data/lib/openhab/dsl/items/generic_item.rb +0 -229
  217. data/lib/openhab/dsl/items/group_item.rb +0 -127
  218. data/lib/openhab/dsl/items/item_equality.rb +0 -59
  219. data/lib/openhab/dsl/items/item_registry.rb +0 -54
  220. data/lib/openhab/dsl/items/items.rb +0 -109
  221. data/lib/openhab/dsl/items/location_item.rb +0 -59
  222. data/lib/openhab/dsl/items/metadata.rb +0 -326
  223. data/lib/openhab/dsl/items/number_item.rb +0 -17
  224. data/lib/openhab/dsl/items/numeric_item.rb +0 -87
  225. data/lib/openhab/dsl/items/persistence.rb +0 -307
  226. data/lib/openhab/dsl/items/player_item.rb +0 -58
  227. data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
  228. data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
  229. data/lib/openhab/dsl/items/semantics.rb +0 -227
  230. data/lib/openhab/dsl/items/string_item.rb +0 -51
  231. data/lib/openhab/dsl/items/switch_item.rb +0 -70
  232. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
  233. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
  234. data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
  235. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
  236. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
  237. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
  238. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
  239. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
  240. data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
  241. data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
  242. data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
  243. data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
  244. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
  245. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
  246. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
  247. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
  248. data/lib/openhab/dsl/openhab.rb +0 -30
  249. data/lib/openhab/dsl/persistence.rb +0 -27
  250. data/lib/openhab/dsl/rules/item_event.rb +0 -19
  251. data/lib/openhab/dsl/rules/rule.rb +0 -160
  252. data/lib/openhab/dsl/rules/rule_config.rb +0 -147
  253. data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
  254. data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
  255. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
  256. data/lib/openhab/dsl/states.rb +0 -89
  257. data/lib/openhab/dsl/things.rb +0 -147
  258. data/lib/openhab/dsl/time/month_day.rb +0 -180
  259. data/lib/openhab/dsl/time/time_of_day.rb +0 -235
  260. data/lib/openhab/dsl/timers/manager.rb +0 -119
  261. data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
  262. data/lib/openhab/dsl/timers/timer.rb +0 -132
  263. data/lib/openhab/dsl/timers.rb +0 -77
  264. data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
  265. data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
  266. data/lib/openhab/dsl/types/on_off_type.rb +0 -28
  267. data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
  268. data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
  269. data/lib/openhab/dsl/types/point_type.rb +0 -180
  270. data/lib/openhab/dsl/types/quantity_type.rb +0 -265
  271. data/lib/openhab/dsl/types/refresh_type.rb +0 -18
  272. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
  273. data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
  274. data/lib/openhab/dsl/types/types.rb +0 -83
  275. data/lib/openhab/dsl/types/un_def_type.rb +0 -22
  276. data/lib/openhab/dsl/types/up_down_type.rb +0 -32
  277. data/lib/openhab/dsl/units.rb +0 -45
  278. data/lib/openhab/log/configuration.rb +0 -21
  279. data/lib/openhab/log/logger.rb +0 -282
  280. data/lib/openhab/version.rb +0 -9
  281. data/lib/openhab.rb +0 -38
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ class << self
7
+ # @!visibility private
8
+ def prepend_accepted_data_types
9
+ concrete_item_classes.each do |k|
10
+ k.prepend(AcceptedDataTypes)
11
+ end
12
+ end
13
+ end
14
+
15
+ module AcceptedDataTypes
16
+ # @see Item#accepted_command_types
17
+ def accepted_command_types
18
+ super.map { |k| k.is_a?(java.lang.Class) ? k.ruby_class : k }
19
+ end
20
+
21
+ # @see Item#accepted_data_types
22
+ def accepted_data_types
23
+ super.map { |k| k.is_a?(java.lang.Class) ? k.ruby_class : k }
24
+ end
25
+ end
26
+ private_constant :AcceptedDataTypes
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dimmer_item"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ java_import org.openhab.core.library.items.ColorItem
9
+
10
+ #
11
+ # {ColorItem} represents a color values, e.g. for LED lights.
12
+ #
13
+ # Note that it inherits from {DimmerItem}, so you can call {#on}, {#off},
14
+ # {#on?}, {#off?}, etc. on it. Its state type is an {HSBType},
15
+ # which is stored as Hue, Saturation, and Brightness, but has easy
16
+ # helpers for working with RGB values of various forms.
17
+ #
18
+ # @example Sending commands
19
+ # HueBulb << "#ff0000" # send 'red' as a command
20
+ # HueBulb.on
21
+ # HueBulb.dim
22
+ #
23
+ # @example Inspect state
24
+ # HueBulb.on? # => true
25
+ # HueBulb.state.red # => 100%
26
+ # HueBulb.state.hue # => 0 °
27
+ # HueBulb.state.brightness # => 100%
28
+ # HueBulb.state.to_rgb # => [100%, 0%, 0%]
29
+ # HueBulb.state.rgb # => 16711680
30
+ # HueBulb.state.to_hex # => "0xff0000"
31
+ # HueBulb.state.on? # => true
32
+ # HueBulb.state.red.to_byte # => 255
33
+ # HueBulb.state.blue.to_byte # => 0
34
+ #
35
+ # @!attribute [r] state
36
+ # @return [HSBType, nil]
37
+ #
38
+ class ColorItem < DimmerItem
39
+ # Make sure to do the String => HSBType conversion in Ruby,
40
+ # where we add support for hex
41
+ # @!visibility private
42
+ def format_type(type)
43
+ return Types::HSBType.new(type) if type.respond_to?(:to_str)
44
+
45
+ super
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ # @!parse ColorItem = OpenHAB::Core::Items::ColorItem
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generic_item"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ java_import org.openhab.core.library.items.ContactItem
9
+
10
+ #
11
+ # A {ContactItem} can be used for sensors that return an "open" or
12
+ # "closed" as a state.
13
+ #
14
+ # This is useful for doors, windows, etc.
15
+ #
16
+ # @!attribute [r] state
17
+ # @return [OpenClosedType, nil]
18
+ #
19
+ # @example
20
+ # rule 'Log state of all doors on system startup' do
21
+ # on_load
22
+ # run do
23
+ # Doors.each do |door|
24
+ # case door.state
25
+ # when OPEN then logger.info("#{door.name} is Open")
26
+ # when CLOSED then logger.info("#{door.name} is Open")
27
+ # else logger.info("#{door.name} is not initialized")
28
+ # end
29
+ # end
30
+ # end
31
+ # end
32
+ #
33
+ class ContactItem < GenericItem
34
+ # @!method open?
35
+ # Check if the item state == {OPEN}
36
+ # @return [true,false]
37
+ #
38
+ # @example Log open contacts
39
+ # Contacts.select(&:open?).each { |contact| logger.info("Contact #{contact.name} is open")}
40
+
41
+ # @!method closed?
42
+ # Check if the item state == {CLOSED}
43
+ # @return [true,false]
44
+ #
45
+ # @example Log closed contacts
46
+ # Contacts.select(&:closed?).each { |contact| logger.info("Contact #{contact.name} is closed")}
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ # @!parse ContactItem = OpenHAB::Core::Items::ContactItem
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generic_item"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ java_import org.openhab.core.library.items.DateTimeItem
9
+
10
+ #
11
+ # A DateTimeItem stores a timestamp including a valid time zone.
12
+ #
13
+ # @!attribute [r] state
14
+ # @return [DateTimeType, nil]
15
+ #
16
+ # @example DateTime items can be updated and commanded with Ruby Time objects or Java ZonedDateTime objects
17
+ # Example_DateTimeItem << Time.now
18
+ # Example_DateTimeItem << ZonedDateTime.now
19
+ #
20
+ # @example Math operations (+ and -) are available to make calculations with time in a few different ways
21
+ # Example_DateTimeItem.state + 600 # Number of seconds
22
+ # Example_DateTimeItem.state - '01:15' # Subtracts 1h 15 min
23
+ # Example_DateTimeItem.state + 2.hours # Use the helper library's duration methods
24
+ #
25
+ # Example_DateTimeItem.state - Example_DateTimeItem2.state # Calculate the time difference, in seconds
26
+ # Example_DateTimeItem.state - '2021-01-01 15:40' # Calculates time difference
27
+ #
28
+ # @example Comparisons between different time objects can be performed
29
+ # Example_DateTimeItem.state == Example_DateTimeItem2.state # Equality, works across time zones
30
+ # Example_DateTimeItem.state > Date.parse('2021-01-31') # After midnight jan 31st 2021
31
+ # Example_DateTimeItem.state <= Time.now # Before or equal to now
32
+ # Example_DateTimeItem.state < LocalTime::NOON # Before noon
33
+ #
34
+ # @example LocalTime ranges created also work
35
+ # case Example_DateTimeItem.state
36
+ # when LocalTime.parse('00:00')...LocalTime.parse('08:00')
37
+ # logger.info('Example_DateTimeItem is between 00:00..08:00')
38
+ # when LocalTime.parse('08:00')...LocalTime.parse('16:00')
39
+ # logger.info('Example_DateTimeItem is between 08:00..16:00')
40
+ # when LocalTime.parse('16:00')..LocalTime.parse'23:59')
41
+ # logger.info('Example_DateTimeItem is between 16:00...23:59')
42
+ # end
43
+ #
44
+ class DateTimeItem < GenericItem
45
+ # Time types need formatted as ISO8601
46
+ # @!visibility private
47
+ def format_type(command)
48
+ return command if command.is_a?(Types::DateTimeType)
49
+ return Types::DateTimeType.new(command.to_zoned_date_time) if command.respond_to?(:to_zoned_date_time)
50
+ return Types::DateTimeType.new(DSL.try_parse_time_like(command.to_str)) if command.respond_to?(:to_str)
51
+
52
+ super
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ # @!parse DateTimeItem = OpenHAB::Core::Items::DateTimeItem
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "numeric_item"
4
+ require_relative "switch_item"
5
+
6
+ module OpenHAB
7
+ module Core
8
+ module Items
9
+ java_import org.openhab.core.library.items.DimmerItem
10
+
11
+ #
12
+ # A DimmerItem can be used as a switch (ON/OFF), but it also accepts
13
+ # percent values to reflect the dimmed state.
14
+ #
15
+ # @!attribute [r] state
16
+ # @return [PercentType, nil]
17
+ #
18
+ # @example
19
+ # DimmerOne << DimmerOne.state - 5
20
+ # DimmerOne << 100 - DimmerOne.state
21
+ #
22
+ # @example Turn on all dimmers in group
23
+ # Dimmers.members.each(&:on)
24
+ #
25
+ # @example Turn off all dimmers in group
26
+ # Dimmers.members.each(&:off)
27
+ #
28
+ # @example Turn on switches that are off
29
+ # Dimmers.select(&:off?).each(&:on)
30
+ #
31
+ # @example Turn off switches that are on
32
+ # Dimmers.select(&:on?).each(&:off)
33
+ #
34
+ # @example Dimmers can be selected in an enumerable with grep.
35
+ # items.grep(DimmerItem)
36
+ # .each { |dimmer| logger.info("#{dimmer.name} is a Dimmer") }
37
+ #
38
+ # @example Dimmers can also be used in case statements with ranges.
39
+ # items.grep(DimmerItem)
40
+ # .each do |dimmer|
41
+ # case dimmer.state
42
+ # when (0..50)
43
+ # logger.info("#{dimmer.name} is less than 50%")
44
+ # when (51..100)
45
+ # logger.info("#{dimmer.name} is greater than 50%")
46
+ # end
47
+ # end
48
+ #
49
+ # @example
50
+ # rule 'Dim a switch on system startup over 100 seconds' do
51
+ # on_load
52
+ # 100.times do
53
+ # run { DimmerSwitch.dim }
54
+ # delay 1.second
55
+ # end
56
+ # end
57
+ #
58
+ # @example
59
+ # rule 'Dim a switch on system startup by 5, pausing every second' do
60
+ # on_load
61
+ # 100.step(-5, 0) do |level|
62
+ # run { DimmerSwitch << level }
63
+ # delay 1.second
64
+ # end
65
+ # end
66
+ #
67
+ # @example
68
+ # rule 'Turn off any dimmers curently on at midnight' do
69
+ # every :day
70
+ # run do
71
+ # items.grep(DimmerItem)
72
+ # .select(&:on?)
73
+ # .each(&:off)
74
+ # end
75
+ # end
76
+ #
77
+ # @example
78
+ # rule 'Turn off any dimmers set to less than 50 at midnight' do
79
+ # every :day
80
+ # run do
81
+ # items.grep(DimmerItem)
82
+ # .select { |i| (1...50).cover?(i.state) }
83
+ # .each(&:off)
84
+ # end
85
+ # end
86
+ #
87
+ class DimmerItem < SwitchItem
88
+ include NumericItem
89
+
90
+ #
91
+ # Dim the dimmer
92
+ #
93
+ # @param [Integer] amount to dim by
94
+ # If 1 is the amount, the DECREASE command is sent, otherwise the
95
+ # current state - amount is sent as a command.
96
+ #
97
+ # @return [Integer] level target for dimmer
98
+ #
99
+ # @example
100
+ # DimmerOne.dim
101
+ # DimmerOne.dim(2)
102
+ #
103
+ def dim(amount = 1)
104
+ target = [state&.-(amount), 0].compact.max
105
+ command(target)
106
+ target
107
+ end
108
+
109
+ #
110
+ # Brighten the dimmer
111
+ #
112
+ # @param [Integer] amount to brighten by
113
+ # If 1 is the amount, the INCREASE command is sent, otherwise the
114
+ # current state + amount is sent as a command.
115
+ #
116
+ # @return [Integer] level target for dimmer
117
+ #
118
+ # @example
119
+ # DimmerOne.brighten
120
+ # DimmerOne.brighten(2)
121
+ #
122
+ def brighten(amount = 1)
123
+ target = [state&.+(amount), 100].compact.min
124
+ command(target)
125
+ target
126
+ end
127
+
128
+ # @!method increase
129
+ # Send the {INCREASE} command to the item
130
+ # @return [DimmerItem] `self`
131
+
132
+ # @!method decrease
133
+ # Send the {DECREASE} command to the item
134
+ # @return [DimmerItem] `self`
135
+
136
+ # raw numbers translate directly to PercentType, not a DecimalType
137
+ # @!visibility private
138
+ def format_type(command)
139
+ return Types::PercentType.new(command) if command.is_a?(Numeric)
140
+
141
+ super
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ # @!parse DimmerItem = OpenHAB::Core::Items::DimmerItem
@@ -0,0 +1,292 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ java_import org.openhab.core.items.GenericItem
7
+
8
+ #
9
+ # The abstract base class for all items.
10
+ #
11
+ # @see org.openhab.core.items.GenericItem
12
+ #
13
+ class GenericItem
14
+ # @!parse include Item
15
+
16
+ # rubocop:disable Naming/MethodName these mimic Java fields, which are
17
+ # actually methods
18
+ class << self
19
+ # manually define this, since the Java side doesn't
20
+ # @!visibility private
21
+ def ACCEPTED_COMMAND_TYPES
22
+ [org.openhab.core.types.RefreshType.java_class].freeze
23
+ end
24
+
25
+ # manually define this, since the Java side doesn't
26
+ # @!visibility private
27
+ def ACCEPTED_DATA_TYPES
28
+ [org.openhab.core.types.UnDefType.java_class].freeze
29
+ end
30
+
31
+ # @!visibility private
32
+ #
33
+ # Override to support {Proxy}
34
+ #
35
+ # Item.=== isn't actually included (on the Ruby side) into
36
+ # {GenericItem}
37
+ #
38
+ def ===(other)
39
+ other.is_a?(self)
40
+ end
41
+ end
42
+ # rubocop:enable Naming/MethodName
43
+
44
+ # @!attribute [r] name
45
+ # The item's name.
46
+ # @return [String]
47
+
48
+ # @!attribute [r] label
49
+ # The item's descriptive label.
50
+ # @return [String, nil]
51
+
52
+ # @!visibility private
53
+ alias_method :hash, :hash_code
54
+
55
+ # @!attribute [r] raw_state
56
+ #
57
+ # Get the raw item state.
58
+ #
59
+ # The state of the item, including possibly {NULL} or {UNDEF}
60
+ #
61
+ # @return [State]
62
+ #
63
+ alias_method :raw_state, :state
64
+
65
+ #
66
+ # Check if the item has a state (not {UNDEF} or {NULL})
67
+ #
68
+ # @return [true, false]
69
+ #
70
+ def state?
71
+ !raw_state.is_a?(Types::UnDefType)
72
+ end
73
+
74
+ # @!attribute [r] formatted_state
75
+ #
76
+ # Format the item's state according to its state description
77
+ #
78
+ # This may include running a transformation.
79
+ #
80
+ # @return [String]
81
+ #
82
+ # @example
83
+ # logger.info(Exterior_WindDirection.formatted_state) # => "NE (36°)"
84
+ #
85
+ def formatted_state
86
+ # use to_string, not to_s, to get the original openHAB toString(), instead of any overrides
87
+ # the JRuby library has defined
88
+ raw_state_string = raw_state.to_string
89
+
90
+ return raw_state_string unless (pattern = state_description&.pattern)
91
+
92
+ transformed_state_string = org.openhab.core.transform.TransformationHelper.transform(OSGi.bundle_context,
93
+ pattern,
94
+ raw_state_string)
95
+ return state.format(pattern) if transformed_state_string.nil? || transformed_state_string == raw_state_string
96
+
97
+ transformed_state_string
98
+ rescue org.openhab.core.transform.TransformationException
99
+ raw_state_string
100
+ end
101
+
102
+ #
103
+ # @!attribute [r] state
104
+ # @return [State, nil]
105
+ # openHAB item state if state is not {UNDEF} or {NULL}, nil otherwise.
106
+ # This makes it easy to use with the
107
+ # [Ruby safe navigation operator `&.`](https://docs.ruby-lang.org/en/master/syntax/calling_methods_rdoc.html#label-Safe+Navigation+Operator)
108
+ # Use {#undef?} or {#null?} to check for those states.
109
+ #
110
+ def state
111
+ raw_state if state?
112
+ end
113
+
114
+ # @!method null?
115
+ # Check if the item state == {NULL}
116
+ # @return [true,false]
117
+
118
+ # @!method undef?
119
+ # Check if the item state == {UNDEF}
120
+ # @return [true,false]
121
+
122
+ #
123
+ # Send a command to this item
124
+ #
125
+ # When this method is chained after the {OpenHAB::DSL::Items::Ensure::Ensurable#ensure ensure}
126
+ # method, or issued inside an {OpenHAB::DSL.ensure_states ensure_states} block,
127
+ # the command will only be sent if the item is not already in the same state.
128
+ #
129
+ # @param [Command] command command to send to the item
130
+ # @return [self, nil] nil when `ensure` is in effect and the item was already in the same state,
131
+ # otherwise the item.
132
+ #
133
+ # @see DSL::Items::TimedCommand#command Timed Command
134
+ # @see OpenHAB::DSL.ensure_states ensure_states
135
+ # @see DSL::Items::Ensure::Ensurable#ensure ensure
136
+ #
137
+ def command(command)
138
+ command = format_command(command)
139
+ logger.trace "Sending Command #{command} to #{name}"
140
+ $events.send_command(self, command)
141
+ Proxy.new(self)
142
+ end
143
+
144
+ # not an alias to allow easier stubbing and overriding
145
+ def <<(command)
146
+ command(command)
147
+ end
148
+
149
+ # @!parse alias_method :<<, :command
150
+
151
+ # @!method refresh
152
+ # Send the {REFRESH} command to the item
153
+ # @return [Item] `self`
154
+
155
+ #
156
+ # Send an update to this item
157
+ #
158
+ # @param [State] state
159
+ # @return [self, nil] nil when `ensure` is in effect and the item was already in the same state,
160
+ # otherwise the item.
161
+ #
162
+ def update(state)
163
+ state = format_update(state)
164
+ logger.trace "Sending Update #{state} to #{name}"
165
+ $events.post_update(self, state)
166
+ Proxy.new(self)
167
+ end
168
+
169
+ # @!visibility private
170
+ def format_command(command)
171
+ command = format_type(command)
172
+ return command if command.is_a?(Types::Command)
173
+
174
+ command = command.to_s
175
+ org.openhab.core.types.TypeParser.parse_command(getAcceptedCommandTypes, command) || command
176
+ end
177
+
178
+ # @!visibility private
179
+ def format_update(state)
180
+ state = format_type(state)
181
+ return state if state.is_a?(Types::State)
182
+
183
+ state = state.to_s
184
+ org.openhab.core.types.TypeParser.parse_state(getAcceptedDataTypes, state) || StringType.new(state)
185
+ end
186
+
187
+ # formats a {Types::Type} to send to the event bus
188
+ # @!visibility private
189
+ def format_type(type)
190
+ # actual Type types can be sent directly without conversion
191
+ # make sure to use Type, because this method is used for both
192
+ # #update and #command
193
+ return type if type.is_a?(Types::Type)
194
+ return NULL if type.nil?
195
+
196
+ type.to_s
197
+ end
198
+
199
+ #
200
+ # Defers notifying openHAB of modifications to multiple attributes until the block is complete.
201
+ #
202
+ # @param [true, false] force When true, allow modifications to file-based items.
203
+ # Normally a FrozenError is raised when attempting to modify file-based items, since
204
+ # they will then be out-of-sync with the definition on disk. Advanced users may do this
205
+ # knowingly and intentionally though, so an escape hatch is provided to allow runtime
206
+ # modifications.
207
+ # @yield
208
+ # @return [Object] the block's return value
209
+ #
210
+ # @example Modify label and tags for an item
211
+ # MySwitch.modify do
212
+ # MySwitch.label = "New Label"
213
+ # MySwitch.tags = :labeled
214
+ # end
215
+ #
216
+ def modify(force: false)
217
+ raise ArgumentError, "you must pass a block to modify" unless block_given?
218
+ return yield if instance_variable_defined?(:@modifying) && @modifying
219
+
220
+ begin
221
+ provider = self.provider
222
+ if provider && !provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
223
+ raise FrozenError, "Cannot modify item #{name} from provider #{provider.inspect}." unless force
224
+
225
+ provider = nil
226
+ logger.debug("Forcing modifications to non-managed item #{name}")
227
+ end
228
+ @modified = false
229
+ @modifying = true
230
+
231
+ r = yield
232
+
233
+ provider&.update(self) if @modified
234
+ r
235
+ ensure
236
+ @modifying = false
237
+ end
238
+ end
239
+
240
+ # @!attribute [rw] label
241
+ # The item's descriptive label.
242
+ # @return [String]
243
+ def label=(value)
244
+ modify do
245
+ next if label == value
246
+
247
+ @modified = true
248
+ set_label(value)
249
+ end
250
+ end
251
+
252
+ # @!attribute [rw] category
253
+ # The item's category.
254
+ # @return [String]
255
+ def category=(value)
256
+ modify do
257
+ value = value&.to_s
258
+ next if category == value
259
+
260
+ @modified = true
261
+ set_category(value)
262
+ end
263
+ end
264
+
265
+ # @!attribute [rw] tags
266
+ # The item's tags
267
+ # @return [Array<String>]
268
+ # @overload tags
269
+ # Returns the item's tags.
270
+ # @return [Array<String>]
271
+ # @overload tags=(values)
272
+ # Sets the item's tags.
273
+ #
274
+ # To remove all tags, assign an empty array or nil.
275
+ # @param [Array<String,Symbol,Semantics::Tag>] values Tags to set.
276
+ # @return [void]
277
+ def tags=(values)
278
+ modify do
279
+ values = DSL::Items::ItemBuilder.normalize_tags(*values)
280
+ next if values.to_set == tags.to_set
281
+
282
+ @modified = true
283
+ remove_all_tags
284
+ add_tags(values)
285
+ end
286
+ end
287
+ end
288
+ end
289
+ end
290
+ end
291
+
292
+ # @!parse GenericItem = OpenHAB::Core::Items::GenericItem