openhab-scripting 4.46.2 → 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 -36
@@ -1,105 +1,187 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'openhab/log/logger'
4
- require_relative 'item_proxy'
5
-
6
- # Automation lookup and injection of OpenHab entities
3
+ require "ruby2_keywords"
7
4
 
8
5
  module OpenHAB
9
6
  module Core
10
7
  #
11
- # Manages access to OpenHAB entities
8
+ # Manages access to openHAB entities
9
+ #
10
+ # You can access openHAB items and things directly using their name, anywhere `EntityLookup` is available.
11
+ #
12
+ # @note Thing UIDs are separated by a colon `:`. Since it is not a valid symbol for an identifier,
13
+ # it must be replaced with an underscore `_`. So to access `astro:sun:home`, use `astro_sun_home`
14
+ # as an alternative to `things["astro:sun:home"]`
15
+ #
16
+ # @see OpenHAB::DSL.items items[]
17
+ # @see OpenHAB::DSL.things things[]
18
+ #
19
+ # @example Accessing Items and Groups
20
+ # gAll_Lights # Access the gAll_Lights group. It is the same as items["gAll_Lights"]
21
+ # Kitchen_Light.on # The openHAB object for the Kitchen_Light item and send an ON command
22
+ #
23
+ # @example Accessing Things
24
+ # smtp_mail_local.send_mail('me@example.com', 'Subject', 'Dear Person, ...')
25
+ # # Is equivalent to:
26
+ # things['smtp:mail:local'].send_mail('me@example.com', 'Subject', 'Dear Person, ...')
12
27
  #
13
28
  module EntityLookup
14
- include OpenHAB::Log
29
+ # @!visibility private
30
+ module ClassMethods
31
+ # @!attribute [w] create_dummy_items
32
+ # @return [true,false]
33
+ def create_dummy_items=(value)
34
+ @create_dummy_items = value
35
+ end
15
36
 
16
- #
17
- # Automatically looks up OpenHAB items and things in appropriate registries
18
- #
19
- # @param [method] method Name of item to lookup
20
- # @param [<Type>] args method arguments
21
- # @param [<Type>] block supplied to missing method
22
- #
23
- # @return [Object] Item or Thing if found in registry
24
- #
25
- def method_missing(method, *args, &block)
26
- logger.trace("method missing, performing OpenHab Lookup for: #{method}")
27
- EntityLookup.lookup_entity(method) || super
28
- end
37
+ # @return [Boolean] if dummy items should be created in this context
38
+ def create_dummy_items?
39
+ defined?(@create_dummy_items) && @create_dummy_items
40
+ end
29
41
 
30
- #
31
- # Checks if this method responds to the missing method
32
- #
33
- # @param [String] method_name Name of the method to check
34
- # @param [Boolean] _include_private boolean if private methods should be checked
35
- #
36
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
37
- #
38
- def respond_to_missing?(method_name, _include_private = false)
39
- logger.trace("Checking if OpenHAB entites exist for #{method_name}")
40
- method_name = method_name.to_s if method_name.is_a? Symbol
42
+ # @!visibility private
43
+ def inherited(klass)
44
+ super
45
+
46
+ EntityLookup.included(klass)
47
+ end
41
48
 
42
- method_name == 'scriptLoaded' ||
43
- method_name == 'scriptUnloaded' ||
44
- EntityLookup.lookup_entity(method_name) ||
49
+ # @!visibility private
50
+ def included(klass)
45
51
  super
52
+
53
+ EntityLookup.included(klass)
54
+ end
46
55
  end
47
56
 
48
- #
49
- # Looks up an OpenHAB entity
50
- # items are checked first, then things
51
- #
52
- # @param [String] name of entity to lookup in item or thing registry
53
- #
54
- # @return [Thing,Item] if found, nil otherwise
55
- #
56
- def self.lookup_entity(name)
57
- lookup_item(name) || lookup_thing(name)
57
+ # @!visibility private
58
+ def self.included(klass)
59
+ klass.singleton_class.prepend(ClassMethods)
60
+ klass.ancestors.each do |ancestor|
61
+ next unless ancestor.singleton_class.ancestors.include?(ClassMethods)
62
+ next if ancestor.create_dummy_items?.nil?
63
+
64
+ klass.create_dummy_items = ancestor.create_dummy_items?
65
+ break
66
+ end
58
67
  end
59
68
 
60
69
  #
61
- # Looks up a Thing in the OpenHAB registry replacing '_' with ':'
62
- #
63
- # @param [String] name of Thing to lookup in Thing registry
70
+ # Automatically looks up openHAB items and things in appropriate registries
64
71
  #
