Skip to content

Commit

Permalink
Beeper switch for haier platform added
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed Aug 18, 2024
1 parent 2ba4c95 commit 1a959f1
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 19 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ This component requires a `UART Bus <https://esphome.io/components/uart#uart>`_
name: Haier AC
uart_id: ac_port
wifi_signal: true
beeper: true
display: true
visual:
min_temperature: 16 °C
Expand Down
4 changes: 3 additions & 1 deletion components/haier/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ def validate_visual(config):
): cv.ensure_list(
cv.enum(SUPPORTED_HON_CONTROL_METHODS, upper=True)
),
cv.Optional(CONF_BEEPER, default=True): cv.boolean,
cv.Optional(CONF_BEEPER): cv.invalid(
f"The {CONF_BEEPER} option is deprecated, use beeper_on/beeper_off actions or beeper switch for a haier platform instead"
),
cv.Optional(
CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE
): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50),
Expand Down
27 changes: 21 additions & 6 deletions components/haier/hon_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ HonClimate::HonClimate()

HonClimate::~HonClimate() {}

void HonClimate::set_beeper_state(bool state) { this->beeper_status_ = state; }
void HonClimate::set_beeper_state(bool state) {
if (state != this->settings_.beeper_state) {
this->settings_.beeper_state = state;
this->beeper_switch_->publish_state(state);
this->rtc_.save(&this->settings_);
}
}

bool HonClimate::get_beeper_state() const { return this->beeper_status_; }
bool HonClimate::get_beeper_state() const { return this->settings_.beeper_state; }

