mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 17:27:45 +01:00
fix template binary_sensor publish_initial_state option (#5033)
This commit is contained in:
parent
c3ef12d580
commit
e823067a6b
2 changed files with 15 additions and 4 deletions
|
@ -6,11 +6,21 @@ namespace template_ {
|
||||||
|
|
||||||
static const char *const TAG = "template.binary_sensor";
|
static const char *const TAG = "template.binary_sensor";
|
||||||
|
|
||||||
void TemplateBinarySensor::loop() {
|
void TemplateBinarySensor::setup() {
|
||||||
if (!this->f_.has_value())
|
if (!this->publish_initial_state_)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto s = (*this->f_)();
|
if (this->f_ != nullptr) {
|
||||||
|
this->publish_initial_state(*this->f_());
|
||||||
|
} else {
|
||||||
|
this->publish_initial_state(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void TemplateBinarySensor::loop() {
|
||||||
|
if (this->f_ == nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto s = this->f_();
|
||||||
if (s.has_value()) {
|
if (s.has_value()) {
|
||||||
this->publish_state(*s);
|
this->publish_state(*s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,13 +10,14 @@ class TemplateBinarySensor : public Component, public binary_sensor::BinarySenso
|
||||||
public:
|
public:
|
||||||
void set_template(std::function<optional<bool>()> &&f) { this->f_ = f; }
|
void set_template(std::function<optional<bool>()> &&f) { this->f_ = f; }
|
||||||
|
|
||||||
|
void setup() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
optional<std::function<optional<bool>()>> f_{};
|
std::function<optional<bool>()> f_{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace template_
|
} // namespace template_
|
||||||
|
|
Loading…
Reference in a new issue