openhab-scripting 4.1.4 → 4.2.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/dsl.rb +6 -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/generic_item.rb +197 -0
- data/lib/openhab/dsl/items/group_item.rb +56 -92
- data/lib/openhab/dsl/items/image_item.rb +5 -41
- data/lib/openhab/dsl/items/item_registry.rb +49 -0
- data/lib/openhab/dsl/items/items.rb +71 -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 +66 -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/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 +31 -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
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
require 'openhab/dsl/items/comparable_item'
|
6
|
+
|
7
|
+
module OpenHAB
|
8
|
+
module DSL
|
9
|
+
module Items
|
10
|
+
# Mixin for implementing type coercieon, equality, and arithmetic for
|
11
|
+
# number-like items
|
12
|
+
module NumericItem
|
13
|
+
include Comparable
|
14
|
+
include ComparableItem
|
15
|
+
|
16
|
+
# apply meta-programming methods to including class
|
17
|
+
def self.included(klass)
|
18
|
+
klass.extend Forwardable
|
19
|
+
klass.delegate %i[+ - * / % | positive? negative? to_d to_f to_i to_int zero?] => :state
|
20
|
+
# remove the JRuby default == so that we can inherit the Ruby method
|
21
|
+
klass.remove_method :==
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Check if NumericItem is truthy? as per defined by library
|
26
|
+
#
|
27
|
+
# @return [Boolean] True if item is not in state +UNDEF+ or +NULL+ and value is not zero.
|
28
|
+
#
|
29
|
+
def truthy?
|
30
|
+
state && !state.zero?
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Type Coercion
|
35
|
+
#
|
36
|
+
# Coerce object to a NumericType
|
37
|
+
#
|
38
|
+
# @param [Types::NumericType, Numeric] other object to coerce to a
|
39
|
+
# DateTimeType
|
40
|
+
#
|
41
|
+
# @return [[Types::NumericType, Types::NumericType]]
|
42
|
+
#
|
43
|
+
def coerce(other)
|
44
|
+
logger.trace("Coercing #{self} as a request from #{other.class}")
|
45
|
+
return [other, nil] unless state?
|
46
|
+
return [other, state] if other.is_a?(Types::NumericType) || other.respond_to?(:to_d)
|
47
|
+
|
48
|
+
raise TypeError, "can't convert #{other.class} into #{self.class}"
|
49
|
+
end
|
50
|
+
|
51
|
+
# strip trailing zeros from commands
|
52
|
+
# @!visibility private
|
53
|
+
def format_type(command)
|
54
|
+
# DecimalType and PercentType we want to make sure we don't have extra zeros
|
55
|
+
if command.instance_of?(Types::DecimalType) || command.instance_of?(Types::PercentType)
|
56
|
+
return command.to_big_decimal.strip_trailing_zeros.to_plain_string
|
57
|
+
end
|
58
|
+
# BigDecimal types have trailing zeros stripped
|
59
|
+
return command.to_java.strip_trailing_zeros.to_plain_string if command.is_a?(BigDecimal)
|
60
|
+
|
61
|
+
super
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module DSL
|
5
|
+
module Items
|
6
|
+
#
|
7
|
+
# Persistence extension for Items
|
8
|
+
#
|
9
|
+
module Persistence
|
10
|
+
java_import Java::OrgOpenhabCoreTypesUtil::UnitUtils
|
11
|
+
|
12
|
+
# A wrapper for OpenHAB's HistoricItem that returns the state directly
|
13
|
+
class HistoricState < SimpleDelegator
|
14
|
+
attr_reader :timestamp, :state
|
15
|
+
|
16
|
+
def initialize(state, timestamp)
|
17
|
+
@state = state
|
18
|
+
@timestamp = timestamp
|
19
|
+
super(@state)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# All persistence methods that could return a QuantityType
|
24
|
+
QUANTITY_METHODS = %i[average_since
|
25
|
+
delta_since
|
26
|
+
deviation_since
|
27
|
+
sum_since
|
28
|
+
variance_since].freeze
|
29
|
+
|
30
|
+
# All persistence methods that require a timestamp
|
31
|
+
PERSISTENCE_METHODS = (QUANTITY_METHODS +
|
32
|
+
%i[changed_since
|
33
|
+
evolution_rate
|
34
|
+
historic_state
|
35
|
+
maximum_since
|
36
|
+
minimum_since
|
37
|
+
updated_since]).freeze
|
38
|
+
private_constant :QUANTITY_METHODS, :PERSISTENCE_METHODS
|
39
|
+
|
40
|
+
%i[persist last_update].each do |method|
|
41
|
+
define_method(method) do |service = nil|
|
42
|
+
service ||= persistence_service
|
43
|
+
PersistenceExtensions.public_send(method, self, service&.to_s)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Return the previous state of the item
|
49
|
+
#
|
50
|
+
# @param skip_equal [Boolean] if true, skips equal state values and
|
51
|
+
# searches the first state not equal the current state
|
52
|
+
# @param service [String] the name of the PersistenceService to use
|
53
|
+
#
|
54
|
+
# @return the previous state or nil if no previous state could be found,
|
55
|
+
# or if the default persistence service is not configured or
|
56
|
+
# does not refer to a valid service
|
57
|
+
#
|
58
|
+
def previous_state(service = nil, skip_equal: false)
|
59
|
+
service ||= persistence_service
|
60
|
+
result = PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
|
61
|
+
HistoricState.new(quantify(result.state), result.timestamp)
|
62
|
+
end
|
63
|
+
|
64
|
+
PERSISTENCE_METHODS.each do |method|
|
65
|
+
define_method(method) do |timestamp, service = nil|
|
66
|
+
service ||= persistence_service
|
67
|
+
result = PersistenceExtensions.public_send(method, self, to_zdt(timestamp), service&.to_s)
|
68
|
+
if result.is_a? Java::OrgOpenhabCorePersistence::HistoricItem
|
69
|
+
return HistoricState.new(quantify(result.state), result.timestamp)
|
70
|
+
end
|
71
|
+
|
72
|
+
QUANTITY_METHODS.include?(method) ? quantify(result) : result
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
#
|
79
|
+
# Convert timestamp to ZonedDateTime if it's a TemporalAmount
|
80
|
+
#
|
81
|
+
# @param [Object] timestamp to convert
|
82
|
+
#
|
83
|
+
# @return [ZonedDateTime]
|
84
|
+
#
|
85
|
+
def to_zdt(timestamp)
|
86
|
+
if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
|
87
|
+
logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
|
88
|
+
Java::JavaTime::ZonedDateTime.now.minus(timestamp)
|
89
|
+
else
|
90
|
+
timestamp
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Convert value to QuantityType if it is a DecimalType and a unit is defined
|
96
|
+
#
|
97
|
+
# @param [Object] value The value to convert
|
98
|
+
#
|
99
|
+
# @return [Object] QuantityType or the original value
|
100
|
+
#
|
101
|
+
def quantify(value)
|
102
|
+
if value.is_a?(Types::DecimalType) && state_description&.pattern
|
103
|
+
item_unit = UnitUtils.parse_unit(state_description.pattern)
|
104
|
+
logger.trace("Unitizing #{value} with unit #{item_unit}")
|
105
|
+
Types::QuantityType.new(value.to_big_decimal, item_unit)
|
106
|
+
else
|
107
|
+
value
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
#
|
112
|
+
# Get the specified persistence service from the current thread local variable
|
113
|
+
#
|
114
|
+
# @return [Object] Persistence service name as String or Symbol, or nil if not set
|
115
|
+
#
|
116
|
+
def persistence_service
|
117
|
+
Thread.current.thread_variable_get(:persistence_service)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -1,48 +1,57 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'forwardable'
|
4
|
-
require 'java'
|
5
|
-
require 'openhab/dsl/items/item_command'
|
6
|
-
require 'openhab/dsl/items/item_delegate'
|
7
|
-
|
8
3
|
module OpenHAB
|
9
4
|
module DSL
|
10
5
|
module Items
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
#
|
31
|
-
|
32
|
-
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
|
37
|
-
#
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
6
|
+
java_import org.openhab.core.library.items.PlayerItem
|
7
|
+
|
8
|
+
# Adds methods to core OpenHAB NumberItem type to make it more natural in
|
9
|
+
# Ruby
|
10
|
+
class PlayerItem < GenericItem
|
11
|
+
remove_method :==
|
12
|
+
|
13
|
+
# @!method play?
|
14
|
+
# Check if the item state == +PLAYING+
|
15
|
+
# @return [Boolean]
|
16
|
+
|
17
|
+
# @deprecated
|
18
|
+
# @!parse alias play? playing?
|
19
|
+
|
20
|
+
# @!method paused?
|
21
|
+
# Check if the item state == +PAUSED+
|
22
|
+
# @return [Boolean]
|
23
|
+
|
24
|
+
# @!method rewinding?
|
25
|
+
# Check if the item state == +REWIND+
|
26
|
+
# @return [Boolean]
|
27
|
+
|
28
|
+
# @!method fast_forwarding?
|
29
|
+
# Check if the item state == +FASTFORWARD+
|
30
|
+
# @return [Boolean]
|
31
|
+
|
32
|
+
# @!method play
|
33
|
+
# Send the +PLAY+ command to the item
|
34
|
+
# @return [PlayerItem] +self+
|
35
|
+
|
36
|
+
# @!method pause
|
37
|
+
# Send the +PAUSE+ command to the item
|
38
|
+
# @return [PlayerItem] +self+
|
39
|
+
|
40
|
+
# @!method rewind
|
41
|
+
# Send the +REWIND+ command to the item
|
42
|
+
# @return [PlayerItem] +self+
|
43
|
+
|
44
|
+
# @!method fast_forward
|
45
|
+
# Send the +FASTFORWARD+ command to the item
|
46
|
+
# @return [PlayerItem] +self+
|
47
|
+
|
48
|
+
# @!method next
|
49
|
+
# Send the +NEXT+ command to the item
|
50
|
+
# @return [PlayerItem] +self+
|
51
|
+
|
52
|
+
# @!method previous
|
53
|
+
# Send the +PREVIOUS+ command to the item
|
54
|
+
# @return [PlayerItem] +self+
|
46
55
|
end
|
47
56
|
end
|
48
57
|
end
|
@@ -1,94 +1,42 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require 'java'
|
5
|
-
require 'openhab/dsl/items/item_command'
|
6
|
-
require 'openhab/dsl/items/item_delegate'
|
3
|
+
require_relative 'numeric_item'
|
7
4
|
|
8
5
|
module OpenHAB
|
9
6
|
module DSL
|
10
7
|
module Items
|
11
|
-
|
12
|
-
# Delegator to OpenHAB Rollershutter Item
|
13
|
-
#
|
14
|
-
class RollershutterItem < Numeric
|
15
|
-
extend Forwardable
|
16
|
-
extend OpenHAB::DSL::Items::ItemCommand
|
17
|
-
extend OpenHAB::DSL::Items::ItemDelegate
|
18
|
-
include Comparable
|
8
|
+
java_import org.openhab.core.library.items.RollershutterItem
|
19
9
|
|
20
|
-
|
10
|
+
# Adds methods to core OpenHAB RollershutterItem type to make it more natural in
|
11
|
+
# Ruby
|
12
|
+
class RollershutterItem < GenericItem
|
13
|
+
include NumericItem
|
21
14
|
|
22
|
-
|
15
|
+
alias position state
|
23
16
|
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
# @param [Java::OrgOpenhabCoreLibraryItems::RollershutterItem] rollershutter_item
|
28
|
-
# The OpenHAB RollershutterItem to delegate to
|
29
|
-
#
|
30
|
-
def initialize(rollershutter_item)
|
31
|
-
logger.trace("Wrapping #{rollershutter_item}")
|
32
|
-
@rollershutter_item = rollershutter_item
|
17
|
+
# @!method up?
|
18
|
+
# Check if the item state == +UP+
|
19
|
+
# @return [Boolean]
|
33
20
|
|
34
|
-
|
35
|
-
|
21
|
+
# @!method down?
|
22
|
+
# Check if the item state == +DOWN+
|
23
|
+
# @return [Boolean]
|
36
24
|
|
37
|
-
|
38
|
-
|
25
|
+
# @!method up
|
26
|
+
# Send the +UP+ command to the item
|
27
|
+
# @return [RollershutterItem] +self+
|
39
28
|
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# @return [Java::OrgOpenhabCoreLibraryTypes::PercentType] the position of the rollershutter
|
44
|
-
#
|
45
|
-
def position
|
46
|
-
state&.as(PercentType)
|
47
|
-
end
|
29
|
+
# @!method down
|
30
|
+
# Send the +DOWN+ command to the item
|
31
|
+
# @return [RollershutterItem] +self+
|
48
32
|
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# @param [Object] other object to compare against
|
53
|
-
#
|
54
|
-
# @return [Integer] -1, 0 or 1 depending on the result of the comparison
|
55
|
-
#
|
56
|
-
def <=>(other)
|
57
|
-
return nil unless state?
|
33
|
+
# @!method stop
|
34
|
+
# Send the +STOP+ command to the item
|
35
|
+
# @return [RollershutterItem] +self+
|
58
36
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
when RollershutterItem then position.compare_to(other.position)
|
63
|
-
when UpDownType then state.as(UpDownType) == other
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
#
|
68
|
-
# Coerce self into other to enable calculations
|
69
|
-
#
|
70
|
-
# @param [Numeric] other Other numeric to coerce into
|
71
|
-
#
|
72
|
-
# @return [Array<Numeric>] an array of other and self coerced into other's type
|
73
|
-
#
|
74
|
-
def coerce(other)
|
75
|
-
raise ArgumentError, "Cannot coerce to #{other.class}" unless other.is_a? Numeric
|
76
|
-
|
77
|
-
case other
|
78
|
-
when Integer then [other, position&.int_value]
|
79
|
-
when Float then [other, position&.float_value]
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
#
|
84
|
-
# Define math operations
|
85
|
-
#
|
86
|
-
%i[+ - * / %].each do |operator|
|
87
|
-
define_method(operator) do |other|
|
88
|
-
right, left = coerce(other)
|
89
|
-
left&.send(operator, right)
|
90
|
-
end
|
91
|
-
end
|
37
|
+
# @!method move
|
38
|
+
# Send the +MOVE+ command to the item
|
39
|
+
# @return [RollershutterItem] +self+
|
92
40
|
end
|
93
41
|
end
|
94
42
|
end
|
@@ -1,63 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'bigdecimal'
|
4
3
|
require 'forwardable'
|
5
|
-
|
6
|
-
require 'openhab/dsl/items/
|
4
|
+
|
5
|
+
require 'openhab/dsl/items/comparable_item'
|
7
6
|
|
8
7
|
module OpenHAB
|
9
8
|
module DSL
|
10
9
|
module Items
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
|
10
|
+
java_import org.openhab.core.library.items.StringItem
|
11
|
+
|
12
|
+
# Adds methods to core OpenHAB StringItem type to make it more natural in
|
13
|
+
# Ruby
|
14
|
+
class StringItem < GenericItem
|
15
15
|
extend Forwardable
|
16
|
-
extend OpenHAB::DSL::Items::ItemCommand
|
17
16
|
include Comparable
|
18
|
-
|
17
|
+
include ComparableItem
|
18
|
+
|
19
|
+
remove_method :==
|
19
20
|
|
20
21
|
# @return [Regex] Regular expression matching blank strings
|
21
22
|
BLANK_RE = /\A[[:space:]]*\z/.freeze
|
22
23
|
private_constant :BLANK_RE
|
23
24
|
|
24
|
-
def_item_delegator :@string_item
|
25
|
-
|
26
|
-
item_type Java::OrgOpenhabCoreLibraryItems::StringItem
|
27
|
-
|
28
|
-
#
|
29
|
-
# Create a new StringItem
|
30
|
-
#
|
31
|
-
# @param [Java::Org::openhab::core::library::items::StringItem] string_item OpenHAB string item to delegate to
|
32
|
-
#
|
33
|
-
def initialize(string_item)
|
34
|
-
@string_item = string_item
|
35
|
-
|
36
|
-
item_missing_delegate { @string_item }
|
37
|
-
item_missing_delegate { @string_item.state&.to_full_string&.to_s }
|
38
|
-
|
39
|
-
super()
|
40
|
-
end
|
41
|
-
|
42
|
-
#
|
43
|
-
# Convert the StringItem into a String
|
44
|
-
#
|
45
|
-
# @return [String] String representation of the StringItem or
|
46
|
-
# nil if underlying OpenHAB StringItem does not have a state
|
47
|
-
#
|
48
|
-
def to_str
|
49
|
-
@string_item.state&.to_full_string&.to_s
|
50
|
-
end
|
51
|
-
|
52
25
|
#
|
53
26
|
# Detect if the string is blank (not set or only whitespace)
|
54
27
|
#
|
55
28
|
# @return [Boolean] True if string item is not set or contains only whitespace, false otherwise
|
56
29
|
#
|
57
30
|
def blank?
|
58
|
-
return true unless
|
31
|
+
return true unless state?
|
59
32
|
|
60
|
-
|
33
|
+
state.empty? || BLANK_RE.match?(self)
|
61
34
|
end
|
62
35
|
|
63
36
|
#
|
@@ -66,27 +39,12 @@ module OpenHAB
|
|
66
39
|
# @return [Boolean] True if item is not in state UNDEF or NULL and value is not blank
|
67
40
|
#
|
68
41
|
def truthy?
|
69
|
-
|
42
|
+
state? && !blank?
|
70
43
|
end
|
71
44
|
|
72
|
-
#
|
73
|
-
#
|
74
|
-
|
75
|
-
# @param [Object] other object to compare to
|
76
|
-
#
|
77
|
-
# @return [Integer] -1,0,1 or nil depending on value supplied,
|
78
|
-
# nil comparison to supplied object is not possible.
|
79
|
-
#
|
80
|
-
def <=>(other)
|
81
|
-
case other
|
82
|
-
when StringItem
|
83
|
-
@string_item.state <=> other.state
|
84
|
-
when String
|
85
|
-
@string_item.state.to_s <=> other
|
86
|
-
else
|
87
|
-
@string_item.state <=> other
|
88
|
-
end
|
89
|
-
end
|
45
|
+
# any method that exists on String gets forwarded to state (which will forward as
|
46
|
+
# necessary)
|
47
|
+
delegate (String.instance_methods - instance_methods) => :state
|
90
48
|
end
|
91
49
|
end
|
92
50
|
end
|
@@ -0,0 +1,62 @@
|
|
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.SwitchItem
|
10
|
+
|
11
|
+
# Alias class names for easy is_a? comparisons
|
12
|
+
::Switch = SwitchItem
|
13
|
+
|
14
|
+
# Adds methods to core OpenHAB SwitchItem type to make it more natural in
|
15
|
+
# Ruby
|
16
|
+
class SwitchItem < GenericItem
|
17
|
+
remove_method :==
|
18
|
+
|
19
|
+
def truthy?
|
20
|
+
on?
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Send a command to invert the state of the switch
|
25
|
+
#
|
26
|
+
# @return [Types::OnOffType] Inverted state
|
27
|
+
#
|
28
|
+
def toggle
|
29
|
+
command(!self)
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Return the inverted state of the switch: +ON+ if the switch is +OFF+,
|
34
|
+
# +UNDEF+ or +NULL+; +OFF+ if the switch is +ON+
|
35
|
+
#
|
36
|
+
# @return [Types::OnOffType] Inverted state
|
37
|
+
#
|
38
|
+
def !
|
39
|
+
return !state if state?
|
40
|
+
|
41
|
+
ON
|
42
|
+
end
|
43
|
+
|
44
|
+
# @!method on?
|
45
|
+
# Check if the item state == +ON+
|
46
|
+
# @return [Boolean]
|
47
|
+
|
48
|
+
# @!method off?
|
49
|
+
# Check if the item state == +OFF+
|
50
|
+
# @return [Boolean]
|
51
|
+
|
52
|
+
# @!method on
|
53
|
+
# Send the +ON+ command to the item
|
54
|
+
# @return [SwitchItem] +self+
|
55
|
+
|
56
|
+
# @!method off
|
57
|
+
# Send the +OFF+ command to the item
|
58
|
+
# @return [SwitchItem] +self+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module OpenHAB
|
4
4
|
module DSL
|
5
|
-
#
|
5
|
+
# Common base class for array-like collections that have lookup
|
6
6
|
# methods to avoid instantiating the elements if you only use
|
7
7
|
# the lookup method
|
8
8
|
#
|
@@ -11,7 +11,7 @@ module OpenHAB
|
|
11
11
|
module LazyArray
|
12
12
|
include Enumerable
|
13
13
|
|
14
|
-
# Calls the given block once for each
|
14
|
+
# Calls the given block once for each object, passing that object as a
|
15
15
|
# parameter. Returns self.
|
16
16
|
#
|
17
17
|
# If no block is given, an Enumerator is returned.
|
@@ -20,20 +20,22 @@ module OpenHAB
|
|
20
20
|
self
|
21
21
|
end
|
22
22
|
|
23
|
-
#
|
23
|
+
# Implicitly convertible to array
|
24
|
+
#
|
25
|
+
# @return [Array]
|
26
|
+
#
|
24
27
|
def to_ary
|
25
28
|
to_a
|
26
29
|
end
|
27
30
|
|
28
|
-
#
|
29
|
-
# exclude mutating methods though
|
31
|
+
# Delegate any other methods to the actual array, exclude mutating methods
|
30
32
|
def method_missing(method, *args, &block)
|
31
33
|
return to_a.send(method, *args, &block) if method[-1] != '!' && Array.instance_methods.include?(method)
|
32
34
|
|
33
35
|
super
|
34
36
|
end
|
35
37
|
|
36
|
-
#
|
38
|
+
# @!visibility private
|
37
39
|
def respond_to_missing?(method, include_private = false)
|
38
40
|
return true if method[-1] != '!' && Array.instance_methods.include?(method.to_sym)
|
39
41
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'openhab/dsl/monkey_patch/events/item_state'
|
4
3
|
require 'openhab/dsl/monkey_patch/events/item_event'
|
4
|
+
require 'openhab/dsl/monkey_patch/events/item_state'
|
5
5
|
require 'openhab/dsl/monkey_patch/events/item_state_changed'
|
6
|
-
require 'openhab/dsl/monkey_patch/events/thing_status_info'
|
7
6
|
require 'openhab/dsl/monkey_patch/events/item_command'
|
7
|
+
require 'openhab/dsl/monkey_patch/events/thing_status_info'
|