openhab-scripting 4.15.0 → 4.18.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: fa5e8f47863d4b84f260403bcfc62b027fe118a9db4475044c8d25e202a84837
4
- data.tar.gz: 7e5b9b690dd7c734c2d4bebda07ed5280d0ff718be1c760ce5f2bfb7846a2c43
3
+ metadata.gz: 775d56e757a494c9b854b963f2c15393c2521597cd83e5d36452a41033508339
4
+ data.tar.gz: 9dfd101285ad28b310156016e524a370352b192147f3fbf87d130135b5a48b9d
5
5
  SHA512:
6
- metadata.gz: 0a9e776aa7e5c784a62c803d6cbb99218ff592ffa60217b0f6604929aea5f3df4ce05a74647780592f87a673fed8cf120087d9bcb519446c3e1beb1072ce947a
7
- data.tar.gz: b4d4d3dd48427e67be2dd19e1c91757b0ed8e44ab9550af1804aa2f60ec6beef507ffa316e120867371df83efc5f887092d9023107533e0961c24290a752736d
6
+ metadata.gz: 773d6bc398e5b24a9aecfb265c6025c75c4863ddf80932e875e9cb60eea4e6fdbf8036803d17c25dfd429127fc5164f1264b4bc48f7fce2cee1cd3fde837372a
7
+ data.tar.gz: 2e19d2dd05b40da151bd85a253e022209c93819acd20872bd1a30f02d9ad96cdb931c81499f7c222c2c487f962cb56f9db851dee6e3c038a671bac4d44f5481d
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ java_import org.openhab.core.thing.Channel
8
+ java_import org.openhab.core.thing.ChannelUID
9
+
10
+ # Adds methods to core OpenHAB Channel to make it more natural in Ruby
11
+ class Channel
12
+ extend Forwardable
13
+
14
+ delegate %i[thing item items] => :uid
15
+ end
16
+
17
+ # Adds methods to core OpenHAB ChannelUID to make it more natural in Ruby
18
+ class ChannelUID
19
+ # Return the thing this channel is associated with.
20
+ #
21
+ # @return [Thing] The thing associated with this channel or nil
22
+ def thing
23
+ things[thing_uid]
24
+ end
25
+
26
+ # Return the item if this channel is linked with an item. If a channel is linked to more than one item,
27
+ # this method only returns the first item.
28
+ #
29
+ # @return [GenericItem] The item associated with this channel or nil
30
+ def item
31
+ items.first
32
+ end
33
+
34
+ # Returns all of the channel's linked items.
35
+ #
36
+ # @return [Array<GenericItem>] An array of things or an empty array
37
+ def items
38
+ registry = OpenHAB::Core::OSGI.service('org.openhab.core.thing.link.ItemChannelLinkRegistry')
39
+ registry.get_linked_items(self).to_a
40
+ end
41
+ end
42
+ end
43
+ end
@@ -12,6 +12,7 @@ require 'openhab/dsl/monkey_patch/actions/actions'
12
12
  require 'openhab/dsl/rules/rule'
13
13
  require 'openhab/dsl/rules/terse'
14
14
  require 'openhab/dsl/actions'
15
+ require 'openhab/dsl/channel'
15
16
  require 'openhab/dsl/timers'
16
17
  require 'openhab/dsl/group'
17
18
  require 'openhab/dsl/things'
@@ -131,6 +131,25 @@ module OpenHAB
131
131
  group_names.map { |name| Groups.groups[name] }
132
132
  end
133
133
 
134
+ # Return the item's thing if this item is linked with a thing. If an item is linked to more than one thing,
135
+ # this method only returns the first thing.
136
+ #
137
+ # @return [Thing] The thing associated with this item or nil
138
+ def thing
139
+ all_linked_things.first
140
+ end
141
+ alias linked_thing thing
142
+
143
+ # Returns all of the item's linked things.
144
+ #
145
+ # @return [Array] An array of things or an empty array
146
+ def things
147
+ registry = OpenHAB::Core::OSGI.service('org.openhab.core.thing.link.ItemChannelLinkRegistry')
148
+ channels = registry.get_bound_channels(name).to_a
149
+ channels.map(&:thing_uid).uniq.map { |tuid| Object.things[tuid] }
150
+ end
151
+ alias all_linked_things things
152
+
134
153
  #
135
154
  # Check equality without type conversion
136
155
  #
@@ -20,6 +20,14 @@ module OpenHAB
20
20
  on?
21
21
  end
22
22
 
23
+ # Convert boolean commands to ON/OFF
24
+ # @!visibility private
25
+ def format_type(command)
26
+ return Types::OnOffType.from(command) if [true, false].include?(command)
27
+
28
+ super
29
+ end
30
+
23
31
  #
24
32
  # Send a command to invert the state of the switch
25
33
  #
@@ -38,7 +38,6 @@ module OpenHAB
38
38
  config.guard = Guard::Guard.new(only_if: config.only_if, not_if: config.not_if)
39
39
  logger.trace { config.inspect }
40
40
  process_rule_config(config)
41
- config
42
41
  rescue StandardError => e
43
42
  re_raise_with_backtrace(e)
44
43
  end
@@ -89,6 +88,7 @@ module OpenHAB
89
88
  Rules.script_rules << rule
90
89
  add_rule(rule)
91
90
  rule.execute(nil, { 'event' => Struct.new(:attachment).new(config.start_attachment) }) if config.on_start?
91
+ rule
92
92
  end
93
93
 
94
94
  #
@@ -33,20 +33,56 @@ module OpenHAB
33
33
  def changed(*items, to: nil, from: nil, for: nil, attach: nil)
34
34
  separate_groups(items).map do |item|
35
35
  logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
36
+
36
37
  # for is a reserved word in ruby, so use local_variable_get :for
