Skip to content

Commit

Permalink
Code and docs aligned with ESPHome requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Dudnytskyi committed Sep 6, 2024
1 parent d1a019b commit a82196d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 32 deletions.
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ This component requires a `UART Bus <https://esphome.io/components/uart#uart>`_
Configuration variables:
------------------------

- **id** (*Optional*, `ID <https://esphome.io/guides/configuration-types.html#config-id>`_): Manually specify the ID used for code generation.
- **uart_id** (*Optional*, `ID <https://esphome.io/guides/configuration-types.html#config-id>`_): ID of the UART port to communicate with AC.
- **protocol** (*Optional*, string): Defines communication protocol with AC. Possible values: ``hon`` or ``smartair2``. The default value is ``smartair2``.
- **name** (**Required**, string): The name of the climate device.
- **wifi_signal** (*Optional*, boolean): If ``true`` - send wifi signal level to AC.
- **answer_timeout** (*Optional*, `Time <https://esphome.io/guides/configuration-types.html#config-time>`_): Responce timeout. The default value is ``200ms``.
- **alternative_swing_control** (*Optional*, boolean): (supported by smartAir2 only) If ``true`` - use alternative values to control swing mode. Use only if the original control method is not working for your AC.
Expand Down
15 changes: 8 additions & 7 deletions components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() {
#endif

void HaierClimateBase::save_settings() {
HaierBaseSettings settings { this->get_health_mode(), this->get_display_state() };
HaierBaseSettings settings{this->get_health_mode(), this->get_display_state()};
if (!this->base_rtc_.save(&settings)) {
ESP_LOGW(TAG, "Failed to save settings");
}
}

bool HaierClimateBase::get_display_state() const {
return (this->display_status_ == SwitchState::ON) || (this->display_status_ == SwitchState::PENDING_ON);
return (this->display_status_ == SwitchState::ON) || (this->display_status_ == SwitchState::PENDING_ON);
}

void HaierClimateBase::set_display_state(bool state) {
Expand Down Expand Up @@ -305,7 +305,7 @@ void HaierClimateBase::loop() {
if ((this->health_mode_switch_ != nullptr) && (this->health_mode_switch_->state != this->get_health_mode())) {
this->health_mode_switch_->publish_state(this->get_health_mode());
}
#endif // USE_SWITCH
#endif // USE_SWITCH
}

void HaierClimateBase::process_protocol_reset() {
Expand Down Expand Up @@ -349,13 +349,14 @@ bool HaierClimateBase::prepare_pending_action() {
ClimateTraits HaierClimateBase::traits() { return traits_; }

void HaierClimateBase::initialization() {
constexpr uint32_t restore_settings_version = 0xA77D21EF;
this->base_rtc_ = global_preferences->make_preference<HaierBaseSettings>(this->get_object_id_hash() ^ restore_settings_version);
constexpr uint32_t restore_settings_version = 0xA77D21EF;
this->base_rtc_ =
global_preferences->make_preference<HaierBaseSettings>(this->get_object_id_hash() ^ restore_settings_version);
HaierBaseSettings recovered;
if (!this->base_rtc_.load(&recovered)) {
recovered = { false, true };
recovered = {false, true};
}
this->display_status_= recovered.display_state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF;
this->display_status_ = recovered.display_state ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF;
this->health_mode_ = recovered.health_mode ? SwitchState::PENDING_ON : SwitchState::PENDING_OFF;
#ifdef USE_SWITCH
if (this->display_switch_ != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class HaierClimateBase : public esphome::Component,
ActionRequest action;
esphome::optional<haier_protocol::HaierMessage> message;
};
enum class SwitchState{
enum class SwitchState {
OFF = 0b00,
ON = 0b01,
PENDING_OFF = 0b10,
Expand Down
31 changes: 19 additions & 12 deletions components/haier/hon_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ void HonClimate::set_quiet_mode_state(bool state) {
}
}

bool HonClimate::get_quiet_mode_state() const { return (this->quiet_mode_state_ == SwitchState::ON) || (this->quiet_mode_state_ == SwitchState::PENDING_ON); }
bool HonClimate::get_quiet_mode_state() const {
return (this->quiet_mode_state_ == SwitchState::ON) || (this->quiet_mode_state_ == SwitchState::PENDING_ON);
}

esphome::optional<hon_protocol::VerticalSwingMode> HonClimate::get_vertical_airflow() const {
return this->current_vertical_swing_;
Expand Down Expand Up @@ -497,7 +499,8 @@ haier_protocol::HaierMessage HonClimate::get_power_message(bool state) {
void HonClimate::initialization() {
HaierClimateBase::initialization();
constexpr uint32_t restore_settings_version = 0x57EB59DDUL;
this->hon_rtc_ = global_preferences->make_preference<HonSettings>(this->get_object_id_hash() ^ restore_settings_version);
this->hon_rtc_ =
global_preferences->make_preference<HonSettings>(this->get_object_id_hash() ^ restore_settings_version);
HonSettings recovered;
if (this->hon_rtc_.load(&recovered)) {
this->settings_ = recovered;
Expand Down Expand Up @@ -661,9 +664,9 @@ haier_protocol::HaierMessage HonClimate::get_control_message() {
out_data->beeper_status = ((!this->get_beeper_state()) || (!has_hvac_settings)) ? 1 : 0;
control_out_buffer[4] = 0; // This byte should be cleared before setting values
out_data->display_status = this->get_display_state() ? 1 : 0;
this->display_status_ = (SwitchState)((uint8_t)this->display_status_ & 0b01);
this->display_status_ = (SwitchState) ((uint8_t) this->display_status_ & 0b01);
out_data->health_mode = this->get_health_mode() ? 1 : 0;
this->health_mode_ = (SwitchState)((uint8_t)this->health_mode_ & 0b01);
this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
control_out_buffer, this->real_control_packet_size_);
Expand Down Expand Up @@ -929,13 +932,13 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
if (this->mode == CLIMATE_MODE_OFF) {
// AC just turned on from remote need to turn off display
this->force_send_control_ = true;
} else if ((((uint8_t)this->health_mode_) & 0b10) == 0) {
} else if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF;
}
}
}
// Health mode
if ((((uint8_t)this->health_mode_) & 0b10) == 0) {
if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
bool old_health_mode = this->get_health_mode();
this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF;
should_publish = should_publish || (old_health_mode != this->get_health_mode());
Expand Down Expand Up @@ -994,7 +997,8 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
}
{
// Quiet mode, should be after climate mode
if ((this->mode != CLIMATE_MODE_FAN_ONLY) && (this->mode != CLIMATE_MODE_OFF) && ((((uint8_t)this->quiet_mode_state_) & 0b10) == 0)) {
if ((this->mode != CLIMATE_MODE_FAN_ONLY) && (this->mode != CLIMATE_MODE_OFF) &&
((((uint8_t) this->quiet_mode_state_) & 0b10) == 0)) {
// In proper mode and not in pending state
bool new_quiet_mode = packet.control.quiet_mode != 0;
if (new_quiet_mode != this->get_quiet_mode_state()) {
Expand All @@ -1007,17 +1011,20 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
{
// Swing mode
ClimateSwingMode old_swing_mode = this->swing_mode;
const std::set<ClimateSwingMode>& swing_modes = traits_.get_supported_swing_modes();
const std::set<ClimateSwingMode> &swing_modes = traits_.get_supported_swing_modes();
bool vertical_swing_supported = swing_modes.find(CLIMATE_SWING_VERTICAL) != swing_modes.end();
bool horizontal_swing_supported = swing_modes.find(CLIMATE_SWING_HORIZONTAL) != swing_modes.end();
if (horizontal_swing_supported && (packet.control.horizontal_swing_mode == (uint8_t) hon_protocol::HorizontalSwingMode::AUTO)) {
if (vertical_swing_supported && (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
if (horizontal_swing_supported &&
(packet.control.horizontal_swing_mode == (uint8_t) hon_protocol::HorizontalSwingMode::AUTO)) {
if (vertical_swing_supported &&
(packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
this->swing_mode = CLIMATE_SWING_BOTH;
} else {
this->swing_mode = CLIMATE_SWING_HORIZONTAL;
}
} else {
if (vertical_swing_supported && (packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
if (vertical_swing_supported &&
(packet.control.vertical_swing_mode == (uint8_t) hon_protocol::VerticalSwingMode::AUTO)) {
this->swing_mode = CLIMATE_SWING_VERTICAL;
} else {
this->swing_mode = CLIMATE_SWING_OFF;
Expand Down Expand Up @@ -1075,7 +1082,7 @@ void HonClimate::fill_control_messages_queue_() {
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::HEALTH_MODE,
this->get_health_mode() ? ONE_BUF : ZERO_BUF, 2));
this->health_mode_ = (SwitchState)((uint8_t)this->health_mode_ & 0b01);
this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
}
// Climate mode
ClimateMode climate_mode = this->mode;
Expand Down
12 changes: 6 additions & 6 deletions components/haier/smartair2_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ haier_protocol::HaierMessage Smartair2Climate::get_control_message() {
}
}
out_data->display_status = this->get_display_state() ? 0 : 1;
this->display_status_ = (SwitchState)((uint8_t)this->display_status_ & 0b01);
this->display_status_ = (SwitchState) ((uint8_t) this->display_status_ & 0b01);
out_data->health_mode = this->get_health_mode() ? 1 : 0;
this->health_mode_ = (SwitchState)((uint8_t)this->health_mode_ & 0b01);
this->health_mode_ = (SwitchState) ((uint8_t) this->health_mode_ & 0b01);
return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, 0x4D5F, control_out_buffer,
sizeof(smartair2_protocol::HaierPacketControl));
}
Expand Down Expand Up @@ -451,20 +451,20 @@ haier_protocol::HandlerError Smartair2Climate::process_status_message_(const uin
// Display status
// should be before "Climate mode" because it is changing this->mode
if (packet.control.ac_power != 0) {
// if AC is off display status always ON so process it only when AC is on
bool disp_status = packet.control.display_status == 0;
// if AC is off display status always ON so process it only when AC is on
bool disp_status = packet.control.display_status == 0;
if (disp_status != this->get_display_state()) {
// Do something only if display status changed
if (this->mode == CLIMATE_MODE_OFF) {
// AC just turned on from remote need to turn off display
this->force_send_control_ = true;
} else if ((((uint8_t)this->health_mode_) & 0b10) == 0) {
} else if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
this->display_status_ = disp_status ? SwitchState::ON : SwitchState::OFF;
}
}
}
// Health mode
if ((((uint8_t)this->health_mode_) & 0b10) == 0) {
if ((((uint8_t) this->health_mode_) & 0b10) == 0) {
bool old_health_mode = this->get_health_mode();
this->health_mode_ = packet.control.health_mode == 1 ? SwitchState::ON : SwitchState::OFF;
should_publish = should_publish || (old_health_mode != this->get_health_mode());
Expand Down
6 changes: 4 additions & 2 deletions components/haier/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
}
)


async def to_code(config):
full_id, parent = await cg.get_variable_with_full_id(config[CONF_HAIER_ID])

Expand All @@ -75,5 +76,6 @@ async def to_code(config):
await cg.register_parented(sw_var, parent)
cg.add(getattr(parent, f"set_{switch_type}_switch")(sw_var))
else:
raise ValueError(f"{switch_type} switch is only supported for hon climate")

raise ValueError(
f"{switch_type} switch is only supported for hon climate"
)
2 changes: 0 additions & 2 deletions docs/esphome-docs/climate/haier.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ This component requires a :ref:`uart` to be setup.
Configuration variables:
------------------------

- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation.
- **uart_id** (*Optional*, :ref:`config-id`): ID of the UART port to communicate with AC.
- **protocol** (*Optional*, string): Defines communication protocol with AC. Possible values: ``hon`` or ``smartair2``. The default value is ``smartair2``.
- **name** (**Required**, string): The name of the climate device.
- **wifi_signal** (*Optional*, boolean): If ``true`` - send wifi signal level to AC.
- **answer_timeout** (*Optional*, :ref:`config-time`): Responce timeout. The default value is ``200ms``.
- **alternative_swing_control** (*Optional*, boolean): (supported by smartAir2 only) If ``true`` - use alternative values to control swing mode. Use only if the original control method is not working for your AC.
Expand Down

0 comments on commit a82196d

Please sign in to comment.