Class: ESPHome::Entities::Time

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

Instance Attribute Summary

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

Instance Method Details

#command(state) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/esphome/entities/time.rb', line 26

def command(state)
  if defined?(ActiveSupport::Duration)
    parts = state.parts
    device.send(Api::TimeCommandRequest.new(key:,
                                            hour: parts[:hours] || 0,
                                            minute: parts[:minutes] || 0,
                                            second: parts[:seconds] || 0))
  else
    device.send(Api::TimeCommandRequest.new(key:, hour: state[0], minute: state[1], second: state[2]))
  end
end

#formatted_stateObject



20
21
22
23
24
# File 'lib/esphome/entities/time.rb', line 20

def formatted_state
  return format("%02d:%02d:%02d", *state) if state.is_a?(Array)

  super
end

#update(state_response) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/esphome/entities/time.rb', line 8

def update(state_response)
  @state = if state_response.missing_state
             nil
           elsif defined?(ActiveSupport::Duration)
             state_response.hour.hours +
               state_response.minute.minutes +
               state_response.second.seconds
           else
             [state_response.hour, state_response.minute, state_response.second]
           end
end