Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Fix components sending invalid state on startup if integration not ready yet #195

Merged
merged 2 commits into from Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/esphomelib/binary_sensor/binary_sensor.cpp
Expand Up @@ -26,6 +26,7 @@ void BinarySensor::publish_state(bool state) {

}
void BinarySensor::send_value_(bool state) {
this->has_value_ = true;
this->value = state;
this->state_callback_.call(state);
}
Expand Down Expand Up @@ -75,6 +76,9 @@ void BinarySensor::add_filters(std::vector<Filter *> filters) {
this->add_filter(filter);
}
}
bool BinarySensor::has_value() const {
return this->has_value_;
}

PressTrigger::PressTrigger(BinarySensor *parent) {
parent->add_on_state_callback([this](bool state) {
Expand Down
3 changes: 3 additions & 0 deletions src/esphomelib/binary_sensor/binary_sensor.h
Expand Up @@ -82,6 +82,8 @@ class BinarySensor : public Nameable {

bool value{false};

bool has_value() const;

protected:
// ========== OVERRIDE METHODS ==========
// (You'll only need this when creating your own custom binary sensor)
Expand All @@ -91,6 +93,7 @@ class BinarySensor : public Nameable {
CallbackManager<void(bool)> state_callback_{};
optional<std::string> device_class_{}; ///< Stores the override of the device class
Filter *filter_list_{nullptr};
bool has_value_{false};
};

class PressTrigger : public Trigger<NoArg> {
Expand Down
Expand Up @@ -60,7 +60,8 @@ void MQTTBinarySensorComponent::send_discovery(JsonBuffer &buffer, JsonObject &r
}

void MQTTBinarySensorComponent::send_initial_state() {
this->publish_state(this->binary_sensor_->value);
if (this->binary_sensor_->has_value())
this->publish_state(this->binary_sensor_->value);
}
bool MQTTBinarySensorComponent::is_internal() {
return this->binary_sensor_->is_internal();
Expand Down
4 changes: 4 additions & 0 deletions src/esphomelib/cover/cover.cpp
Expand Up @@ -23,12 +23,16 @@ void Cover::add_on_publish_state_callback(std::function<void(CoverState)> &&f) {
void Cover::publish_state(CoverState state) {
if (this->state == state)
return;
this->has_value_ = true;
this->state = state;
this->state_callback_.call(state);
}
bool Cover::optimistic() {
return false;
}
bool Cover::has_value() const {
return this->has_value_;
}

} // namespace cover

Expand Down
4 changes: 3 additions & 1 deletion src/esphomelib/cover/cover.h
Expand Up @@ -64,7 +64,10 @@ class Cover : public Nameable {

CoverState state{COVER_MAX};

bool has_value() const;

protected:
bool has_value_{false};
CallbackManager<void(CoverState)> state_callback_{};
};

Expand Down Expand Up @@ -151,7 +154,6 @@ StopAction<T> *Cover::make_stop_action() {
return new StopAction<T>(this);
}


} // namespace cover

ESPHOMELIB_NAMESPACE_END
Expand Down
3 changes: 2 additions & 1 deletion src/esphomelib/cover/mqtt_cover_component.cpp
Expand Up @@ -57,7 +57,8 @@ std::string MQTTCoverComponent::friendly_name() const {
return this->cover_->get_name();
}
void MQTTCoverComponent::send_initial_state() {
this->publish_state(this->cover_->state);
if (this->cover_->has_value())
this->publish_state(this->cover_->state);
}
bool MQTTCoverComponent::is_internal() {
return this->cover_->is_internal();
Expand Down
3 changes: 2 additions & 1 deletion src/esphomelib/sensor/mqtt_sensor_component.cpp
Expand Up @@ -82,7 +82,8 @@ void MQTTSensorComponent::send_discovery(JsonBuffer &buffer, JsonObject &root, m
config.command_topic = false;
}
void MQTTSensorComponent::send_initial_state() {
this->publish_state(this->sensor_->value);
if (this->sensor_->has_value())
this->publish_state(this->sensor_->value);
}
bool MQTTSensorComponent::is_internal() {
return this->sensor_->is_internal();
Expand Down
4 changes: 4 additions & 0 deletions src/esphomelib/sensor/sensor.cpp
Expand Up @@ -143,6 +143,7 @@ float Sensor::get_raw_value() const {
std::string Sensor::unique_id() { return ""; }

void Sensor::send_value_to_frontend(float value) {
this->has_value_ = true;
this->value = value;
this->callback_.call(value);
}
Expand All @@ -155,6 +156,9 @@ RawSensorValueTrigger *Sensor::make_raw_value_trigger() {
ValueRangeTrigger *Sensor::make_value_range_trigger() {
return new ValueRangeTrigger(this);
}
bool Sensor::has_value() const {
return this->has_value_;
}

PollingSensorComponent::PollingSensorComponent(const std::string &name, uint32_t update_interval)
: PollingComponent(update_interval), Sensor(name) {}
Expand Down
4 changes: 4 additions & 0 deletions src/esphomelib/sensor/sensor.h
Expand Up @@ -186,6 +186,9 @@ class Sensor : public Nameable {
float value{NAN}; ///< Stores the last filtered value. Public because of lambdas.
float raw_value{NAN}; ///< Stores the last raw value. Public because of lambdas.

/// Return whether this sensor has gotten a full value (that passed through all filters) yet.
bool has_value() const;

protected:
friend Filter;
friend MQTTSensorComponent;
Expand All @@ -198,6 +201,7 @@ class Sensor : public Nameable {
optional<std::string> icon_; /// Override the icon advertised to Home Assistant, otherwise sensor's icon will be used.
optional<int8_t> accuracy_decimals_; ///< Override the accuracy in decimals, otherwise the sensor's values will be used.
Filter *filter_list_{nullptr}; ///< Store all active filters.
bool has_value_{false};
};

class PollingSensorComponent : public PollingComponent, public Sensor {
Expand Down