Skip to content

Commit

Permalink
Formating code according to ESPHome requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Dudnytskyi committed Jul 4, 2024
1 parent 6b13d22 commit c6863e2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
13 changes: 7 additions & 6 deletions components/haier/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
automation.Trigger.template(cg.const_char_ptr, cg.size_t),
)


def validate_visual(config):
if CONF_VISUAL in config:
visual_config = config[CONF_VISUAL]
Expand Down Expand Up @@ -205,9 +206,7 @@ def validate_visual(config):
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_ON_STATUS_MESSAGE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
StatusMessageTrigger
),
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StatusMessageTrigger),
}
),
}
Expand Down Expand Up @@ -246,10 +245,12 @@ def validate_visual(config):
CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE
): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50),
cv.Optional(
CONF_SENSORS_PACKET_SIZE, default=PROTOCOL_DEFAULT_SENSORS_PACKET_SIZE
CONF_SENSORS_PACKET_SIZE,
default=PROTOCOL_DEFAULT_SENSORS_PACKET_SIZE,
): cv.int_range(min=PROTOCOL_MIN_SENSORS_PACKET_SIZE, max=50),
cv.Optional(
CONF_STATUS_MESSAGE_HEADER_SIZE, default=PROTOCOL_STATUS_MESSAGE_HEADER_SIZE
CONF_STATUS_MESSAGE_HEADER_SIZE,
default=PROTOCOL_STATUS_MESSAGE_HEADER_SIZE,
): cv.int_range(min=PROTOCOL_STATUS_MESSAGE_HEADER_SIZE),
cv.Optional(
CONF_SUPPORTED_PRESETS,
Expand Down Expand Up @@ -515,6 +516,6 @@ async def to_code(config):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(cg.const_char_ptr, "data"), (cg.size_t, "data_size")], conf
)
)
# https://github.com/paveldn/HaierProtocol
cg.add_library("pavlodn/HaierProtocol", "0.9.31")
2 changes: 1 addition & 1 deletion components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void HaierClimateBase::send_custom_command(const haier_protocol::HaierMessage &m
this->action_request_ = PendingAction({ActionRequest::SEND_CUSTOM_COMMAND, message});
}

