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
@@ -0,0 +1,416 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+
5
+ require_relative "generic_item"
6
+
7
+ module OpenHAB
8
+ module Core
9
+ module Items
10
+ #
11
+ # Items extensions to support
12
+ # {https://www.openhab.org/docs/configuration/persistence.html openHAB's Persistence} feature.
13
+ #
14
+ # @see OpenHAB::DSL.persistence Persistence Block
15
+ #
16
+ # @example The following examples are based on these items
17
+ # Number UV_Index
18
+ # Number:Power Power_Usage "Power Usage [%.2f W]"
19
+ #
20
+ # @example Getting persistence data from the system default persistence service
21
+ # UV_Index.average_since(1.hour.ago) # returns a DecimalType
22
+ # Power_Usage.average_since(12.hours.ago) # returns a QuantityType that corresponds to the item's type
23
+ #
24
+ # @example Querying a non-default persistence service
25
+ # UV_Index.average_since(1.hour.ago, :influxdb)
26
+ # Power_Usage.average_since(12.hours.ago, :rrd4j)
27
+ #
28
+ # @example Comparison using Quantity
29
+ # # Because Power_Usage has a unit, the return value
30
+ # # from average_since is a QuantityType which can be
31
+ # # compared against a string with quantity
32
+ # if Power_Usage.average_since(15.minutes.ago) > 5 | "kW"
33
+ # logger.info("The power usage exceeded its 15 min average)
34
+ # end
35
+ #
36
+ # @example HistoricState
37
+ # max = Power_Usage.maximum_since(LocalTime::MIDNIGHT)
38
+ # logger.info("Max power usage today: #{max}, at: #{max.timestamp})
39
+ #
40
+ module Persistence
41
+ GenericItem.prepend(self)
42
+
43
+ #
44
+ # A state class with an added timestamp attribute.
45
+ #
46
+ # This wraps {org.openhab.core.persistence.HistoricItem HistoricItem}
47
+ # to allow implicitly treating the object as its state, and wrapping of
48
+ # that state into a {QuantityType} as necessary.
49
+ #
50
+ class HistoricState < SimpleDelegator
51
+ # @!attribute [r] state
52
+ # @return [Types::State]
53
+ alias_method :state, :__getobj__
54
+
55
+ def initialize(state, historic_item)
56
+ @historic_item = historic_item
57
+ super(state)
58
+ end
59
+
60
+ # @!attribute [r] timestamp
61
+ # @return [ZonedDateTime]
62
+ def timestamp
63
+ @historic_item.timestamp
64
+ end
65
+ end
66
+
67
+ # All persistence methods that could return a QuantityType
68
+ QUANTITY_METHODS = %i[average_since
69
+ delta_since
70
+ deviation_since
71
+ sum_since
72
+ variance_since].freeze
73
+
74
+ # All persistence methods that require a timestamp
75
+ # Note the _between methods are automatically created from the _since methods
76
+ PERSISTENCE_METHODS = (QUANTITY_METHODS +
77
+ %i[changed_since?
78
+ count_since
79
+ count_state_changes_since
80
+ historic_state
81
+ maximum_since
82
+ minimum_since
83
+ updated_since?]).freeze
84
+ private_constant :QUANTITY_METHODS, :PERSISTENCE_METHODS
85
+
86
+ # @!method persist(service = nil)
87
+ # Persists the state of the item
88
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
89
+ # @return [void]
90
+
91
+ # @!method last_update(service = nil)
92
+ # Returns the time the item was last updated.
93
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
94
+ # @return [ZonedDateTime, nil] The timestamp of the last update
95
+
96
+ # @!method average_since(timestamp, service = nil)
97
+ # Returns the average value of the item's state since the given time
98
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
99
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
100
+ # @return [DecimalType, QuantityType, nil] The average value since `timestamp`,
101
+ # or nil if no previous state could be found.
102
+
103
+ # @!method average_between(start, finish, service = nil)
104
+ # Returns the average value of the item's state between two points in time
105
+ # @param [#to_zoned_date_time] start The point in time from which to search
106
+ # @param [#to_zoned_date_time] finish The point in time to which to search
107
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
108
+ # @return [DecimalType, QuantityType, nil] The average value between `start` and `finish`,
109
+ # or nil if no previous state could be found.
110
+
111
+ # @!method delta_since(timestamp, service = nil)
112
+ # Returns the difference value of the item's state since the given time
113
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
114
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
115
+ # @return [DecimalType, QuantityType, nil] The difference value since `timestamp`,
116
+ # or nil if no previous state could be found.
117
+
118
+ # @!method delta_between(start, finish, service = nil)
119
+ # Returns the difference value of the item's state between two points in time
120
+ # @param [#to_zoned_date_time] start The point in time from which to search
121
+ # @param [#to_zoned_date_time] finish The point in time to which to search
122
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
123
+ # @return [DecimalType, QuantityType, nil] The difference value between `start` and `finish`,
124
+ # or nil if no previous state could be found.
125
+
126
+ # @!method deviation_since(timestamp, service = nil)
127
+ # Returns the standard deviation of the item's state since the given time
128
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
129
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
130
+ # @return [DecimalType, QuantityType, nil] The standard deviation since `timestamp`,
131
+ # or nil if no previous state could be found.
132
+
133
+ # @!method deviation_between(start, finish, service = nil)
134
+ # Returns the standard deviation of the item's state between two points in time
135
+ # @param [#to_zoned_date_time] start The point in time from which to search
136
+ # @param [#to_zoned_date_time] finish The point in time to which to search
137
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
138
+ # @return [DecimalType, QuantityType, nil] The standard deviation between `start` and `finish`,
139
+ # or nil if no previous state could be found.
140
+
141
+ # @!method sum_since(timestamp, service = nil)
142
+ # Returns the sum of the item's state since the given time
143
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
144
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
145
+ # @return [DecimalType, QuantityType, nil] The sum since `timestamp`,
146
+ # or nil if no previous state could be found.
147
+
148
+ # @!method sum_between(start, finish, service = nil)
149
+ # Returns the sum of the item's state between two points in time
150
+ # @param [#to_zoned_date_time] start The point in time from which to search
151
+ # @param [#to_zoned_date_time] finish The point in time to which to search
152
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
153
+ # @return [DecimalType, QuantityType, nil] The sum between `start` and `finish`,
154
+ # or nil if no previous state could be found.
155
+
156
+ # @!method variance_since(timestamp, service = nil)
157
+ # Returns the variance of the item's state since the given time
158
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
159
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
160
+ # @return [DecimalType, QuantityType, nil] The variance since `timestamp`,
161
+ # or nil if no previous state could be found.
162
+
163
+ # @!method variance_between(start, finish, service = nil)
164
+ # Returns the variance of the item's state between two points in time
165
+ # @param [#to_zoned_date_time] start The point in time from which to search
166
+ # @param [#to_zoned_date_time] finish The point in time to which to search
167
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
168
+ # @return [DecimalType, QuantityType, nil] The variance between `start` and `finish`,
169
+ # or nil if no previous state could be found.
170
+
171
+ # @!method changed_since?(timestamp, service = nil)
172
+ # Whether the item's state has changed since the given time
173
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
174
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
175
+ # @return [true,false] True if the item's state has changed since the given `timestamp`, False otherwise.
176
+
177
+ # @!method changed_between?(start, finish, service = nil)
178
+ # Whether the item's state changed between two points in time
179
+ # @param [#to_zoned_date_time] start The point in time from which to search
180
+ # @param [#to_zoned_date_time] finish The point in time to which to search
181
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
182
+ # @return [true,false] True if the item's state changed between `start` and `finish`, False otherwise.
183
+
184
+ # @!method evolution_rate(timestamp, service = nil)
185
+ # Returns the evolution rate of the item's state
186
+ # @return [DecimalType, QuantityType, nil] The evolution rate since `timestamp`,
187
+ # or nil if no previous state could be found.
188
+ # @overload evolution_rate(timestamp, service = nil)
189
+ # Returns the evolution rate of the item's state since the given time
190
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
191
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
192
+ # @return [DecimalType, QuantityType, nil] The evolution rate since `timestamp`,
193
+ # or nil if no previous state could be found.
194
+ # @overload evolution_rate(start, finish, service = nil)
195
+ # Returns the evolution rate of the item's state between two points in time
196
+ # @param [#to_zoned_date_time] start The point in time from which to search
197
+ # @param [#to_zoned_date_time] finish The point in time to which to search
198
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
199
+ # @return [DecimalType, QuantityType, nil] The evolution rate between `start` and `finish`,
200
+ # or nil if no previous state could be found.
201
+
202
+ # @!method historic_state(timestamp, service = nil)
203
+ # Returns the the item's state at the given time
204
+ # @param [#to_zoned_date_time] timestamp The point in time at which to search
205
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
206
+ # @return [HistoricState, nil] The item's state at `timestamp`,
207
+ # or nil if no previous state could be found.
208
+
209
+ # @!method maximum_since(timestamp, service = nil)
210
+ # Returns the maximum value of the item's state since the given time
211
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
212
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
213
+ # @return [HistoricState, nil] The maximum value since `timestamp`,
214
+ # or nil if no previous state could be found.
215
+
216
+ # @!method maximum_between(start, finish, service = nil)
217
+ # Returns the maximum value of the item's state between two points in time
218
+ # @param [#to_zoned_date_time] start The point in time from which to search
219
+ # @param [#to_zoned_date_time] finish The point in time to which to search
220
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
221
+ # @return [HistoricState, nil] The maximum value between `start` and `finish`,
222
+ # or nil if no previous state could be found.
223
+
224
+ # @!method minimum_since(timestamp, service = nil)
225
+ # Returns the minimum value of the item's state since the given time
226
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
227
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
228
+ # @return [HistoricState, nil] The minimum value since `timestamp`,
229
+ # or nil if no previous state could be found.
230
+
231
+ # @!method minimum_between(start, finish, service = nil)
232
+ # Returns the minimum value of the item's state between two points in time
233
+ # @param [#to_zoned_date_time] start The point in time from which to search
234
+ # @param [#to_zoned_date_time] finish The point in time to which to search
235
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
236
+ # @return [HistoricState, nil] The minimum value between `start` and `finish`,
237
+ # or nil if no previous state could be found.
238
+
239
+ # @!method updated_since?(timestamp, service = nil)
240
+ # Whether the item's state has been updated since the given time
241
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
242
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
243
+ # @return [true,false] True if the item's state has been updated since the given `timestamp`, False otherwise.
244
+
245
+ # @!method updated_between?(start, finish, service = nil)
246
+ # Whether the item's state was updated between two points in time
247
+ # @param [#to_zoned_date_time] start The point in time from which to search
248
+ # @param [#to_zoned_date_time] finish The point in time to which to search
249
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
250
+ # @return [true,false] True if the item's state was updated between `start` and `finish`, False otherwise.
251
+
252
+ # @!method count_since(timestamp, service = nil)
253
+ # Returns the number of available historic data points from a point in time until now.
254
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
255
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
256
+ # @return [Integer] The number of values persisted for this item.
257
+
258
+ # @!method count_between(start, finish, service = nil)
259
+ # Returns the number of available historic data points between two points in time.
260
+ # @param [#to_zoned_date_time] start The point in time from which to search
261
+ # @param [#to_zoned_date_time] finish The point in time to which to search
262
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
263
+ # @return [Integer] The number of values persisted for this item.
264
+
265
+ # @!method count_state_changes_since(timestamp, service = nil)
266
+ # Returns the number of changes in historic data points from a point in time until now.
267
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
268
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
269
+ # @return [Integer] The number of values persisted for this item.
270
+
271
+ # @!method count_state_changes_between(start, finish, service = nil)
272
+ # Returns the number of changes in historic data points between two points in time.
273
+ # @param [#to_zoned_date_time] start The point in time from which to search
274
+ # @param [#to_zoned_date_time] finish The point in time to which to search
275
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
276
+ # @return [Integer] The number of values persisted for this item.
277
+
278
+ %i[persist last_update].each do |method|
279
+ define_method(method) do |service = nil|
280
+ service ||= persistence_service
281
+ Actions::PersistenceExtensions.public_send(method, self, service&.to_s)
282
+ end
283
+ end
284
+
285
+ #
286
+ # Return the previous state of the item
287
+ #
288
+ # @param skip_equal [true,false] if true, skips equal state values and
289
+ # searches the first state not equal the current state
290
+ # @param service [String] the name of the PersistenceService to use
291
+ #
292
+ # @return [HistoricState, nil] the previous state or nil if no previous state could be found,
293
+ # or if the default persistence service is not configured or
294
+ # does not refer to a valid service
295
+ #
296
+ def previous_state(service = nil, skip_equal: false)
297
+ service ||= persistence_service
298
+ result = Actions::PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
299
+ HistoricState.new(quantify(result.state), result) if result
300
+ end
301
+
302
+ PERSISTENCE_METHODS.each do |method|
303
+ define_method(method) do |timestamp, service = nil|
304
+ service ||= persistence_service
305
+ result = Actions::PersistenceExtensions.public_send(
306
+ method.to_s.delete_suffix("?"),
307
+ self,
308
+ timestamp.to_zoned_date_time,
309
+ service&.to_s
310
+ )
311
+ wrap_result(result, method)
312
+ end
313
+
314
+ next unless /_since\??$/.match?(method.to_s)
315
+
316
+ between_method = method.to_s.sub("_since", "_between").to_sym
317
+ define_method(between_method) do |start, finish, service = nil|
318
+ service ||= persistence_service
319
+ result = Actions::PersistenceExtensions.public_send(
320
+ between_method.to_s.delete_suffix("?"),
321
+ self,
322
+ start.to_zoned_date_time,
323
+ finish.to_zoned_date_time,
324
+ service&.to_s
325
+ )
326
+ wrap_result(result, method)
327
+ end
328
+ end
329
+
330
+ # evolution_rate's "between" method is overloaded with the same name
331
+ method = :evolution_rate
332
+ define_method(method) do |start, finish_or_service = nil, service = nil|
333
+ if service.nil?
334
+ if finish_or_service.respond_to?(:to_zoned_date_time)
335
+ service = persistence_service
336
+ finish = finish_or_service
337
+ else
338
+ service = finish_or_service || persistence_service
339
+ finish = nil
340
+ end
341
+ else
342
+ finish = finish_or_service
343
+ end
344
+
345
+ result = if finish
346
+ Actions::PersistenceExtensions.public_send(
347
+ method,
348
+ self,
349
+ start.to_zoned_date_time,
350
+ finish.to_zoned_date_time,
351
+ service&.to_s
352
+ )
353
+ else
354
+ Actions::PersistenceExtensions.public_send(
355
+ method,
356
+ self,
357
+ start.to_zoned_date_time,
358
+ service&.to_s
359
+ )
360
+ end
361
+ wrap_result(result, method)
362
+ end
363
+
364
+ alias_method :state_changes_since, :count_state_changes_since
365
+ alias_method :state_changes_between, :count_state_changes_between
366
+
367
+ private
368
+
369
+ #
370
+ # Convert value to QuantityType if it is a DecimalType and a unit is defined
371
+ #
372
+ # @param [Object] value The value to convert
373
+ #
374
+ # @return [Object] QuantityType or the original value
375
+ #
376
+ def quantify(value)
377
+ if value.is_a?(DecimalType) && respond_to?(:unit) && unit
378
+ logger.trace("Unitizing #{value} with unit #{unit}")
379
+ QuantityType.new(value.to_big_decimal, unit)
380
+ else
381
+ value
382
+ end
383
+ end
384
+
385
+ #
386
+ # Wrap the result into a more convenient object type depending on the method and result.
387
+ #
388
+ # @param [Object] result the raw result type to be wrapped
389
+ # @param [Symbol] method the name of the called method
390
+ #
391
+ # @return [HistoricState] a {HistoricState} object if the result was a HistoricItem
392
+ # @return [QuantityType] a `QuantityType` object if the result was an average, delta, deviation,
393
+ # sum, or variance.
394
+ # @return [Object] the original result object otherwise.
395
+ #
396
+ def wrap_result(result, method)
397
+ if result.is_a?(org.openhab.core.persistence.HistoricItem)
398
+ return HistoricState.new(quantify(result.state), result)
399
+ end
400
+ return quantify(result) if QUANTITY_METHODS.include?(method)
401
+
402
+ result
403
+ end
404
+
405
+ #
406
+ # Get the specified persistence service from the current thread local variable
407
+ #
408
+ # @return [Object] Persistence service name as String or Symbol, or nil if not set
409
+ #
410
+ def persistence_service
411
+ Thread.current[:openhab_persistence_service]
412
+ end
413
+ end
414
+ end
415
+ end
416
+ end
@@ -0,0 +1,66 @@
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.PlayerItem
9
+
10
+ #
11
+ # A {PlayerItem} allows control of a player, e.g. an audio player.
12
+ #
13
+ # @!attribute [r] state
14
+ # @return [PlayPauseType, RewindFastforwardType, nil]
15
+ #
16
+ # @example Start playing on a player item
17
+ # Chromecast.play
18
+ # @example Check if a player is paused
19
+ # logger.warn("#{item.name} is paused") if Chromecast.paused?
20
+ #
21
+ class PlayerItem < GenericItem
22
+ # @!method play?
23
+ # Check if the item state == {PLAY}
24
+ # @return [true,false]
25
+
26
+ # @!method paused?
27
+ # Check if the item state == {PAUSE}
28
+ # @return [true,false]
29
+
30
+ # @!method rewinding?
31
+ # Check if the item state == {REWIND}
32
+ # @return [true,false]
33
+
34
+ # @!method fast_forwarding?
35
+ # Check if the item state == {FASTFORWARD}
36
+ # @return [true,false]
37
+
38
+ # @!method play
39
+ # Send the {PLAY} command to the item
40
+ # @return [PlayerItem] `self`
41
+
42
+ # @!method pause
43
+ # Send the {PAUSE} command to the item
44
+ # @return [PlayerItem] `self`
45
+
46
+ # @!method rewind
47
+ # Send the {REWIND} command to the item
48
+ # @return [PlayerItem] `self`
49
+
50
+ # @!method fast_forward
51
+ # Send the {FASTFORWARD} command to the item
52
+ # @return [PlayerItem] `self`
53
+
54
+ # @!method next
55
+ # Send the {NEXT} command to the item
56
+ # @return [PlayerItem] `self`
57
+
58
+ # @!method previous
59
+ # Send the {PREVIOUS} command to the item
60
+ # @return [PlayerItem] `self`
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ # @!parse PlayerItem = OpenHAB::Core::Items::PlayerItem
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ #
7
+ # Provides {Item Items} created in Ruby to openHAB
8
+ #
9
+ class Provider < Core::Provider
10
+ include org.openhab.core.items.ItemProvider
11
+
12
+ class << self
13
+ #
14
+ # The Item registry
15
+ #
16
+ # @return [org.openhab.core.items.ItemRegistry]
17
+ #
18
+ def registry
19
+ $ir
20
+ end
21
+ end
22
+
23
+ #
24
+ # Remove an item from this provider
25
+ #
26
+ # @param [String] item_name
27
+ # @param [true, false] recursive
28
+ # @return [Item, nil] The removed item, if found.
29
+ #
30
+ def remove(item_name, recursive = false) # rubocop:disable Style/OptionalBooleanParameter matches Java method
31
+ return nil unless @elements.key?(item_name)
32
+
33
+ item = super(item_name)
34
+ item.members.each { |member| remove(member.name, true) } if recursive && item.is_a?(GroupItem)
35
+ item
36
+ end
37
+
38
+ def initialize
39
+ super(unload_priority: 50)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+ require_relative "../proxy"
5
+
6
+ module OpenHAB
7
+ module Core
8
+ module Items
9
+ # Class is a proxy to underlying {Item}
10
+ # @!visibility private
11
+ class Proxy < Delegator
12
+ # Not really an Item, but pretends to be
13
+ # @!parse include Item
14
+
15
+ # @!visibility private
16
+ EVENTS = [Events::ItemAddedEvent::TYPE, Events::ItemUpdatedEvent::TYPE, Events::ItemRemovedEvent::TYPE].freeze
17
+ # @!visibility private
18
+ UID_METHOD = :name
19
+
20
+ include Core::Proxy
21
+
22
+ # @return [String]
23
+ attr_reader :name
24
+
25
+ #
26
+ # Set the proxy item (called by super)
27
+ #
28
+ def __setobj__(item)
29
+ @item = item.is_a?(Item) ? item : nil
30
+ @name ||= item.name if item
31
+ end
32
+
33
+ #
34
+ # @return [Item, nil]
35
+ #
36
+ def __getobj__
37
+ @item
38
+ end
39
+
40
+ # @return [Module]
41
+ def class
42
+ return Item if __getobj__.nil?
43
+
44
+ __getobj__.class
45
+ end
46
+
47
+ # @return [true, false]
48
+ def is_a?(klass)
49
+ obj = __getobj__
50
+ # only claim to be a Delegator if we're backed by an actual item at the moment
51
+ klass == Item || obj.is_a?(klass) || klass == Proxy || (!obj.nil? && super)
52
+ end
53
+ alias_method :kind_of?, :is_a?
54
+
55
+ #
56
+ # Need to check if `self` _or_ the delegate is an instance of the
57
+ # given class
58
+ #
59
+ # So that {#==} can work
60
+ #
61
+ # @return [true, false]
62
+ #
63
+ # @!visibility private
64
+ def instance_of?(klass)
65
+ __getobj__.instance_of?(klass) || super
66
+ end
67
+
68
+ #
69
+ # Check if delegates are equal for comparison
70
+ #
71
+ # Otherwise items can't be used in Java maps
72
+ #
73
+ # @return [true, false]
74
+ #
75
+ # @!visibility private
76
+ def ==(other)
77
+ return __getobj__ == other.__getobj__ if other.instance_of?(Proxy)
78
+
79
+ super
80
+ end
81
+
82
+ #
83
+ # Non equality comparison
84
+ #
85
+ # @return [true, false]
86
+ #
87
+ # @!visibility private
88
+ def !=(other)
89
+ !(self == other) # rubocop:disable Style/InverseMethods
90
+ end
91
+
92
+ # @return [GroupItem::Members]
93
+ # @raise [NoMethodError] if item is not a GroupItem, or a dummy.
94
+ def members
95
+ return GroupItem::Members.new(self) if __getobj__.nil?
96
+
97
+ __getobj__.members
98
+ end
99
+
100
+ # @return [String]
101
+ def inspect
102
+ return super unless __getobj__.nil?
103
+
104
+ "#<OpenHAB::Core::Items::Proxy #{name}>"
105
+ end
106
+
107
+ #
108
+ # Supports inspect from IRB when we're a dummy item.
109
+ #
110
+ # @return [void]
111
+ # @!visibility private
112
+ def pretty_print(printer)
113
+ return super unless __getobj__.nil?
114
+
115
+ printer.text(inspect)
116
+ end
117
+
118
+ # needs to return `false` if we know we're not a {GroupItem}
119
+ def respond_to?(method, *args)
120
+ obj = __getobj__
121
+ return obj.respond_to?(method, *args) if method.to_sym == :members && !obj.nil?
122
+
123
+ super
124
+ end
125
+
126
+ # needs to return `false` if we know we're not a {GroupItem}
127
+ def respond_to_missing?(method, *args)
128
+ obj = __getobj__
129
+ return obj.respond_to_missing?(method, *args) if method.to_sym == :members && !obj.nil?
130
+
131
+ super
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end