65
- # @return [Thing] if found, nil otherwise
72
+ # @return [Item, Things::Thing, nil]
66
73
  #
67
- def self.lookup_thing(name)
68
- logger.trace("Looking up thing(#{name})")
69
- # Convert from : syntax to underscore
70
- name = name.to_s if name.is_a? Symbol
74
+ ruby2_keywords def method_missing(method, *args)
75
+ return super unless args.empty? && !block_given?
71
76
 
72
- # Thing UIDs have at least 3 segements
73
- return if name.count('_') < 3
77
+ logger.trace("method missing, performing openHAB Lookup for: #{method}")
78
+ EntityLookup.lookup_entity(method,
79
+ create_dummy_items: self.class.respond_to?(:create_dummy_items?) &&
80
+ self.class.create_dummy_items?) || super
81
+ end
74
82
 
75
- name = name.tr('_', ':')
76
- $things.get(org.openhab.core.thing.ThingUID.new(name))
83
+ # @!visibility private
84
+ def respond_to_missing?(method, *)
85
+ logger.trace("Checking if openHAB entities exist for #{method}")
86
+ EntityLookup.lookup_entity(method) || super
77
87
  end
78
88
 
79
- #
80
- # Lookup OpenHAB items in item registry
81
- #
82
- # @param [String] name of item to lookup
83
- #
84
- # @return [Item] OpenHAB item if registry contains a matching item, nil othewise
85
- #
86
- def self.lookup_item(name)
87
- logger.trace("Looking up item(#{name})")
88
- name = name.to_s if name.is_a? Symbol
89
- item = $ir.get(name)
90
- ItemProxy.new(item) unless item.nil?
89
+ # @!visibility private
90
+ def instance_eval_with_dummy_items(&block)
91
+ DSL::ThreadLocal.thread_local(openhab_create_dummy_items: self.class.create_dummy_items?) do
92
+ instance_eval(&block)
93
+ end
94
+ end
95
+
96
+ class << self
97
+ #
98
+ # Looks up an openHAB entity
99
+ # items are checked first, then things
100
+ #
101
+ # @!visibility private
102
+ #
103
+ # @param [String] name of entity to lookup in item or thing registry
104
+ # @param [true, false] create_dummy_items If a dummy {Item} should be created if an actual item can't be found
105
+ #
106
+ # @return [Item, Things::Thing, nil]
107
+ #
108
+ def lookup_entity(name, create_dummy_items: false)
109
+ # make sure we have a nil return
110
+ create_dummy_items = nil if create_dummy_items == false
111
+ lookup_item(name) || lookup_thing_const(name) || (create_dummy_items && Items::Proxy.new(name.to_sym))
112
+ end
113
+
114
+ #
115
+ # Looks up a Thing in the openHAB registry
116
+ #
117
+ # @!visibility private
118
+ #
119
+ # @param [String] uid name of Thing to lookup in Thing registry
120
+ #
121
+ # @return [Things::Thing, nil]
122
+ #
123
+ def lookup_thing(uid)
124
+ logger.trace("Looking up thing '#{uid}'")
125
+ uid = uid.to_s if uid.is_a?(Symbol)
126
+
127
+ uid = Things::ThingUID.new(uid) unless uid.is_a?(Things::ThingUID)
128
+ thing = $things.get(uid)
129
+ return unless thing
130
+
131
+ logger.trace("Retrieved Thing(#{thing}) from registry for uid: #{uid}")
132
+ Things::Proxy.new(thing)
133
+ end
134
+
135
+ #
136
+ # Looks up a Thing in the openHAB registry replacing `_` with `:`
137
+ #
138
+ # @!visibility private
139
+ #
140
+ # @param [String] name of Thing to lookup in Thing registry
141
+ #
142
+ # @return [Things::Thing, nil]
143
+ #
144
+ def lookup_thing_const(name)
145
+ name = name.to_s if name.is_a?(Symbol)
146
+
147
+ if name.is_a?(String)
148
+ # Thing UIDs have at least 3 segments, separated by `_`
149
+ return if name.count("_") < 2
150
+
151
+ # Convert from _ syntax to :
152
+ name = name.tr("_", ":")
153
+ end
154
+ lookup_thing(name)
155
+ end
156
+
157
+ #
158
+ # Lookup openHAB items in item registry
159
+ #
160
+ # @!visibility private
161
+ #
162
+ # @param [String] name of item to lookup
163
+ #
164
+ # @return [Item, nil]
165
+ #
166
+ def lookup_item(name)
167
+ logger.trace("Looking up item '#{name}'")
168
+ name = name.to_s if name.is_a?(Symbol)
169
+ item = $ir.get(name)
170
+ Items::Proxy.new(item) unless item.nil?
171
+ end
91
172
  end
