Skip to content

Commit

Permalink
Format adjustments to align with ESPHome requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed Dec 30, 2023
1 parent 2cbaa64 commit 36436d1
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 156 deletions.
11 changes: 4 additions & 7 deletions components/haier/binary_sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
ICON_RADIATOR,
)
from ..climate import (
CONF_HAIER_ID,
HonClimate,
CONF_HAIER_ID,
HonClimate,
)

BinarySensorTypeEnum = HonClimate.enum("SubBinarySensorType", True)
Expand Down Expand Up @@ -57,11 +57,8 @@
{
cv.Required(CONF_HAIER_ID): cv.use_id(HonClimate),
}
).extend(
{
cv.Optional(type): schema for type, schema in SENSOR_TYPES.items()
}
)
).extend({cv.Optional(type): schema for type, schema in SENSOR_TYPES.items()})


async def to_code(config):
paren = await cg.get_variable(config[CONF_HAIER_ID])
Expand Down
130 changes: 70 additions & 60 deletions components/haier/hon_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ hon_protocol::HorizontalSwingMode get_horizontal_swing_mode(AirflowHorizontalDir
}

HonClimate::HonClimate()
: cleaning_status_(CleaningState::NO_CLEANING),
got_valid_outdoor_temp_(false),
active_alarms_{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} {
: cleaning_status_(CleaningState::NO_CLEANING), got_valid_outdoor_temp_(false), active_alarms_{0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00} {
last_status_message_ = std::unique_ptr<uint8_t[]>(new uint8_t[sizeof(hon_protocol::HaierPacketControl)]);
this->fan_mode_speed_ = (uint8_t) hon_protocol::FanMode::FAN_MID;
this->other_modes_fan_speed_ = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
Expand Down Expand Up @@ -367,10 +367,12 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_USER_DATA);
static const haier_protocol::HaierMessage BIG_DATA_REQUEST(
haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_BIG_DATA);
if ((this->protocol_phase_ == ProtocolPhases::SENDING_FIRST_STATUS_REQUEST) || (!this->should_get_big_data_()))
if ((this->protocol_phase_ == ProtocolPhases::SENDING_FIRST_STATUS_REQUEST) ||
(!this->should_get_big_data_())) {
this->send_message_(STATUS_REQUEST, this->use_crc_);
else
} else {
this->send_message_(BIG_DATA_REQUEST, this->use_crc_);
}
this->last_status_request_ = now;
}
break;
Expand Down Expand Up @@ -689,75 +691,82 @@ void HonClimate::process_alarm_message_(const uint8_t *packet, uint8_t size, boo

#ifdef USE_SENSOR
void HonClimate::set_sub_sensor(SubSensorType type, sensor::Sensor *sens) {
if (type < SubSensorType::__SUB_SENSOR_TYPE_COUNT) {
if (type >= SubSensorType::__BIG_DATA_FRAME_SUB_SENSORS) {
if ((this->sub_sensors_[(size_t)type] != nullptr) && (sens == nullptr))
if (type < SubSensorType::SUB_SENSOR_TYPE_COUNT) {
if (type >= SubSensorType::BIG_DATA_FRAME_SUB_SENSORS) {
if ((this->sub_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
this->big_data_sensors_--;
else if ((this->sub_sensors_[(size_t)type] == nullptr) && (sens != nullptr))
} else if ((this->sub_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
this->big_data_sensors_++;
}
}
this->sub_sensors_[(size_t)type] = sens;
this->sub_sensors_[(size_t) type] = sens;
}
}

void HonClimate::update_sub_sensor(SubSensorType type, float value) {
if (type < SubSensorType::__SUB_SENSOR_TYPE_COUNT) {
size_t _index = (size_t)type;
if ((this->sub_sensors_[_index] != nullptr) && ((!this->sub_sensors_[_index]->has_state()) || (this->sub_sensors_[_index]->raw_state != value)))
this->sub_sensors_[_index]->publish_state(value);
void HonClimate::update_sub_sensor_(SubSensorType type, float value) {
if (type < SubSensorType::SUB_SENSOR_TYPE_COUNT) {
size_t index = (size_t) type;
if ((this->sub_sensors_[index] != nullptr) &&
((!this->sub_sensors_[index]->has_state()) || (this->sub_sensors_[index]->raw_state != value)))
this->sub_sensors_[index]->publish_state(value);
}
}
#endif // USE_SENSOR
#endif // USE_SENSOR

#ifdef USE_BINARY_SENSOR
void HonClimate::set_sub_binary_sensor(SubBinarySensorType type, binary_sensor::BinarySensor *sens) {
if (type < SubBinarySensorType::__SUB_BINARY_SENSOR_TYPE_COUNT) {
if ((this->sub_binary_sensors_[(size_t)type] != nullptr) && (sens == nullptr))
if (type < SubBinarySensorType::SUB_BINARY_SENSOR_TYPE_COUNT) {
if ((this->sub_binary_sensors_[(size_t) type] != nullptr) && (sens == nullptr)) {
this->big_data_sensors_--;
else if ((this->sub_binary_sensors_[(size_t)type] == nullptr) && (sens != nullptr))
} else if ((this->sub_binary_sensors_[(size_t) type] == nullptr) && (sens != nullptr)) {
this->big_data_sensors_++;
this->sub_binary_sensors_[(size_t)type] = sens;
}
this->sub_binary_sensors_[(size_t) type] = sens;
}
}

void HonClimate::update_sub_binary_sensor(SubBinarySensorType type, uint8_t value) {
if (value < 2) {
void HonClimate::update_sub_binary_sensor_(SubBinarySensorType type, uint8_t value) {
if (value < 2) {
bool converted_value = value == 1;
size_t _index = (size_t)type;
if ((this->sub_binary_sensors_[_index] != nullptr) && ((!this->sub_binary_sensors_[_index]->has_state()) || (this->sub_binary_sensors_[_index]->state != converted_value)))
this->sub_binary_sensors_[_index]->publish_state(converted_value);
size_t index = (size_t) type;
if ((this->sub_binary_sensors_[index] != nullptr) && ((!this->sub_binary_sensors_[index]->has_state()) ||
(this->sub_binary_sensors_[index]->state != converted_value)))
this->sub_binary_sensors_[index]->publish_state(converted_value);
}
}
#endif // USE_BINARY_SENSOR
#endif // USE_BINARY_SENSOR

haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) {

size_t expected_size = 2 + sizeof(hon_protocol::HaierPacketControl) + sizeof(hon_protocol::HaierPacketSensors) + this->extra_control_packet_bytes_;
size_t expected_size = 2 + sizeof(hon_protocol::HaierPacketControl) + sizeof(hon_protocol::HaierPacketSensors) +
this->extra_control_packet_bytes_;
if (size < expected_size)
return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
uint16_t subtype = (((uint16_t)packet_buffer[0]) << 8) + packet_buffer[1];
uint16_t subtype = (((uint16_t) packet_buffer[0]) << 8) + packet_buffer[1];
if ((subtype == 0x7D01) && (size >= expected_size + 4 + sizeof(hon_protocol::HaierPacketBigData))) {
// Got BigData packet
const hon_protocol::HaierPacketBigData* bd_packet = (const hon_protocol::HaierPacketBigData*)(&packet_buffer[expected_size + 4]);
const hon_protocol::HaierPacketBigData *bd_packet =
(const hon_protocol::HaierPacketBigData *) (&packet_buffer[expected_size + 4]);
#ifdef USE_SENSOR
this->update_sub_sensor(SubSensorType::INDOOR_COIL_TEMPERATURE, bd_packet->indoor_coil_temperature / 2 - 20);
this->update_sub_sensor(SubSensorType::OUTDOOR_COIL_TEMPERATURE, bd_packet->outdoor_coil_temperature - 64);
this->update_sub_sensor(SubSensorType::OUTDOOR_DEFROST_TEMPERATURE, bd_packet->outdoor_coil_temperature - 64);
this->update_sub_sensor(SubSensorType::OUTDOOR_IN_AIR_TEMPERATURE, bd_packet->outdoor_in_air_temperature - 64);
this->update_sub_sensor(SubSensorType::OUTDOOR_OUT_AIR_TEMPERATURE, bd_packet->outdoor_out_air_temperature - 64);
this->update_sub_sensor(SubSensorType::POWER, UINT16_BE(bd_packet->power));
this->update_sub_sensor(SubSensorType::COMPRESSOR_FREQUENCY, bd_packet->compressor_frequency);
this->update_sub_sensor(SubSensorType::COMPRESSOR_CURRENT, UINT16_BE(bd_packet->compressor_current) / 10.0);
this->update_sub_sensor(SubSensorType::EXPANSION_VALVE_OPEN_DEGREE, UINT16_BE(bd_packet->expansion_valve_open_degree) / 4095.0);
#endif // USE_SENSOR
this->update_sub_sensor_(SubSensorType::INDOOR_COIL_TEMPERATURE, bd_packet->indoor_coil_temperature / 2.0 - 20);
this->update_sub_sensor_(SubSensorType::OUTDOOR_COIL_TEMPERATURE, bd_packet->outdoor_coil_temperature - 64);
this->update_sub_sensor_(SubSensorType::OUTDOOR_DEFROST_TEMPERATURE, bd_packet->outdoor_coil_temperature - 64);
this->update_sub_sensor_(SubSensorType::OUTDOOR_IN_AIR_TEMPERATURE, bd_packet->outdoor_in_air_temperature - 64);
this->update_sub_sensor_(SubSensorType::OUTDOOR_OUT_AIR_TEMPERATURE, bd_packet->outdoor_out_air_temperature - 64);
this->update_sub_sensor_(SubSensorType::POWER, UINT16_BE(bd_packet->power));
this->update_sub_sensor_(SubSensorType::COMPRESSOR_FREQUENCY, bd_packet->compressor_frequency);
this->update_sub_sensor_(SubSensorType::COMPRESSOR_CURRENT, UINT16_BE(bd_packet->compressor_current) / 10.0);
this->update_sub_sensor_(SubSensorType::EXPANSION_VALVE_OPEN_DEGREE,
UINT16_BE(bd_packet->expansion_valve_open_degree) / 4095.0);
#endif // USE_SENSOR
#ifdef USE_BINARY_SENSOR
this->update_sub_binary_sensor(SubBinarySensorType::OUTDOOR_FAN_STATUS, bd_packet->outdoor_fan_status);
this->update_sub_binary_sensor(SubBinarySensorType::DEFROST_STATUS, bd_packet->defrost_status);
this->update_sub_binary_sensor(SubBinarySensorType::COMPRESSOR_STATUS, bd_packet->compressor_status);
this->update_sub_binary_sensor(SubBinarySensorType::INDOOR_FAN_STATUS, bd_packet->indoor_fan_status);
this->update_sub_binary_sensor(SubBinarySensorType::FOUR_WAY_VALVE_STATUS, bd_packet->four_way_valve_status);
this->update_sub_binary_sensor(SubBinarySensorType::INDOOR_ELECTRIC_HEATING_STATUS, bd_packet->indoor_electric_heating_status);
#endif // USE_BINARY_SENSOR
this->update_sub_binary_sensor_(SubBinarySensorType::OUTDOOR_FAN_STATUS, bd_packet->outdoor_fan_status);
this->update_sub_binary_sensor_(SubBinarySensorType::DEFROST_STATUS, bd_packet->defrost_status);
this->update_sub_binary_sensor_(SubBinarySensorType::COMPRESSOR_STATUS, bd_packet->compressor_status);
this->update_sub_binary_sensor_(SubBinarySensorType::INDOOR_FAN_STATUS, bd_packet->indoor_fan_status);
this->update_sub_binary_sensor_(SubBinarySensorType::FOUR_WAY_VALVE_STATUS, bd_packet->four_way_valve_status);
this->update_sub_binary_sensor_(SubBinarySensorType::INDOOR_ELECTRIC_HEATING_STATUS,
bd_packet->indoor_electric_heating_status);
#endif // USE_BINARY_SENSOR
}
struct {
hon_protocol::HaierPacketControl control;
Expand All @@ -771,15 +780,16 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
ESP_LOGW(TAG, "HVAC error, code=0x%02X", packet.sensors.error_status);
}
#ifdef USE_SENSOR
if ((this->sub_sensors_[(size_t)SubSensorType::OUTDOOR_TEMPERATURE] != nullptr) &&
if ((this->sub_sensors_[(size_t) SubSensorType::OUTDOOR_TEMPERATURE] != nullptr) &&
(this->got_valid_outdoor_temp_ || (packet.sensors.outdoor_temperature > 0))) {
this->got_valid_outdoor_temp_ = true;
this->update_sub_sensor(SubSensorType::OUTDOOR_TEMPERATURE, (float) (packet.sensors.outdoor_temperature + PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET));
this->update_sub_sensor_(SubSensorType::OUTDOOR_TEMPERATURE,
(float) (packet.sensors.outdoor_temperature + PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET));
}
if ((this->sub_sensors_[(size_t)SubSensorType::HUMIDITY] != nullptr) && (packet.sensors.room_humidity <= 100)) {
this->update_sub_sensor(SubSensorType::HUMIDITY, (float) packet.sensors.room_humidity);
if ((this->sub_sensors_[(size_t) SubSensorType::HUMIDITY] != nullptr) && (packet.sensors.room_humidity <= 100)) {
this->update_sub_sensor_(SubSensorType::HUMIDITY, (float) packet.sensors.room_humidity);
}
#endif // USE_SENSOR
#endif // USE_SENSOR
bool should_publish = false;
{
// Extra modes/presets
Expand Down Expand Up @@ -1193,21 +1203,21 @@ bool HonClimate::prepare_pending_action() {

void HonClimate::process_protocol_reset() {
HaierClimateBase::process_protocol_reset();
#ifdef USE_SENSOR
for (size_t index = 0; index < (size_t) SubSensorType::__SUB_SENSOR_TYPE_COUNT; index++) {
if ((this->sub_sensors_[index] != nullptr) && this->sub_sensors_[index]->has_state())
this->sub_sensors_[index]->publish_state(NAN);
#ifdef USE_SENSOR
for (auto &sub_sensor : this->sub_sensors_) {
if ((sub_sensor != nullptr) && sub_sensor->has_state())
sub_sensor->publish_state(NAN);
}
#endif // USE_SENSOR
#endif // USE_SENSOR
this->got_valid_outdoor_temp_ = false;
this->hvac_hardware_info_.reset();
}

bool HonClimate::should_get_big_data_() {
if (this->big_data_sensors_ > 0) {
static uint8_t _counter = 0;
_counter = (_counter + 1) % 3;
return _counter == 1;
static uint8_t counter = 0;
counter = (counter + 1) % 3;
return counter == 1;
}
return false;
}
Expand Down
26 changes: 14 additions & 12 deletions components/haier/hon_climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ enum class HonControlMethod { MONITOR_ONLY = 0, SET_GROUP_PARAMETERS, SET_SINGLE

class HonClimate : public HaierClimateBase {
#ifdef USE_SENSOR
public:
public:
enum class SubSensorType {
// Used data based sensors
OUTDOOR_TEMPERATURE = 0,
Expand All @@ -55,31 +55,33 @@ class HonClimate : public HaierClimateBase {
COMPRESSOR_FREQUENCY,
COMPRESSOR_CURRENT,
EXPANSION_VALVE_OPEN_DEGREE,
__SUB_SENSOR_TYPE_COUNT,
__BIG_DATA_FRAME_SUB_SENSORS = INDOOR_COIL_TEMPERATURE,
SUB_SENSOR_TYPE_COUNT,
BIG_DATA_FRAME_SUB_SENSORS = INDOOR_COIL_TEMPERATURE,
};
void set_sub_sensor(SubSensorType type, sensor::Sensor *sens);
protected:
void update_sub_sensor(SubSensorType type, float value);
sensor::Sensor *sub_sensors_[(size_t) SubSensorType::__SUB_SENSOR_TYPE_COUNT]{nullptr};

protected:
void update_sub_sensor_(SubSensorType type, float value);
sensor::Sensor *sub_sensors_[(size_t) SubSensorType::SUB_SENSOR_TYPE_COUNT]{nullptr};
#endif
#ifdef USE_BINARY_SENSOR
public:
public:
enum class SubBinarySensorType {
OUTDOOR_FAN_STATUS = 0,
DEFROST_STATUS,
COMPRESSOR_STATUS,
INDOOR_FAN_STATUS,
FOUR_WAY_VALVE_STATUS,
INDOOR_ELECTRIC_HEATING_STATUS,
__SUB_BINARY_SENSOR_TYPE_COUNT,
SUB_BINARY_SENSOR_TYPE_COUNT,
};
void set_sub_binary_sensor(SubBinarySensorType type, binary_sensor::BinarySensor *sens);
protected:
void update_sub_binary_sensor(SubBinarySensorType type, uint8_t value);
binary_sensor::BinarySensor *sub_binary_sensors_[(size_t) SubBinarySensorType::__SUB_BINARY_SENSOR_TYPE_COUNT]{nullptr};

protected:
void update_sub_binary_sensor_(SubBinarySensorType type, uint8_t value);
binary_sensor::BinarySensor *sub_binary_sensors_[(size_t) SubBinarySensorType::SUB_BINARY_SENSOR_TYPE_COUNT]{nullptr};
#endif
public:
public:
HonClimate();
HonClimate(const HonClimate &) = delete;
HonClimate &operator=(const HonClimate &) = delete;
Expand Down
22 changes: 11 additions & 11 deletions components/haier/hon_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,35 +140,35 @@ struct HaierPacketSensors {

struct HaierPacketBigData {
// 29
uint8_t power[2]; // AC power consumption (0W - 65535W, 1W step)
uint8_t power[2]; // AC power consumption (0W - 65535W, 1W step)
// 31
uint8_t indoor_coil_temperature; // 0.5°C step, -20°C offset (0=-20°C)
// 32
uint8_t outdoor_out_air_temperature; // 1°C step, -64°C offset (0=-64°C)
// 33
uint8_t outdoor_coil_temperature; // 1°C step, -64°C offset (0=-64°C)
uint8_t outdoor_coil_temperature; // 1°C step, -64°C offset (0=-64°C)
// 34
uint8_t outdoor_in_air_temperature; // 1°C step, -64°C offset (0=-64°C)
// 35
uint8_t outdoor_defrost_temperature; // 1°C step, -64°C offset (0=-64°C)
// 36
uint8_t compressor_frequency; // 1Hz step, 0Hz - 127Hz
uint8_t compressor_frequency; // 1Hz step, 0Hz - 127Hz
// 37
uint8_t compressor_current[2]; // 0.1A step, 0.0A - 51.1A (0x0000 - 0x01FF)
// 39
uint8_t outdoor_fan_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t defrost_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t outdoor_fan_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t defrost_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t : 0;
// 40
uint8_t compressor_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t indoor_fan_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t four_way_valve_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t indoor_electric_heating_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t compressor_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t indoor_fan_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t four_way_valve_status : 2; // 0 - off, 1 - on, 2 - information not available
uint8_t indoor_electric_heating_status : 2; // 0 - off, 1 - on, 2 - information not available
// 41
uint8_t expansion_valve_open_degree[2]; // 0 - 4095
uint8_t expansion_valve_open_degree[2]; // 0 - 4095
};

#define UINT16_BE(x) (((uint16_t)((x)[0]) << 8) + (x)[1])
#define UINT16_BE(x) (((uint16_t) ((x)[0]) << 8) + (x)[1])

struct DeviceVersionAnswer {
char protocol_version[8];
Expand Down
8 changes: 2 additions & 6 deletions components/haier/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
STATE_CLASS_MEASUREMENT,
UNIT_AMPERE,
UNIT_CELSIUS,
UNIT_CELSIUS,
UNIT_HERTZ,
UNIT_PERCENT,
UNIT_WATT,
Expand Down Expand Up @@ -139,11 +138,8 @@
{
cv.Required(CONF_HAIER_ID): cv.use_id(HonClimate),
}
).extend(
{
cv.Optional(type): schema for type, schema in SENSOR_TYPES.items()
}
)
).extend({cv.Optional(type): schema for type, schema in SENSOR_TYPES.items()})


async def to_code(config):
paren = await cg.get_variable(config[CONF_HAIER_ID])
Expand Down
10 changes: 0 additions & 10 deletions configs/binary_sensor/compressor_status.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
binary_sensor:
- platform: haier
haier_id: ${device_id}
outdoor_fan_status:
name: ${device_name} Outdoor Fan Status
defrost_status:
name: ${device_name} Defrost Status
compressor_status:
name: ${device_name} Compressor Status
indoor_fan_status:
name: ${device_name} Indoor Fan Status
four_way_valve_status:
name: ${device_name} Four-way Valve Status
indoor_electric_heating_status:
name: ${device_name} Indoor Electric Heating Status
10 changes: 0 additions & 10 deletions configs/binary_sensor/defrost_status.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
binary_sensor:
- platform: haier
haier_id: ${device_id}
outdoor_fan_status:
name: ${device_name} Outdoor Fan Status
defrost_status:
name: ${device_name} Defrost Status
compressor_status:
name: ${device_name} Compressor Status
indoor_fan_status:
name: ${device_name} Indoor Fan Status
four_way_valve_status:
name: ${device_name} Four-way Valve Status
indoor_electric_heating_status:
name: ${device_name} Indoor Electric Heating Status
Loading

0 comments on commit 36436d1

Please sign in to comment.