openhab-scripting 4.46.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,32 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'forwardable'
4
- require 'time'
5
- require 'java'
3
+ require "forwardable"
4
+ require "time"
5
+
6
+ require_relative "type"
6
7
 
7
8
  module OpenHAB
8
- module DSL
9
+ module Core
9
10
  module Types
10
11
  DateTimeType = org.openhab.core.library.types.DateTimeType
11
12
 
12
- # global alias - required for jrubyscripting addon <= OH3.2.0
13
- ::DateTimeType = DateTimeType if ::DateTimeType.is_a?(java.lang.Class)
14
-
15
- # @deprecated
16
- # Backwards-compatible alias
17
- DateTime = DateTimeType
18
-
19
- # rubocop: disable Metrics/ClassLength class has a single responsibility
20
-
21
- #
22
- # Add methods to core OpenHAB DateTimeType to make it behave as a Ruby
23
- # Time object
24
- #
25
- # Any method not explicitly defined is forwarded to the +ZonedDateTime+
26
- # or +Time+ representation of this object as appropriate.
27
- #
13
+ # {DateTimeType} uses a {ZonedDateTime} internally.
28
14
  class DateTimeType
29
- # @!parse include Type
15
+ # @!parse include Command, State
30
16
 
31
17
  # remove the JRuby default == so that we can inherit the Ruby method
32
18
  remove_method :==
@@ -47,8 +33,8 @@ module OpenHAB
47
33
 
48
34
  class << self
49
35
  #
50
- # Parses a String representing a time into an OpenHAB DateTimeType. First tries to parse it
51
- # using java's DateTimeType's parser, then falls back to the Ruby Time.parse
36
+ # Parses a String representing a time into a {DateTimeType}. First tries to parse it
37
+ # using Java's parser, then falls back to the Ruby `Time.parse`.
52
38
  #
53
39
  # @param [String] time_string
54
40
  #
@@ -57,32 +43,25 @@ module OpenHAB
57
43
  def parse(time_string)
58
44
  time_string = "#{time_string}Z" if TIME_ONLY_REGEX.match?(time_string)
59
45
  DateTimeType.new(time_string)
60
- rescue java.lang.StringIndexOutOfBoundsException, java.lang.IllegalArgumentException
61
- # Try ruby's Time.parse if OpenHAB's DateTimeType parser fails
46
+ rescue java.lang.StringIndexOutOfBoundsException, java.lang.IllegalArgumentException => e
47
+ # Try Ruby's Time.parse if DateTimeType parser fails
62
48
  begin
63
- DateTimeType.new(::Time.parse(time_string))
49
+ DateTimeType.new(::Time.parse(time_string).to_zoned_date_time)
64
50
  rescue ArgumentError
65
- raise ArgumentError, "Unable to parse #{time_string} into a DateTimeType"
51
+ raise ArgumentError, e.message
66
52
  end
67
53
  end
54
+ end
68
55
 
69
- # parses a String representing a duration
70
- #
71
- # for internal use
72
- #
73
- # @return [java.time.Duration]
74
- #
75
- # @!visibility private
76
- def parse_duration(time_string)
77
- # convert from common HH:MM to ISO8601 for parsing
78
- if (match = time_string.match(TIME_ONLY_REGEX))
79
- time_string = "PT#{match[:hours]}H#{match[:minutes]}M#{match[:seconds] || 0}S"
80
- end
81
- java.time.Duration.parse(time_string)
82
- end
56
+ # @param [ZonedDateTime, nil] context
57
+ # A {ZonedDateTime} used to fill in missing
58
+ # fields during conversion. Not used in this class.
59
+ # @return [ZonedTimeTime]
60
+ def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
61
+ zoned_date_time
83
62
  end
84
63
 
85
- # act like a ruby Time
64
+ # act like a Ruby Time
86
65
  def_delegator :zoned_date_time, :month_value, :month
87
66
  def_delegator :zoned_date_time, :day_of_month, :mday
88
67
  def_delegator :zoned_date_time, :day_of_year, :yday
@@ -90,32 +69,29 @@ module OpenHAB
90
69
  def_delegator :zoned_date_time, :second, :sec
91
70
  def_delegator :zoned_date_time, :nano, :nsec
