openhab-scripting 5.2.0 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d56223cd6766da527070ec31571d7a22cffa7d69ec754a1a1dbeb06c9354d56
4
- data.tar.gz: c635cf86c119c54c4b665f28cd91b0804a58c868d23f7a99420d4183f60bf195
3
+ metadata.gz: a914cbf39d73f385d8f250fb88a72737cabec9106871009f0b4679176251680a
4
+ data.tar.gz: 402616a86763701e48146d24581c340287c060bb8e86fb28e9c2e1947e19b129
5
5
  SHA512:
6
- metadata.gz: 79e7ac98695e1fb75a6bf100226a555d2d1d8f7a96efa0779b42439e262bfb63777ee99545782885f509c08f21dc77b2b504fd13078a5e5bc1d82ce456e3cca9
7
- data.tar.gz: d735f987f2540cba7b358a4f0550ed723ef807b0a4c5e12ab21177cc844f65a2eec1080778b996e702cd72a5d744b1a7c97fe52ba5679a17a13b4988ccd2bf76
6
+ metadata.gz: a860c8a2969e3a10b513650f265a3291f1cc5b91fa0b9d1775f40e254904175fe6e077881cb24ebef13677085705c3ae2248420064154da61a7ced4af4a91c67
7
+ data.tar.gz: 13e3da787706aee119334f4e768a9af3d2eaefc38afb8b2e6618e74e6591bc8cfcfa7ff137fa514d2bed2831e416f6112d8a27b7a6a8a205fdf8eefdbaafff31
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module CoreExt
5
+ module Ruby
6
+ # @!visibility private
7
+ module Object
8
+ class << self
9
+ attr_reader :top_self
10
+ end
11
+
12
+ # @!visibility private
13
+ module ClassMethods
14
+ # capture methods defined at top level (which get added to Object)
15
+ def method_added(method)
16
+ return super unless equal?(::Object)
17
+
18
+ if DSL.private_instance_methods.include?(method)
19
+ # Duplicate methods that conflict with DSL onto `main`'s singleton class,
20
+ # so that they'll take precedence over DSL's method.
21
+ Object.top_self.singleton_class.define_method(method, instance_method(method))
22
+ end
23
+
24
+ super
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -22,6 +22,12 @@ module OpenHAB
22
22
  # group_item Equipment, tags: Semantics::HVAC, thing: "binding:thing"
23
23
  # string_item Mode, tags: Semantics::Control, channel: "mode"
24
24
  # end
25
+ #
26
+ # # dimension Temperature inferred
27
+ # number_item OutdoorTemp, format: "%.1f %unit%", unit: "°F"
28
+ #
29
+ # # unit lx, dimension Illuminance, format "%s %unit%" inferred
30
+ # number_item OutdoorBrightness, state: 10_000 | "lx"
25
31
  # end
26
32
  #
27
33
  module Builder
@@ -156,9 +162,22 @@ module OpenHAB
156
162
  # @return [String, nil]
157
163
  attr_accessor :label
158
164
  # Unit dimension (for number items only)
165
+ #
166
+ # If {unit} is provided, and {dimension} is not, it will be inferred.
167
+ #
159
168
  # @return [String, nil]
160
169
  attr_accessor :dimension
170
+ # Unit (for number items only)
171
+ #
172
+ # Due to {format} inference, setting the unit is cross-compatible with
173
+ # openHAB 3.4 and 4.0.
174
+ #
175
+ # @return [String, nil]
176
+ attr_reader :unit
161
177
  # The formatting pattern for the item's state
178
+ #
179
+ # If {unit} is provided, and {format} is not, it will be inferred.
180
+ #
162
181
  # @return [String, nil]
163
182
  attr_accessor :format
164
183
  # The icon to be associated with the item
@@ -179,13 +198,16 @@ module OpenHAB
179
198
  # @return [Core::Items::Metadata::NamespaceHash]
180
199
  attr_reader :metadata
181
200
  # Initial state
201
+ #
202
+ # If {state} is set to a {QuantityType}, and {unit} is not set, it will be inferred.
203
+ #
182
204
  # @return [Core::Types::State]
183
- attr_accessor :state
205
+ attr_reader :state
184
206
 
185
207
  class << self
186
208
  # @!visibility private
187
209
  def item_factory
188
- @item_factory ||= org.openhab.core.library.CoreItemFactory.new
210
+ @item_factory ||= OpenHAB::OSGi.service("org.openhab.core.items.ItemFactory")
189
211
  end
190
212
 
191
213
  #
@@ -214,6 +236,7 @@ module OpenHAB
214
236
  end
215
237
 
216
238
  # @param dimension [Symbol, nil] The unit dimension for a {NumberItem} (see {ItemBuilder#dimension})
239
+ # @param unit [Symbol, String, nil] The unit for a {NumberItem} (see {ItemBuilder#unit})
217
240
  # @param format [String, nil] The formatting pattern for the item's state (see {ItemBuilder#format})
