Skip to content

Commit

Permalink
Format andjustments to align with ESPHome requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Dudnytskyi committed Oct 13, 2023
1 parent 54137e4 commit d682a45
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 163 deletions.
24 changes: 18 additions & 6 deletions components/haier/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def validate_visual(config):
PROTOCOL_SMARTAIR2: BASE_CONFIG_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(Smartair2Climate),
cv.Optional(CONF_ALTERNATIVE_SWING_CONTROL, default=False) : cv.boolean,
cv.Optional(
CONF_ALTERNATIVE_SWING_CONTROL, default=False
): cv.boolean,
cv.Optional(
CONF_SUPPORTED_PRESETS,
default=list(
Expand All @@ -209,11 +211,15 @@ def validate_visual(config):
PROTOCOL_HON: BASE_CONFIG_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(HonClimate),
cv.Optional(CONF_CONTROL_METHOD, default="SET_GROUP_PARAMETERS") : cv.ensure_list(
cv.Optional(
CONF_CONTROL_METHOD, default="SET_GROUP_PARAMETERS"
): cv.ensure_list(
cv.enum(SUPPORTED_HON_CONTROL_METHODS, upper=True)
),
cv.Optional(CONF_BEEPER, default=True): cv.boolean,
cv.Optional(CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50),
cv.Optional(
CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE
): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50),
cv.Optional(
CONF_SUPPORTED_PRESETS,
default=list(SUPPORTED_CLIMATE_PRESETS_HON_OPTIONS.keys()),
Expand Down Expand Up @@ -422,7 +428,7 @@ async def to_code(config):
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
await climate.register_climate(var, config)

cg.add(var.set_send_wifi(config[CONF_WIFI_SIGNAL]))
if CONF_CONTROL_METHOD in config:
cg.add(var.set_control_method(config[CONF_CONTROL_METHOD]))
Expand All @@ -442,8 +448,14 @@ async def to_code(config):
if CONF_ANSWER_TIMEOUT in config:
cg.add(var.set_answer_timeout(config[CONF_ANSWER_TIMEOUT]))
if CONF_ALTERNATIVE_SWING_CONTROL in config:
cg.add(var.set_alternative_swing_control(config[CONF_ALTERNATIVE_SWING_CONTROL]))
cg.add(
var.set_alternative_swing_control(config[CONF_ALTERNATIVE_SWING_CONTROL])
)
if CONF_CONTROL_PACKET_SIZE in config:
cg.add(var.set_extra_control_packet_bytes_size(config[CONF_CONTROL_PACKET_SIZE] - PROTOCOL_CONTROL_PACKET_SIZE))
cg.add(
var.set_extra_control_packet_bytes_size(
config[CONF_CONTROL_PACKET_SIZE] - PROTOCOL_CONTROL_PACKET_SIZE
)
)
# https://github.com/paveldn/HaierProtocol
cg.add_library("pavlodn/HaierProtocol", "0.9.23")
58 changes: 28 additions & 30 deletions components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using namespace esphome::climate;
using namespace esphome::uart;


namespace esphome {
namespace haier {

Expand Down Expand Up @@ -56,12 +55,11 @@ const char *HaierClimateBase::phase_to_string_(ProtocolPhases phase) {
}
#endif

bool check_timeout_(std::chrono::steady_clock::time_point now,
std::chrono::steady_clock::time_point tpoint, size_t timeout) {
bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint,
size_t timeout) {
return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
}


HaierClimateBase::HaierClimateBase()
: haier_protocol_(*this),
protocol_phase_(ProtocolPhases::SENDING_INIT_1),
Expand Down Expand Up @@ -107,19 +105,19 @@ void HaierClimateBase::reset_to_idle_() {
}

bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return check_timeout_(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
return check_timeout(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
}

bool HaierClimateBase::is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return check_timeout_(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
return check_timeout(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
}

bool HaierClimateBase::is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return check_timeout_(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
return check_timeout(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
}

bool HaierClimateBase::is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return check_timeout_(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
return check_timeout(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
}

#ifdef USE_WIFI
Expand Down Expand Up @@ -169,9 +167,7 @@ void HaierClimateBase::set_supported_swing_modes(const std::set<climate::Climate
this->traits_.add_supported_swing_mode(climate::CLIMATE_SWING_OFF);
}

void HaierClimateBase::set_answer_timeout(uint32_t timeout) {
this->haier_protocol_.set_answer_timeout(timeout);
}
void HaierClimateBase::set_answer_timeout(uint32_t timeout) { this->haier_protocol_.set_answer_timeout(timeout); }

void HaierClimateBase::set_supported_modes(const std::set<climate::ClimateMode> &modes) {
this->traits_.set_supported_modes(modes);
Expand All @@ -187,15 +183,16 @@ void HaierClimateBase::set_supported_presets(const std::set<climate::ClimatePres

void HaierClimateBase::set_send_wifi(bool send_wifi) { this->send_wifi_signal_ = send_wifi; }

haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(haier_protocol::FrameType request_message_type,
haier_protocol::FrameType expected_request_message_type,
haier_protocol::FrameType answer_message_type,
haier_protocol::FrameType expected_answer_message_type,
ProtocolPhases expected_phase) {
haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(
haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
ProtocolPhases expected_phase) {
haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK;
if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) && (request_message_type != expected_request_message_type))
if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
(request_message_type != expected_request_message_type))
result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) && (answer_message_type != expected_answer_message_type))
if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
(answer_message_type != expected_answer_message_type))
result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
if ((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_))
result = haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
Expand All @@ -204,19 +201,20 @@ haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(haier_protocol
return result;
}

haier_protocol::HandlerError HaierClimateBase::report_network_status_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
haier_protocol::HandlerError result = this->answer_preprocess_(
request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
haier_protocol::FrameType::CONFIRM, ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
haier_protocol::HandlerError HaierClimateBase::report_network_status_answer_handler_(
haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
size_t data_size) {
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
haier_protocol::FrameType::CONFIRM, ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
this->set_phase(ProtocolPhases::IDLE);
return result;
}

haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(haier_protocol::FrameType request_type) {
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type, phase_to_string_(this->protocol_phase_));
ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type,
phase_to_string_(this->protocol_phase_));
#else
ESP_LOGW(TAG, "Answer timeout for command %02X, phase %d", (uint8_t) request_type, (int) this->protocol_phase_);
#endif
Expand All @@ -240,8 +238,7 @@ void HaierClimateBase::setup() {

void HaierClimateBase::dump_config() {
LOG_CLIMATE("", "Haier Climate", this);
ESP_LOGCONFIG(TAG, " Device communication status: %s",
this->valid_connection() ? "established" : "none");
ESP_LOGCONFIG(TAG, " Device communication status: %s", this->valid_connection() ? "established" : "none");
}

void HaierClimateBase::loop() {
Expand Down Expand Up @@ -324,7 +321,7 @@ void HaierClimateBase::control(const ClimateCall &call) {
ESP_LOGW(TAG, "Can't send control packet, first poll answer not received");
return; // cancel the control, we cant do it without a poll answer.
}
if (this->current_hvac_settings_.valid) {
if (this->current_hvac_settings_.valid) {
ESP_LOGW(TAG, "New settings come faster then processed!");
}
{
Expand All @@ -349,9 +346,10 @@ void HaierClimateBase::HvacSettings::reset() {
this->swing_mode.reset();
this->target_temperature.reset();
this->preset.reset();
}
}

void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats, std::chrono::milliseconds interval) {
void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats,
std::chrono::milliseconds interval) {
this->haier_protocol_.send_message(command, use_crc, num_repeats, interval);
this->last_request_timestamp_ = std::chrono::steady_clock::now();
}
Expand Down
18 changes: 11 additions & 7 deletions components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ class HaierClimateBase : public esphome::Component,
virtual void process_pending_action();
esphome::climate::ClimateTraits traits() override;
// Answer handlers
haier_protocol::HandlerError answer_preprocess_(haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
haier_protocol::HandlerError answer_preprocess_(haier_protocol::FrameType request_message_type,
haier_protocol::FrameType expected_request_message_type,
haier_protocol::FrameType answer_message_type,
haier_protocol::FrameType expected_answer_message_type,
ProtocolPhases expected_phase);
haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type, haier_protocol::FrameType message_type,
haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
// Timeout handler
haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type);
// Helper functions
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_retries = 0, std::chrono::milliseconds interval = std::chrono::milliseconds::zero());
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats = 0,
std::chrono::milliseconds interval = std::chrono::milliseconds::zero());
virtual void set_phase(ProtocolPhases phase);
void reset_to_idle_();
bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
Expand All @@ -120,8 +124,8 @@ class HaierClimateBase : public esphome::Component,
esphome::optional<esphome::climate::ClimatePreset> preset;
bool valid;
HvacSettings() : valid(false){};
HvacSettings(const HvacSettings&) = default;
HvacSettings& operator=(const HvacSettings&) = default;
HvacSettings(const HvacSettings &) = default;
HvacSettings &operator=(const HvacSettings &) = default;
void reset();
};
haier_protocol::ProtocolHandler haier_protocol_;
Expand All @@ -143,7 +147,7 @@ class HaierClimateBase : public esphome::Component,
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
std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level
};

} // namespace haier
Expand Down
Loading

0 comments on commit d682a45

Please sign in to comment.