Class: OpenHAB::Core::Types::PercentType
- Inherits:
-
DecimalType
- Object
- DecimalType
- OpenHAB::Core::Types::PercentType
- Defined in:
- lib/openhab/core/types/percent_type.rb
Overview
PercentType extends DecimalType by putting constraints for its value on top (0-100).
Direct Known Subclasses
Instance Method Summary collapse
-
#down? ⇒ true, false
Check if DOWN.
-
#off? ⇒ true, false
Check if OFF.
-
#on? ⇒ true, false
Check if ON.
-
#scale(range) ⇒ Numeric
Scale the value to a particular range.
-
#to_byte ⇒ Integer
scale the value to fit in a single byte.
-
#up? ⇒ true, false
Check if UP.
Methods inherited from DecimalType
#-@, #<=>, #coerce, #initialize, #|
Methods included from NumericType
#+@, #eql?, #to_d, #to_f, #to_i
Constructor Details
This class inherits a constructor from OpenHAB::Core::Types::DecimalType
Instance Method Details
#down? ⇒ true, false
55 56 57 |
# File 'lib/openhab/core/types/percent_type.rb', line 55 def down? !!as(UpDownType)&.down? end |
#off? ⇒ true, false
33 34 35 |
# File 'lib/openhab/core/types/percent_type.rb', line 33 def off? as(OnOffType).off? end |
#on? ⇒ true, false
22 23 24 |
# File 'lib/openhab/core/types/percent_type.rb', line 22 def on? as(OnOffType).on? end |
#scale(range) ⇒ Numeric
Scale the value to a particular range
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/openhab/core/types/percent_type.rb', line 71 def scale(range) unless range.is_a?(Range) && range.min.is_a?(Numeric) && range.max.is_a?(Numeric) raise ArgumentError, "range must be a Range of Numerics" end result = (to_d * (range.max - range.min) / 100) + range.min case range.max when Integer then result.round when Float then result.to_f else result end end |
#to_byte ⇒ Integer
scale the value to fit in a single byte
87 88 89 |
# File 'lib/openhab/core/types/percent_type.rb', line 87 def to_byte scale(0..255) end |