openhab-scripting 4.15.1 → 4.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openhab/dsl/channel.rb +43 -0
- data/lib/openhab/dsl/dsl.rb +2 -0
- data/lib/openhab/dsl/items/generic_item.rb +19 -0
- data/lib/openhab/dsl/items/switch_item.rb +8 -0
- data/lib/openhab/dsl/rules/triggers/changed.rb +43 -7
- data/lib/openhab/dsl/rules/triggers/channel.rb +14 -12
- data/lib/openhab/dsl/things.rb +35 -5
- data/lib/openhab/dsl/types/type.rb +14 -0
- data/lib/openhab/dsl/uid.rb +55 -0
- data/lib/openhab/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a1c3efdcd1a6b1b37802084ae9905afca8ea737cf870e1d51c0df7fabafd7f2
|
4
|
+
data.tar.gz: 3bf072ccd50f0271a2633d174e95e8fd4de112f669ba40e0c5f325be6b30946f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f3d963c6d2bc0b148dc122ec3483e301a04ee70c3f3cdf9b04a98b289f4dd823f1192344767aed9712d2745f01be7a51e62043d76a652dbdafd63ff7a98d065
|
7
|
+
data.tar.gz: d6a98e072d3462a1d74fd9d968c8a3eeeb66f7df747aee054640b6d2f9ca76358dc7e10bc93a2106b60bc508778f83ef649dfd2e3153f6d6c2e43590cfeeed53
|
@@ -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
|
data/lib/openhab/dsl/dsl.rb
CHANGED
@@ -12,12 +12,14 @@ 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'
|
18
19
|
require 'openhab/dsl/between'
|
19
20
|
require 'openhab/dsl/gems'
|
20
21
|
require 'openhab/dsl/persistence'
|
22
|
+
require 'openhab/dsl/uid'
|
21
23
|
require 'openhab/dsl/units'
|
22
24
|
require 'openhab/dsl/states'
|
23
25
|
|
@@ -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
|
#
|
@@ -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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
#
|
@@ -15,16 +15,19 @@ module OpenHAB
|
|
15
15
|
#
|
16
16
|
# Creates a channel trigger
|
17
17
|
#
|
18
|
-
# @param [
|
18
|
+
# @param [String, Channel, ChannelUID, Array<String, Channel, ChannelUID>] channels
|
19
|
+
# channels to create triggers for in form of 'binding_id:type_id:thing_id#channel_id'
|
19
20
|
# or 'channel_id' if thing is provided
|
20
|
-
# @param [
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
def channel(*channels, thing: nil, triggered: nil, attach: nil)
|
25
|
-
channels.flatten.each do |channel|
|
26
|
-
channel =
|
27
|
-
|
21
|
+
# @param [String, Thing, ThingUID, Array<String, Thing, ThingUID>] thing
|
22
|
+
# thing(s) to create trigger for if not specified with the channel
|
23
|
+
# @param [String, Array<String>] triggered specific triggering condition(s) to match for trigger
|
24
|
+
#
|
25
|
+
def channel(*channels, thing: nil, triggered: nil, attach: nil) # rubocop:disable Metrics/AbcSize
|
26
|
+
channels.flatten.product([thing].flatten).each do |(channel, t)|
|
27
|
+
channel = channel.uid if channel.is_a?(org.openhab.core.thing.Channel)
|
28
|
+
t = t.uid if t.is_a?(Thing)
|
29
|
+
channel = [t, channel].compact.join(':')
|
30
|
+
logger.trace("Creating channel trigger for channel(#{channel}), thing(#{t}), trigger(#{triggered})")
|
28
31
|
[triggered].flatten.each do |trigger|
|
29
32
|
create_channel_trigger(channel, trigger, attach)
|
30
33
|
end
|
@@ -36,14 +39,13 @@ module OpenHAB
|
|
36
39
|
#
|
37
40
|
# Create a trigger for a channel
|
38
41
|
#
|
39
|
-
# @param [
|
40
|
-
# @param [
|
42
|
+
# @param [String] channel to look for triggers
|
43
|
+
# @param [String] trigger specific channel trigger to match
|
41
44
|
#
|
42
45
|
#
|
43
46
|
def create_channel_trigger(channel, trigger, attach)
|
44
47
|
config = { 'channelUID' => channel }
|
45
48
|
config['event'] = trigger.to_s unless trigger.nil?
|
46
|
-
config['channelUID'] = channel
|
47
49
|
logger.trace("Creating Change Trigger for #{config}")
|
48
50
|
append_trigger(Trigger::CHANNEL_EVENT, config, attach: attach)
|
49
51
|
end
|
data/lib/openhab/dsl/things.rb
CHANGED
@@ -14,13 +14,28 @@ module OpenHAB
|
|
14
14
|
# Support for OpenHAB Things
|
15
15
|
#
|
16
16
|
module Things
|
17
|
-
java_import
|
17
|
+
java_import org.openhab.core.thing.ThingStatus
|
18
18
|
include OpenHAB::Log
|
19
19
|
|
20
20
|
#
|
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
|
-
|
90
|
-
thing = $things.get(
|
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
|
#
|
@@ -41,6 +41,20 @@ module OpenHAB
|
|
41
41
|
equals(other)
|
42
42
|
end
|
43
43
|
|
44
|
+
#
|
45
|
+
# Case equality
|
46
|
+
#
|
47
|
+
# @return [Boolean] if the values are of the same Type
|
48
|
+
# or item state of the same type
|
49
|
+
#
|
50
|
+
def ===(other)
|
51
|
+
logger.trace("(#{self.class}) #{self} === #{other} (#{other.class})")
|
52
|
+
other = other.state if other.respond_to?(:state)
|
53
|
+
return false unless instance_of?(other.class)
|
54
|
+
|
55
|
+
eql?(other)
|
56
|
+
end
|
57
|
+
|
44
58
|
#
|
45
59
|
# Check equality, including type conversion
|
46
60
|
#
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module DSL
|
5
|
+
java_import org.openhab.core.common.AbstractUID
|
6
|
+
java_import org.openhab.core.thing.ThingTypeUID
|
7
|
+
|
8
|
+
# Adds methods to core OpenHAB AbstractUID to make it more natural in Ruby
|
9
|
+
class AbstractUID
|
10
|
+
# implicit conversion to string
|
11
|
+
alias to_str to_s
|
12
|
+
# inspect result is just the string representation
|
13
|
+
alias inspect to_s
|
14
|
+
|
15
|
+
# compares if equal to `other`, including string conversion
|
16
|
+
# @return [true, false]
|
17
|
+
def ==(other)
|
18
|
+
return true if equals(other)
|
19
|
+
|
20
|
+
to_s == other
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Adds methods to core OpenHAB ThingUID to make it more natural in Ruby
|
25
|
+
class ThingUID
|
26
|
+
# Returns the id of the binding this thing belongs to
|
27
|
+
# @return [String]
|
28
|
+
def binding_id
|
29
|
+
get_segment(0)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Adds methods to core OpenHAB ThingTypeUID to make it more natural in Ruby
|
34
|
+
class ThingTypeUID
|
35
|
+
# Returns the id of the binding this thing type belongs to
|
36
|
+
# @return [String]
|
37
|
+
def binding_id
|
38
|
+
get_segment(0)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# have to remove == from all descendant classes so that they'll inherit
|
43
|
+
# the new implementation
|
44
|
+
[org.openhab.core.items.MetadataKey,
|
45
|
+
org.openhab.core.thing.UID,
|
46
|
+
org.openhab.core.thing.ChannelUID,
|
47
|
+
org.openhab.core.thing.ChannelGroupUID,
|
48
|
+
org.openhab.core.thing.ThingUID,
|
49
|
+
org.openhab.core.thing.ThingTypeUID,
|
50
|
+
org.openhab.core.thing.type.ChannelTypeUID,
|
51
|
+
org.openhab.core.thing.type.ChannelGroupTypeUID].each do |klass|
|
52
|
+
klass.remove_method(:==)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/openhab/version.rb
CHANGED
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.
|
4
|
+
version: 4.19.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
|
+
date: 2021-12-01 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
|
@@ -135,6 +136,7 @@ files:
|
|
135
136
|
- lib/openhab/dsl/types/types.rb
|
136
137
|
- lib/openhab/dsl/types/un_def_type.rb
|
137
138
|
- lib/openhab/dsl/types/up_down_type.rb
|
139
|
+
- lib/openhab/dsl/uid.rb
|
138
140
|
- lib/openhab/dsl/units.rb
|
139
141
|
- lib/openhab/log/configuration.rb
|
140
142
|
- lib/openhab/log/logger.rb
|