void HaierClimateBase::add_status_message_callback(std::function<void(const char*, size_t)> &&callback) {
void HaierClimateBase::add_status_message_callback(std::function<void(const char *, size_t)> &&callback) {
this->status_message_callback_.add(std::move(callback));
}

Expand Down
11 changes: 5 additions & 6 deletions components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class HaierClimateBase : public esphome::Component,
void set_answer_timeout(uint32_t timeout);
void set_send_wifi(bool send_wifi);
void send_custom_command(const haier_protocol::HaierMessage &message);
void add_status_message_callback(std::function<void(const char*, size_t)> &&callback);
void add_status_message_callback(std::function<void(const char *, size_t)> &&callback);

protected:
enum class ProtocolPhases {
Expand Down Expand Up @@ -142,19 +142,18 @@ class HaierClimateBase : public esphome::Component,
esphome::climate::ClimateTraits traits_;
HvacSettings current_hvac_settings_;
HvacSettings next_hvac_settings_;
std::unique_ptr<uint8_t[]> last_status_message_{ nullptr };
std::unique_ptr<uint8_t[]> last_status_message_{nullptr};
std::chrono::steady_clock::time_point last_request_timestamp_; // For interval between messages
std::chrono::steady_clock::time_point last_valid_status_timestamp_; // For protocol timeout
std::chrono::steady_clock::time_point last_status_request_; // To request AC status
std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level
CallbackManager<void(const char*, size_t)> status_message_callback_{};
CallbackManager<void(const char *, size_t)> status_message_callback_{};
};

class StatusMessageTrigger : public Trigger<const char*, size_t> {
class StatusMessageTrigger : public Trigger<const char *, size_t> {
public:
explicit StatusMessageTrigger(HaierClimateBase *parent) {
parent->add_status_message_callback(
[this](const char* data, size_t data_size) { this->trigger(data, data_size); });
parent->add_status_message_callback([this](const char *data, size_t data_size) { this->trigger(data, data_size); });
}
};

Expand Down
55 changes: 27 additions & 28 deletions components/haier/hon_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ constexpr size_t ALARM_STATUS_REQUEST_INTERVAL_MS = 600000;
const uint8_t ONE_BUF[] = {0x00, 0x01};
const uint8_t ZERO_BUF[] = {0x00, 0x00};


HonClimate::HonClimate()
: cleaning_status_(CleaningState::NO_CLEANING), got_valid_outdoor_temp_(false), active_alarms_{0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
Expand Down Expand Up @@ -177,11 +176,11 @@ haier_protocol::HandlerError HonClimate::status_handler_(haier_protocol::FrameTy
this->last_status_message_.reset(new uint8_t[this->real_control_packet_size_]);
};
if (data_size >= this->real_control_packet_size_ + 2) {
memcpy(this->last_status_message_.get(), data + 2 + this->status_message_header_size_, this->real_control_packet_size_);
this->status_message_callback_.call((const char*) data, data_size);
memcpy(this->last_status_message_.get(), data + 2 + this->status_message_header_size_,
this->real_control_packet_size_);
this->status_message_callback_.call((const char *) data, data_size);
} else {
ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size,
this->real_control_packet_size_);
ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size, this->real_control_packet_size_);
}
switch (this->protocol_phase_) {
case ProtocolPhases::SENDING_FIRST_STATUS_REQUEST:
Expand Down Expand Up @@ -487,7 +486,7 @@ void HonClimate::initialization() {
}

haier_protocol::HaierMessage HonClimate::get_control_message() {
static uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
uint8_t control_out_buffer[haier_protocol::MAX_FRAME_SIZE];
memcpy(control_out_buffer, this->last_status_message_.get(), this->real_control_packet_size_);
hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
control_out_buffer[4] = 0; // This byte should be cleared before setting values
Expand Down Expand Up @@ -766,7 +765,8 @@ void HonClimate::update_sub_text_sensor_(SubTextSensorType type, const std::stri
#endif // USE_TEXT_SENSOR

haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) {
size_t expected_size = 2 + this->status_message_header_size_ + this->real_control_packet_size_ + this->real_sensors_packet_size_;
size_t expected_size =
2 + this->status_message_header_size_ + this->real_control_packet_size_ + this->real_sensors_packet_size_;
if (size < expected_size) {
ESP_LOGW(TAG, "Unexpected message size %d (expexted >= %d)", size, expected_size);
return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
Expand Down Expand Up @@ -804,7 +804,8 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
hon_protocol::HaierPacketControl control;
hon_protocol::HaierPacketSensors sensors;
} packet;
memcpy(&packet.control, packet_buffer + 2 + this->status_message_header_size_, sizeof(hon_protocol::HaierPacketControl));
memcpy(&packet.control, packet_buffer + 2 + this->status_message_header_size_,
sizeof(hon_protocol::HaierPacketControl));
memcpy(&packet.sensors, packet_buffer + 2 + this->status_message_header_size_ + this->real_control_packet_size_,
sizeof(hon_protocol::HaierPacketSensors));
if (packet.sensors.error_status != 0) {
Expand Down Expand Up @@ -1171,14 +1172,14 @@ void HonClimate::fill_control_messages_queue_() {
(uint8_t) hon_protocol::DataParameters::SET_POINT,
buffer, 2));
}
// Vertical swing mode
// Vertical swing mode
if (climate_control.swing_mode.has_value()) {
uint8_t vertical_swing_buf[] = {0x00, (uint8_t) hon_protocol::VerticalSwingMode::AUTO};
uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) hon_protocol::HorizontalSwingMode::AUTO};
uint8_t vertical_swing_buf[] = {0x00, (uint8_t) hon_protocol::VerticalSwingMode::AUTO};
uint8_t horizontal_swing_buf[] = {0x00, (uint8_t) hon_protocol::HorizontalSwingMode::AUTO};
switch (climate_control.swing_mode.value()) {
case CLIMATE_SWING_OFF:
horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
vertical_swing_buf[1] = (uint8_t) this->settings_.last_vertiacal_swing;
break;
case CLIMATE_SWING_VERTICAL:
horizontal_swing_buf[1] = (uint8_t) this->settings_.last_horizontal_swing;
Expand All @@ -1191,14 +1192,14 @@ void HonClimate::fill_control_messages_queue_() {
}
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::HORIZONTAL_SWING_MODE,
horizontal_swing_buf, 2));
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::HORIZONTAL_SWING_MODE,
horizontal_swing_buf, 2));
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::VERTICAL_SWING_MODE,
vertical_swing_buf, 2));
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::VERTICAL_SWING_MODE,
vertical_swing_buf, 2));
}
// Fan mode
if (climate_control.fan_mode.has_value()) {
Expand Down Expand Up @@ -1254,16 +1255,14 @@ bool HonClimate::prepare_pending_action() {
haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
control_out_buffer, this->real_control_packet_size_);
return true;
}
else if (this->control_method_ == HonControlMethod::SET_SINGLE_PARAMETER) {
this->action_request_.value().message = haier_protocol::HaierMessage(
haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::SELF_CLEANING,
ONE_BUF, 2);
return true;
}
else {
} else if (this->control_method_ == HonControlMethod::SET_SINGLE_PARAMETER) {
this->action_request_.value().message =
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::SELF_CLEANING,
ONE_BUF, 2);
return true;
} else {
this->action_request_.reset();
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions components/haier/hon_climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class HonClimate : public HaierClimateBase {
void start_self_cleaning();
void start_steri_cleaning();
void set_extra_control_packet_bytes_size(size_t size) { this->extra_control_packet_bytes_ = size; };
void set_extra_sensors_packet_bytes_size(size_t size) { this->extra_sensors_packet_bytes_ = size; };
void set_extra_sensors_packet_bytes_size(size_t size) { this->extra_sensors_packet_bytes_ = size; };
void set_status_message_header_size(size_t size) { this->status_message_header_size_ = size; };
void set_control_method(HonControlMethod method) { this->control_method_ = method; };
void add_alarm_start_callback(std::function<void(uint8_t, const char *)> &&callback);
Expand Down Expand Up @@ -163,8 +163,8 @@ class HonClimate : public HaierClimateBase {
int extra_control_packet_bytes_{0};
int extra_sensors_packet_bytes_{4};
int status_message_header_size_{0};
int real_control_packet_size_{ sizeof(hon_protocol::HaierPacketControl) };
int real_sensors_packet_size_{ sizeof(hon_protocol::HaierPacketSensors) + 4 };
int real_control_packet_size_{sizeof(hon_protocol::HaierPacketControl)};
int real_sensors_packet_size_{sizeof(hon_protocol::HaierPacketSensors) + 4};
HonControlMethod control_method_;
std::queue<haier_protocol::HaierMessage> control_messages_queue_;
CallbackManager<void(uint8_t, const char *)> alarm_start_callback_{};
Expand Down
2 changes: 1 addition & 1 deletion components/haier/smartair2_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ haier_protocol::HandlerError Smartair2Climate::status_handler_(haier_protocol::F
} else {
if (data_size >= sizeof(smartair2_protocol::HaierPacketControl) + 2) {
memcpy(this->last_status_message_.get(), data + 2, sizeof(smartair2_protocol::HaierPacketControl));
this->status_message_callback_.call((const char*) data, data_size);
this->status_message_callback_.call((const char *) data, data_size);
} else {
ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size,
sizeof(smartair2_protocol::HaierPacketControl));
Expand Down

0 comments on commit c6863e2

Please sign in to comment.