Class: ESPHome::Entities::Number

Inherits:
ESPHome::Entity show all
Includes:
HasDeviceClass, HasState
Defined in:
lib/esphome/entities/number.rb

Instance Attribute Summary collapse

Attributes inherited from ESPHome::Entity

#device, #disabled_by_default, #entity_category, #icon, #key, #name, #object_id_

Instance Method Summary collapse

Methods inherited from ESPHome::Entity

#inspect

Constructor Details

#initialize(_device, list_entities_response) ⇒ Number

Returns a new instance of Number.



11
12
13
14
15
16
17
18
19
# File 'lib/esphome/entities/number.rb', line 11

def initialize(_device, list_entities_response)
  super

  @range = Range.new(list_entities_response.min_value, list_entities_response.max_value)
  @max_value = list_entities_response.max_value
  @step = list_entities_response.step
  @unit_of_measurement = list_entities_response.unit_of_measurement
  @mode = list_entities_response.mode[12..].downcase.to_sym
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/esphome/entities/number.rb', line 9

def mode
  @mode
end

#rangeObject (readonly)

Returns the value of attribute range.



9
10
11
# File 'lib/esphome/entities/number.rb', line 9

def range
  @range
end

#stepObject (readonly)

Returns the value of attribute step.



9
10
11
# File 'lib/esphome/entities/number.rb', line 9

def step
  @step
end

#unit_of_measurementObject (readonly)

Returns the value of attribute unit_of_measurement.



9
10
11
# File 'lib/esphome/entities/number.rb', line 9

def unit_of_measurement
  @unit_of_measurement
end

Instance Method Details

#accuracy_decimalsObject



21
22
23
24
25
26
27
28
29
# File 'lib/esphome/entities/number.rb', line 21

def accuracy_decimals
  str = step.to_s
  return 0 unless str.include?(".")

  decimals = str.split(".").last
  return 0 if decimals == "0"

  decimals.size
end

#formatted_state(state = self.state) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/esphome/entities/number.rb', line 31

def formatted_state(state = self.state)
  result = if state
             format("%.#{accuracy_decimals}f", state)
           else
             "-"
           end
  result += " #{unit_of_measurement}" if unit_of_measurement
  result
end

#set(state) ⇒ Object



41
42
43
# File 'lib/esphome/entities/number.rb', line 41

def set(state)
  device.send(Api::NumberCommandRequest.new(key:, state:))
end