92
71
  def_delegator :zoned_date_time, :to_epoch_second, :to_i
72
+ def_delegator :zoned_date_time, :to_time
93
73
 
94
- alias day mday
74
+ # @!visibility private
75
+ alias_method :day, :mday
95
76
 
96
77
  #
97
78
  # Create a new instance of DateTimeType
98
79
  #
99
- # @param value [ZonedDateTime, Time, String, Numeric]
80
+ # @param value [#to_zoned_date_time, #to_time, #to_str, #to_d, nil]
100
81
  #
101
- def initialize(value = nil) # rubocop:disable Metrics
102
- if value.respond_to?(:to_time)
103
- time = value.to_time
104
- instant = java.time.Instant.ofEpochSecond(time.to_i, time.nsec)
105
- zone_id = java.time.ZoneId.of_offset('UTC', java.time.ZoneOffset.of_total_seconds(time.utc_offset))
106
- super(ZonedDateTime.ofInstant(instant, zone_id))
82
+ def initialize(value = nil)
83
+ if value.respond_to?(:to_zoned_date_time)
84
+ super(value.to_zoned_date_time)
85
+ return
86
+ elsif value.respond_to?(:to_time)
87
+ super(value.to_time.to_zoned_date_time)
107
88
  return
108
89
  elsif value.respond_to?(:to_str)
109
90
  # strings respond_do?(:to_d), but we want to avoid that conversion
110
91
  super(value.to_str)
111
92
  return
112
93
  elsif value.respond_to?(:to_d)
113
- time = value.to_d
114
- super(ZonedDateTime.ofInstant(
115
- java.time.Instant.ofEpochSecond(time.to_i,
116
- ((time % 1) * 1_000_000_000).to_i),
117
- java.time.ZoneId.systemDefault
118
- ))
94
+ super(Time.at(value.to_d).to_zoned_date_time)
119
95
  return
120
96
  end
121
97
 
@@ -125,7 +101,7 @@ module OpenHAB
125
101
  #
126
102
  # Check equality without type conversion
127
103
  #
128
- # @return [Boolean] if the same value is represented, without type
104
+ # @return [true,false] if the same value is represented, without type
129
105
  # conversion
130
106
  def eql?(other)
131
107
  return false unless other.instance_of?(self.class)
@@ -136,31 +112,19 @@ module OpenHAB
136
112
  #
137
113
  # Comparison
138
114
  #
139
- # @param [DateTimeType, Items::DateTimeItem, Time,
140
- # String] other object to compare to
115
+ # @param [Object] other object to compare to
141
116
  #
142
- # @return [Integer, nil] -1, 0, +1 depending on whether +other+ is
117
+ # @return [Integer, nil] -1, 0, +1 depending on whether `other` is
143
118
  # less than, equal to, or greater than self
144
119
  #
145
- # nil is returned if the two values are incomparable
120
+ # `nil` is returned if the two values are incomparable.
146
121
  #
147
- def <=>(other) # rubocop:disable Metrics
122
+ def <=>(other)
148
123
  logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
149
124
  if other.is_a?(self.class)
150
- zoned_date_time.to_instant.compare_to(other.zoned_date_time.to_instant)
151
- elsif other.is_a?(Items::DateTimeItem) ||
152
- (other.is_a?(Items::GroupItem) && other.base_item.is_a?(Items::DateTimeItem))
153
- return nil unless other.state?
154
-
155
- zoned_date_time.to_instant.compare_to(other.state.zoned_date_time.to_instant)
156
- elsif other.is_a?(TimeOfDay::TimeOfDay) || other.is_a?(TimeOfDay::TimeOfDayRangeElement)
157
- to_tod <=> other
125
+ zoned_date_time <=> other.zoned_date_time
158
126
  elsif other.respond_to?(:to_time)
159
127
  to_time <=> other.to_time
160
- elsif other.respond_to?(:to_str)
161
- time_string = other.to_str
162
- time_string = "#{time_string}T00:00:00#{zone}" if DATE_ONLY_REGEX.match?(time_string)
163
- self <=> DateTimeType.parse(time_string)
164
128
  elsif other.respond_to?(:coerce)
165
129
  return nil unless (lhs, rhs = other.coerce(self))
166
130
 
@@ -173,42 +137,16 @@ module OpenHAB
173
137
  #
