Fix more unchecked optional access errors (batch 2)

Fix clang-tidy bugprone-unchecked-optional-access in speed fan,
speaker media player, and sprinkler components.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
J. Nick Koston
2026-02-27 17:59:09 -10:00
co-authored by Claude Opus 4.6
parent e535c51847
commit 554c395efa
3 changed files with 32 additions and 28 deletions
@@ -134,7 +134,7 @@ void SpeakerMediaPlayer::watch_media_commands_() {
delete media_command.url.value();
}
if (media_command.file.has_value()) {
playlist_item.file = media_command.file.value();
playlist_item.file = *media_command.file;
}
if (this->single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
@@ -437,18 +437,18 @@ void SpeakerMediaPlayer::control(const media_player::MediaPlayerCall &call) {
MediaCallCommand media_command;
if (this->single_pipeline_() || (call.get_announcement().has_value() && call.get_announcement().value())) {
if (auto ann = call.get_announcement(); this->single_pipeline_() || (ann.has_value() && *ann)) {
media_command.announce = true;
} else {
media_command.announce = false;
}
if (call.get_media_url().has_value()) {
media_command.url = new std::string(
call.get_media_url().value()); // Must be manually deleted after receiving media_command from a queue
if (auto media_url = call.get_media_url(); media_url.has_value()) {
media_command.url =
new std::string(*media_url); // Must be manually deleted after receiving media_command from a queue
if (call.get_command().has_value()) {
if (call.get_command().value() == media_player::MEDIA_PLAYER_COMMAND_ENQUEUE) {
if (auto cmd = call.get_command(); cmd.has_value()) {
if (*cmd == media_player::MEDIA_PLAYER_COMMAND_ENQUEUE) {
media_command.enqueue = true;
}
}
@@ -457,18 +457,18 @@ void SpeakerMediaPlayer::control(const media_player::MediaPlayerCall &call) {
return;
}
if (call.get_volume().has_value()) {
media_command.volume = call.get_volume().value();
if (auto vol = call.get_volume(); vol.has_value()) {
media_command.volume = vol;
// Wait 0 ticks for queue to be free, volume sets aren't that important!
xQueueSend(this->media_control_command_queue_, &media_command, 0);
return;
}
if (call.get_command().has_value()) {
media_command.command = call.get_command().value();
if (auto cmd = call.get_command(); cmd.has_value()) {
media_command.command = cmd;
TickType_t ticks_to_wait = portMAX_DELAY;
if ((call.get_command().value() == media_player::MEDIA_PLAYER_COMMAND_VOLUME_UP) ||
(call.get_command().value() == media_player::MEDIA_PLAYER_COMMAND_VOLUME_DOWN)) {
if ((*cmd == media_player::MEDIA_PLAYER_COMMAND_VOLUME_UP) ||
(*cmd == media_player::MEDIA_PLAYER_COMMAND_VOLUME_DOWN)) {
ticks_to_wait = 0; // Wait 0 ticks for queue to be free, volume sets aren't that important!
}
xQueueSend(this->media_control_command_queue_, &media_command, ticks_to_wait);
+8 -8
View File
@@ -21,14 +21,14 @@ void SpeedFan::setup() {
void SpeedFan::dump_config() { LOG_FAN("", "Speed Fan", this); }
void SpeedFan::control(const fan::FanCall &call) {
if (call.get_state().has_value())
this->state = *call.get_state();
if (call.get_speed().has_value())
this->speed = *call.get_speed();
if (call.get_oscillating().has_value())
this->oscillating = *call.get_oscillating();
if (call.get_direction().has_value())
this->direction = *call.get_direction();
if (auto val = call.get_state(); val.has_value())
this->state = *val;
if (auto val = call.get_speed(); val.has_value())
this->speed = *val;
if (auto val = call.get_oscillating(); val.has_value())
this->oscillating = *val;
if (auto val = call.get_direction(); val.has_value())
this->direction = *val;
this->apply_preset_mode_(call);
this->write_state_();
+11 -7
View File
@@ -44,7 +44,7 @@ SprinklerControllerSwitch::SprinklerControllerSwitch() = default;
void SprinklerControllerSwitch::loop() {
// Loop is only enabled when f_ has a value (see setup())
auto s = (*this->f_)();
auto s = (*this->f_)(); // NOLINT(bugprone-unchecked-optional-access)
if (s.has_value()) {
this->publish_state(*s);
}
@@ -89,19 +89,20 @@ void SprinklerValveOperator::loop() {
uint32_t now = App.get_loop_component_start_time();
switch (this->state_) {
case STARTING:
if ((now - *this->start_millis_) > this->start_delay_) {
if ((now - this->start_millis_.value()) > this->start_delay_) { // NOLINT(bugprone-unchecked-optional-access)
this->run_(); // start_delay_ has been exceeded, so ensure both valves are on and update the state
}
break;
case ACTIVE:
if ((now - *this->start_millis_) > (this->start_delay_ + this->run_duration_)) {
if ((now - this->start_millis_.value()) > // NOLINT(bugprone-unchecked-optional-access)
(this->start_delay_ + this->run_duration_)) {
this->stop(); // start_delay_ + run_duration_ has been exceeded, start shutting down
}
break;
case STOPPING:
if ((now - *this->stop_millis_) > this->stop_delay_) {
if ((now - this->stop_millis_.value()) > this->stop_delay_) { // NOLINT(bugprone-unchecked-optional-access)
this->kill_(); // stop_delay_has been exceeded, ensure all valves are off
}
break;
@@ -1067,7 +1068,8 @@ uint32_t Sprinkler::total_cycle_time_enabled_incomplete_valves() {
if (this->valve_is_enabled_(valve)) {
enabled_valve_count++;
if (!this->valve_cycle_complete_(valve)) {
if (!this->active_valve().has_value() || (valve != this->active_valve().value())) {
auto active = this->active_valve();
if (!active.has_value() || (valve != *active)) {
total_time_remaining += this->valve_run_duration_adjusted(valve);
incomplete_valve_count++;
} else {
@@ -1190,8 +1192,10 @@ switch_::Switch *Sprinkler::valve_switch(const size_t valve_number) {
}
switch_::Switch *Sprinkler::valve_pump_switch(const size_t valve_number) {
if (this->is_a_valid_valve(valve_number) && this->valve_[valve_number].pump_switch_index.has_value()) {
return this->pump_[this->valve_[valve_number].pump_switch_index.value()];
if (this->is_a_valid_valve(valve_number)) {
if (auto idx = this->valve_[valve_number].pump_switch_index; idx.has_value()) {
return this->pump_[*idx];
}
}
return nullptr;
}