openhab-scripting 4.1.3 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/openhab/core/entity_lookup.rb +1 -57
- data/lib/openhab/dsl/actions.rb +2 -3
- data/lib/openhab/dsl/dsl.rb +8 -12
- data/lib/openhab/dsl/group.rb +1 -5
- data/lib/openhab/dsl/items/comparable_item.rb +49 -0
- data/lib/openhab/dsl/items/contact_item.rb +41 -0
- data/lib/openhab/dsl/items/date_time_item.rb +64 -0
- data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
- data/lib/openhab/dsl/items/ensure.rb +93 -0
- data/lib/openhab/dsl/items/generic_item.rb +174 -0
- data/lib/openhab/dsl/items/group_item.rb +121 -89
- data/lib/openhab/dsl/items/image_item.rb +5 -41
- data/lib/openhab/dsl/items/item_equality.rb +36 -0
- data/lib/openhab/dsl/items/item_registry.rb +49 -0
- data/lib/openhab/dsl/items/items.rb +80 -35
- data/lib/openhab/dsl/items/metadata.rb +325 -0
- data/lib/openhab/dsl/items/number_item.rb +6 -312
- data/lib/openhab/dsl/items/numeric_item.rb +68 -0
- data/lib/openhab/dsl/items/persistence.rb +122 -0
- data/lib/openhab/dsl/items/player_item.rb +49 -40
- data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
- data/lib/openhab/dsl/items/string_item.rb +16 -58
- data/lib/openhab/dsl/items/switch_item.rb +62 -0
- data/lib/openhab/dsl/lazy_array.rb +8 -6
- data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
- data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
- data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
- data/lib/openhab/dsl/rules/terse.rb +24 -0
- data/lib/openhab/dsl/states.rb +1 -1
- data/lib/openhab/dsl/time_of_day.rb +3 -5
- data/lib/openhab/dsl/types/comparable_type.rb +21 -0
- data/lib/openhab/dsl/types/date_time_type.rb +334 -0
- data/lib/openhab/dsl/types/decimal_type.rb +187 -0
- data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
- data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
- data/lib/openhab/dsl/types/numeric_type.rb +39 -0
- data/lib/openhab/dsl/types/on_off_type.rb +29 -0
- data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
- data/lib/openhab/dsl/types/percent_type.rb +68 -0
- data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
- data/lib/openhab/dsl/types/quantity_type.rb +275 -0
- data/lib/openhab/dsl/types/refresh_type.rb +18 -0
- data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
- data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
- data/lib/openhab/dsl/types/string_type.rb +88 -0
- data/lib/openhab/dsl/types/type.rb +72 -0
- data/lib/openhab/dsl/types/types.rb +77 -0
- data/lib/openhab/dsl/types/un_def_type.rb +22 -0
- data/lib/openhab/dsl/types/up_down_type.rb +32 -0
- data/lib/openhab/dsl/units.rb +11 -6
- data/lib/openhab/version.rb +1 -1
- data/lib/openhab.rb +0 -1
- metadata +34 -28
- data/lib/openhab/dsl/items/datetime_item.rb +0 -75
- data/lib/openhab/dsl/items/item_command.rb +0 -90
- data/lib/openhab/dsl/items/item_delegate.rb +0 -125
- data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
- data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
- data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
- data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
- data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
- data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
- data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
- data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
- data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
- data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
- data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
- data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
- data/lib/openhab/dsl/types/datetime.rb +0 -338
- data/lib/openhab/dsl/types/quantity.rb +0 -300
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a39d84f4797ad4d9807e581919e5a681e0b6ba52df5a64f395bc2409cb2d7f38
|
|
4
|
+
data.tar.gz: d56eb7cb6e9a3a50dd07407bed0a9be23f3b719b676c1e2d4bbf82685f049f73
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4bfda0871cefc7b2a0a95ea0d8528ab50111192727f4380f441c922eb5214573a59c9e0c7f39b9c13b26f9ee733607548445477f5b9a79ee7c1028cbbf064db6
|
|
7
|
+
data.tar.gz: 73002d6de64e98d48dd4f5b0a7ad0439c2bed9b15cdabfe36cffbf026930b0cdeac98da0b748e477362f28a19bdfdb2c0223749dbe6ae353ddc8a4e7b52341b6
|
|
@@ -3,13 +3,6 @@
|
|
|
3
3
|
require 'pp'
|
|
4
4
|
require 'java'
|
|
5
5
|
require 'set'
|
|
6
|
-
require 'openhab/dsl/group'
|
|
7
|
-
require 'openhab/log/logger'
|
|
8
|
-
require 'openhab/dsl/items/number_item'
|
|
9
|
-
require 'openhab/dsl/items/string_item'
|
|
10
|
-
require 'openhab/dsl/items/datetime_item'
|
|
11
|
-
require 'openhab/dsl/items/rollershutter_item'
|
|
12
|
-
require 'openhab/dsl/items/group_item'
|
|
13
6
|
|
|
14
7
|
# Automation lookup and injection of OpenHab entities
|
|
15
8
|
|
|
@@ -68,52 +61,6 @@ module OpenHAB
|
|
|
68
61
|
lookup_item(name) || lookup_thing(name)
|
|
69
62
|
end
|
|
70
63
|
|
|
71
|
-
#
|
|
72
|
-
# Decorate items with Ruby wrappers
|
|
73
|
-
#
|
|
74
|
-
# @param [Array] items Array of items to decorate
|
|
75
|
-
#
|
|
76
|
-
# @return [Array] Array of decorated items
|
|
77
|
-
#
|
|
78
|
-
def self.decorate_items(*items)
|
|
79
|
-
items.flatten.map { |item| decorate_item(item) }
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
#
|
|
83
|
-
# Decorate item with Ruby wrappers
|
|
84
|
-
#
|
|
85
|
-
# @param [Object] item the item object to decorate
|
|
86
|
-
#
|
|
87
|
-
# @return [Object] the ruby wrapper for the item
|
|
88
|
-
#
|
|
89
|
-
# rubocop: disable Metrics/MethodLength
|
|
90
|
-
# rubocop: disable Metrics/CyclomaticComplexity
|
|
91
|
-
# Disabled line length and branch size - case dispatch pattern
|
|
92
|
-
def self.decorate_item(item)
|
|
93
|
-
logger.trace("Decorating #{item.class}")
|
|
94
|
-
case item
|
|
95
|
-
when Java::OrgOpenhabCoreItems::GroupItem
|
|
96
|
-
OpenHAB::DSL::Items::GroupItem.new(item)
|
|
97
|
-
when Java::OrgOpenhabCoreLibraryItems::NumberItem
|
|
98
|
-
OpenHAB::DSL::Items::NumberItem.new(item)
|
|
99
|
-
when Java::OrgOpenhabCoreLibraryItems::StringItem
|
|
100
|
-
OpenHAB::DSL::Items::StringItem.new(item)
|
|
101
|
-
when Java::OrgOpenhabCoreLibraryItems::DateTimeItem
|
|
102
|
-
OpenHAB::DSL::Items::DateTimeItem.new(item)
|
|
103
|
-
when Java::OrgOpenhabCoreLibraryItems::RollershutterItem
|
|
104
|
-
OpenHAB::DSL::Items::RollershutterItem.new(item)
|
|
105
|
-
when Java::OrgOpenhabCoreLibraryItems::PlayerItem
|
|
106
|
-
OpenHAB::DSL::Items::PlayerItem.new(item)
|
|
107
|
-
when Java::OrgOpenhabCoreLibraryItems::ImageItem
|
|
108
|
-
OpenHAB::DSL::Items::ImageItem.new(item)
|
|
109
|
-
else
|
|
110
|
-
logger.trace("Returning undecorated item #{item.class}")
|
|
111
|
-
item
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
# rubocop: enable Metrics/MethodLength
|
|
115
|
-
# rubocop: enable Metrics/CyclomaticComplexity
|
|
116
|
-
|
|
117
64
|
#
|
|
118
65
|
# Looks up a Thing in the OpenHAB registry replacing '_' with ':'
|
|
119
66
|
#
|
|
@@ -145,10 +92,7 @@ module OpenHAB
|
|
|
145
92
|
def self.lookup_item(name)
|
|
146
93
|
logger.trace("Looking up item(#{name})")
|
|
147
94
|
name = name.to_s if name.is_a? Symbol
|
|
148
|
-
# rubocop: disable Style/GlobalVars
|
|
149
|
-
item = $ir.get(name)
|
|
150
|
-
# rubocop: enable Style/GlobalVars
|
|
151
|
-
decorate_item(item)
|
|
95
|
+
$ir.get(name) # rubocop: disable Style/GlobalVars
|
|
152
96
|
end
|
|
153
97
|
end
|
|
154
98
|
end
|
data/lib/openhab/dsl/actions.rb
CHANGED
|
@@ -9,7 +9,6 @@ module OpenHAB
|
|
|
9
9
|
# Module to import and streamline access to OpenHAB actions
|
|
10
10
|
#
|
|
11
11
|
module Actions
|
|
12
|
-
java_import org.openhab.core.library.types.PercentType
|
|
13
12
|
include OpenHAB::Log
|
|
14
13
|
|
|
15
14
|
OpenHAB::Core::OSGI.services('org.openhab.core.model.script.engine.action.ActionService')&.each do |service|
|
|
@@ -83,7 +82,7 @@ module OpenHAB
|
|
|
83
82
|
# @return [void]
|
|
84
83
|
#
|
|
85
84
|
def say(text, voice: nil, sink: nil, volume: nil)
|
|
86
|
-
volume = PercentType.new(volume
|
|
85
|
+
volume = Types::PercentType.new(volume) unless volume.is_a?(Types::PercentType) || volume.nil?
|
|
87
86
|
Voice.say text, voice, sink, volume
|
|
88
87
|
end
|
|
89
88
|
|
|
@@ -97,7 +96,7 @@ module OpenHAB
|
|
|
97
96
|
# @return [void]
|
|
98
97
|
#
|
|
99
98
|
def play_sound(filename, sink: nil, volume: nil)
|
|
100
|
-
volume = PercentType.new(volume
|
|
99
|
+
volume = Types::PercentType.new(volume) unless volume.is_a?(Types::PercentType) || volume.nil?
|
|
101
100
|
Audio.playSound sink, filename, volume
|
|
102
101
|
end
|
|
103
102
|
end
|
data/lib/openhab/dsl/dsl.rb
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'java'
|
|
4
3
|
require 'openhab/log/logger'
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
# the order of these is important
|
|
6
|
+
require 'openhab/dsl/types/types'
|
|
7
|
+
require 'openhab/dsl/items/items'
|
|
6
8
|
require 'openhab/dsl/monkey_patch/ruby/ruby'
|
|
7
|
-
|
|
8
|
-
require 'openhab/dsl/monkey_patch/
|
|
9
|
+
|
|
10
|
+
require 'openhab/dsl/monkey_patch/events/events'
|
|
9
11
|
require 'openhab/dsl/monkey_patch/actions/actions'
|
|
10
12
|
require 'openhab/dsl/rules/rule'
|
|
13
|
+
require 'openhab/dsl/rules/terse'
|
|
11
14
|
require 'openhab/dsl/actions'
|
|
12
15
|
require 'openhab/dsl/timers'
|
|
13
16
|
require 'openhab/dsl/group'
|
|
14
17
|
require 'openhab/dsl/things'
|
|
15
|
-
require 'openhab/dsl/items/items'
|
|
16
|
-
require 'openhab/dsl/items/datetime_item'
|
|
17
|
-
require 'openhab/dsl/items/image_item'
|
|
18
|
-
require 'openhab/dsl/items/number_item'
|
|
19
|
-
require 'openhab/dsl/items/player_item'
|
|
20
|
-
require 'openhab/dsl/items/group_item'
|
|
21
18
|
require 'openhab/dsl/time_of_day'
|
|
22
19
|
require 'openhab/dsl/gems'
|
|
23
20
|
require 'openhab/dsl/persistence'
|
|
24
21
|
require 'openhab/dsl/units'
|
|
25
|
-
require 'openhab/dsl/types/datetime'
|
|
26
|
-
require 'openhab/dsl/types/quantity'
|
|
27
22
|
require 'openhab/dsl/states'
|
|
28
23
|
|
|
29
24
|
module OpenHAB
|
|
@@ -40,6 +35,7 @@ module OpenHAB
|
|
|
40
35
|
base.send :include, OpenHAB::DSL::Items
|
|
41
36
|
base.send :include, OpenHAB::DSL::Persistence
|
|
42
37
|
base.send :include, OpenHAB::DSL::Rules
|
|
38
|
+
base.send :include, OpenHAB::DSL::Rules::Terse
|
|
43
39
|
base.send :include, OpenHAB::DSL::States
|
|
44
40
|
base.send :include, OpenHAB::DSL::Things
|
|
45
41
|
base.send :include, OpenHAB::DSL::Timers
|
data/lib/openhab/dsl/group.rb
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'java'
|
|
4
3
|
require 'singleton'
|
|
5
4
|
|
|
6
|
-
require 'openhab/core/entity_lookup'
|
|
7
|
-
require 'openhab/dsl/items/group_item'
|
|
8
5
|
require 'openhab/dsl/lazy_array'
|
|
9
6
|
|
|
10
7
|
module OpenHAB
|
|
@@ -35,8 +32,7 @@ module OpenHAB
|
|
|
35
32
|
|
|
36
33
|
# explicit conversion to array
|
|
37
34
|
def to_a
|
|
38
|
-
|
|
39
|
-
Core::EntityLookup.decorate_items(items)
|
|
35
|
+
$ir.items.grep(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
|
|
40
36
|
end
|
|
41
37
|
end
|
|
42
38
|
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenHAB
|
|
4
|
+
module DSL
|
|
5
|
+
module Items
|
|
6
|
+
# Mixin for implementing the comparison operator for Item types that
|
|
7
|
+
# support it
|
|
8
|
+
module ComparableItem
|
|
9
|
+
#
|
|
10
|
+
# Comparison
|
|
11
|
+
#
|
|
12
|
+
# @param [GenericItem, Types::Type, Object] other object to
|
|
13
|
+
# compare to
|
|
14
|
+
#
|
|
15
|
+
# If this item is +NULL+ or +UNDEF+, and +other+ is nil, they are
|
|
16
|
+
# considered equal
|
|
17
|
+
#
|
|
18
|
+
# If this item is +NULL+ or +UNDEF+, and other is a {GenericItem}, they
|
|
19
|
+
# are only considered equal if the other item is in the exact same
|
|
20
|
+
# state (i.e. +NULL+ != +UNDEF+)
|
|
21
|
+
#
|
|
22
|
+
# Otherwise, the state of this item is compared with +other+
|
|
23
|
+
#
|
|
24
|
+
# @return [Integer, nil] -1, 0, +1 depending on whether +other+ is
|
|
25
|
+
# less than, equal to, or greater than self
|
|
26
|
+
#
|
|
27
|
+
# nil is returned if the two values are incomparable
|
|
28
|
+
#
|
|
29
|
+
def <=>(other)
|
|
30
|
+
logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
|
|
31
|
+
# if we're NULL or UNDEF, implement special logic
|
|
32
|
+
unless state?
|
|
33
|
+
# if comparing to nil, consider ourselves equal
|
|
34
|
+
return 0 if other.nil?
|
|
35
|
+
# if the other object is an Item, only consider equal if we're
|
|
36
|
+
# in the same _kind_ of UnDefType state
|
|
37
|
+
return raw_state == other.raw_state if other.is_a?(GenericItem) && !other.state?
|
|
38
|
+
|
|
39
|
+
# otherwise, it's a non-nil thing comparing to nil, which is undefined
|
|
40
|
+
return nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# delegate to how the state compares to the other object
|
|
44
|
+
state <=> other
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenHAB
|
|
4
|
+
module DSL
|
|
5
|
+
#
|
|
6
|
+
# Patches OpenHAB items
|
|
7
|
+
#
|
|
8
|
+
module Items
|
|
9
|
+
java_import org.openhab.core.library.items.ContactItem
|
|
10
|
+
|
|
11
|
+
# Alias class for ContactItem
|
|
12
|
+
::Contact = ContactItem
|
|
13
|
+
|
|
14
|
+
# Adds methods to core OpenHAB ContactItem type to make it more natural
|
|
15
|
+
# in Ruby
|
|
16
|
+
class ContactItem < GenericItem
|
|
17
|
+
remove_method :==
|
|
18
|
+
|
|
19
|
+
#
|
|
20
|
+
# Return the inverted state of the contact: +CLOSED+ if the contact is
|
|
21
|
+
# +OPEN+, +UNDEF+ or +NULL+; +OPEN+ if the contact is +CLOSED+
|
|
22
|
+
#
|
|
23
|
+
# @return [Types::OpenClosedType] Inverted state
|
|
24
|
+
#
|
|
25
|
+
def !
|
|
26
|
+
return !state if state?
|
|
27
|
+
|
|
28
|
+
CLOSED
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @!method open?
|
|
32
|
+
# Check if the item state == +OPEN+
|
|
33
|
+
# @return [Boolean]
|
|
34
|
+
|
|
35
|
+
# @!method closed?
|
|
36
|
+
# Check if the item state == +CLOSED+
|
|
37
|
+
# @return [Boolean]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'forwardable'
|
|
4
|
+
require 'time'
|
|
5
|
+
|
|
6
|
+
require 'openhab/dsl/items/comparable_item'
|
|
7
|
+
|
|
8
|
+
module OpenHAB
|
|
9
|
+
module DSL
|
|
10
|
+
module Items
|
|
11
|
+
java_import org.openhab.core.library.items.DateTimeItem
|
|
12
|
+
|
|
13
|
+
# Adds methods to core OpenHAB DateTimeItem type to make it more natural
|
|
14
|
+
# in Ruby
|
|
15
|
+
class DateTimeItem < GenericItem
|
|
16
|
+
extend Forwardable
|
|
17
|
+
include Comparable
|
|
18
|
+
include ComparableItem
|
|
19
|
+
|
|
20
|
+
# !@visibility private
|
|
21
|
+
def ==(other)
|
|
22
|
+
# need to check if we're referring to the same item before
|
|
23
|
+
# forwarding to <=> (and thus checking equality with state)
|
|
24
|
+
return true if equal?(other) || eql?(other)
|
|
25
|
+
|
|
26
|
+
super
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
#
|
|
30
|
+
# Type Coercion
|
|
31
|
+
#
|
|
32
|
+
# Coerce object to a DateTimeType
|
|
33
|
+
#
|
|
34
|
+
# @param [Types::DateTimeType, Time] other object to coerce to a
|
|
35
|
+
# DateTimeType
|
|
36
|
+
#
|
|
37
|
+
# @return [[Types::DateTimeType, Types::DateTimeType]]
|
|
38
|
+
#
|
|
39
|
+
def coerce(other)
|
|
40
|
+
logger.trace("Coercing #{self} as a request from #{other.class}")
|
|
41
|
+
return [other, nil] unless state?
|
|
42
|
+
return [other, state] if other.is_a?(Types::DateTimeType) || other.respond_to?(:to_time)
|
|
43
|
+
|
|
44
|
+
raise TypeError, "can't convert #{other.class} into #{self.class}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# any method that exists on DateTimeType, Java's ZonedDateTime, or
|
|
48
|
+
# Ruby's Time class gets forwarded to state (which will forward as
|
|
49
|
+
# necessary)
|
|
50
|
+
delegate ((Types::DateTimeType.instance_methods +
|
|
51
|
+
java.time.ZonedDateTime.instance_methods +
|
|
52
|
+
Time.instance_methods) - instance_methods) => :state
|
|
53
|
+
|
|
54
|
+
# Time types need formatted as ISO8601
|
|
55
|
+
# @!visibility private
|
|
56
|
+
def format_type(command)
|
|
57
|
+
return command.iso8601 if command.respond_to?(:iso8601)
|
|
58
|
+
|
|
59
|
+
super
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openhab/dsl/time_of_day'
|
|
4
|
+
|
|
5
|
+
require_relative 'numeric_item'
|
|
6
|
+
|
|
7
|
+
module OpenHAB
|
|
8
|
+
module DSL
|
|
9
|
+
#
|
|
10
|
+
# Patches OpenHAB items
|
|
11
|
+
#
|
|
12
|
+
module Items
|
|
13
|
+
java_import org.openhab.core.library.items.DimmerItem
|
|
14
|
+
|
|
15
|
+
# Alias class
|
|
16
|
+
::Dimmer = DimmerItem
|
|
17
|
+
|
|
18
|
+
# Adds methods to core OpenHAB DimmerItem type to make it more natural in
|
|
19
|
+
# Ruby
|
|
20
|
+
class DimmerItem < SwitchItem
|
|
21
|
+
include NumericItem
|
|
22
|
+
|
|
23
|
+
#
|
|
24
|
+
# Dim the dimmer
|
|
25
|
+
#
|
|
26
|
+
# @param [Integer] amount to dim by
|
|
27
|
+
#
|
|
28
|
+
# @return [Integer] level target for dimmer
|
|
29
|
+
#
|
|
30
|
+
def dim(amount = 1)
|
|
31
|
+
target = [state&.-(amount), 0].compact.max
|
|
32
|
+
command(target)
|
|
33
|
+
target
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
#
|
|
37
|
+
# Brighten the dimmer
|
|
38
|
+
#
|
|
39
|
+
# @param [Integer] amount to brighten by
|
|
40
|
+
#
|
|
41
|
+
# @return [Integer] level target for dimmer
|
|
42
|
+
#
|
|
43
|
+
def brighten(amount = 1)
|
|
44
|
+
target = [state&.+(amount), 100].compact.min
|
|
45
|
+
command(target)
|
|
46
|
+
target
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @!method increase
|
|
50
|
+
# Send the +INCREASE+ command to the item
|
|
51
|
+
# @return [DimmerItem] +self+
|
|
52
|
+
|
|
53
|
+
# @!method decrease
|
|
54
|
+
# Send the +DECREASE+ command to the item
|
|
55
|
+
# @return [DimmerItem] +self+
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'generic_item'
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module DSL
|
|
7
|
+
module Items
|
|
8
|
+
# Functionality to implement +ensure+/+ensure_states+
|
|
9
|
+
module Ensure
|
|
10
|
+
# Contains the global +ensure_states+ method
|
|
11
|
+
module EnsureStates
|
|
12
|
+
# Global method that takes a block and for the duration of the block
|
|
13
|
+
# all commands sent will check if the item is in the command's state
|
|
14
|
+
# before sending the command.
|
|
15
|
+
#
|
|
16
|
+
# @example Turn on several switches only if they're not already on
|
|
17
|
+
# ensure_states do
|
|
18
|
+
# Switch1.on
|
|
19
|
+
# Switch2.on
|
|
20
|
+
# end
|
|
21
|
+
def ensure_states
|
|
22
|
+
old = Thread.current[:ensure_states]
|
|
23
|
+
Thread.current[:ensure_states] = true
|
|
24
|
+
yield
|
|
25
|
+
ensure
|
|
26
|
+
Thread.current[:ensure_states] = old
|
|
27
|
+
end
|
|
28
|
+
module_function :ensure_states
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Contains the +ensure+ method mixed into {GenericItem} and {GroupItem::GroupMembers}
|
|
32
|
+
module Ensurable
|
|
33
|
+
# Fluent method call that you can chain commands on to, that will
|
|
34
|
+
# then automatically ensure that the item is not in the command's
|
|
35
|
+
# state before sending the command.
|
|
36
|
+
#
|
|
37
|
+
# @example Turn switch on only if it's not on
|
|
38
|
+
# MySwitch.ensure.on
|
|
39
|
+
# @example Turn on all switches in a group that aren't already on
|
|
40
|
+
# MySwitchGroup.members.ensure.on
|
|
41
|
+
def ensure
|
|
42
|
+
GenericItemDelegate.new(self)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Extensions for {Items::GenericItem} to implement {Ensure}'s
|
|
47
|
+
# functionality
|
|
48
|
+
module GenericItem
|
|
49
|
+
include Ensurable
|
|
50
|
+
|
|
51
|
+
# If +ensure_states+ is active (by block or chained method), then
|
|
52
|
+
# check if this item is in the command's state before actually
|
|
53
|
+
# sending the command
|
|
54
|
+
def command(command)
|
|
55
|
+
return super unless Thread.current[:ensure_states]
|
|
56
|
+
return if command == state
|
|
57
|
+
|
|
58
|
+
super
|
|
59
|
+
end
|
|
60
|
+
alias << command
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# "anonymous" class that wraps any method call in +ensure_states+
|
|
64
|
+
# before forwarding to the wrapped object
|
|
65
|
+
# @!visibility private
|
|
66
|
+
class GenericItemDelegate
|
|
67
|
+
def initialize(item)
|
|
68
|
+
@item = item
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# activate +ensure_states+ before forwarding to the wrapped object
|
|
72
|
+
def method_missing(method, *args, &block)
|
|
73
|
+
return super unless @item.respond_to?(method)
|
|
74
|
+
|
|
75
|
+
ensure_states do
|
|
76
|
+
@item.__send__(method, *args, &block)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# .
|
|
81
|
+
def respond_to_missing?(method, include_private = false)
|
|
82
|
+
@item.respond_to?(method, include_private) || super
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
GenericItem.prepend(Ensure::GenericItem)
|
|
88
|
+
GroupItem::GroupMembers.include(Ensure::Ensurable)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
Object.include OpenHAB::DSL::Items::Ensure::EnsureStates
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openhab/dsl/items/metadata'
|
|
4
|
+
require 'openhab/dsl/items/persistence'
|
|
5
|
+
|
|
6
|
+
require_relative 'item_equality'
|
|
7
|
+
|
|
8
|
+
module OpenHAB
|
|
9
|
+
module DSL
|
|
10
|
+
module Items
|
|
11
|
+
java_import org.openhab.core.items.GenericItem
|
|
12
|
+
|
|
13
|
+
# Adds methods to core OpenHAB DimmerItem type to make it more natural in
|
|
14
|
+
# Ruby
|
|
15
|
+
class GenericItem
|
|
16
|
+
include Log
|
|
17
|
+
include ItemEquality
|
|
18
|
+
|
|
19
|
+
prepend Metadata
|
|
20
|
+
prepend Persistence
|
|
21
|
+
|
|
22
|
+
# rubocop:disable Naming/MethodName these mimic Java fields, which are
|
|
23
|
+
# actually methods
|
|
24
|
+
class << self
|
|
25
|
+
# manually define this, since the Java side doesn't
|
|
26
|
+
# @!visibility private
|
|
27
|
+
def ACCEPTED_COMMAND_TYPES
|
|
28
|
+
[org.openhab.core.types.RefreshType.java_class].freeze
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# manually define this, since the Java side doesn't
|
|
32
|
+
# @!visibility private
|
|
33
|
+
def ACCEPTED_DATA_TYPES
|
|
34
|
+
[org.openhab.core.types.UnDefType.java_class].freeze
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
# rubocop:enable Naming/MethodName
|
|
38
|
+
|
|
39
|
+
alias hash hash_code
|
|
40
|
+
|
|
41
|
+
# Get the raw item state.
|
|
42
|
+
#
|
|
43
|
+
# The state of the item, including possibly +NULL+ or +UNDEF+
|
|
44
|
+
#
|
|
45
|
+
# @return [Types::Type]
|
|
46
|
+
#
|
|
47
|
+
alias raw_state state
|
|
48
|
+
|
|
49
|
+
remove_method(:==)
|
|
50
|
+
|
|
51
|
+
#
|
|
52
|
+
# Send a command to this item
|
|
53
|
+
#
|
|
54
|
+
# @param [Types::Type] command to send to object
|
|
55
|
+
#
|
|
56
|
+
#
|
|
57
|
+
def command(command)
|
|
58
|
+
command = format_type_pre(command)
|
|
59
|
+
logger.trace "Sending Command #{command} to #{id}"
|
|
60
|
+
org.openhab.core.model.script.actions.BusEvent.sendCommand(self, command)
|
|
61
|
+
self
|
|
62
|
+
end
|
|
63
|
+
alias << command
|
|
64
|
+
|
|
65
|
+
#
|
|
66
|
+
# Send an update to this item
|
|
67
|
+
#
|
|
68
|
+
# @param [Types::Type] update the item
|
|
69
|
+
#
|
|
70
|
+
#
|
|
71
|
+
def update(update)
|
|
72
|
+
update = format_type_pre(update)
|
|
73
|
+
logger.trace "Sending Update #{update} to #{id}"
|
|
74
|
+
org.openhab.core.model.script.actions.BusEvent.postUpdate(self, update)
|
|
75
|
+
self
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
#
|
|
79
|
+
# Check if the item has a state (not +UNDEF+ or +NULL+)
|
|
80
|
+
#
|
|
81
|
+
# @return [Boolean]
|
|
82
|
+
#
|
|
83
|
+
def state?
|
|
84
|
+
!raw_state.is_a?(Types::UnDefType)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
#
|
|
88
|
+
# Get the item state
|
|
89
|
+
#
|
|
90
|
+
# @return [Types::Type, nil]
|
|
91
|
+
# OpenHAB item state if state is not +UNDEF+ or +NULL+, nil otherwise
|
|
92
|
+
#
|
|
93
|
+
def state
|
|
94
|
+
raw_state if state?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
#
|
|
98
|
+
# Get an ID for the item, using the item label if set, otherwise item name
|
|
99
|
+
#
|
|
100
|
+
# @return [String] label if set otherwise name
|
|
101
|
+
#
|
|
102
|
+
def id
|
|
103
|
+
label || name
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
#
|
|
107
|
+
# Get the string representation of the state of the item
|
|
108
|
+
#
|
|
109
|
+
# @return [String] State of the item as a string
|
|
110
|
+
#
|
|
111
|
+
def to_s
|
|
112
|
+
raw_state.to_s # call the super state to include UNDEF/NULL
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
#
|
|
116
|
+
# Inspect the item
|
|
117
|
+
#
|
|
118
|
+
# @return [String] details of the item
|
|
119
|
+
#
|
|
120
|
+
def inspect
|
|
121
|
+
to_string
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
#
|
|
125
|
+
# Return all groups that this item is part of
|
|
126
|
+
#
|
|
127
|
+
# @return [Array<Group>] All groups that this item is part of
|
|
128
|
+
#
|
|
129
|
+
def groups
|
|
130
|
+
group_names.map { |name| Groups.groups[name] }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
#
|
|
134
|
+
# Check equality without type conversion
|
|
135
|
+
#
|
|
136
|
+
# @return [Boolean] if the same Item is represented, without checking
|
|
137
|
+
# state
|
|
138
|
+
def eql?(other)
|
|
139
|
+
other.instance_of?(self.class) && hash == other.hash
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# @!method null?
|
|
143
|
+
# Check if the item state == +NULL+
|
|
144
|
+
# @return [Boolean]
|
|
145
|
+
|
|
146
|
+
# @!method undef?
|
|
147
|
+
# Check if the item state == +UNDEF+
|
|
148
|
+
# @return [Boolean]
|
|
149
|
+
|
|
150
|
+
# @!method refresh
|
|
151
|
+
# Send the +REFRESH+ command to the item
|
|
152
|
+
# @return [GenericItem] +self+
|
|
153
|
+
|
|
154
|
+
# formats a {Types::Type} to send to the event bus
|
|
155
|
+
# @!visibility private
|
|
156
|
+
def format_type(command)
|
|
157
|
+
# actual Type types can be sent directly without conversion
|
|
158
|
+
return command if command.is_a?(Types::Type)
|
|
159
|
+
|
|
160
|
+
command.to_s
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
private
|
|
164
|
+
|
|
165
|
+
# convert items to their state before formatting, so that subclasses
|
|
166
|
+
# only have to deal with Types
|
|
167
|
+
def format_type_pre(command)
|
|
168
|
+
command = command.state if command.is_a?(GenericItem)
|
|
169
|
+
format_type(command)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|