218
241
  # @param icon [Symbol, nil] The icon to be associated with the item (see {ItemBuilder#icon})
219
242
  # @param group [String,
@@ -246,6 +269,7 @@ module OpenHAB
246
269
  def initialize(type, name = nil, label = nil,
247
270
  provider:,
248
271
  dimension: nil,
272
+ unit: nil,
249
273
  format: nil,
250
274
  icon: nil,
251
275
  group: nil,
@@ -274,6 +298,7 @@ module OpenHAB
274
298
  @label = label
275
299
  @dimension = dimension
276
300
  @format = format
301
+ self.unit = unit
277
302
  @icon = icon
278
303
  @groups = []
279
304
  @tags = []
@@ -291,7 +316,7 @@ module OpenHAB
291
316
  self.alexa(alexa) if alexa
292
317
  self.ga(ga) if ga
293
318
  self.homekit(homekit) if homekit
294
- @state = state
319
+ self.state = state
295
320
 
296
321
  self.group(*group)
297
322
  self.group(*groups)
@@ -434,6 +459,39 @@ module OpenHAB
434
459
  @expire += ",command=#{command}" if command
435
460
  end
436
461
 
462
+ # @!attribute [w] unit
463
+ #
464
+ # Unit (for number items only).
465
+ #
466
+ # If dimension or format are not yet set, they will be inferred.
467
+ #
468
+ # @return [String, nil]
469
+ def unit=(unit)
470
+ @unit = unit
471
+
472
+ self.dimension ||= unit && org.openhab.core.types.util.UnitUtils.parse_unit(unit)&.then do |u|
473
+ org.openhab.core.types.util.UnitUtils.get_dimension_name(u)
474
+ end
475
+ self.format ||= unit && (if Gem::Version.new(Core::VERSION) >= Gem::Version.new("4.0.0.M3")
476
+ "%s %unit%"
477
+ else
478
+ "%s #{unit.gsub("%", "%%")}"
479
+ end)
480
+ end
481
+
482
+ # @!attribute [w] state
483
+ #
484
+ # Initial state
485
+ #
486
+ # If {state} is set to a {QuantityType}, and {unit} is not set, it will be inferred.
487
+ #
488
+ # @return [Core::Types::State]
489
+ def state=(state)
490
+ @state = state
491
+
492
+ self.unit ||= state.unit.to_s if state.is_a?(QuantityType)
493
+ end
494
+
437
495
  # @!visibility private
438
496
  def build
439
497
  item = create_item
@@ -450,6 +508,7 @@ module OpenHAB
450
508
  item.metadata["autoupdate"] = autoupdate.to_s unless autoupdate.nil?
451
509
  item.metadata["expire"] = expire if expire
452
510
  item.metadata["stateDescription"] = { "pattern" => format } if format
511
+ item.metadata["unit"] = unit if unit
453
512
  item.state = item.format_update(state) unless state.nil?
454
513
  item
455
514
  end
@@ -488,6 +547,12 @@ module OpenHAB
488
547
  class GroupItemBuilder < ItemBuilder
489
548
  include Builder
490
549
 
550
+ # This has to be duplicated here, since {Builder} includes DSL, so DSL#unit
551
+ # will be seen first, but we really want ItemBuilder#unit
552
+
553
+ # (see ItemBuilder#unit)
554
+ attr_reader :unit
555
+
491
556
  Builder.public_instance_methods.each do |m|
492
557
  next unless Builder.instance_method(m).owner == Builder
493
558
 
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of openHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.2.0"
7
+ VERSION = "5.3.0"
8
8
  end
9
9
  end
data/lib/openhab/dsl.rb CHANGED
@@ -975,5 +975,8 @@ OpenHAB::Core::Items.import_into_global_namespace
975
975
 
976
976
  # Extend `main` with DSL methods
977
977
  singleton_class.include(OpenHAB::DSL)
978
+ # Patch Object to work around https://github.com/openhab/openhab-jruby/issues/4
979
+ Object.extend(OpenHAB::CoreExt::Ruby::Object::ClassMethods)
980
+ OpenHAB::CoreExt::Ruby::Object.instance_variable_set(:@top_self, self)
978
981
 
979
982
  logger.debug "openHAB JRuby Scripting Library Version #{OpenHAB::DSL::VERSION} Loaded"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.0
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-05-02 00:00:00.000000000 Z
13
+ date: 2023-05-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -504,6 +504,7 @@ files:
504
504
  - lib/openhab/core_ext/ruby/date_time.rb
505
505
  - lib/openhab/core_ext/ruby/module.rb
506
506
  - lib/openhab/core_ext/ruby/numeric.rb
507
+ - lib/openhab/core_ext/ruby/object.rb
507
508
  - lib/openhab/core_ext/ruby/range.rb
508
509
  - lib/openhab/core_ext/ruby/symbol.rb
509
510
  - lib/openhab/core_ext/ruby/time.rb