openhab-scripting 5.3.0 → 5.4.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: a914cbf39d73f385d8f250fb88a72737cabec9106871009f0b4679176251680a
4
- data.tar.gz: 402616a86763701e48146d24581c340287c060bb8e86fb28e9c2e1947e19b129
3
+ metadata.gz: 0522f086fca640c17a05634e13924dd9e39fc0630fba03954d4c0ffcfc1739c7
4
+ data.tar.gz: 9c01b1545d9e999ecc9ff0836691ef858794085ffb09433a3636c7594251e13b
5
5
  SHA512:
6
- metadata.gz: a860c8a2969e3a10b513650f265a3291f1cc5b91fa0b9d1775f40e254904175fe6e077881cb24ebef13677085705c3ae2248420064154da61a7ced4af4a91c67
7
- data.tar.gz: 13e3da787706aee119334f4e768a9af3d2eaefc38afb8b2e6618e74e6591bc8cfcfa7ff137fa514d2bed2831e416f6112d8a27b7a6a8a205fdf8eefdbaafff31
6
+ metadata.gz: c1c7e125bb92764cc330924cacc30b03bf8759633bf34e51000897614af32efa2bcddfd2c48e5514be160d6c2d625eb38449c4d6203c5ca418bf67868d302aa2
7
+ data.tar.gz: 61742a93f67041588c6f6af954f95dd7515d8ee2d913812f5ac32212a8ddc49f979da4712125d1b15ca327b130ec81102308edbe5689f43e386d24b46f185357
@@ -74,19 +74,19 @@ module OpenHAB
74
74
  #
75
75
  # Returns all Scenes (rules tagged with "Scene")
76
76
  #
77
- # @return [Array<Rule>] A list of all the scenes
77
+ # @return [TaggedArray] A list of all the scenes
78
78
  #
79
79
  def scenes
80
- tagged("Scene")
80
+ @scenes ||= TaggedArray.new("Scene")
81
81
  end
82
82
 
83
83
  #
84
84
  # Returns all Scripts (rules tagged with "Script")
85
85
  #
86
- # @return [Array<Rule>] A list of all the scripts
86
+ # @return [TaggedArray] A list of all the scripts
87
87
  #
88
88
  def scripts
89
- tagged("Script")
89
+ @scripts ||= TaggedArray.new("Script")
90
90
  end
91
91
  end
92
92
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Rules
8
+ #
9
+ # Provides access to a set of openHAB {Rule rules}, and acts like an array.
10
+ #
11
+ class TaggedArray
12
+ include LazyArray
13
+
14
+ def initialize(tag)
15
+ @tag = tag
16
+ end
17
+
18
+ #
19
+ # Gets a specific Rule
20
+ #
21
+ # @param [String] uid Rule UID
22
+ # @return [Rule, nil]
23
+ #
24
+ def [](uid)
25
+ rule = $rules.get(uid)
26
+ rule.tagged?(@tag) ? rule : nil
27
+ end
28
+ alias_method :include?, :[]
29
+ alias_method :key?, :[]
30
+ # @deprecated
31
+ alias_method :has_key?, :[]
32
+
33
+ #
34
+ # Explicit conversion to array
35
+ #
36
+ # @return [Array<Rule>]
37
+ #
38
+ def to_a
39
+ $rules.all.to_a.tagged(@tag)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -35,8 +35,7 @@ module OpenHAB
35
35
  super()
36
36
  set_name(config.name)
37
37
  set_description(config.description)
38
- tags = DSL::Items::ItemBuilder.normalize_tags(*config.tags)
39
- set_tags(tags.to_set)
38
+ set_tags(config.tags.to_set)
40
39
  set_triggers(config.triggers)
41
40
  self.uid = config.uid
42
41
  @run_context = config.caller
@@ -104,7 +104,37 @@ module OpenHAB
104
104
  ThreadLocal.thread_local(openhab_rule_type: "script", openhab_rule_uid: id) do
105
105
  builder = BuilderDSL.new(block.binding)
106
106
  builder.uid(id)
107
- builder.tags(["Script"])
107
+ builder.tags("Script")
108
+ builder.name(name)
109
+ builder.script(&block)
110
+ logger.trace { builder.inspect }
111
+ builder.build(provider, script)
112
+ end
113
+ end
114
+
115
+ #
116
+ # Create a new scene
117
+ #
118
+ # A scene is a rule with no triggers. It can be called by various other actions,
119
+ # such as the Run Rules action.
120
+ #
121
+ # @param [String] name A descriptive name
122
+ # @param [String] id The script's ID
123
+ # @yield [] Block executed when the script is executed.
124
+ # @return [Core::Rules::Rule]
125
+ #
126
+ def scene(name = nil, id: nil, script: nil, &block)
127
+ raise ArgumentError, "Block is required" unless block
128
+
129
+ id ||= NameInference.infer_rule_id_from_block(block)
130
+ name ||= id
131
+ script ||= block.source rescue nil # rubocop:disable Style/RescueModifier
132
+
133
+ builder = nil
134
+ ThreadLocal.thread_local(openhab_rule_type: "script", openhab_rule_uid: id) do
135
+ builder = BuilderDSL.new(block.binding)
136
+ builder.uid(id)
137
+ builder.tags("Scene")
108
138
  builder.name(name)
