ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
6namespace esphome {
7namespace status_led {
8
9static 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
uint32_t get_app_state() const
virtual void setup()=0
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
void current_values_as_binary(bool *binary)
The result of all the current_values_as_* methods have gamma correction applied.
virtual void set_state(bool state)
Enable or disable this binary output.
void write_state(light::LightState *state) override
void setup_state(light::LightState *state) override
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const uint32_t STATUS_LED_ERROR
Definition component.cpp:40
const uint32_t STATUS_LED_MASK
Definition component.cpp:37
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27
Application App
Global storage of Application pointer - only one Application can exist.
const uint32_t STATUS_LED_WARNING
Definition component.cpp:39