37
- if (wait_duration = binding.local_variable_get(:for))
38
- changed_wait(item, to: to, from: from, duration: wait_duration, attach: attach)
39
- else
40
- # Place in array and flatten to support multiple to elements or single or nil
41
- [to].flatten.map do |to_state|
42
- [from].flatten.map { |from_state| create_changed_trigger(item, from_state, to_state, attach) }
43
- end
38
+ wait_duration = binding.local_variable_get(:for)
39
+
40
+ each_state(from, to) do |from_state, to_state|
41
+ changed_or_wait(item, from_state, to_state, wait_duration, attach)
44
42
  end
45
43
  end.flatten
46
44
  end
47
45
 
48
46
  private
49
47
 
48
+ #
49
+ # Run block for each state combination
50
+ #
51
+ # @param [Item State, Array<Item State>] from state to restrict trigger to
52
+ # @param [Item State, Array<Item State>] to state to restrict trigger to
53
+ #
54
+ # @yieldparam [Item State] from_state from state
55
+ # @yieldparam [Item State] to_state to state
56
+ #
57
+ # @return [Array] array of block return values
58
+ #
59
+ def each_state(from, to)
60
+ [to].flatten.each_with_object([]) do |to_state, agg|
61
+ [from].flatten.each do |from_state|
62
+ agg.push(yield(from_state, to_state))
63
+ end
64
+ end
65
+ end
66
+
67
+ #
68
+ # Create regular or delayed trigger based on duration
69
+ #
70
+ # @param [Object] item item to create trigger for
71
+ # @param [Item State] from state to restrict trigger to
72
+ # @param [Item State] to state to restrict trigger to
73
+ # @param [OpenHAB::Core::Duration, nil] duration ruration to delay trigger until to state is met
74
+ # @param attach attachment
75
+ #
76
+ # @return [Trigger] OpenHAB triggers
77
+ #
78
+ def changed_or_wait(item, from, to, duration, attach)
79
+ if duration
80
+ changed_wait(item, from: from, to: to, duration: duration, attach: attach)
81
+ else
82
+ create_changed_trigger(item, from, to, attach)
83
+ end
84
+ end
85
+
50
86
  #
51
87
  # Create a TriggerDelay for for an item or group that is changed for a specific duration
52
88
  #
@@ -21,6 +21,21 @@ module OpenHAB
21
21
  # Ruby Delegator for Thing
22
22
  #
23
23
  class Thing < SimpleDelegator
24
+ # Array wrapper class to allow searching a list of channels
25
+ # by channel id
26
+ class ChannelsArray < Array
27
+ # Allows indexing by both integer as an array or channel id acting like a hash.
28
+ # @param [Integer, String] index Numeric index or string channel id to search for.
29
+ def [](index)
30
+ if index.respond_to?(:to_str)
31
+ key = index.to_str
32
+ return find { |channel| channel.uid.id == key }
33
+ end
34
+
35
+ super
36
+ end
37
+ end
38
+
24
39
  include OpenHAB::DSL::Actions
25
40
  include OpenHAB::Log
26
41
 
@@ -45,6 +60,12 @@ module OpenHAB
45
60
  define_method("#{thingstatus.to_s.downcase}?") { status == ThingStatus.value_of(thingstatus) }
46
61
  end
47
62
 
63
+ # Returns the list of channels associated with this Thing
64
+ # @return [Array] channels
65
+ def channels
66
+ ChannelsArray.new(super.to_a)
67
+ end
68
+
48
69
  private
49
70
 
50
71
  java_import org.openhab.core.automation.annotation.RuleAction
@@ -83,11 +104,11 @@ module OpenHAB
83
104
  include LazyArray
84
105
  include Singleton
85
106
 
86
- # Gets a specific thing by name in the format binding_id:type_id:thing_id
87
- # @return Thing specified by name or nil if name does not exist in thing registry
107
+ # Gets a specific thing by name in the format binding_id:type_id:thing_id or via the ThingUID
108
+ # @return Thing specified by name/UID or nil if name/UID does not exist in thing registry
88
109
  def [](uid)
89
- thing_uid = org.openhab.core.thing.ThingUID.new(*uid.split(':'))
90
- thing = $things.get(thing_uid) # rubocop: disable Style/GlobalVars
110
+ uid = generate_thing_uid(uid) unless uid.is_a?(org.openhab.core.thing.ThingUID)
111
+ thing = $things.get(uid) # rubocop: disable Style/GlobalVars
91
112
  return unless thing
92
113
 
93
114
  logger.trace("Retrieved Thing(#{thing}) from registry for uid: #{uid}")
@@ -100,6 +121,15 @@ module OpenHAB
100
121
  def to_a
101
122
  $things.getAll.map { |thing| Thing.new(thing) } # rubocop: disable Style/GlobalVars
102
123
  end
124
+
125
+ private
126
+
127
+ # Returns a ThingUID given a string like object
128
+ #
129
+ # @return ThingUID generated by given name
130
+ def generate_thing_uid(uid)
131
+ org.openhab.core.thing.ThingUID.new(*uid.split(':'))
132
+ end
103
133
  end
104
134
 
105
135
  #
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.15.0'
8
+ VERSION = '4.18.0'
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.15.0
4
+ version: 4.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-24 00:00:00.000000000 Z
11
+ date: 2021-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,6 +54,7 @@ files:
54
54
  - lib/openhab/core/thread_local.rb
55
55
  - lib/openhab/dsl/actions.rb
56
56
  - lib/openhab/dsl/between.rb
57
+ - lib/openhab/dsl/channel.rb
57
58
  - lib/openhab/dsl/dsl.rb
58
59
  - lib/openhab/dsl/gems.rb
59
60
  - lib/openhab/dsl/group.rb