esphome::optional<hon_protocol::VerticalSwingMode> HonClimate::get_vertical_airflow() const {
return this->current_vertical_swing_;
Expand Down Expand Up @@ -474,13 +480,13 @@ haier_protocol::HaierMessage HonClimate::get_power_message(bool state) {
}

void HonClimate::initialization() {
constexpr uint32_t restore_settings_version = 0xE834D8DCUL;
constexpr uint32_t restore_settings_version = 0x2A3613DCUL;
this->rtc_ = global_preferences->make_preference<HonSettings>(this->get_object_id_hash() ^ restore_settings_version);
HonSettings recovered;
if (this->rtc_.load(&recovered)) {
this->settings_ = recovered;
} else {
this->settings_ = {hon_protocol::VerticalSwingMode::CENTER, hon_protocol::HorizontalSwingMode::CENTER};
this->settings_ = {hon_protocol::VerticalSwingMode::CENTER, hon_protocol::HorizontalSwingMode::CENTER, true};
}
this->current_vertical_swing_ = this->settings_.last_vertiacal_swing;
this->current_horizontal_swing_ = this->settings_.last_horizontal_swing;
Expand Down Expand Up @@ -638,7 +644,7 @@ haier_protocol::HaierMessage HonClimate::get_control_message() {
out_data->horizontal_swing_mode = (uint8_t) this->pending_horizontal_direction_.value();
this->pending_horizontal_direction_.reset();
}
out_data->beeper_status = ((!this->beeper_status_) || (!has_hvac_settings)) ? 1 : 0;
out_data->beeper_status = ((!this->settings_.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->display_status_ ? 1 : 0;
out_data->health_mode = this->health_mode_ ? 1 : 0;
Expand Down Expand Up @@ -765,6 +771,15 @@ void HonClimate::update_sub_text_sensor_(SubTextSensorType type, const std::stri
}
#endif // USE_TEXT_SENSOR

#ifdef USE_SWITCH
void HonClimate::set_beeper_switch(switch_::Switch *sw) {
this->beeper_switch_ = sw;
if (this->beeper_switch_ != nullptr) {
this->beeper_switch_->publish_state(this->settings_.beeper_state);
}
}
#endif // USE_SWITCH

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_;
Expand Down Expand Up @@ -1017,7 +1032,7 @@ void HonClimate::fill_control_messages_queue_() {
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::BEEPER_STATUS,
this->beeper_status_ ? ZERO_BUF : ONE_BUF, 2));
this->settings_.beeper_state ? ZERO_BUF : ONE_BUF, 2));
}
// Health mode
{
Expand Down
12 changes: 11 additions & 1 deletion components/haier/hon_climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif
#include "esphome/core/automation.h"
#include "haier_base.h"
#include "hon_packet.h"
Expand All @@ -28,6 +31,7 @@ enum class HonControlMethod { MONITOR_ONLY = 0, SET_GROUP_PARAMETERS, SET_SINGLE
struct HonSettings {
hon_protocol::VerticalSwingMode last_vertiacal_swing;
hon_protocol::HorizontalSwingMode last_horizontal_swing;
bool beeper_state;
};

class HonClimate : public HaierClimateBase {
Expand Down Expand Up @@ -86,6 +90,13 @@ class HonClimate : public HaierClimateBase {
protected:
void update_sub_text_sensor_(SubTextSensorType type, const std::string &value);
text_sensor::TextSensor *sub_text_sensors_[(size_t) SubTextSensorType::SUB_TEXT_SENSOR_TYPE_COUNT]{nullptr};
#endif
#ifdef USE_SWITCH
public:
void set_beeper_switch(switch_::Switch *sw);

protected:
switch_::Switch *beeper_switch_{nullptr};
#endif
public:
HonClimate();
Expand Down Expand Up @@ -153,7 +164,6 @@ class HonClimate : public HaierClimateBase {
bool functions_[5];
};

bool beeper_status_;
CleaningState cleaning_status_;
bool got_valid_outdoor_temp_;
esphome::optional<hon_protocol::VerticalSwingMode> pending_vertical_direction_{};
Expand Down
17 changes: 11 additions & 6 deletions components/haier/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@
DisplaySwitch,
icon=ICON_LED_ON,
entity_category=ENTITY_CATEGORY_CONFIG,
default_restore_mode="DISABLED",
),
cv.Optional(CONF_HEALTH_MODE): switch.switch_schema(
HealthModeSwitch,
icon=ICON_LEAF,
default_restore_mode="DISABLED",
),
# Beeper switch is only supported for HonClimate
cv.Optional(CONF_BEEPER): switch.switch_schema(
BeeperSwitch,
icon=ICON_VOLUME_HIGH,
entity_category=ENTITY_CATEGORY_CONFIG,
default_restore_mode="DISABLED",
),
}
)
Expand All @@ -52,14 +56,15 @@
async def to_code(config):
full_id, parent = await cg.get_variable_with_full_id(config[CONF_HAIER_ID])

for switch_type in [CONF_DISPLAY, CONF_HEALTH_MODE]:
if conf := config.get(switch_type):
swtch = await switch.new_switch(conf)
await cg.register_parented(swtch, parent)
# for switch_type in [CONF_DISPLAY, CONF_HEALTH_MODE]:
# if conf := config.get(switch_type):
# sw_var = await switch.new_switch(conf)
# await cg.register_parented(sw_var, parent)
if conf := config.get(CONF_BEEPER):
if full_id.type is HonClimate:
swtch = await switch.new_switch(conf)
await cg.register_parented(swtch, parent)
sw_var = await switch.new_switch(conf)
await cg.register_parented(sw_var, parent)
cg.add(getattr(parent, f"set_beeper_switch")(sw_var))
else:
raise ValueError("Beeper switch is only supported for hon climate")

14 changes: 14 additions & 0 deletions components/haier/switch/beeper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "beeper.h"

namespace esphome {
namespace haier {

void BeeperSwitch::write_state(bool state) {
if (this->parent_->get_beeper_state() != state) {
this->parent_->set_beeper_state(state);
}
this->publish_state(state);
}

} // namespace haier
} // namespace esphome
18 changes: 18 additions & 0 deletions components/haier/switch/beeper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "esphome/components/switch/switch.h"
#include "../hon_climate.h"

namespace esphome {
namespace haier {

class BeeperSwitch : public switch_::Switch, public Parented<HonClimate> {
public:
BeeperSwitch() = default;

protected:
void write_state(bool state) override;
};

} // namespace haier
} // namespace esphome
1 change: 0 additions & 1 deletion configs/climate/haier_hon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
name: ${device_name}
uart_id: ${uart_id}
wifi_signal: ${send_wifi} # Optional, default true, enables WiFI signal transmission from ESP to AC
beeper: true # Optional, default true, disables beep on commands from ESP
visual: # Optional, you can use it to limit min and max temperatures in UI (not working for remote!)
min_temperature: 16 °C
max_temperature: 30 °C
Expand Down
1 change: 0 additions & 1 deletion docs/esphome-docs/climate/haier.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ This component requires a :ref:`uart` to be setup.
name: Haier AC
uart_id: ac_port
wifi_signal: true
beeper: true
display: true
visual:
min_temperature: 16 °C
Expand Down
1 change: 0 additions & 1 deletion docs/examples/max-hon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ climate:
name: Haier hOn Climate
uart_id: haier_uart
wifi_signal: true
beeper: true
visual:
min_temperature: 16 °C
max_temperature: 30 °C
Expand Down
1 change: 0 additions & 1 deletion docs/hon_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Maximum configuration witch will use all possible options will look like this:
name: Haier hOn Climate
uart_id: haier_uart
wifi_signal: true
beeper: true
visual:
min_temperature: 16 °C
max_temperature: 30 °C
Expand Down

0 comments on commit 1a959f1

Please sign in to comment.