174
138
  # Coerce object to a DateTimeType
175
139
  #
176
- # @param [Items::DateTimeItem, Time] other object to coerce to a
177
- # DateTimeType
140
+ # @param [Time] other object to coerce to a DateTimeType
178
141
  #
179
- # @return [[DateTimeType, DateTimeType]]
142
+ # @return [[DateTimeType, DateTimeType], nil]
180
143
  #
181
144
  def coerce(other)
182
145
  logger.trace("Coercing #{self} as a request from #{other.class}")
183
- if other.is_a?(Items::DateTimeItem)
184
- return unless other.state?
185
-
186
- [other.state, self]
187
- elsif other.respond_to?(:to_time)
188
- [DateTimeType.new(other), self]
189
- end
190
- end
191
-
192
- #
193
- # Convert this DateTimeType to a ruby Time object
194
- #
195
- # @return [Time] A Time object representing the same instant and timezone
196
- #
197
- def to_time
198
- ::Time.at(to_i, nsec, :nsec).localtime(utc_offset)
146
+ return [other, zoned_date_time] if other.respond_to?(:to_zoned_date_time)
147
+ return [DateTimeType.new(other), self] if other.respond_to?(:to_time)
199
148
  end
200
149
 
201
- #
202
- # Convert the time part of this DateTimeType to a TimeOfDay object
203
- #
204
- # @return [TimeOfDay] A TimeOfDay object representing the time
205
- #
206
- def to_time_of_day
207
- TimeOfDay::TimeOfDay.new(h: hour, m: minute, s: second)
208
- end
209
-
210
- alias to_tod to_time_of_day
211
-
212
150
  #
213
151
  # Returns the value of time as a floating point number of seconds since the Epoch
214
152
  #
@@ -230,7 +168,7 @@ module OpenHAB
230
168
  #
231
169
  # Returns true if time represents a time in UTC (GMT)
232
170
  #
233
- # @return [Boolean] true if utc_offset == 0, false otherwise
171
+ # @return [true,false] true if utc_offset == 0, false otherwise
234
172
  #
235
173
  def utc?
236
174
  utc_offset.zero?
@@ -263,7 +201,7 @@ module OpenHAB
263
201
  end
264
202
 
265
203
  #
266
- # Forward missing methods to the +ZonedDateTime+ object or a ruby +Time+
204
+ # Forward missing methods to the `ZonedDateTime` object or a ruby `Time`
267
205
  # object representing the same instant
268
206
  #
269
207
  def method_missing(method, *args, &block)
@@ -275,17 +213,14 @@ module OpenHAB
275
213
 
276
214
  # Add other to self
277
215
  #
278
- # @param other [java.time.Duration, String, Numeric]
216
+ # @param other [Duration, Numeric]
279
217
  #
280
218
  # @return [DateTimeType]
281
- def +(other) # rubocop:disable Metrics
282
- if other.is_a?(java.time.Duration)
219
+ def +(other)
220
+ if other.is_a?(Duration)
283
221
  DateTimeType.new(zoned_date_time.plus(other))
284
- elsif other.respond_to?(:to_str)
285
- other = self.class.parse_duration(other.to_str)
286
- self + other
287
222
  elsif other.respond_to?(:to_d)
288
- DateTimeType.new(zoned_date_time.plusNanos((other.to_d * 1_000_000_000).to_i))
223
+ DateTimeType.new(zoned_date_time.plus_nanos((other.to_d * 1_000_000_000).to_i))
289
224
  elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d))
290
225
  lhs + rhs
291
226
  else
@@ -301,24 +236,16 @@ module OpenHAB
301
236
  # if other is a DateTime-like object, the result is a Duration
302
237
  # representing how long between the two instants in time.
303
238
  #
304
- # @param other [java.time.Duration, Time, String, Numeric]
239
+ # @param other [Duration, Time, Numeric]
305
240
  #
306
- # @return [DateTimeType, java.Time.Duration]
307
- def -(other) # rubocop:disable Metrics
308
- if other.is_a?(java.time.Duration)
241
+ # @return [DateTimeType, Duration]
242
+ def -(other)
243
+ if other.is_a?(Duration)
309
244
  DateTimeType.new(zoned_date_time.minus(other))
