diff --git a/esphome/components/mcp4461/output/mcp4461_output.cpp b/esphome/components/mcp4461/output/mcp4461_output.cpp index fe56f728aa..9cfeae86fa 100644 --- a/esphome/components/mcp4461/output/mcp4461_output.cpp +++ b/esphome/components/mcp4461/output/mcp4461_output.cpp @@ -10,6 +10,10 @@ namespace mcp4461 { static const char *const TAG = "mcp4461.output"; void Mcp4461Wiper::write_state(float state) { + if (this->parent_->is_failed()) { + ESP_LOGW(TAG, "Parent MCP4461 component has failed! Aborting"); + return; + } uint8_t wiper_idx = static_cast(this->wiper_); ESP_LOGV(TAG, "Got value %02f from frontend", state); const float max_taps = 256.0; @@ -26,6 +30,10 @@ void Mcp4461Wiper::write_state(float state) { } void Mcp4461Wiper::set_initial_value(float initial_value) { + if (this->parent_->is_failed()) { + ESP_LOGW(TAG, "Parent MCP4461 component has failed! Aborting"); + return; + } if (initial_value >= 0.000 && initial_value <= 0.256) { this->initial_value_ = static_cast(initial_value * 1000); // Use the value @@ -40,17 +48,26 @@ void Mcp4461Wiper::set_initial_value(float initial_value) { uint16_t Mcp4461Wiper::get_wiper_level() { return this->parent_->get_wiper_level(this->wiper_); } void Mcp4461Wiper::save_level() { + if (this->parent_->is_failed()) { + ESP_LOGW(TAG, "Parent MCP4461 component has failed! Aborting"); + return; + } uint8_t wiper_idx = static_cast(this->wiper_); if (wiper_idx > 3) { ESP_LOGW(TAG, "Cannot save level for nonvolatile wiper %" PRIu8 " !", wiper_idx); return; } uint8_t nonvolatile_wiper_idx = wiper_idx + 4; + this->parent_->reg[nonvolatile_wiper_idx].state = this->parent_->reg_[wiper_idx].state; Mcp4461WiperIdx nonvolatile_wiper = static_cast(nonvolatile_wiper_idx); this->parent_->set_wiper_level(nonvolatile_wiper, this->state_); } void Mcp4461Wiper::enable_wiper() { + if (this->parent_->is_failed()) { + ESP_LOGW(TAG, "Parent MCP4461 component has failed! Aborting"); + return; + } uint8_t wiper_idx = static_cast(this->wiper_); if (wiper_idx > 3) { ESP_LOGW(TAG, "Cannot enable nonvolatile wiper %" PRIu8 " !", wiper_idx); @@ -69,20 +86,30 @@ void Mcp4461Wiper::disable_wiper() { } void Mcp4461Wiper::increase_wiper() { + if (this->parent_->is_failed()) { + ESP_LOGW(TAG, "Parent MCP4461 component has failed! Aborting"); + return; + } uint8_t wiper_idx = static_cast(this->wiper_); if (wiper_idx > 3) { ESP_LOGW(TAG, "Cannot increase nonvolatile wiper %" PRIu8 " !", wiper_idx); return; } + this->state_ = this->state_ + 1.0; this->parent_->increase_wiper(this->wiper_); } void Mcp4461Wiper::decrease_wiper() { + if (this->parent_->is_failed()) { + ESP_LOGW(TAG, "Parent MCP4461 component has failed! Aborting"); + return; + } uint8_t wiper_idx = static_cast(this->wiper_); if (wiper_idx > 3) { ESP_LOGW(TAG, "Cannot decrease nonvolatile wiper %" PRIu8 " !", wiper_idx); return; } + this->state_ = this->state_ - 1.0; this->parent_->decrease_wiper(this->wiper_); }