12 .bit_mark = GREE_BIT_MARK, .one_space = GREE_ONE_SPACE, .zero_space = GREE_ZERO_SPACE};
14static constexpr uint8_t GREE_SWING_AUTO_MASK = 0x40;
17static constexpr uint8_t GREE_YAW1F_FOLLOWUP_BYTE = 0xA0;
21static constexpr uint8_t supported_feature_mask(
Model model) {
23 return GREE_MODE_BIT_TURBO | GREE_MODE_BIT_LIGHT | GREE_MODE_BIT_XFAN;
25 return GREE_MODE_BIT_LIGHT;
28 return GREE_MODE_BIT_TURBO | GREE_MODE_BIT_LIGHT | GREE_MODE_BIT_HEALTH | GREE_MODE_BIT_XFAN;
31static constexpr uint8_t default_feature_bits(
Model model) {
32 return supported_feature_mask(model) & GREE_MODE_BIT_LIGHT;
35#ifdef USE_GREE_FAHRENHEIT
37 if (!std::isfinite(target_celsius)) {
43 const int desired_f =
static_cast<int>(roundf(clamp<float>(
celsius_to_fahrenheit(target_celsius), MIN_F, MAX_F)));
45 const int tenths_above_min = 10 * desired_f - (10 * 32 + 18 * GREE_TEMP_MIN);
46 return static_cast<uint8_t
>((tenths_above_min + 8) / 9);
50struct GreeFrameSpaces {
55static GreeFrameSpaces frame_spaces(
Model model) {
57 return {GREE_YAC1FB9_HEADER_SPACE, GREE_YAC1FB9_MESSAGE_SPACE};
58 return {GREE_HEADER_SPACE, GREE_MESSAGE_SPACE};
61static void write_frame(remote_base::RemoteTransmitData *data,
const GreeState &frame,
const GreeFrameSpaces &spaces) {
62 data->item(GREE_HEADER_MARK, spaces.header);
63 data->item_bytes<GREE_BIT_TIMINGS>(frame.data(), 4);
65 data->item(GREE_BIT_MARK, GREE_ZERO_SPACE);
66 data->item(GREE_BIT_MARK, GREE_ONE_SPACE);
67 data->item(GREE_BIT_MARK, GREE_ZERO_SPACE);
68 data->item(GREE_BIT_MARK, spaces.message);
69 data->item_bytes<GREE_BIT_TIMINGS>(frame.data() + 4, 4);
70 data->mark(GREE_BIT_MARK);
73#ifdef USE_GREE_RECEIVER
74static constexpr uint8_t GREE_MODE_MASK = 0x07;
75static constexpr uint8_t GREE_POWER_MASK = 0x08;
76static constexpr uint8_t GREE_FAN_MASK = 0x30;
77static constexpr uint8_t GREE_SLEEP_MASK = 0x80;
78static constexpr uint8_t GREE_TEMP_MASK = 0x0F;
79static constexpr uint8_t GREE_TIMER_HALF_HOUR_MASK = 0x10;
80static constexpr uint8_t GREE_TIMER_TENS_HOUR_MASK = 0x60;
81static constexpr uint8_t GREE_TIMER_HOURS_MASK = 0x0F;
82static constexpr uint8_t GREE_BYTE5_FIXED_MASK = 0xB8;
83static constexpr uint8_t GREE_BYTE5_FIXED_VALUE = 0x20;
85static bool read_frame(remote_base::RemoteReceiveData &data,
GreeState &frame,
const GreeFrameSpaces &spaces) {
86 return data.expect_item(GREE_HEADER_MARK, spaces.header) &&
data.expect_bytes<GREE_BIT_TIMINGS>(frame.data(), 4) &&
87 data.expect_item(GREE_BIT_MARK, GREE_ZERO_SPACE) &&
data.expect_item(GREE_BIT_MARK, GREE_ONE_SPACE) &&
88 data.expect_item(GREE_BIT_MARK, GREE_ZERO_SPACE) &&
data.expect_item(GREE_BIT_MARK, spaces.message) &&
89 data.expect_bytes<GREE_BIT_TIMINGS>(frame.data() + 4, 4) &&
data.expect_mark(GREE_BIT_MARK);
94 return state[3] == GREE_YAW1F_FOLLOWUP_BYTE &&
state[7] == GREE_YAW1F_FOLLOWUP_BYTE &&
109 const uint8_t timer_hours =
state[2] & GREE_TIMER_HOURS_MASK;
110 const uint8_t timer_tens_hours = (
state[1] & GREE_TIMER_TENS_HOUR_MASK) >> 5;
111 const bool timer_half_hour =
state[1] & GREE_TIMER_HALF_HOUR_MASK;
113 if (timer_hours > 9 || timer_tens_hours > 2)
115 if (timer_tens_hours < 2)
117 return timer_hours < 4 || (timer_hours == 4 && !timer_half_hour);
120static bool is_valid_vertical_swing(
bool automatic, uint8_t
position) {
129 const bool automatic =
state[0] & GREE_SWING_AUTO_MASK;
130 const uint8_t vertical =
state[4] & 0x0F;
131 const uint8_t horizontal =
state[4] >> 4;
133 if (!is_valid_vertical_swing(automatic, vertical))
136 return horizontal == (automatic ? GREE_HDIR_SWING : GREE_HDIR_MANUAL);
137 return horizontal == GREE_HDIR_MANUAL;
141 if (
mode == GREE_MODE_COOL)
143 if (
mode == GREE_MODE_DRY)
145 if (
mode == GREE_MODE_FAN)
147 if (
mode == GREE_MODE_HEAT)
154 if (fan == GREE_FAN_AUTO)
157 if (fan == GREE_FAN_1)
159 if (fan == GREE_FAN_2)
163 if (fan == GREE_FAN_1)
165 if (fan == GREE_FAN_2)
172 const GreeFrameSpaces spaces = frame_spaces(this->
model_);
176 write_frame(data,
state, spaces);
181 followup[3] = followup[7] = GREE_YAW1F_FOLLOWUP_BYTE;
182 data->space(GREE_YAW1F_FOLLOWUP_SPACE);
183 write_frame(data, followup, spaces);
189#ifdef USE_GREE_RECEIVER
191 const GreeFrameSpaces spaces = frame_spaces(this->
model_);
201 if (!data.expect_space(GREE_YAW1F_FOLLOWUP_SPACE) || !read_frame(data, followup, spaces) ||
202 !is_yaw1f_followup(followup)) {
203 ESP_LOGV(TAG,
"YAW1F frame dropped: second message invalid");
210 if (data.is_valid(1) || (data.is_valid() && data.peek() > 0)) {
211 ESP_LOGV(TAG,
"Frame dropped: extra pulses follow it (receiver idle may be too long)");
223 for (uint8_t
pos = 4;
pos < GREE_STATE_FRAME_SIZE - 1;
pos++)
225 return (sum & 0x0F) << 4;
228#ifdef USE_GREE_RECEIVER
241 const uint8_t
target_temperature = clamp<uint8_t>(data.target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX);
243 state[2] = data.feature_bits & supported_feature_mask(model);
248 state[4] = vertical_swing;
252 state[2] |= GREE_MODEL_A_BIT;
254 state[2] |= GREE_FAN_TURBO_BIT;
255 if (vertical_swing == GREE_VDIR_SWING) {
256 state[0] |= GREE_SWING_AUTO_MASK;
258 state[4] = model ==
GREE_YX1FF ? GREE_YX1FF_VANE_SWING : GREE_VDIR_SWING;
265 if (vertical_swing == GREE_VDIR_SWING || horizontal_swing == GREE_HDIR_SWING)
266 state[0] |= GREE_SWING_AUTO_MASK;
271 state[4] = vertical_swing | (horizontal_swing << 4);
275 if (vertical_swing == GREE_VDIR_SWING)
276 state[0] |= GREE_SWING_AUTO_MASK;
280 state[0] |= GREE_PRESET_SLEEP_BIT;
282 if (data.fahrenheit) {
283 state[3] |= GREE_FAHRENHEIT_BIT;
284 if (data.half_degree)
285 state[3] |= GREE_TEMP_HALF_DEGREE_BIT;
292#ifdef USE_GREE_RECEIVER
302 return GREE_MODE_ON | GREE_MODE_COOL;
304 return GREE_MODE_ON | GREE_MODE_DRY;
306 return GREE_MODE_ON | GREE_MODE_HEAT;
308 return GREE_MODE_ON | GREE_MODE_AUTO;
310 return GREE_MODE_ON | GREE_MODE_FAN;
311 return GREE_MODE_OFF;
322 return GREE_FAN_AUTO;
331 return GREE_FAN_AUTO;
338 return GREE_HDIR_SWING;
340 return GREE_HDIR_MANUAL;
348 return GREE_VDIR_SWING;
350 return GREE_VDIR_MANUAL;
354#ifdef USE_GREE_RECEIVER
356 if (is_auxiliary_frame(model,
state))
359 const uint8_t
mode =
state[0] & GREE_MODE_MASK;
360 const bool power =
state[0] & GREE_POWER_MASK;
361 const uint8_t fan =
state[0] & GREE_FAN_MASK;
367 if (
mode > GREE_MODE_HEAT ||
temperature > GREE_TEMP_MAX - GREE_TEMP_MIN)
370 const uint8_t vertical =
state[4] & 0x0F;
371 const uint8_t horizontal = (
state[4] >> 4) & 0x07;
372 bool swing_vertical =
false;
373 bool swing_horizontal =
false;
380 swing_vertical = is_valid_vertical_swing(
true, vertical);
381 swing_horizontal = horizontal == GREE_HDIR_SWING;
390 swing_vertical = (
state[0] & GREE_SWING_AUTO_MASK) || is_valid_vertical_swing(
true, vertical);
396 .target_temperature =
static_cast<uint8_t
>(GREE_TEMP_MIN +
temperature),
401 .feature_bits =
static_cast<uint8_t
>(
state[2] & supported_feature_mask(model)),
402 .fahrenheit =
static_cast<bool>(
state[3] & GREE_FAHRENHEIT_BIT),
403 .half_degree =
static_cast<bool>(
state[3] & GREE_TEMP_HALF_DEGREE_BIT),
406 if (swing_vertical && swing_horizontal) {
408 }
else if (swing_vertical) {
410 }
else if (swing_horizontal) {
415 data.mode = decode_operation_mode(
mode);
417 data.fan_mode = decode_fan_mode(model, fan);
423 const uint8_t
mode =
state[0] & GREE_MODE_MASK;
424 const bool power =
state[0] & GREE_POWER_MASK;
425 const uint8_t fan =
state[0] & GREE_FAN_MASK;
426 const bool swing =
state[0] & GREE_SWING_AUTO_MASK;
427 const bool turbo =
state[2] & GREE_FAN_TURBO_BIT;
432 if (
mode > GREE_MODE_HEAT ||
temperature > GREE_TEMP_MAX - GREE_TEMP_MIN || !is_valid_timer(
state) ||
433 static_cast<bool>(
state[2] & GREE_MODEL_A_BIT) != power ||
434 (
state[3] & ~(GREE_FAHRENHEIT_BIT | GREE_TEMP_HALF_DEGREE_BIT)) != 0x50 || !is_valid_swing(model,
state) ||
435 (
state[5] & GREE_BYTE5_FIXED_MASK) != GREE_BYTE5_FIXED_VALUE ||
state[6] != 0x00 || (
state[7] & 0x0F) != 0x00 ||
436 (model ==
GREE_YX1FF && turbo && fan != GREE_FAN_3)) {
442 .target_temperature =
static_cast<uint8_t
>(GREE_TEMP_MIN +
temperature),
447 .feature_bits =
static_cast<uint8_t
>(
state[2] & supported_feature_mask(model)),
448 .fahrenheit =
static_cast<bool>(
state[3] & GREE_FAHRENHEIT_BIT),
449 .half_degree =
static_cast<bool>(
state[3] & GREE_TEMP_HALF_DEGREE_BIT),
453 data.mode = decode_operation_mode(
mode);
479 const uint8_t bit = gree_feature_bit(feature);
480 if (!(supported_feature_mask(this->
model_) & bit)) {
481 ESP_LOGW(TAG,
"GREE model %u does not support feature %u",
static_cast<unsigned>(this->
model_),
482 static_cast<unsigned>(feature));
483#ifdef USE_GREE_FEATURE_SWITCH
497#ifdef USE_GREE_FEATURE_SWITCH
508#ifdef USE_GREE_FEATURE_SWITCH
510 climate_ir::ClimateIR::setup();
513 bool restored =
false;
516 if (feature_switch ==
nullptr)
518 if (
auto initial = feature_switch->get_initial_state_with_restore_mode()) {
521 *initial != feature_switch->is_inverted());
538#ifdef USE_GREE_RECEIVER
548 const bool clear_turbo =
552#ifdef USE_GREE_FEATURE_SWITCH
562 .target_temperature =
563 static_cast<uint8_t
>(roundf(clamp<float>(this->
target_temperature, GREE_TEMP_MIN, GREE_TEMP_MAX))),
569#ifdef USE_GREE_FAHRENHEIT
572 climate_data.target_temperature = GREE_TEMP_MIN + (half_steps >> 1);
573 climate_data.fahrenheit =
true;
574 climate_data.half_degree = half_steps & 1;
584#ifdef USE_GREE_RECEIVER
587 if (!
state.has_value())
591 if (!decoded.has_value()) {
592 ESP_LOGD_HEX(TAG,
state->data(),
state->size(), GREE_STATE_FRAME_SIZE,
"GREE frame rejected for model %u: %s",
593 static_cast<unsigned>(this->model_));
597 this->mode = decoded->mode;
598 const int half_steps = 2 * (decoded->target_temperature - GREE_TEMP_MIN) + decoded->half_degree;
599 float target = GREE_TEMP_MIN + 0.5f * half_steps;
601 if (decoded->fahrenheit) {
604 const int fahrenheit_degree = (10 * 32 + 18 * GREE_TEMP_MIN + 9 * half_steps) / 10;
610 this->
preset = decoded->preset;
612#ifdef USE_GREE_FEATURE_SWITCH
BedjetMode mode
BedJet operating mode.
constexpr void insert(ValueType value)
Add a single value to the set (std::set compatibility)
constexpr void erase(ValueType value)
Remove a value from the set (std::set compatibility)
This class is used to encode all control actions on a climate device.
const optional< ClimateFanMode > & get_fan_mode() const
ClimateMode mode
The active mode of the climate device.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
float target_temperature
The target temperature of the climate device.
ClimateSwingMode swing_mode
The active swing mode of the climate device.
void publish_state()
Publish the state of the climate device, to be called from integrations.
optional< ClimatePreset > preset
The active preset of the climate device.
climate::ClimatePresetMask presets_
climate::ClimateFanModeMask fan_modes_
climate::ClimateSwingModeMask swing_modes_
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
static optional< GreeClimateData > decode_legacy(Model model, const GreeState &state)
static GreeState encode(Model model, const GreeClimateData &data)
static uint8_t encode_fan_mode(Model model, climate::ClimateFanMode fan_mode)
static uint8_t encode_operation_mode(climate::ClimateMode mode)
static uint8_t encode_vertical_swing(climate::ClimateSwingMode swing_mode)
static uint8_t encode_horizontal_swing(climate::ClimateSwingMode swing_mode)
static optional< GreeClimateData > decode_model_a(Model model, const GreeState &state)
static optional< GreeClimateData > decode(Model model, const GreeState &state)
Decode a frame whose checksum the protocol layer already checked.
void publish_feature_states_()
void publish_feature_state_(GreeFeature feature)
std::array< switch_::Switch *, 4 > feature_switches_
void control(const climate::ClimateCall &call) override
bool on_receive(remote_base::RemoteReceiveData data) override
void register_feature_switch(GreeFeature feature, switch_::Switch *feature_switch)
void set_feature_state(GreeFeature feature, bool enabled)
void set_model(Model model)
bool get_feature_state(GreeFeature feature) const
void transmit_state() override
static bool valid_checksum(const GreeState &state)
static uint8_t calculate_checksum(const GreeState &state)
void encode(remote_base::RemoteTransmitData *data, const GreeState &state) const
optional< GreeState > decode(remote_base::RemoteReceiveData data) const
bool is_valid(uint32_t offset=0) const
void set_carrier_frequency(uint32_t carrier_frequency)
void reserve(uint32_t len)
RemoteTransmitterBase * transmitter_
Base class for all switches.
ClimateSwingMode swing_mode
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_SLEEP
Device is prepared for sleep.
ClimateSwingMode
Enum for all modes a climate swing can be in NOTE: If adding values, update ClimateSwingModeMask in c...
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
ClimateMode
Enum for all modes a climate device can be in.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
ClimateFanMode
NOTE: If adding values, update ClimateFanModeMask in climate_traits.h to use the new last value.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_QUIET
The fan mode is set to Quiet.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
optional< climate::ClimateFanMode > decode_fan_mode(uint8_t fan_field, optional< climate::ClimateFanMode > current_mode)
Unassigned values keep the current fan mode.
std::array< uint8_t, GREE_STATE_FRAME_SIZE > GreeState
ESPHOME_LOG_TAG(TAG, "gree.climate")
uint8_t fahrenheit_half_steps(float target_celsius)
Return k = 2 * temp_field + half_degree_bit (0..28) for a Celsius target.
std::span< const uint8_t > data
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
A bit is a mark (one_mark or zero_mark, both bit_mark unless set) and a space (one_space or zero_spac...