Class: OpenHAB::Core::Types::PercentType

Inherits:
DecimalType show all
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

HSBType

Instance Method Summary collapse

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

Check if DOWN

Note that DOWN is defined as 100% exactly.

Returns:

  • (true, false)


55
56
57
# File 'lib/openhab/core/types/percent_type.rb', line 55

def down?
  !!as(UpDownType)&.down?
end

#off?true, false

Check if OFF

Note that OFF is defined as 0% exactly.

Returns:

  • (true, false)


33
34
35
# File 'lib/openhab/core/types/percent_type.rb', line 33

def off?
  as(OnOffType).off?
end

#on?true, false

Check if ON

Note that ON is defined as any value besides 0%.

Returns:

  • (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

Parameters:

  • range (Range)

    the range as a numeric

Returns:

  • (Numeric)

    the value as a percentage of the 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_byteInteger

scale the value to fit in a single byte

Returns:

  • (Integer)

    an integer in the range 0-255



87
88
89
# File 'lib/openhab/core/types/percent_type.rb', line 87

def to_byte
  scale(0..255)
end

#up?true, false

Check if UP

Note that UP is defined as 0% exactly.

Returns:

  • (true, false)


44
45
46
# File 'lib/openhab/core/types/percent_type.rb', line 44

def up?
  !!as(UpDownType)&.up?
end