Class: ESPHome::Entities::Fan

Inherits:
ESPHome::Entity show all
Includes:
HasState
Defined in:
lib/esphome/entities/fan.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) ⇒ Fan

Returns a new instance of Fan.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/esphome/entities/fan.rb', line 14

def initialize(_device, list_entities_response)
  super

  @supports_oscillation = list_entities_response.supports_oscillation
  @supports_speed = list_entities_response.supports_speed
  @speed_count = list_entities_response.supported_speed_count if speed?
  @speed_count = 3 if speed_count.zero? && speed?
  @preset_modes = list_entities_response.supported_preset_modes.map(&:freeze).freeze

  @oscillating = @speed = @direction = @preset_mode = nil
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



8
9
10
# File 'lib/esphome/entities/fan.rb', line 8

def direction
  @direction
end

#preset_modeObject (readonly)

Returns the value of attribute preset_mode.



8
9
10
# File 'lib/esphome/entities/fan.rb', line 8

def preset_mode
  @preset_mode
end

#preset_modesObject (readonly)

Returns the value of attribute preset_modes.



8
9
10
# File 'lib/esphome/entities/fan.rb', line 8

def preset_modes
  @preset_modes
end

#speedObject (readonly)

Returns the value of attribute speed.



8
9
10
# File 'lib/esphome/entities/fan.rb', line 8

def speed
  @speed
end

#speed_countObject (readonly)

Returns the value of attribute speed_count.



8
9
10
# File 'lib/esphome/entities/fan.rb', line 8

def speed_count
  @speed_count
end

Instance Method Details

#formatted_stateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/esphome/entities/fan.rb', line 38

def formatted_state
  result = case state
           when nil then "-"
           when true then "on"
           else "off"
           end
  if speed?
    result +=
      if speed_count == 100
        " #{speed || "-"}%"
      else
        " #{speed || "-"}/#{speed_count}"
      end
  end
  result += " oscillating" if oscillating?
  result += " reversed" if direction == :reverse
  result += " (#{preset_mode})" if preset_mode
  result
end

#oscillating?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/esphome/entities/fan.rb', line 34

def oscillating?
  @oscillating
end

#oscillation?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/esphome/entities/fan.rb', line 26

def oscillation?
  @supports_oscillation
end

#speed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/esphome/entities/fan.rb', line 30

def speed?
  @supports_speed
end

#update(state_response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/esphome/entities/fan.rb', line 58

def update(state_response)
  @state = state_response.state
  @oscillating = state_response.oscillating
  @speed = state_response.speed_level
  if speed? && speed.nil?
    @speed = { FAN_SPEED_LOW: 1,
               FAN_SPEED_MEDIUM: 2,
               FAN_SPEED_HIGH: 3 }[state_response.speed]
  end
  @direction = state_response.direction[14..].downcase.to_sym
  @preset_mode = state_response.preset_mode.empty? ? nil : state_response.preset_mode
end