mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 08:28:12 +01:00
allow using a binary output for the status led (#4532)
* allow using a binary output for the status led * lint * output status as well * simplify --------- Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
a8bb2a42a1
commit
dfc7cd7f5d
3 changed files with 42 additions and 21 deletions
|
@ -1,26 +1,35 @@
|
||||||
from esphome import pins
|
from esphome import pins
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.components import light
|
from esphome.components import light, output
|
||||||
from esphome.const import CONF_OUTPUT_ID, CONF_PIN
|
from esphome.const import CONF_OUTPUT, CONF_OUTPUT_ID, CONF_PIN
|
||||||
from .. import status_led_ns
|
from .. import status_led_ns
|
||||||
|
|
||||||
|
AUTO_LOAD = ["output"]
|
||||||
|
|
||||||
StatusLEDLightOutput = status_led_ns.class_(
|
StatusLEDLightOutput = status_led_ns.class_(
|
||||||
"StatusLEDLightOutput", light.LightOutput, cg.Component
|
"StatusLEDLightOutput", light.LightOutput, cg.Component
|
||||||
)
|
)
|
||||||
|
|
||||||
CONFIG_SCHEMA = light.BINARY_LIGHT_SCHEMA.extend(
|
CONFIG_SCHEMA = cv.All(
|
||||||
{
|
light.BINARY_LIGHT_SCHEMA.extend(
|
||||||
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(StatusLEDLightOutput),
|
{
|
||||||
cv.Required(CONF_PIN): pins.gpio_output_pin_schema,
|
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(StatusLEDLightOutput),
|
||||||
}
|
cv.Optional(CONF_PIN): pins.gpio_output_pin_schema,
|
||||||
|
cv.Optional(CONF_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
cv.has_at_least_one_key(CONF_PIN, CONF_OUTPUT),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
|
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
|
||||||
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
if CONF_PIN in config:
|
||||||
cg.add(var.set_pin(pin))
|
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
||||||
|
cg.add(var.set_pin(pin))
|
||||||
|
if CONF_OUTPUT in config:
|
||||||
|
out = await cg.get_variable(config[CONF_OUTPUT])
|
||||||
|
cg.add(var.set_output(out))
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
# cg.add(cg.App.register_component(var))
|
|
||||||
await light.register_light(var, config)
|
await light.register_light(var, config)
|
||||||
|
|
|
@ -15,10 +15,10 @@ void StatusLEDLightOutput::loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((new_state & STATUS_LED_ERROR) != 0u) {
|
if ((new_state & STATUS_LED_ERROR) != 0u) {
|
||||||
this->pin_->digital_write(millis() % 250u < 150u);
|
this->output_state_(millis() % 250u < 150u);
|
||||||
this->last_app_state_ = new_state;
|
this->last_app_state_ = new_state;
|
||||||
} else if ((new_state & STATUS_LED_WARNING) != 0u) {
|
} else if ((new_state & STATUS_LED_WARNING) != 0u) {
|
||||||
this->pin_->digital_write(millis() % 1500u < 250u);
|
this->output_state_(millis() % 1500u < 250u);
|
||||||
this->last_app_state_ = new_state;
|
this->last_app_state_ = new_state;
|
||||||
} else if (new_state != this->last_app_state_) {
|
} else if (new_state != this->last_app_state_) {
|
||||||
// if no error/warning -> restore light state or turn off
|
// if no error/warning -> restore light state or turn off
|
||||||
|
@ -26,17 +26,16 @@ void StatusLEDLightOutput::loop() {
|
||||||
|
|
||||||
if (lightstate_)
|
if (lightstate_)
|
||||||
lightstate_->current_values_as_binary(&state);
|
lightstate_->current_values_as_binary(&state);
|
||||||
|
|
||||||
this->pin_->digital_write(state);
|
|
||||||
this->last_app_state_ = new_state;
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Restoring light state %s", ONOFF(state));
|
ESP_LOGD(TAG, "Restoring light state %s", ONOFF(state));
|
||||||
|
|
||||||
|
this->output_state_(state);
|
||||||
|
this->last_app_state_ = new_state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusLEDLightOutput::setup_state(light::LightState *state) {
|
void StatusLEDLightOutput::setup_state(light::LightState *state) {
|
||||||
lightstate_ = state;
|
lightstate_ = state;
|
||||||
ESP_LOGD(TAG, "'%s': Setting initital state", state->get_name().c_str());
|
ESP_LOGD(TAG, "'%s': Setting initial state", state->get_name().c_str());
|
||||||
this->write_state(state);
|
this->write_state(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,16 +46,18 @@ void StatusLEDLightOutput::write_state(light::LightState *state) {
|
||||||
// if in warning/error, don't overwrite the status_led
|
// if in warning/error, don't overwrite the status_led
|
||||||
// once it is back to OK, the loop will restore the state
|
// once it is back to OK, the loop will restore the state
|
||||||
if ((App.get_app_state() & (STATUS_LED_ERROR | STATUS_LED_WARNING)) == 0u) {
|
if ((App.get_app_state() & (STATUS_LED_ERROR | STATUS_LED_WARNING)) == 0u) {
|
||||||
this->pin_->digital_write(binary);
|
|
||||||
ESP_LOGD(TAG, "'%s': Setting state %s", state->get_name().c_str(), ONOFF(binary));
|
ESP_LOGD(TAG, "'%s': Setting state %s", state->get_name().c_str(), ONOFF(binary));
|
||||||
|
this->output_state_(binary);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusLEDLightOutput::setup() {
|
void StatusLEDLightOutput::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Setting up Status LED...");
|
ESP_LOGCONFIG(TAG, "Setting up Status LED...");
|
||||||
|
|
||||||
this->pin_->setup();
|
if (this->pin_ != nullptr) {
|
||||||
this->pin_->digital_write(false);
|
this->pin_->setup();
|
||||||
|
this->pin_->digital_write(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusLEDLightOutput::dump_config() {
|
void StatusLEDLightOutput::dump_config() {
|
||||||
|
@ -64,5 +65,12 @@ void StatusLEDLightOutput::dump_config() {
|
||||||
LOG_PIN(" Pin: ", this->pin_);
|
LOG_PIN(" Pin: ", this->pin_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StatusLEDLightOutput::output_state_(bool state) {
|
||||||
|
if (this->pin_ != nullptr)
|
||||||
|
this->pin_->digital_write(state);
|
||||||
|
if (this->output_ != nullptr)
|
||||||
|
this->output_->set_state(state);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace status_led
|
} // namespace status_led
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
#include "esphome/components/light/light_output.h"
|
#include "esphome/components/light/light_output.h"
|
||||||
|
#include "esphome/components/output/binary_output.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace status_led {
|
namespace status_led {
|
||||||
|
@ -10,6 +11,7 @@ namespace status_led {
|
||||||
class StatusLEDLightOutput : public light::LightOutput, public Component {
|
class StatusLEDLightOutput : public light::LightOutput, public Component {
|
||||||
public:
|
public:
|
||||||
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
||||||
|
void set_output(output::BinaryOutput *output) { output_ = output; }
|
||||||
|
|
||||||
light::LightTraits get_traits() override {
|
light::LightTraits get_traits() override {
|
||||||
auto traits = light::LightTraits();
|
auto traits = light::LightTraits();
|
||||||
|
@ -31,9 +33,11 @@ class StatusLEDLightOutput : public light::LightOutput, public Component {
|
||||||
float get_loop_priority() const override { return 50.0f; }
|
float get_loop_priority() const override { return 50.0f; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
GPIOPin *pin_;
|
GPIOPin *pin_{nullptr};
|
||||||
|
output::BinaryOutput *output_{nullptr};
|
||||||
light::LightState *lightstate_{};
|
light::LightState *lightstate_{};
|
||||||
uint32_t last_app_state_{0xFFFF};
|
uint32_t last_app_state_{0xFFFF};
|
||||||
|
void output_state_(bool state);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace status_led
|
} // namespace status_led
|
||||||
|
|
Loading…
Reference in a new issue