16static constexpr uint16_t CMD_GET_SETTING_STATUS = 0x0020;
17static constexpr uint16_t CMD_SET_SETTING_STATUS = 0x4020;
18static constexpr uint16_t CMD_GET_OPERATION_MODE = 0x0030;
19static constexpr uint16_t CMD_SET_OPERATION_MODE = 0x4030;
20static constexpr uint16_t CMD_GET_SETPOINT = 0x0040;
21static constexpr uint16_t CMD_SET_SETPOINT = 0x4040;
22static constexpr uint16_t CMD_GET_FAN_SPEED = 0x0050;
23static constexpr uint16_t CMD_SET_FAN_SPEED = 0x4050;
24static constexpr uint16_t CMD_GET_SENSOR_INFORMATION = 0x0110;
25static constexpr uint16_t CMD_SET_FLAG = 0x4000;
30static constexpr uint8_t DEVICE_MODE_HEAT = 4;
32static constexpr uint32_t PUBLISH_DELAY_MS = 800;
37static constexpr std::array<uint16_t, 5> POLL_CMDS{CMD_GET_SETTING_STATUS, CMD_GET_OPERATION_MODE, CMD_GET_SETPOINT,
38 CMD_GET_FAN_SPEED, CMD_GET_SENSOR_INFORMATION};
39static constexpr std::array<uint8_t, 2> NO_ARGS{0x00, 0x00};
41static uint32_t get_command_cooldown(uint16_t cmd) {
43 case CMD_GET_SETTING_STATUS:
44 case CMD_GET_OPERATION_MODE:
45 case CMD_GET_SETPOINT:
46 case CMD_GET_FAN_SPEED:
47 case CMD_GET_SENSOR_INFORMATION:
49 case CMD_SET_SETTING_STATUS:
51 case CMD_SET_OPERATION_MODE:
53 case CMD_SET_SETPOINT:
55 case CMD_SET_FAN_SPEED:
63 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
68 auto mode_opt =
call.get_mode();
69 if (mode_opt.has_value()) {
72 this->
enqueue_query_(CMD_SET_SETTING_STATUS, std::array<uint8_t, 3>{0x20, 0x01, 0});
74 const auto *it = std::find(DEVICE_MODES.begin(), DEVICE_MODES.end(),
mode);
75 if (it == DEVICE_MODES.end()) {
76 ESP_LOGW(TAG,
"Unsupported mode: %d",
mode);
78 const auto device_mode =
static_cast<uint8_t
>(it - DEVICE_MODES.begin());
79 this->
enqueue_query_(CMD_SET_OPERATION_MODE, std::array<uint8_t, 3>{0x20, 0x01, device_mode});
80 this->
enqueue_query_(CMD_SET_SETTING_STATUS, std::array<uint8_t, 3>{0x20, 0x01, 1});
85 if (
auto high =
call.get_target_temperature_high(); high.has_value()) {
88 if (
auto low =
call.get_target_temperature_low(); low.has_value()) {
91 if (!setpoint_args.
empty()) {
94 auto fan_mode_opt =
call.get_fan_mode();
95 if (fan_mode_opt.has_value()) {
96 uint8_t
fan_mode = fan_mode_opt.value();
97 uint8_t fan_mode_out = 255;
112 ESP_LOGW(TAG,
"Unsupported fan mode: %d",
fan_mode);
115 if (fan_mode_out != 255) {
118 std::array<uint8_t, 6>{0x20, 0x01, fan_mode_out, 0x21, 0x01, fan_mode_out});
125 ESP_LOGW(TAG,
"[%s] %s, disconnecting", this->
parent_->
address_str(), LOG_STR_ARG(reason));
132 case ESP_GAP_BLE_NC_REQ_EVT:
133 if (!this->
parent()->check_addr(param->ble_security.key_notif.bd_addr))
135 esp_ble_confirm_reply(param->ble_security.key_notif.bd_addr,
true);
136 ESP_LOGI(TAG,
"ESP_GAP_BLE_NC_REQ_EVT, the passkey Notify number: %06" PRIu32,
137 param->ble_security.key_notif.passkey);
139 case ESP_GAP_BLE_AUTH_CMPL_EVT: {
140 if (!this->
parent()->check_addr(param->ble_security.auth_cmpl.bd_addr))
142 if (!param->ble_security.auth_cmpl.success) {
143 ESP_LOGW(TAG,
"Authentication failed, reason: 0x%x", param->ble_security.auth_cmpl.fail_reason);
149 if (nfy ==
nullptr || wwr ==
nullptr) {
167 esp_ble_gattc_cb_param_t *param) {
169 case ESP_GATTC_DISCONNECT_EVT: {
189 case ESP_GATTC_WRITE_DESCR_EVT:
191 if (param->write.status != ESP_GATT_OK) {
192 ESP_LOGE(TAG,
"Failed writing the notification descriptor, status = 0x%x", param->write.status);
196 case ESP_GATTC_SEARCH_CMPL_EVT: {
202 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
203 if (param->reg_for_notify.handle != this->notify_handle_) {
206 if (param->reg_for_notify.status != ESP_GATT_OK) {
210 this->
node_state = espbt::ClientState::ESTABLISHED;
215 case ESP_GATTC_NOTIFY_EVT: {
216 if (param->notify.handle != this->notify_handle_) {
217 ESP_LOGW(TAG,
"Different notify handle");
229 ESP_LOGV(TAG,
"Got update request...");
230 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
231 ESP_LOGV(TAG,
"...but device is disconnected");
245 ESP_LOGW(TAG,
"Chunk discarded: invalid length");
248 ESP_LOGW(TAG,
"Chunk discarded: invalid chunk id %u", chunk[0]);
251 ESP_LOGW(TAG,
"Message discarded: too long");
259 Query *same =
nullptr;
260 Query *mode_behind =
nullptr;
262 if (queued.cmd == cmd) {
264 }
else if (cmd == CMD_SET_SETTING_STATUS && queued.cmd == CMD_SET_OPERATION_MODE && same !=
nullptr) {
265 mode_behind = &queued;
269 if (same !=
nullptr) {
272 if (mode_behind !=
nullptr) {
273 std::swap(*same, *mode_behind);
279 Query query{.cmd = cmd, .args = {}};
281 std::copy(args.begin(), args.end(), query.args.begin());
282 if (!this->query_queue_.push(query)) {
283 ESP_LOGW(TAG,
"Query queue full, dropping command 0x%04X", cmd);
289 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
304 for (
auto cmd : POLL_CMDS) {
309 const uint16_t cmd = query.
cmd;
310 const bool is_set = (cmd & CMD_SET_FLAG) != 0;
324 static_assert(MAX_QUERY_ARGS + MESSAGE_HEADER_SIZE <= MAX_CHUNK_SIZE - 1,
"A query must fit in one chunk");
326 std::array<uint8_t, MAX_CHUNK_SIZE> frame;
327 const uint8_t msg_len = MESSAGE_HEADER_SIZE + query.
args.
size();
334 std::copy(query.
args.
begin(), query.
args.
end(), frame.begin() + 1 + MESSAGE_HEADER_SIZE);
336 ESP_LOGV(TAG,
"Writing query. ID: 0x%04X, len: %u", query.
cmd, msg_len + 1);
337 ESP_LOGV_HEX(TAG, frame.data(), msg_len + 1, MAX_CHUNK_SIZE,
"%s");
340 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->wwr_handle_,
341 msg_len + 1, frame.data(), ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
343 ESP_LOGW(TAG,
"[%s] Writing command 0x%04X failed, status=%d", this->
parent_->
address_str(), query.
cmd, err);
359 ESP_LOGW(TAG,
"Discarding message: invalid length");
363 ESP_LOGV(TAG,
"Parsing message. ID: 0x%04X, len: %u",
message_function_id(msg), msg.size());
364 ESP_LOGV_HEX(TAG, msg.data(), msg.size(), 256,
"%s");
367 case CMD_GET_SETTING_STATUS: {
374 case CMD_GET_OPERATION_MODE: {
381 case CMD_GET_SETPOINT: {
382 if (
auto high =
find_arg(msg, 0x20); high.size() == 2) {
385 if (
auto low =
find_arg(msg, 0x21); low.size() == 2) {
390 case CMD_GET_FAN_SPEED: {
393 if (
value.size() != 1) {
416 case CMD_GET_SENSOR_INFORMATION: {
void status_set_warning()
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void status_clear_warning()
espbt::ClientState node_state
This class is used to encode all control actions on a climate device.
ClimateMode mode
The active mode of the climate device.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
float current_temperature
The current temperature of the climate device, as reported from the integration.
void publish_state()
Publish the state of the climate device, to be called from integrations.
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
void update_mode_from_status_()
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
StaticRingBuffer< Query, QUERY_QUEUE_SIZE > query_queue_
void enqueue_query_(uint16_t cmd, std::span< const uint8_t > args)
MessageAssembler assembler_
void control(const climate::ClimateCall &call) override
void dump_config() override
static constexpr uint32_t QUERY_TIMEOUT_ID
bool write_query_(const Query &query)
void process_incoming_chunk_(std::span< const uint8_t > chunk)
void fail_connection_(const LogString *reason)
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
void parse_cb_(std::span< const uint8_t > msg)
static constexpr uint32_t PUBLISH_TIMEOUT_ID
ChunkResult feed(std::span< const uint8_t > chunk)
std::span< const uint8_t > message() const
uint8_t * get_remote_bda()
const char * address_str() const
esp_err_t register_for_notify(uint16_t char_handle)
Register for notifications, holding the service release until the registration completes.
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
void disconnect() override
ESPHOME_LOG_TAG(TAG, "climate")
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.
@ 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_HIGH
The fan mode is set to High.
bool is_valid_message(std::span< const uint8_t > msg)
True when the length byte matches and the header (length, reserved, function id) is present.
@ CHUNK_RESULT_INVALID_ID
@ CHUNK_RESULT_INVALID_LENGTH
void append_setpoint(QueryArgs &args, uint8_t id, float temperature)
Appends a setpoint record {id, 2, msb, lsb}, clamped to the supported range.
std::span< const uint8_t > find_arg(std::span< const uint8_t > msg, uint8_t id)
Returns the value of argument id, or an empty span when it is absent, has no value or the records are...
uint16_t message_function_id(std::span< const uint8_t > msg)
bool merge_args(QueryArgs &args, std::span< const uint8_t > incoming)
Merges the records {id, len, value...} of incoming into args: a record whose id is already present is...
float decode_setpoint(std::span< const uint8_t > value)
Decodes a two byte setpoint value; the caller checks the size.
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).