310
245
  elsif other.respond_to?(:to_time)
311
246
  to_time - other.to_time
312
- elsif other.respond_to?(:to_str)
313
- time_string = other.to_str
314
- other = if TIME_ONLY_REGEX.match?(time_string)
315
- self.class.parse_duration(time_string)
316
- else
317
- DateTimeType.parse(time_string)
318
- end
319
- self - other
320
247
  elsif other.respond_to?(:to_d)
321
- DateTimeType.new(zoned_date_time.minusNanos((other.to_d * 1_000_000_000).to_i))
248
+ DateTimeType.new(zoned_date_time.minus_nanos((other.to_d * 1_000_000_000).to_i))
322
249
  elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d))
323
250
  lhs - rhs
324
251
  else
@@ -326,7 +253,8 @@ module OpenHAB
326
253
  end
327
254
  end
328
255
  end
329
- # rubocop: enable Metrics/ClassLength
330
256
  end
331
257
  end
332
258
  end
259
+
260
+ # @!parse DateTimeType = OpenHAB::Core::Types::DateTimeType
@@ -1,32 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'comparable_type'
4
- require_relative 'numeric_type'
3
+ require_relative "comparable_type"
4
+ require_relative "numeric_type"
5
+
6
+ require_relative "type"
5
7
 
6
8
  module OpenHAB
7
- module DSL
9
+ module Core
8
10
  module Types
9
11
  DecimalType = org.openhab.core.library.types.DecimalType
10
12
 
11
13
  #
12
- # Add methods to core OpenHAB DecimalType to make it behave as a Ruby
13
- # BigDecimal object
14
+ # {DecimalType} uses a {java.math.BigDecimal} internally and thus can be
15
+ # used for integers, longs and floating point numbers alike.
16
+ #
17
+ # @example DecimalType can be used in case statements with ranges
18
+ # # Check if number item is less than 50
19
+ # case NumberOne.state
20
+ # when (0...50)
21
+ # logger.info("#{NumberOne} is less than 50")
22
+ # when (50..100)
23
+ # logger.info("#{NumberOne} is greater than 50")
24
+ # end
14
25
  #
26
+ # @example DecimalType can be compared directly against Numeric
27
+ # if NumberOne.state > 10
28
+ # logger.info("Item #{NumberOne.name} is greater than 10")
29
+ # end
15
30
  #
16
- # Any method not explicitly defined is forwarded to the +BigDecimal+
17
- # representation of this object.
31
+ # @example DecimalType can be compared against the value of another DecimalType
32
+ # if NumberOne.state > NumberTwo.state
33
+ # logger.info("Item #{NumberOne} (#{NumberOne.state}) is greater than #{NumberTwo} (#{NumberTwo.state})")
34
+ # end
18
35
  #
19
36
  class DecimalType
20
- # @!parse include Type
37
+ # @!parse include Command, State
21
38
  include NumericType
22
39
  include ComparableType
23
40
 
41
+ #
42
+ # @!method initialize(value)
24
43
  #
25
44
  # Create a new instance of DecimalType
26
45
  #
27
- # @param [java.math.BigDecimal, Items::NumericItem, Numeric] args Create a DecimalType from the given value
46
+ # @param [java.math.BigDecimal, Numeric] value Create a DecimalType from the given value
28
47
  #
29
- def initialize(*args) # rubocop:disable Metrics
48
+ def initialize(*args)
30
49
  unless args.length == 1
31
50
  super
32
51
  return
@@ -39,9 +58,6 @@ module OpenHAB
39
58
  super(value.to_java.strip_trailing_zeros)
40
59
  elsif value.is_a?(DecimalType)
41
60
  super(value.to_big_decimal)
42
- elsif value.is_a?(Items::NumericItem) ||
43
- (value.is_a?(Items::GroupItem) && value.base_item.is_a?(Items::NumericItem))
44
- super(value.state)
45
61
  elsif value.respond_to?(:to_d)
46
62
  super(value.to_d.to_java.strip_trailing_zeros)
47
63
  else # rubocop:disable Lint/DuplicateBranch
@@ -54,9 +70,9 @@ module OpenHAB
54
70
  #
55
71
  # Convert DecimalType to a QuantityType
56
72
  #
