openhab-scripting 2.15.0 → 2.16.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openhab.rb +12 -16
- data/lib/openhab/core/entity_lookup.rb +162 -0
- data/lib/openhab/core/openhab_setup.rb +31 -0
- data/lib/openhab/core/osgi.rb +61 -0
- data/lib/openhab/dsl/actions.rb +105 -0
- data/lib/openhab/dsl/dsl.rb +47 -0
- data/lib/openhab/{core/dsl → dsl}/gems.rb +0 -1
- data/lib/openhab/dsl/group.rb +100 -0
- data/lib/openhab/dsl/items/items.rb +46 -0
- data/lib/openhab/dsl/items/number_item.rb +352 -0
- data/lib/openhab/dsl/items/string_item.rb +120 -0
- data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
- data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
- data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
- data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
- data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
- data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
- data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +7 -0
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
- data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
- data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
- data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
- data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
- data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
- data/lib/openhab/dsl/monkey_patch/types/types.rb +8 -0
- data/lib/openhab/dsl/persistence.rb +25 -0
- data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
- data/lib/openhab/dsl/rules/guard.rb +134 -0
- data/lib/openhab/dsl/rules/property.rb +102 -0
- data/lib/openhab/dsl/rules/rule.rb +116 -0
- data/lib/openhab/dsl/rules/rule_config.rb +151 -0
- data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
- data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
- data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
- data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
- data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
- data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
- data/lib/openhab/dsl/states.rb +61 -0
- data/lib/openhab/dsl/things.rb +91 -0
- data/lib/openhab/dsl/time_of_day.rb +228 -0
- data/lib/openhab/dsl/timers.rb +77 -0
- data/lib/openhab/dsl/types/quantity.rb +290 -0
- data/lib/openhab/dsl/units.rb +39 -0
- data/lib/openhab/log/configuration.rb +21 -0
- data/lib/openhab/log/logger.rb +172 -0
- data/lib/openhab/version.rb +1 -1
- metadata +55 -56
- data/lib/openhab/configuration.rb +0 -16
- data/lib/openhab/core/cron.rb +0 -27
- data/lib/openhab/core/debug.rb +0 -34
- data/lib/openhab/core/dsl.rb +0 -51
- data/lib/openhab/core/dsl/actions.rb +0 -107
- data/lib/openhab/core/dsl/entities.rb +0 -140
- data/lib/openhab/core/dsl/group.rb +0 -93
- data/lib/openhab/core/dsl/items/items.rb +0 -51
- data/lib/openhab/core/dsl/items/number_item.rb +0 -318
- data/lib/openhab/core/dsl/items/string_item.rb +0 -120
- data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
- data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
- data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
- data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
- data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
- data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
- data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
- data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -167
- data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
- data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
- data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
- data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
- data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
- data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
- data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
- data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -6
- data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -24
- data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
- data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
- data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
- data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -7
- data/lib/openhab/core/dsl/persistence.rb +0 -27
- data/lib/openhab/core/dsl/property.rb +0 -96
- data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -348
- data/lib/openhab/core/dsl/rule/guard.rb +0 -136
- data/lib/openhab/core/dsl/rule/rule.rb +0 -117
- data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
- data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
- data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
- data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
- data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
- data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
- data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
- data/lib/openhab/core/dsl/states.rb +0 -63
- data/lib/openhab/core/dsl/things.rb +0 -93
- data/lib/openhab/core/dsl/time_of_day.rb +0 -229
- data/lib/openhab/core/dsl/timers.rb +0 -79
- data/lib/openhab/core/dsl/types/quantity.rb +0 -292
- data/lib/openhab/core/dsl/units.rb +0 -41
- data/lib/openhab/core/log.rb +0 -170
- data/lib/openhab/core/patch_load_path.rb +0 -7
- data/lib/openhab/core/startup_delay.rb +0 -23
- data/lib/openhab/osgi.rb +0 -59
@@ -0,0 +1,120 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
require 'forwardable'
|
5
|
+
require 'java'
|
6
|
+
|
7
|
+
module OpenHAB
|
8
|
+
module DSL
|
9
|
+
module Items
|
10
|
+
#
|
11
|
+
# Delegator to OpenHAB String Item
|
12
|
+
#
|
13
|
+
class StringItem
|
14
|
+
extend Forwardable
|
15
|
+
include Comparable
|
16
|
+
|
17
|
+
# @return [Regex] Regular expression matching blank strings
|
18
|
+
BLANK_RE = /\A[[:space:]]*\z/.freeze
|
19
|
+
private_constant :BLANK_RE
|
20
|
+
|
21
|
+
def_delegator :@string_item, :to_s
|
22
|
+
|
23
|
+
#
|
24
|
+
# Create a new StringItem
|
25
|
+
#
|
26
|
+
# @param [Java::Org::openhab::core::library::items::StringItem] string_item OpenHAB string item to delegate to
|
27
|
+
#
|
28
|
+
def initialize(string_item)
|
29
|
+
@string_item = string_item
|
30
|
+
super()
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Convert the StringItem into a String
|
35
|
+
#
|
36
|
+
# @return [String] String representation of the StringItem or
|
37
|
+
# nil if underlying OpenHAB StringItem does not have a state
|
38
|
+
#
|
39
|
+
def to_str
|
40
|
+
@string_item.state&.to_full_string&.to_s
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Detect if the string is blank (not set or only whitespace)
|
45
|
+
#
|
46
|
+
# @return [Boolean] True if string item is not set or contains only whitespace, false otherwise
|
47
|
+
#
|
48
|
+
def blank?
|
49
|
+
return true unless @string_item.state?
|
50
|
+
|
51
|
+
@string_item.state.to_full_string.to_s.empty? || BLANK_RE.match?(self)
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Check if StringItem is truthy? as per defined by library
|
56
|
+
#
|
57
|
+
# @return [Boolean] True if item is not in state UNDEF or NULL and value is not blank
|
58
|
+
#
|
59
|
+
def truthy?
|
60
|
+
@string_item.state? && blank? == false
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Compare StringItem to supplied object
|
65
|
+
#
|
66
|
+
# @param [Object] other object to compare to
|
67
|
+
#
|
68
|
+
# @return [Integer] -1,0,1 or nil depending on value supplied,
|
69
|
+
# nil comparison to supplied object is not possible.
|
70
|
+
#
|
71
|
+
def <=>(other)
|
72
|
+
case other
|
73
|
+
when StringItem
|
74
|
+
@string_item.state <=> other.state
|
75
|
+
when String
|
76
|
+
@string_item.state.to_s <=> other
|
77
|
+
else
|
78
|
+
@string_item.state <=> other
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# Forward missing methods to Openhab String Item or String representation of the item if they are defined
|
84
|
+
#
|
85
|
+
# @param [String] meth method name
|
86
|
+
# @param [Array] args arguments for method
|
87
|
+
# @param [Proc] block <description>
|
88
|
+
#
|
89
|
+
# @return [Object] Value from delegated method in OpenHAB StringItem or Ruby String
|
90
|
+
#
|
91
|
+
def method_missing(meth, *args, &block)
|
92
|
+
if @string_item.respond_to?(meth)
|
93
|
+
@string_item.__send__(meth, *args, &block)
|
94
|
+
elsif @string_item.state&.to_full_string&.to_s.respond_to?(meth)
|
95
|
+
@string_item.state.to_full_string.to_s.__send__(meth, *args, &block)
|
96
|
+
elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
|
97
|
+
::Kernel.instance_method(meth).bind_call(self, *args, &block)
|
98
|
+
else
|
99
|
+
super(meth, *args, &block)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
#
|
104
|
+
# Checks if this method responds to the missing method
|
105
|
+
#
|
106
|
+
# @param [String] method_name Name of the method to check
|
107
|
+
# @param [Boolean] _include_private boolean if private methods should be checked
|
108
|
+
#
|
109
|
+
# @return [Boolean] true if this object will respond to the supplied method, false otherwise
|
110
|
+
#
|
111
|
+
def respond_to_missing?(method_name, _include_private = false)
|
112
|
+
@string_item.respond_to?(method_name) ||
|
113
|
+
@string_item.state&.to_full_string&.to_s.respond_to?(method_name) ||
|
114
|
+
::Kernel.method_defined?(method_name) ||
|
115
|
+
::Kernel.private_method_defined?(method_name)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'java'
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module DSL
|
7
|
+
module MonkeyPatch
|
8
|
+
#
|
9
|
+
# Patches OpenHAB actions
|
10
|
+
#
|
11
|
+
module Actions
|
12
|
+
java_import Java::OrgOpenhabCoreAutomationModuleScriptInternalDefaultscope::ScriptThingActions
|
13
|
+
|
14
|
+
#
|
15
|
+
# MonkeyPatching ScriptThingActions
|
16
|
+
#
|
17
|
+
class ScriptThingActions
|
18
|
+
field_reader :THING_ACTIONS_MAP
|
19
|
+
|
20
|
+
#
|
21
|
+
# Fetch keys for all actions defined in OpenHAB
|
22
|
+
#
|
23
|
+
# @return [Set] of keys for defined actions in the form of 'scope-thing_uid'
|
24
|
+
#
|
25
|
+
def action_keys
|
26
|
+
ScriptThingActions.THING_ACTIONS_MAP.keys
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'java'
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module DSL
|
7
|
+
module MonkeyPatch
|
8
|
+
#
|
9
|
+
# Patches OpenHAB events
|
10
|
+
#
|
11
|
+
module Events
|
12
|
+
java_import Java::OrgOpenhabCoreItemsEvents::ItemCommandEvent
|
13
|
+
|
14
|
+
#
|
15
|
+
# Monkey patch with ruby style accesors
|
16
|
+
#
|
17
|
+
class ItemCommandEvent
|
18
|
+
alias command item_command
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'java'
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module DSL
|
7
|
+
module MonkeyPatch
|
8
|
+
#
|
9
|
+
# Patches OpenHAB events
|
10
|
+
#
|
11
|
+
module Events
|
12
|
+
java_import Java::OrgOpenhabCoreItemsEvents::ItemStateChangedEvent
|
13
|
+
|
14
|
+
#
|
15
|
+
# MonkeyPatch with ruby style accessors
|
16
|
+
#
|
17
|
+
class ItemStateChangedEvent
|
18
|
+
#
|
19
|
+
# Get the item that caused the state change
|
20
|
+
#
|
21
|
+
# @return [Item] Item that caused state change
|
22
|
+
#
|
23
|
+
def item
|
24
|
+
# rubocop:disable Style/GlobalVars
|
25
|
+
$ir.get(item_name)
|
26
|
+
# rubocop:enable Style/GlobalVars
|
27
|
+
end
|
28
|
+
|
29
|
+
alias state item_state
|
30
|
+
alias last old_item_state
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'java'
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module DSL
|
7
|
+
module MonkeyPatch
|
8
|
+
#
|
9
|
+
# Patches OpenHAB events
|
10
|
+
#
|
11
|
+
module Event
|
12
|
+
java_import Java::OrgOpenhabCoreThingEvents::ThingStatusInfoChangedEvent
|
13
|
+
java_import Java::OrgOpenhabCoreThingEvents::ThingStatusInfoEvent
|
14
|
+
#
|
15
|
+
# Monkey patch with ruby style accessors
|
16
|
+
#
|
17
|
+
class ThingStatusInfoChangedEvent
|
18
|
+
alias uid get_thing_uid
|
19
|
+
alias last get_old_status_info
|
20
|
+
alias status status_info
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Monkey patch with ruby style accessors
|
25
|
+
#
|
26
|
+
class ThingStatusInfoEvent
|
27
|
+
alias uid get_thing_uid
|
28
|
+
alias status status_info
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'java'
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module DSL
|
7
|
+
module MonkeyPatch
|
8
|
+
#
|
9
|
+
# Patches OpenHAB items
|
10
|
+
#
|
11
|
+
module Items
|
12
|
+
java_import Java::OrgOpenhabCoreLibraryItems::ContactItem
|
13
|
+
|
14
|
+
#
|
15
|
+
# Alias class for ContactItem
|
16
|
+
#
|
17
|
+
::Contact = ContactItem
|
18
|
+
|
19
|
+
#
|
20
|
+
# Monkey patch Contact Item with Ruby methods
|
21
|
+
#
|
22
|
+
class ContactItem
|
23
|
+
java_import org.openhab.core.library.types.OpenClosedType
|
24
|
+
#
|
25
|
+
# Check if the contact is open
|
26
|
+
#
|
27
|
+
# @return [Boolean] True if contact has state and is open, false otherwise
|
28
|
+
#
|
29
|
+
def open?
|
30
|
+
state? && state == OpenClosedType::OPEN
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Check if the contact is closed
|
35
|
+
#
|
36
|
+
# @return [Boolean] True if contact has state and is closed, false otherwise
|
37
|
+
#
|
38
|
+
def closed?
|
39
|
+
state? && state == OpenClosedType::CLOSED
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Compares contacts to OpenClosedTypes
|
44
|
+
#
|
45
|
+
# @param [Object] other object to compare to
|
46
|
+
#
|
47
|
+
# @return [Boolean] True if contact has a state and state equals other, nil if no state,
|
48
|
+
# result from super if not supplied an OpenClosedType
|
49
|
+
#
|
50
|
+
def ==(other)
|
51
|
+
if other.is_a? OpenClosedType
|
52
|
+
state? && state == other
|
53
|
+
else
|
54
|
+
super
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'java'
|
4
|
+
|
5
|
+
module OpenHAB
|
6
|
+
module DSL
|
7
|
+
module MonkeyPatch
|
8
|
+
#
|
9
|
+
# Patches OpenHAB items
|
10
|
+
#
|
11
|
+
module Items
|
12
|
+
java_import Java::OrgOpenhabCoreLibraryItems::DimmerItem
|
13
|
+
java_import Java::OrgOpenhabCoreItems::GenericItem
|
14
|
+
|
15
|
+
#
|
16
|
+
# Alias class for is_a? testing
|
17
|
+
#
|
18
|
+
::Dimmer = DimmerItem
|
19
|
+
|
20
|
+
#
|
21
|
+
# Monkey Patch DimmerItem
|
22
|
+
#
|
23
|
+
class DimmerItem
|
24
|
+
include Comparable
|
25
|
+
java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
|
26
|
+
java_import Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
|
27
|
+
|
28
|
+
#
|
29
|
+
# Add the current dimmer value to the supplied object
|
30
|
+
#
|
31
|
+
# @param [Object] other object to add the dimmer value to
|
32
|
+
#
|
33
|
+
# @return [Integer] Current dimmer value plus value of supplied object
|
34
|
+
#
|
35
|
+
def +(other)
|
36
|
+
return unless state?
|
37
|
+
|
38
|
+
state.to_big_decimal.intValue + other
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Subtract the supplied object from the current value of the dimmer
|
43
|
+
#
|
44
|
+
# @param [Object] other object to subtract from the dimmer value
|
45
|
+
#
|
46
|
+
# @return [Integer] Current dimmer value minus value of supplied object
|
47
|
+
#
|
48
|
+
def -(other)
|
49
|
+
return unless state?
|
50
|
+
|
51
|
+
state.to_big_decimal.intValue - other
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Dim the dimmer
|
56
|
+
#
|
57
|
+
# @param [Integer] amount to dim by
|
58
|
+
#
|
59
|
+
# @return [Integer] level target for dimmer
|
60
|
+
#
|
61
|
+
def dim(amount = 1)
|
62
|
+
return unless state?
|
63
|
+
|
64
|
+
target = [state.to_big_decimal.intValue - amount, 0].max
|
65
|
+
|
66
|
+
if amount == 1
|
67
|
+
command(IncreaseDecreaseType::DECREASE)
|
68
|
+
else
|
69
|
+
command(target)
|
70
|
+
end
|
71
|
+
|
72
|
+
target
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# Brighten the dimmer
|
77
|
+
#
|
78
|
+
# @param [Integer] amount to brighten by
|
79
|
+
#
|
80
|
+
# @return [Integer] level target for dimmer
|
81
|
+
#
|
82
|
+
def brighten(amount = 1)
|
83
|
+
return unless state?
|
84
|
+
|
85
|
+
target = state.to_big_decimal.intValue + amount
|
86
|
+
|
87
|
+
if amount == 1
|
88
|
+
command(IncreaseDecreaseType::INCREASE)
|
89
|
+
else
|
90
|
+
command(target)
|
91
|
+
end
|
92
|
+
target
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Compare DimmerItem to supplied object
|
97
|
+
#
|
98
|
+
# @param [Object] other object to compare to
|
99
|
+
#
|
100
|
+
# @return [Integer] -1,0,1 or nil depending on value supplied,
|
101
|
+
# nil comparison to supplied object is not possible.
|
102
|
+
#
|
103
|
+
def <=>(other)
|
104
|
+
logger.trace("Comparing #{self} to #{other}")
|
105
|
+
case other
|
106
|
+
when GenericItem, NumberItem then state <=> other.state
|
107
|
+
when DecimalType then state <=> other
|
108
|
+
when Numeric then state.to_big_decimal.to_d <=> other.to_d
|
109
|
+
else compare_to(other)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# Coerce objects into a DimmerItem
|
115
|
+
#
|
116
|
+
# @param [Object] other object to coerce to a DimmerItem if possible
|
117
|
+
#
|
118
|
+
# @return [Object] Numeric when applicable
|
119
|
+
#
|
120
|
+
def coerce(other)
|
121
|
+
logger.trace("Coercing #{self} as a request from #{other.class}")
|
122
|
+
case other
|
123
|
+
when Numeric
|
124
|
+
[other, state.to_big_decimal.to_d]
|
125
|
+
else
|
126
|
+
[other, state]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# Compare DimmerItem to supplied object.
|
132
|
+
# The == operator needs to be overridden because the parent java object
|
133
|
+
# has .equals which overrides the <=> operator above
|
134
|
+
#
|
135
|
+
# @param [Object] other object to compare to
|
136
|
+
#
|
137
|
+
# @return [Integer] true if the two objects contain the same value, false otherwise
|
138
|
+
#
|
139
|
+
def ==(other)
|
140
|
+
(self <=> other).zero?
|
141
|
+
end
|
142
|
+
|
143
|
+
#
|
144
|
+
# Check if dimmer has a state and state is not zero
|
145
|
+
#
|
146
|
+
# @return [Boolean] True if dimmer is not NULL or UNDEF and value is not 0
|
147
|
+
#
|
148
|
+
def truthy?
|
149
|
+
state? && state != DecimalType::ZERO
|
150
|
+
end
|
151
|
+
|
152
|
+
#
|
153
|
+
# Value of dimmer
|
154
|
+
#
|
155
|
+
# @return [Integer] Value of dimmer or nil if state is UNDEF or NULL
|
156
|
+
#
|
157
|
+
def to_i
|
158
|
+
state&.to_big_decimal&.intValue
|
159
|
+
end
|
160
|
+
|
161
|
+
alias to_int to_i
|
162
|
+
|
163
|
+
#
|
164
|
+
# Return the string representation of the dimmer item
|
165
|
+
#
|
166
|
+
# @return [String] String version of the dimmer value
|
167
|
+
#
|
168
|
+
def to_s
|
169
|
+
to_i.to_s
|
170
|
+
end
|
171
|
+
|
172
|
+
#
|
173
|
+
# Check if dimmer is on
|
174
|
+
#
|
175
|
+
# @return [Boolean] True if item is not UNDEF or NULL and has a value greater than 0
|
176
|
+
#
|
177
|
+
def on?
|
178
|
+
state&.to_big_decimal&.intValue&.positive?
|
179
|
+
end
|
180
|
+
|
181
|
+
#
|
182
|
+
# Check if dimmer is off
|
183
|
+
#
|
184
|
+
# @return [Boolean] True if item is not UNDEF or NULL and has a state of 0
|
185
|
+
#
|
186
|
+
def off?
|
187
|
+
state&.to_big_decimal&.intValue&.zero?
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|