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()) {
34 ESP_LOGVV(TAG,
"rx overflow, discarding");
36 }
else if (line !=
nullptr && line[0] !=
'\0') {
37 ESP_LOGVV(TAG,
"rx: '%s'", line);
45 if (line[0] !=
'?' || line[1] ==
'\0' || line[2] !=
'=' || line[3] ==
'\0') {
46 ESP_LOGV(TAG,
"Invalid response: %s", line);
50 const char *hex_str = line + 3;
52 uint8_t
value =
static_cast<uint8_t
>(strtol(hex_str, &end_ptr, 16));
53 if (end_ptr == hex_str) {
54 ESP_LOGW(TAG,
"Failed to parse hex value: %s", line);
58 bool state_changed =
false;
80 ESP_LOGV(TAG,
"s-status: 0x%02X (operation=%d)",
value, new_operation);
81 }
else if (
type ==
'x') {
84 if (
value + ENDPOINT_MARGIN >= 100) {
85 new_position = COVER_OPEN;
86 }
else if (
value <= ENDPOINT_MARGIN) {
87 new_position = COVER_CLOSED;
89 new_position =
value / 100.0f;
91 if (this->
position != new_position) {
96 if (this->
target_position_.has_value() && this->pending_command_ ==
nullptr &&
103 ESP_LOGV(TAG,
"x-status: %d%% (position=%.2f)",
value, this->
position);
122 if ((is_open_cmd && this->
position >= COVER_OPEN) || (!is_open_cmd && this->
position <= COVER_CLOSED)) {
126 const LogString *label = is_open_cmd ? LOG_STR(
"open") : LOG_STR(
"close");
128 ESP_LOGW(TAG,
"Gate did not acknowledge %s command after %u retries", LOG_STR_ARG(label), this->
command_retries_);
134 ESP_LOGD(TAG,
"Gate did not respond, resending %s command (retry %u)", LOG_STR_ARG(label), this->
command_retries_);
154 ESP_LOGVV(TAG,
"tx: %s", query);
160 LOG_COVER(
"",
"APC Proteous Cover",
this);
161 LOG_UPDATE_INTERVAL(
this);
166 traits.set_supports_position(
true);
167 traits.set_supports_stop(
true);
168 traits.set_supports_toggle(
true);
173 ESP_LOGVV(TAG,
"tx: %s", cmd);
186 ESP_LOGD(TAG,
"Sending %s command",
199 ESP_LOGD(TAG,
"Cover already idle, ignoring stop command");
202 ESP_LOGD(TAG,
"Sending stop command");
209 if (
call.get_stop()) {
212 float target = *
call.get_position();
213 ESP_LOGD(TAG,
"Target position: %.2f, current: %.2f", target, this->
position);
217 if (target == COVER_OPEN) {
219 }
else if (target == COVER_CLOSED) {
221 }
else if (target != this->
position) {
225 }
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.
const char * partial()
The partial line, NUL terminated, for prompts with no line end; the first N - 1 characters on the byt...
bool overflowed() const
True only right after the feed() that found the line too long; the rest of that line is then dropped.
size_t length() const
Length of the partial line; 0 after a line is returned and while an overlong line is dropped.
char * feed(uint8_t c)
Feed one byte; returns the completed line (empty lines included), valid until the next call,...
void reset()
Forget the partial line and start a new one with the next byte.
void restart_with(uint8_t c)
Start a new line with c, e.g. the byte that overflowed; c must not be TERMINATOR_CHAR or IGNORE_CHAR.
optional< float > target_position_
static constexpr uint32_t COMMAND_ACK_MS
const char * pending_command_
cover::CoverTraits get_traits() override
void dump_config() override
LineReader< MAX_RESPONSE_LEN+1, '\r', '\n'> line_reader_
void stop_cmd_(uint32_t now)
void control(const cover::CoverCall &call) override
void parse_response_(const char *line)
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_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
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.