Skip to content

Commit 96d86a4

Browse files
committed
Aligned code with esphome merge request
1 parent f3e488d commit 96d86a4

3 files changed

Lines changed: 54 additions & 65 deletions

File tree

components/haier/hon_climate.cpp

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,29 @@ using namespace esphome::uart;
1414

1515
namespace esphome::haier {
1616

17-
static bool vertical_axis_is_auto_(climate::ClimateSwingMode swing_mode) {
17+
static bool vertical_axis_is_auto(climate::ClimateSwingMode swing_mode) {
1818
return (swing_mode == climate::CLIMATE_SWING_VERTICAL) || (swing_mode == climate::CLIMATE_SWING_BOTH);
1919
}
2020

21-
static bool horizontal_axis_is_auto_(climate::ClimateSwingMode swing_mode) {
21+
static bool horizontal_axis_is_auto(climate::ClimateSwingMode swing_mode) {
2222
return (swing_mode == climate::CLIMATE_SWING_HORIZONTAL) || (swing_mode == climate::CLIMATE_SWING_BOTH);
2323
}
2424

2525
#ifdef USE_SELECT
2626
const std::vector<hon_protocol::VerticalSwingMode> VERTICAL_SWING_MODES_ORDER = {
27-
hon_protocol::VerticalSwingMode::HEALTH_UP,
28-
hon_protocol::VerticalSwingMode::MAX_UP,
29-
hon_protocol::VerticalSwingMode::UP,
30-
hon_protocol::VerticalSwingMode::CENTER,
31-
hon_protocol::VerticalSwingMode::DOWN,
32-
hon_protocol::VerticalSwingMode::MAX_DOWN,
33-
hon_protocol::VerticalSwingMode::HEALTH_DOWN,
27+
hon_protocol::VerticalSwingMode::HEALTH_UP, hon_protocol::VerticalSwingMode::MAX_UP,
28+
hon_protocol::VerticalSwingMode::UP, hon_protocol::VerticalSwingMode::CENTER,
29+
hon_protocol::VerticalSwingMode::DOWN, hon_protocol::VerticalSwingMode::MAX_DOWN,
30+
hon_protocol::VerticalSwingMode::HEALTH_DOWN,
3431
};
3532

3633
const std::vector<hon_protocol::HorizontalSwingMode> HORIZONTAL_SWING_MODES_ORDER = {
37-
hon_protocol::HorizontalSwingMode::MAX_LEFT,
38-
hon_protocol::HorizontalSwingMode::LEFT,
39-
hon_protocol::HorizontalSwingMode::CENTER,
40-
hon_protocol::HorizontalSwingMode::RIGHT,
41-
hon_protocol::HorizontalSwingMode::MAX_RIGHT,
34+
hon_protocol::HorizontalSwingMode::MAX_LEFT, hon_protocol::HorizontalSwingMode::LEFT,
35+
hon_protocol::HorizontalSwingMode::CENTER, hon_protocol::HorizontalSwingMode::RIGHT,
36+
hon_protocol::HorizontalSwingMode::MAX_RIGHT,
4237
};
4338

44-
#endif // USE_SELECT
39+
#endif // USE_SELECT
4540

4641
static const char *const TAG = "haier.climate";
4742
constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS = 10000;
@@ -169,12 +164,12 @@ void HonClimate::set_horizontal_airflow(hon_protocol::HorizontalSwingMode direct
169164
this->horizontal_direction_set_time_ = std::chrono::steady_clock::now();
170165
}
171166

172-
std::string HonClimate::get_cleaning_status_text() const {
167+
const char *HonClimate::get_cleaning_status_text() const {
173168
switch (this->cleaning_status_) {
174169
case CleaningState::SELF_CLEAN:
175170
return "Self clean";
176171
case CleaningState::STERI_CLEAN:
177-
return "56°C Steri-Clean";
172+
return "56°C Steri-Clean";
178173
default:
179174
return "No cleaning";
180175
}
@@ -218,29 +213,22 @@ haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haie
218213
}
219214
// All OK
220215
hon_protocol::DeviceVersionAnswer *answr = (hon_protocol::DeviceVersionAnswer *) data;
221-
char tmp[9];
222-
tmp[8] = 0;
223-
strncpy(tmp, answr->protocol_version, 8);
224-
this->hvac_hardware_info_ = HardwareInfo();
225-
this->hvac_hardware_info_.value().protocol_version_ = std::string(tmp);
226-
strncpy(tmp, answr->software_version, 8);
227-
this->hvac_hardware_info_.value().software_version_ = std::string(tmp);
228-
strncpy(tmp, answr->hardware_version, 8);
229-
this->hvac_hardware_info_.value().hardware_version_ = std::string(tmp);
230-
strncpy(tmp, answr->device_name, 8);
231-
this->hvac_hardware_info_.value().device_name_ = std::string(tmp);
216+
HardwareInfo info{}; // zero-init guarantees null-termination
217+
strncpy(info.protocol_version_, answr->protocol_version, HARDWARE_INFO_STR_SIZE - 1);
218+
strncpy(info.software_version_, answr->software_version, HARDWARE_INFO_STR_SIZE - 1);
219+
strncpy(info.hardware_version_, answr->hardware_version, HARDWARE_INFO_STR_SIZE - 1);
220+
strncpy(info.device_name_, answr->device_name, HARDWARE_INFO_STR_SIZE - 1);
221+
info.functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support
222+
info.functions_[1] = (answr->functions[1] & 0x02) != 0; // controller-device mode support
223+
info.functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support
224+
info.functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support
225+
info.functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support
226+
this->use_crc_ = info.functions_[2];
232227
#ifdef USE_TEXT_SENSOR
233-
this->update_sub_text_sensor_(SubTextSensorType::APPLIANCE_NAME, this->hvac_hardware_info_.value().device_name_);
234-
this->update_sub_text_sensor_(SubTextSensorType::PROTOCOL_VERSION,
235-
this->hvac_hardware_info_.value().protocol_version_);
228+
this->update_sub_text_sensor_(SubTextSensorType::APPLIANCE_NAME, info.device_name_);
229+
this->update_sub_text_sensor_(SubTextSensorType::PROTOCOL_VERSION, info.protocol_version_);
236230
#endif
237-
this->hvac_hardware_info_.value().functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support
238-
this->hvac_hardware_info_.value().functions_[1] =
239-
(answr->functions[1] & 0x02) != 0; // controller-device mode support
240-
this->hvac_hardware_info_.value().functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support
241-
this->hvac_hardware_info_.value().functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support
242-
this->hvac_hardware_info_.value().functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support
243-
this->use_crc_ = this->hvac_hardware_info_.value().functions_[2];
231+
this->hvac_hardware_info_ = info;
244232
this->set_phase(ProtocolPhases::SENDING_INIT_2);
245233
return result;
246234
} else {
@@ -431,10 +419,9 @@ void HonClimate::dump_config() {
431419
" Device software version: %s\n"
432420
" Device hardware version: %s\n"
433421
" Device name: %s",
434-
this->hvac_hardware_info_.value().protocol_version_.c_str(),
435-
this->hvac_hardware_info_.value().software_version_.c_str(),
436-
this->hvac_hardware_info_.value().hardware_version_.c_str(),
437-
this->hvac_hardware_info_.value().device_name_.c_str());
422+
this->hvac_hardware_info_.value().protocol_version_,
423+
this->hvac_hardware_info_.value().software_version_,
424+
this->hvac_hardware_info_.value().hardware_version_, this->hvac_hardware_info_.value().device_name_);
438425
ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s",
439426
(this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""),
440427
(this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""),
@@ -544,7 +531,7 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
544531
if (this->action_request_.has_value()) {
545532
if (this->action_request_.value().message.has_value()) {
546533
this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
547-
this->action_request_.value().message.reset();
534+
this->action_request_.value().message.reset(); // NOLINT(bugprone-unchecked-optional-access)
548535
} else {
549536
// Message already sent, reseting request and return to idle
550537
this->action_request_.reset();
@@ -754,13 +741,13 @@ haier_protocol::HaierMessage HonClimate::get_control_message() {
754741
}
755742
const ClimateSwingMode target_swing_mode = this->current_hvac_settings_.swing_mode.value_or(this->swing_mode);
756743
if (this->pending_vertical_direction_.has_value()) {
757-
if (!vertical_axis_is_auto_(target_swing_mode)) {
744+
if (!vertical_axis_is_auto(target_swing_mode)) {
758745
out_data->vertical_swing_mode = (uint8_t) this->pending_vertical_direction_.value();
759746
}
760747
this->pending_vertical_direction_.reset();
761748
}
762749
if (this->pending_horizontal_direction_.has_value()) {
763-
if (!horizontal_axis_is_auto_(target_swing_mode)) {
750+
if (!horizontal_axis_is_auto(target_swing_mode)) {
764751
out_data->horizontal_swing_mode = (uint8_t) this->pending_horizontal_direction_.value();
765752
}
766753
this->pending_horizontal_direction_.reset();
@@ -898,7 +885,7 @@ void HonClimate::set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSe
898885
}
899886
}
900887

901-
void HonClimate::update_sub_text_sensor_(SubTextSensorType type, const std::string &value) {
888+
void HonClimate::update_sub_text_sensor_(SubTextSensorType type, const char *value) {
902889
size_t index = (size_t) type;
903890
if (this->sub_text_sensors_[index] != nullptr)
904891
this->sub_text_sensors_[index]->publish_state(value);
@@ -1218,14 +1205,14 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
12181205
this->current_vertical_swing_ = (hon_protocol::VerticalSwingMode) packet.control.vertical_swing_mode;
12191206
this->current_horizontal_swing_ = (hon_protocol::HorizontalSwingMode) packet.control.horizontal_swing_mode;
12201207
auto now = std::chrono::steady_clock::now();
1221-
bool debounce_vertical = this->vertical_direction_set_time_.has_value() &&
1222-
std::chrono::duration_cast<std::chrono::milliseconds>(
1223-
now - this->vertical_direction_set_time_.value())
1224-
.count() < AIRFLOW_SELECT_DEBOUNCE_MS;
1225-
bool debounce_horizontal = this->horizontal_direction_set_time_.has_value() &&
1226-
std::chrono::duration_cast<std::chrono::milliseconds>(
1227-
now - this->horizontal_direction_set_time_.value())
1228-
.count() < AIRFLOW_SELECT_DEBOUNCE_MS;
1208+
bool debounce_vertical =
1209+
this->vertical_direction_set_time_.has_value() &&
1210+
std::chrono::duration_cast<std::chrono::milliseconds>(now - this->vertical_direction_set_time_.value())
1211+
.count() < AIRFLOW_SELECT_DEBOUNCE_MS;
1212+
bool debounce_horizontal =
1213+
this->horizontal_direction_set_time_.has_value() &&
1214+
std::chrono::duration_cast<std::chrono::milliseconds>(now - this->horizontal_direction_set_time_.value())
1215+
.count() < AIRFLOW_SELECT_DEBOUNCE_MS;
12291216
bool save_vertical = !debounce_vertical &&
12301217
(this->current_vertical_swing_.value() != hon_protocol::VerticalSwingMode::AUTO) &&
12311218
(this->current_vertical_swing_.value() != hon_protocol::VerticalSwingMode::AUTO_SPECIAL) &&
@@ -1449,7 +1436,7 @@ void HonClimate::fill_control_messages_queue_() {
14491436
vertical_swing_buf, 2);
14501437
}
14511438
if (this->pending_vertical_direction_.has_value()) {
1452-
if (!vertical_axis_is_auto_(target_swing_mode)) {
1439+
if (!vertical_axis_is_auto(target_swing_mode)) {
14531440
uint8_t vertical_swing_buf[] = {0x00, (uint8_t) this->pending_vertical_direction_.value()};
14541441
this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
14551442
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
@@ -1459,7 +1446,7 @@ void HonClimate::fill_control_messages_queue_() {
14591446
this->pending_vertical_direction_.reset();
14601447
}
14611448
if (this->pending_horizontal_direction_.has_value()) {
1462-
if (!horizontal_axis_is_auto_(target_swing_mode)) {
1449+
if (!horizontal_axis_is_auto(target_swing_mode)) {
14631450
uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) this->pending_horizontal_direction_.value()};
14641451
this->control_messages_queue_.emplace(haier_protocol::FrameType::CONTROL,
14651452
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
@@ -1498,7 +1485,8 @@ void HonClimate::fill_control_messages_queue_() {
14981485
}
14991486

15001487
void HonClimate::clear_control_messages_queue_() {
1501-
this->control_messages_queue_ = {};
1488+
while (!this->control_messages_queue_.empty())
1489+
this->control_messages_queue_.pop();
15021490
}
15031491

15041492
bool HonClimate::prepare_pending_action() {

components/haier/hon_climate.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class HonClimate : public HaierClimateBase {
9797
void set_sub_text_sensor(SubTextSensorType type, text_sensor::TextSensor *sens);
9898

9999
protected:
100-
void update_sub_text_sensor_(SubTextSensorType type, const std::string &value);
100+
void update_sub_text_sensor_(SubTextSensorType type, const char *value);
101101
text_sensor::TextSensor *sub_text_sensors_[(size_t) SubTextSensorType::SUB_TEXT_SENSOR_TYPE_COUNT]{nullptr};
102102
#endif
103103
#ifdef USE_SWITCH
@@ -110,7 +110,7 @@ class HonClimate : public HaierClimateBase {
110110
switch_::Switch *quiet_mode_switch_{nullptr};
111111
#endif
112112
#ifdef USE_SELECT
113-
public:
113+
public:
114114
void set_vertical_airflow_select(select::Select *sel);
115115
void set_horizontal_airflow_select(select::Select *sel);
116116

@@ -134,7 +134,7 @@ class HonClimate : public HaierClimateBase {
134134
void set_vertical_airflow(hon_protocol::VerticalSwingMode direction);
135135
esphome::optional<hon_protocol::HorizontalSwingMode> get_horizontal_airflow() const;
136136
void set_horizontal_airflow(hon_protocol::HorizontalSwingMode direction);
137-
std::string get_cleaning_status_text() const;
137+
const char *get_cleaning_status_text() const;
138138
CleaningState get_cleaning_status() const;
139139
void start_self_cleaning();
140140
void start_steri_cleaning();
@@ -184,11 +184,12 @@ class HonClimate : public HaierClimateBase {
184184
void fill_control_messages_queue_();
185185
void clear_control_messages_queue_();
186186

187+
static constexpr size_t HARDWARE_INFO_STR_SIZE = 9;
187188
struct HardwareInfo {
188-
std::string protocol_version_;
189-
std::string software_version_;
190-
std::string hardware_version_;
191-
std::string device_name_;
189+
char protocol_version_[HARDWARE_INFO_STR_SIZE];
190+
char software_version_[HARDWARE_INFO_STR_SIZE];
191+
char hardware_version_[HARDWARE_INFO_STR_SIZE];
192+
char device_name_[HARDWARE_INFO_STR_SIZE];
192193
bool functions_[5];
193194
};
194195

components/haier/smartair2_climate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now)
193193
if (this->action_request_.has_value()) {
194194
if (this->action_request_.value().message.has_value()) {
195195
this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
196-
this->action_request_.value().message.reset();
196+
this->action_request_.value().message.reset(); // NOLINT(bugprone-unchecked-optional-access)
197197
} else {
198198
// Message already sent, reseting request and return to idle
199199
this->action_request_.reset();

0 commit comments

Comments
 (0)