109
139
  builder.script(&block)
110
140
  logger.trace { builder.inspect }
@@ -354,7 +384,7 @@ module OpenHAB
354
384
  prop :description
355
385
 
356
386
  #
357
- # @!method tags(tags)
387
+ # @!method tags(*tags)
358
388
  #
359
389
  # Set the rule's tags.
360
390
  #
@@ -1082,6 +1112,7 @@ module OpenHAB
1082
1112
 
1083
1113
  raise ArgumentError, "Missing cron expression or elements" unless expression
1084
1114
 
1115
+ add_tag("Schedule")
1085
1116
  cron = Cron.new(rule_triggers: @rule_triggers)
1086
1117
  cron.trigger(config: { "cronExpression" => expression }, attach: attach)
1087
1118
  end
@@ -1180,6 +1211,7 @@ module OpenHAB
1180
1211
  if value == :day && at.is_a?(Item)
1181
1212
  raise ArgumentError, "Attachments are not supported with dynamic datetime triggers" unless attach.nil?
1182
1213
 
1214
+ add_tag("Schedule")
1183
1215
  return trigger("timer.DateTimeTrigger", itemName: at.name, timeOnly: true)
1184
1216
  end
1185
1217
 
@@ -1561,6 +1593,7 @@ module OpenHAB
1561
1593
  #
1562
1594
  def at(item)
1563
1595
  item = item.name if item.is_a?(Item)
1596
+ add_tag("Schedule")
1564
1597
  trigger("timer.DateTimeTrigger", itemName: item.to_s)
1565
1598
  end
1566
1599
 
@@ -1847,6 +1880,15 @@ module OpenHAB
1847
1880
 
1848
1881
  private
1849
1882
 
1883
+ # Adds a single tag to this rule
1884
+ # @param [String] tag The tag (it will not be normalized)
1885
+ # @return [void]
1886
+ def add_tag(tag)
1887
+ # have to normalize tags first
1888
+ @tags = DSL::Items::ItemBuilder.normalize_tags(*tags)
1889
+ @tags << tag unless @tags.include?(tag)
1890
+ end
1891
+
1850
1892
  # Calls the on_load block, with a delay if specified
1851
1893
  # @yield block to execute on load time
1852
1894
  # @yieldparam [String] module The module ID that identifies this on_load event
@@ -1871,7 +1913,8 @@ module OpenHAB
1871
1913
  # @return [true,false] true if it should be created, false otherwise
1872
1914
  #
1873
1915
  def create_rule?
1874
- return true if tags.include?("Script")
1916
+ @tags = DSL::Items::ItemBuilder.normalize_tags(*tags)
1917
+ return true unless (tags & %w[Script Scene]).empty?
1875
1918
 
1876
1919
  if !triggers?
1877
1920
  logger.warn "Rule '#{uid}' has no triggers, not creating rule"
@@ -51,7 +51,7 @@ module OpenHAB
51
51
  # @return [CronAdjuster]
52
52
  #
53
53
  def getTemporalAdjuster # rubocop:disable Naming/MethodName
54
- org.openhab.core.scheduler.CronAdjuster.new(expression)
54
+ org.openhab.core.scheduler.CronAdjuster.new(@expression)
55
55
  end
56
56
 
57
57
  #
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of openHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.3.0"
7
+ VERSION = "5.4.0"
8
8
  end
9
9
  end
data/lib/openhab/dsl.rb CHANGED
@@ -59,6 +59,11 @@ module OpenHAB
59
59
  rules.build { rule(name, **kwargs, &block) }
60
60
  end
61
61
 
62
+ # (see Rules::Builder#scene)
63
+ def scene(name = nil, id: nil, **kwargs, &block)
64
+ rules.build { scene(name, id: id, **kwargs, &block) }
65
+ end
66
+
62
67
  # (see Rules::Builder#script)
63
68
  def script(name = nil, id: nil, **kwargs, &block)
64
69
  rules.build { script(name, id: id, **kwargs, &block) }
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.3.0
4
+ version: 5.4.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-31 00:00:00.000000000 Z
13
+ date: 2023-06-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -447,6 +447,7 @@ files:
447
447
  - lib/openhab/core/rules/provider.rb
448
448
  - lib/openhab/core/rules/registry.rb
449
449
  - lib/openhab/core/rules/rule.rb
450
+ - lib/openhab/core/rules/tagged_array.rb
450
451
  - lib/openhab/core/script_handling.rb
451
452
  - lib/openhab/core/things.rb
452
453
  - lib/openhab/core/things/channel.rb