analog_threshold: Publish initial state only when available

This avoids spurious triggers since most sensors won't have state during setup().
This commit is contained in:
Adam Goode 2024-01-01 00:20:33 -05:00
parent fb9844463b
commit aa7054af33

View File

@ -7,28 +7,22 @@ namespace analog_threshold {
static const char *const TAG = "analog_threshold.binary_sensor"; static const char *const TAG = "analog_threshold.binary_sensor";
void AnalogThresholdBinarySensor::setup() { void AnalogThresholdBinarySensor::setup() {
float sensor_value = this->sensor_->get_state();
// TRUE state is defined to be when sensor is >= threshold
// so when undefined sensor value initialize to FALSE
if (std::isnan(sensor_value)) {
this->publish_initial_state(false);
} else {
this->publish_initial_state(sensor_value >= (this->lower_threshold_ + this->upper_threshold_) / 2.0f);
}
}
void AnalogThresholdBinarySensor::set_sensor(sensor::Sensor *analog_sensor) {
this->sensor_ = analog_sensor;
this->sensor_->add_on_state_callback([this](float sensor_value) { this->sensor_->add_on_state_callback([this](float sensor_value) {
// if there is an invalid sensor reading, ignore the change and keep the current state // if there is an invalid sensor reading, ignore the change and keep the current state
if (!std::isnan(sensor_value)) { if (!std::isnan(sensor_value)) {
this->publish_state(sensor_value >= (this->state ? this->lower_threshold_ : this->upper_threshold_)); bool threshold_value = sensor_value >= (this->state ? this->lower_threshold_ : this->upper_threshold_);
if (this->has_state()) {
// This is not the initial state.
this->publish_state(threshold_value);
} else {
this->publish_initial_state(threshold_value);
}
} }
}); });
} }
void AnalogThresholdBinarySensor::set_sensor(sensor::Sensor *analog_sensor) { this->sensor_ = analog_sensor; }
void AnalogThresholdBinarySensor::dump_config() { void AnalogThresholdBinarySensor::dump_config() {
LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this); LOG_BINARY_SENSOR("", "Analog Threshold Binary Sensor", this);
LOG_SENSOR(" ", "Sensor", this->sensor_); LOG_SENSOR(" ", "Sensor", this->sensor_);