Class: ESPHome::Entities::Light

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

Returns a new instance of Light.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/esphome/entities/light.rb', line 25

def initialize(_device, list_entities_response)
  super

  @supported_color_modes = list_entities_response.supported_color_modes.map { |mode| mode[11..].downcase.to_sym }
  if @supported_color_modes.empty?
    @supported_color_modes << :brightness if list_entities_response.legacy_supports_brightness
    @supported_color_modes << :rgb if list_entities_response.legacy_supports_rgb
    @supported_color_modes << :white if list_entities_response.legacy_supports_white
    @supported_color_modes << :color_temperature if list_entities_response.legacy_supports_color_temperature
    @supported_color_modes = [:on_off] if @supported_color_modes.empty?
  end
  @supported_color_modes = @supported_color_modes.to_set { |mode| transform_color_mode(mode) }.freeze

  @mireds_range = Range.new(list_entities_response.min_mireds, list_entities_response.max_mireds)

  @effects = list_entities_response.effects.map(&:freeze).freeze

  @brightness = @color_temperature = @red = @green = @blue = @white = @cold_white = @warm_white = nil
end

Instance Attribute Details

#blueObject (readonly)

Returns the value of attribute blue.



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

def blue
  @blue
end

#brightnessObject (readonly)

Returns the value of attribute brightness.



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

def brightness
  @brightness
end

#cold_whiteObject (readonly)

Returns the value of attribute cold_white.



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

def cold_white
  @cold_white
end

#color_brightnessObject (readonly)

Returns the value of attribute color_brightness.



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

def color_brightness
  @color_brightness
end

#color_modeObject (readonly)

Returns the value of attribute color_mode.



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

def color_mode
  @color_mode
end

#color_temperatureObject (readonly)

Returns the value of attribute color_temperature.



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

def color_temperature
  @color_temperature
end

#effectObject (readonly)

Returns the value of attribute effect.



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

def effect
  @effect
end

#effectsObject (readonly)

Returns the value of attribute effects.



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

def effects
  @effects
end

#flash_effectObject (readonly)

Returns the value of attribute flash_effect.



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

def flash_effect
  @flash_effect
end

#greenObject (readonly)

Returns the value of attribute green.



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

def green
  @green
end

#mireds_rangeObject (readonly)

Returns the value of attribute mireds_range.



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

def mireds_range
  @mireds_range
end

#redObject (readonly)

Returns the value of attribute red.



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

def red
  @red
end

#supported_color_modesObject (readonly)

Returns the value of attribute supported_color_modes.



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

def supported_color_modes
  @supported_color_modes
end

#transition_lengthObject (readonly)

Returns the value of attribute transition_length.



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

def transition_length
  @transition_length
end

#warm_whiteObject (readonly)

Returns the value of attribute warm_white.



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

def warm_white
  @warm_white
end

#whiteObject (readonly)

Returns the value of attribute white.



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

def white
  @white
end

Instance Method Details

#brightness?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/esphome/entities/light.rb', line 45

def brightness?
  @supported_color_modes.include?(:brightness)
end

#color_temperature?Boolean

Returns:

  • (Boolean)


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

def color_temperature?
  @supported_color_modes.intersect?(%i[color_temperature rgb_color_temperature])
end

#formatted_stateObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/esphome/entities/light.rb', line 84

def formatted_state
  result = case state
           when nil then "-"
           when true then "on"
           else "off"
           end
  result += " #{brightness || color_brightness || "-"}%" if brightness?

  if effect
    result += effect
  else
    result += " #{color_temperature || "-"} mired" if color_temperature?

    color = []
    color.push(red, green, blue) if rgb?
    color.push(white) if white?
    color.push(cold_white, warm_white) if ww?
    result += " (#{color.map { |c| c || "-" }.join(",")})" unless color.empty?
  end

  result += " @ #{transition_length} ms" if transition_length
  result += " (flash)" if flash_effect

  result
end

#rgb?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/esphome/entities/light.rb', line 53

def rgb?
  @supported_color_modes.intersect?(%i[rgb rgbw rgb_color_temperature rgbww])
end

#update(state_response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/esphome/entities/light.rb', line 65

def update(state_response)
  @state = state_response.state
  @brightness = state_response.brightness if brightness?
  @color_mode = state_response.color_mode
  if rgb?
    @color_brightness = state_response.color_brightness
    @red = state_response.red
    @green = state_response.green
    @blue = state_response.blue
  end
  @white = state_response.white if white?
  @color_temperature = state_response.color_temperature if color_temperature?
  if ww?
    @cold_white = state_response.cold_white
    @warm_white = state_response.warm_white
  end
  @effect = state_response.effect.empty? ? nil : state_response.effect
end

#white?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/esphome/entities/light.rb', line 49

def white?
  @supported_color_modes.intersect?(%i[white ww rgbw rgbww])
end

#ww?Boolean

Returns:

  • (Boolean)


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

def ww?
  @supported_color_modes.intersect?(%i[ww rgbww])
end