ESPHome  2024.11.0
status_led_light.cpp
Go to the documentation of this file.
1 #include "status_led_light.h"
2 #include "esphome/core/log.h"
4 #include <cinttypes>
5 
6 namespace esphome {
7 namespace status_led {
8 
9 static const char *const TAG = "status_led";
10 
12  uint32_t new_state = App.get_app_state() & STATUS_LED_MASK;
13 
14  if (new_state != this->last_app_state_) {
15  ESP_LOGV(TAG, "New app state 0x%08" PRIX32, new_state);
16  }
17 
18  if ((new_state & STATUS_LED_ERROR) != 0u) {
19  this->output_state_(millis() % 250u < 150u);
20  this->last_app_state_ = new_state;
21  } else if ((new_state & STATUS_LED_WARNING) != 0u) {
22  this->output_state_(millis() % 1500u < 250u);
23  this->last_app_state_ = new_state;
24  } else if (new_state != this->last_app_state_) {
25  // if no error/warning -> restore light state or turn off
26  bool state = false;
27 
28  if (lightstate_)
30  ESP_LOGD(TAG, "Restoring light state %s", ONOFF(state));
31 
32  this->output_state_(state);
33  this->last_app_state_ = new_state;
34  }
35 }
36 
39  ESP_LOGD(TAG, "'%s': Setting initial state", state->get_name().c_str());
40  this->write_state(state);
41 }
42 
44  bool binary;
45  state->current_values_as_binary(&binary);
46 
47  // if in warning/error, don't overwrite the status_led
48  // once it is back to OK, the loop will restore the state
50  ESP_LOGD(TAG, "'%s': Setting state %s", state->get_name().c_str(), ONOFF(binary));
51  this->output_state_(binary);
52  }
53 }
54 
56  ESP_LOGCONFIG(TAG, "Setting up Status LED...");
57 
58  if (this->pin_ != nullptr) {
59  this->pin_->setup();
60  this->pin_->digital_write(false);
61  }
62 }
63 
65  ESP_LOGCONFIG(TAG, "Status Led Light:");
66  LOG_PIN(" Pin: ", this->pin_);
67 }
68 
70  if (this->pin_ != nullptr)
71  this->pin_->digital_write(state);
72  if (this->output_ != nullptr)
73  this->output_->set_state(state);
74 }
75 
76 } // namespace status_led
77 } // namespace esphome
virtual void digital_write(bool value)=0
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
virtual void set_state(bool state)
Enable or disable this binary output.
Definition: binary_output.h:34
virtual void setup()=0
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void current_values_as_binary(bool *binary)
The result of all the current_values_as_* methods have gamma correction applied.
void setup_state(light::LightState *state) override
void write_state(light::LightState *state) override
Application App
Global storage of Application pointer - only one Application can exist.
const uint32_t STATUS_LED_WARNING
Definition: component.cpp:39
constexpr const char * c_str() const
Definition: string_ref.h:68
const uint32_t STATUS_LED_ERROR
Definition: component.cpp:40
uint32_t get_app_state() const
Definition: application.h:244
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint32_t STATUS_LED_MASK
Definition: component.cpp:37
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34