Class: ESPHome::Entities::Cover

Inherits:
ESPHome::Entity show all
Includes:
HasAssumedState, HasDeviceClass
Defined in:
lib/esphome/entities/cover.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) ⇒ Cover

Returns a new instance of Cover.



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

def initialize(_device, list_entities_response)
  super

  @supports_position = list_entities_response.supports_position
  @supports_stop = list_entities_response.supports_stop
  @supports_tilt = list_entities_response.supports_tilt
  @position = @tilt = @current_operation = nil
end

Instance Attribute Details

#current_operationObject (readonly)

Returns the value of attribute current_operation.



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

def current_operation
  @current_operation
end

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
end

#tiltObject (readonly)

Returns the value of attribute tilt.



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

def tilt
  @tilt
end

Instance Method Details

#closeObject



97
98
99
100
101
102
103
# File 'lib/esphome/entities/cover.rb', line 97

def close
  device.send(Api::CoverCommandRequest.new(key:,
                                           has_legacy_command: true,
                                           legacy_command: :LEGACY_COVER_COMMAND_CLOSE,
                                           has_position: true,
                                           position: 0.0))
end

#command(position: nil, tilt: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/esphome/entities/cover.rb', line 76

def command(position: nil, tilt: nil)
  command = Api::CoverCommandRequest.new(key:)
  if position
    command.has_position = true
    command.position = position
  end
  if tilt
    command.has_tilt = true
    command.tilt = tilt
  end
  device.send(command)
end

#formatted_operation_segmentObject



61
62
63
# File 'lib/esphome/entities/cover.rb', line 61

def formatted_operation_segment
  "(#{current_operation || "-"})"
end

#formatted_position_segmentObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/esphome/entities/cover.rb', line 45

def formatted_position_segment
  if position?
    formatted_percentage_segment(position)
  elsif position == 1.0 # rubocop:disable Lint/FloatComparison
    "OPEN"
  elsif position == 0.0
    "CLOSED"
  else
    "-"
  end
end

#formatted_segmentsObject



36
37
38
39
40
41
42
43
# File 'lib/esphome/entities/cover.rb', line 36

def formatted_segments
  segments = [formatted_position_segment]
  if tilt?
    segments << "-"
    segments << formatted_tilt_segment
  end
  segments << formatted_operation_segment
end

#formatted_stateObject



32
33
34
# File 'lib/esphome/entities/cover.rb', line 32

def formatted_state
  formatted_segments.join(" ")
end

#formatted_tilt_segmentObject



57
58
59
# File 'lib/esphome/entities/cover.rb', line 57

def formatted_tilt_segment
  formatted_percentage_segment(tilt)
end

#openObject



89
90
91
92
93
94
95
# File 'lib/esphome/entities/cover.rb', line 89

def open
  device.send(Api::CoverCommandRequest.new(key:,
                                           has_legacy_command: true,
                                           legacy_command: :LEGACY_COVER_COMMAND_OPEN,
                                           has_position: true,
                                           position: 1.0))
end

#position?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/esphome/entities/cover.rb', line 20

def position?
  @supports_position
end

#stopObject



105
106
107
108
109
110
# File 'lib/esphome/entities/cover.rb', line 105

def stop
  device.send(Api::CoverCommandRequest.new(key:,
                                           has_legacy_command: true,
                                           legacy_command: :LEGACY_COVER_COMMAND_STOP,
                                           stop: true))
end

#stop?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/esphome/entities/cover.rb', line 28

def stop?
  @supports_stop
end

#tilt?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/esphome/entities/cover.rb', line 24

def tilt?
  @supports_tilt
end

#update(state_response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/esphome/entities/cover.rb', line 65

def update(state_response)
  @position = state_response.position if state_response.position
  @tilt = state_response.tilt if tilt?

  @current_operation = if state_response.current_operation == :COVER_OPERATION_IDLE
                         :idle
                       else
                         state_response.current_operation[19..].downcase.to_sym
                       end
end