8static constexpr const char *START_CMD =
"*1\r";
9static constexpr const char *OPEN_CMD =
"*6\r";
10static constexpr const char *CLOSE_CMD =
"*7\r";
11static constexpr const char *QUERY_S =
"?s\r";
12static constexpr const char *QUERY_X =
"?x\r";
16static constexpr uint8_t ENDPOINT_MARGIN = 2;
22 if (restore.has_value()) {
42 }
else if (data !=
'\n') {
44 ESP_LOGVV(TAG,
"rx overflow, discarding");
55 ESP_LOGV(TAG,
"Invalid response: %s", this->
rx_buffer_);
61 uint8_t value =
static_cast<uint8_t
>(strtol(hex_str, &end_ptr, 16));
62 if (end_ptr == hex_str) {
63 ESP_LOGW(TAG,
"Failed to parse hex value: %s", this->
rx_buffer_);
67 bool state_changed =
false;
89 ESP_LOGV(TAG,
"s-status: 0x%02X (operation=%d)", value, new_operation);
90 }
else if (
type ==
'x') {
93 if (value + ENDPOINT_MARGIN >= 100) {
94 new_position = COVER_OPEN;
95 }
else if (value <= ENDPOINT_MARGIN) {
96 new_position = COVER_CLOSED;
98 new_position = value / 100.0f;
100 if (this->
position != new_position) {
102 state_changed =
true;
105 if (this->
target_position_.has_value() && this->pending_command_ ==
nullptr &&
112 ESP_LOGV(TAG,
"x-status: %d%% (position=%.2f)", value, this->
position);
131 if ((is_open_cmd && this->
position >= COVER_OPEN) || (!is_open_cmd && this->
position <= COVER_CLOSED)) {
135 const LogString *label = is_open_cmd ? LOG_STR(
"open") : LOG_STR(
"close");
137 ESP_LOGW(TAG,
"Gate did not acknowledge %s command after %u retries", LOG_STR_ARG(label), this->
command_retries_);
143 ESP_LOGD(TAG,
"Gate did not respond, resending %s command (retry %u)", LOG_STR_ARG(label), this->
command_retries_);
159 ESP_LOGVV(TAG,
"discarding stale rx '%s' before query", this->
rx_buffer_);
164 ESP_LOGVV(TAG,
"tx: %s", query);
170 LOG_COVER(
"",
"APC Proteous Cover",
this);
171 LOG_UPDATE_INTERVAL(
this);
176 traits.set_supports_position(
true);
177 traits.set_supports_stop(
true);
178 traits.set_supports_toggle(
true);
183 ESP_LOGVV(TAG,
"tx: %s", cmd);
196 ESP_LOGD(TAG,
"Sending %s command",
209 ESP_LOGD(TAG,
"Cover already idle, ignoring stop command");
212 ESP_LOGD(TAG,
"Sending stop command");
219 if (
call.get_stop()) {
222 float target = *
call.get_position();
223 ESP_LOGD(TAG,
"Target position: %.2f, current: %.2f", target, this->
position);
227 if (target == COVER_OPEN) {
229 }
else if (target == COVER_CLOSED) {
231 }
else if (target != this->
position) {
235 }
else if (
call.get_toggle()) {
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
optional< float > target_position_
static constexpr uint32_t COMMAND_ACK_MS
const char * pending_command_
char rx_buffer_[MAX_RESPONSE_LEN+1]
cover::CoverTraits get_traits() override
void dump_config() override
void stop_cmd_(uint32_t now)
static constexpr size_t MAX_RESPONSE_LEN
void control(const cover::CoverCall &call) override
void retry_pending_command_(uint32_t now)
void write_command_(const char *cmd, uint32_t now)
uint32_t pending_command_time_
void send_command_(const char *cmd)
uint32_t last_command_tx_
static constexpr uint8_t MAX_COMMAND_RETRIES
static constexpr uint32_t COMMAND_QUIET_MS
const optional< float > & get_position() const
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
optional< CoverRestoreState > restore_state_()
void publish_state(bool save=true)
Publish the current state of the cover.
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
void write_str(const char *str)
bool read_byte(uint8_t *data)
ESPHOME_LOG_TAG(TAG, "apc_proteous.cover")
CoverOperation
Enum encoding the current operation of a cover.
@ COVER_OPERATION_OPENING
The cover is currently opening.
@ COVER_OPERATION_CLOSING
The cover is currently closing.
@ COVER_OPERATION_IDLE
The cover is currently idle (not moving)
Application App
Global storage of Application pointer - only one Application can exist.