92
173
  end
93
174
  end
94
175
  end
95
176
 
96
177
  #
97
- # Implements const_missing to return OpenHAB items or things if mapping to missing name if they exist
178
+ # Implements const_missing to return openHAB items or things if mapping to missing name if they exist
98
179
  #
99
180
  # @param [String] name Capital string that was not set as a constant and to be looked up
100
181
  #
101
- # @return [Object] OpenHAB Item or Thing if their name exist in OpenHAB item and thing regestries
182
+ # @return [Object] openHAB Item or Thing if their name exist in openHAB item and thing regestries
102
183
  #
103
184
  def Object.const_missing(name)
104
- OpenHAB::Core::EntityLookup.lookup_entity(name) || super
185
+ OpenHAB::Core::EntityLookup.lookup_entity(name,
186
+ create_dummy_items: Thread.current[:openhab_create_dummy_items]) || super
105
187
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Events
6
+ java_import org.openhab.core.events.AbstractEvent
7
+
8
+ # Add attachments event data.
9
+ class AbstractEvent
10
+ # @return [Object]
11
+ attr_accessor :attachment
12
+
13
+ # @return [String]
14
+ alias_method :inspect, :to_s
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Events
6
+ java_import org.openhab.core.items.events.AbstractItemRegistryEvent,
7
+ org.openhab.core.items.events.ItemAddedEvent,
8
+ org.openhab.core.items.events.ItemUpdatedEvent,
9
+ org.openhab.core.items.events.ItemRemovedEvent
10
+
11
+ #
12
+ # The {AbstractEvent} sent when an {Item} is added, updated, or removed
13
+ # from its registry.
14
+ #
15
+ # @!attribute [r] item
16
+ # @return [DTO::ItemDTO] The item that triggered this event.
17
+ #
18
+ class AbstractItemRegistryEvent < AbstractEvent; end
19
+
20
+ #
21
+ # The {AbstractEvent} sent with an `item_added` trigger.
22
+ #
23
+ class ItemAddedEvent < AbstractItemRegistryEvent; end
24
+
25
+ #
26
+ # The {AbstractEvent} sent with an `item_updated` trigger.
27
+ #
28
+ class ItemUpdatedEvent < AbstractItemRegistryEvent; end
29
+
30
+ #
31
+ # The {AbstractEvent} sent with an `item_removed` trigger.
32
+ #
33
+ class ItemRemovedEvent < AbstractItemRegistryEvent; end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Events
6
+ java_import org.openhab.core.thing.events.AbstractThingRegistryEvent,
7
+ org.openhab.core.thing.events.ThingAddedEvent,
8
+ org.openhab.core.thing.events.ThingUpdatedEvent,
9
+ org.openhab.core.thing.events.ThingRemovedEvent
10
+
11
+ #
12
+ # The {AbstractEvent} sent when a {Things::Thing Thing} is added,
13
+ # updated, or removed from its registry.
14
+ #
15
+ #
16
+ # @!attribute [r] thing
17
+ # @return [DTO::AbstractThingDTO] The thing that triggered this event.
18
+ #
19
+ class AbstractThingRegistryEvent < AbstractEvent; end
20
+
21
+ #
22
+ # The {AbstractEvent} sent with a
23
+ # {DSL::Rules::BuilderDSL#thing_added thing_added trigger}.
24
+ #
25
+ class ThingAddedEvent < AbstractThingRegistryEvent; end
26
+
27
+ #
28
+ # The {AbstractEvent} sent with a
29
+ # {DSL::Rules::BuilderDSL#thing_updated thing_updated trigger}.
30
+ #
31
+ class ThingUpdatedEvent < AbstractThingRegistryEvent; end
32
+
33
+ #
34
+ # The {AbstractEvent} sent with a
35
+ # {DSL::Rules::BuilderDSL#thing_removed thing_removed trigger}.
36
+ #
37
+ class ThingRemovedEvent < AbstractThingRegistryEvent; end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "item_event"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Events
8
+ java_import org.openhab.core.items.events.ItemCommandEvent
9
+
10
+ # {AbstractEvent} sent when an item receives a command.
11
+ class ItemCommandEvent < ItemEvent
12
+ # @!attribute [r] command
13
+ # @return [Command] The command sent to the item.
14
+ alias_method :command, :item_command
15
+
16
+ # @!method refresh?
17
+ # Check if {#command} is {REFRESH}
18
+ # @return [true, false]
19
+
20
+ # @!method on?
21
+ # Check if {#command} is (implicitly convertible to) {ON}
22
+ # @return [true, false]
23
+
24
+ # @!method off?
25
+ # Check if {#command} is (implicitly convertible to) {OFF}
26
+ # @return [true, false]
27
+
28
+ # @!method up?
29
+ # Check if {#command} is (implicitly convertible to) {UP}
30
+ # @return [true, false]
31
+
32
+ # @!method down?
33
+ # Check if {#command} is (implicitly convertible to) {DOWN}
34
+ # @return [true, false]
35
+
36
+ # @!method stop?
37
+ # Check if {#command} is {STOP}
38
+ # @return [true, false]
39
+
40
+ # @!method move?
41
+ # Check if {#command} is {MOVE}
42
+ # @return [true, false]
43
+
44
+ # @!method increase?
45
+ # Check if {#command} is {INCREASE}
46
+ # @return [true, false]
47
+
48
+ # @!method decrease?
49
+ # Check if {#command} is {DECREASE}
50
+ # @return [true, false]
51
+
52
+ # @!method play?
53
+ # Check if {#command} is {PLAY}
54
+ # @return [true, false]
55
+
56
+ # @!method pause?
57
+ # Check if {#command} is {PAUSE}
58
+ # @return [true, false]
59
+
60
+ # @!method rewind?
61
+ # Check if {#command} is {REWIND}
62
+ # @return [true, false]
63
+
64
+ # @!method fast_forward?
65
+ # Check if {#command} is {FASTFORWARD}
66
+ # @return [true, false]
67
+
68
+ # @!method next?
69
+ # Check if {#command} is {NEXT}
70
+ # @return [true, false]
71
+
72
+ # @!method previous?
73
+ # Check if {#command} is {PREVIOUS}
74
+ # @return [true, false]
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Events
6
+ java_import org.openhab.core.items.events.ItemEvent
7
+
8
+ #
9
+ # Adds methods to core openHAB ItemEvent to make it more natural in Ruby
10
+ #
11
+ class ItemEvent < AbstractEvent
12
+ #
13
+ # @!attribute [r] item
14
+ # @return [Item] The item that triggered this event.
15
+ #
16
+ def item
17
+ EntityLookup.lookup_item(item_name)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "item_state_event"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Events
8
+ java_import org.openhab.core.items.events.ItemStateChangedEvent
9
+
10
+ #
11
+ # {AbstractEvent} sent when an item's state has changed.
12
+ #
13
+ class ItemStateChangedEvent < ItemEvent
14
+ include ItemState
15
+
16
+ # @!method was_undef?
17
+ # Check if {#was} is {UNDEF}
18
+ # @return [true, false]
19
+
20
+ # @!method was_null?
21
+ # Check if {#was} is {NULL}
22
+ # @return [true, false]
23
+
24
+ # @!method was_on?
25
+ # Check if {#was} is (implicitly convertible to) {ON}
26
+ # @return [true, false]
27
+
28
+ # @!method was_off?
29
+ # Check if {#was} is (implicitly convertible to) {OFF}
30
+ # @return [true, false]
31
+
32
+ # @!method was_up?
33
+ # Check if {#was} is (implicitly convertible to) {UP}
34
+ # @return [true, false]
35
+
36
+ # @!method was_down?
37
+ # Check if {#was} is (implicitly convertible to) {DOWN}
38
+ # @return [true, false]
39
+
40
+ # @!method was_open?
41
+ # Check if {#was} is (implicitly convertible to) {OPEN}
42
+ # @return [true, false]
43
+
44
+ # @!method was_closed?
45
+ # Check if {#was} is (implicitly convertible to) {CLOSED}
46
+ # @return [true, false]
47
+
48
+ # @!method was_playing?
49
+ # Check if {#was} is {PLAY}
50
+ # @return [true, false]
51
+
52
+ # @!method was_paused?
53
+ # Check if {#was} is {PAUSE}
54
+ # @return [true, false]
55
+
56
+ #
57
+ # Check if state was defined (not {UNDEF} or {NULL})
58
+ #
59
+ # @return [true,false]
60
+ #
61
+ def was?
62
+ !old_item_state.is_a?(UnDefType)
63
+ end
64
+
65
+ #
66
+ # @!attribute [r] was
67
+ # @return [State, nil] the prior state of the item if it was not {UNDEF} or {NULL}, `nil` otherwise.
68
+ #
69
+ def was
70
+ old_item_state if was?
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Events
6
+ java_import org.openhab.core.items.events.ItemStateEvent
7
+
8
+ #
9
+ # Helpers common to {ItemStateEvent} and {ItemStateChangedEvent}.
10
+ #
11
+ # Methods that refer to implicit conversion mean that for example
12
+ # a PercentType of 100% will be `true` for {#on?}, etc.
13
+ #
14
+ module ItemState
15
+ # @!method undef?
16
+ # Check if {#state} is {UNDEF}
17
+ # @return [true, false]
18
+
19
+ # @!method null?
20
+ # Check if {#state} is {NULL}
21
+ # @return [true, false]
22
+
23
+ # @!method on?
24
+ # Check if {#state} is (implicitly convertible to) {ON}
25
+ # @return [true, false]
26
+
27
+ # @!method off?
28
+ # Check if {#state} is (implicitly convertible to) {OFF}
29
+ # @return [true, false]
30
+
31
+ # @!method up?
32
+ # Check if {#state} is (implicitly convertible to) {UP}
33
+ # @return [true, false]
34
+
35
+ # @!method down?
36
+ # Check if {#state} is (implicitly convertible to) {DOWN}
37
+ # @return [true, false]
38
+
39
+ # @!method open?
40
+ # Check if {#state} is (implicitly convertible to) {OPEN}
41
+ # @return [true, false]
42
+
43
+ # @!method closed?
44
+ # Check if {#state} is (implicitly convertible to) {CLOSED}
45
+ # @return [true, false]
46
+
47
+ # @!method playing?
48
+ # Check if {#state} is {PLAY}
49
+ # @return [true, false]
50
+
51
+ # @!method paused?
52
+ # Check if {#state} is {PAUSE}
53
+ # @return [true, false]
54
+
55
+ #
56
+ # Check if {#state} is defined (not {UNDEF} or {NULL})
57
+ #
58
+ # @return [true, false]
59
+ #
60
+ def state?
61
+ !item_state.is_a?(UnDefType)
62
+ end
63
+
64
+ #
65
+ # @!attribute [r] state
66
+ # @return [State, nil] the state of the item if it is not {UNDEF} or {NULL}, `nil` otherwise.
67
+ #
68
+ def state
69
+ item_state if state?
70
+ end
71
+ end
72
+
73
+ # {AbstractEvent} sent when an item's state is updated (regardless of if it changed or not).
74
+ class ItemStateEvent < ItemEvent
75
+ include ItemState
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Events
6
+ java_import org.openhab.core.thing.events.ThingStatusInfoChangedEvent,
7
+ org.openhab.core.thing.events.ThingStatusInfoEvent
8
+
9
+ #
10
+ # The {AbstractEvent} sent when a {Things::Thing Thing's} status has changed.
11
+ #
12
+ class ThingStatusInfoChangedEvent < AbstractEvent
13
+ # @!attribute [r] uid
14
+ # @return [Things::ThingUID] The UID of the {Things::Thing Thing} that triggered this event.
15
+ alias_method :uid, :get_thing_uid
16
+ # @!attribute [r] was
17
+ # @return [org.openhab.core.thing.ThingStatusInfo] The thing's prior status.
18
+ alias_method :was, :get_old_status_info
19
+ # @!attribute [r] status
20
+ # @return [org.openhab.core.thing.ThingStatusInfo] The thing's status.
21
+ alias_method :status, :status_info
22
+
23
+ #
24
+ # @!attribute [r] thing
25
+ # @return [Things::Thing] The thing that triggered this event.
26
+ #
27
+ def thing
28
+ EntityLookup.lookup_thing(uid)
29
+ end
30
+ end
31
+
32
+ # The {AbstractEvent} sent when a {Things::Thing}'s status is set.
33
+ class ThingStatusInfoEvent < AbstractEvent
34
+ #
35
+ # @!attribute [r] uid
36
+ # @return [Things::ThingUID] The UID of the {Things::Thing Thing} that triggered this event.
37
+ #
38
+ alias_method :uid, :get_thing_uid
39
+ #
40
+ # @!attribute [r] status
41
+ # @return [org.openhab.core.thing.ThingStatusInfo] The thing's status.
42
+ #
43
+ alias_method :status, :status_info
44
+
45
+ #
46
+ # @!attribute [r] thing
47
+ # @return [Things::Thing] The thing that triggered this event.
48
+ #
49
+ def thing
50
+ EntityLookup.lookup_thing(uid)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ # Contains objects sent to event handlers containing context around the
6
+ # triggered event.
7
+ module Events
8
+ end
9
+ end
10
+ end