57
- # @param [Object] other String or Unit representing an OpenHAB Unit
73
+ # @param [String, javax.measure.units.Unit] other
58
74
  #
59
- # @return [QuantityType] +self+ as a {QuantityType} of the supplied Unit
75
+ # @return [QuantityType] `self` as a {QuantityType} of the supplied Unit
60
76
  #
61
77
  def |(other)
62
78
  other = org.openhab.core.types.util.UnitUtils.parse_unit(other.to_str) if other.respond_to?(:to_str)
@@ -66,25 +82,20 @@ module OpenHAB
66
82
  #
67
83
  # Comparison
68
84
  #
69
- # @param [NumericType, Items::NumericItem, Numeric]
85
+ # @param [NumericType, Numeric]
70
86
  # other object to compare to
71
87
  #
72
- # @return [Integer, nil] -1, 0, +1 depending on whether +other+ is
88
+ # @return [Integer, nil] -1, 0, +1 depending on whether `other` is
73
89
  # less than, equal to, or greater than self
74
90
  #
75
- # nil is returned if the two values are incomparable
91
+ # `nil` is returned if the two values are incomparable.
76
92
  #
77
- def <=>(other) # rubocop:disable Metrics
93
+ def <=>(other)
78
94
  logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
79
- if other.is_a?(QuantityType)
95
+ if other.is_a?(QuantityType) || other.is_a?(HSBType)
80
96
  (other <=> self)&.-@
81
97
  elsif other.is_a?(self.class)
82
98
  compare_to(other)
83
- elsif other.is_a?(Items::NumericItem) ||
84
- (other.is_a?(Items::GroupItem) && other.base_item.is_a?(NumericItem))
85
- return nil unless other.state?
86
-
87
- self <=> other.state
88
99
  elsif other.respond_to?(:to_d)
89
100
  to_d <=> other.to_d
90
101
  elsif other.respond_to?(:coerce)
@@ -97,28 +108,17 @@ module OpenHAB
97
108
  #
98
109
  # Type Coercion
99
110
  #
100
- # Coerce object to a DecimalType
111
+ # Coerce object to a {DecimalType DecimalType}
101
112
  #
102
- # @param [Items::NumericItem, Numeric, Type] other object to
103
- # coerce to a {DecimalType}
113
+ # @param [Numeric] other object to coerce to a {DecimalType DecimalType}
104
114
  #
105
- # if +other+ is a {Type}, +self+ will instead be coerced
106
- # to that type to accomodate comparison with things such as {OnOffType}
115
+ # @return [Array<(DecimalType, DecimalType)>, nil]
107
116
  #
108
- # @return [[DecimalType, DecimalType]]
109
- #
110
- def coerce(other) # rubocop:disable Metrics
117
+ def coerce(other)
111
118
  logger.trace("Coercing #{self} as a request from #{other.class}")
112
- if other.is_a?(Items::NumericItem) ||
113
- (other.is_a?(Items::GroupItem) && other.base_item.is_a?(Items::NumericItem))
114
- return unless other.state?
119
+ return unless other.respond_to?(:to_d)
115
120
 
116
- [other.state, self]
117
- elsif other.is_a?(Type)
118
- [other, as(other.class)]
119
- elsif other.respond_to?(:to_d)
120
- [self.class.new(other.to_d), self]
121
- end
121
+ [self.class.new(other.to_d), self]
122
122
  end
123
123
 
124
124
  #
@@ -132,12 +132,12 @@ module OpenHAB
132
132
  end
133
133
 
134
134
  {
135
- :add => :+,
136
- :subtract => :-,
137
- :multiply => :*,
138
- :divide => :/,
139
- :remainder => :%,
140
- :pow => :**
135
+ add: :+,
136
+ subtract: :-,
137
+ multiply: :*,
138
+ divide: :/,
139
+ remainder: :%,
140
+ pow: :**
141
141
  }.each do |java_op, ruby_op|
142
142
  class_eval( # rubocop:disable Style/DocumentDynamicEvalDefinition https://github.com/rubocop/rubocop/issues/10179
143
143
  # def +(other)
@@ -183,3 +183,5 @@ module OpenHAB
183
183
  end
184
184
  end
185
185
  end
186
+
187
+ # @!parse DecimalType = OpenHAB::Core::Types::DecimalType