mirror of
https://github.com/esphome/esphome.git
synced 2026-11-15 01:25:57 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b32e3cd38b | ||
|
|
1ed510259a | ||
|
|
8663ec4066 | ||
|
|
65f6ee9f8d | ||
|
|
a0124ef6db | ||
|
|
38bbaa3d51 |
No files matched your search
@@ -144,7 +144,6 @@ CONF_HOMEASSISTANT_SERVICES = "homeassistant_services"
|
||||
CONF_HOMEASSISTANT_STATES = "homeassistant_states"
|
||||
CONF_LISTEN_BACKLOG = "listen_backlog"
|
||||
CONF_MAX_SEND_QUEUE = "max_send_queue"
|
||||
CONF_ON_CLIENT_SYNCED = "on_client_synced"
|
||||
CONF_OUTGOING_CONNECTION = "outgoing_connection"
|
||||
CONF_STATE_SUBSCRIPTION_ONLY = "state_subscription_only"
|
||||
|
||||
@@ -454,9 +453,6 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.Optional(CONF_ON_CLIENT_DISCONNECTED): automation.validate_automation(
|
||||
single=True
|
||||
),
|
||||
cv.Optional(CONF_ON_CLIENT_SYNCED): automation.validate_automation(
|
||||
single=True
|
||||
),
|
||||
# Connection limits to prevent memory exhaustion on resource-constrained devices
|
||||
# Each connection uses ~500-1000 bytes of RAM plus system resources
|
||||
# Platform defaults based on available RAM and network stack implementation:
|
||||
@@ -710,34 +706,21 @@ async def to_code(config: ConfigType) -> None:
|
||||
if (wizard_config := config.get(wizard.CONF_WIZARD)) is not None:
|
||||
await wizard.to_code(wizard_config)
|
||||
|
||||
for conf_key, define, callback_method in (
|
||||
(
|
||||
CONF_ON_CLIENT_CONNECTED,
|
||||
"USE_API_CLIENT_CONNECTED_TRIGGER",
|
||||
"set_on_client_connected_callback",
|
||||
),
|
||||
(
|
||||
CONF_ON_CLIENT_DISCONNECTED,
|
||||
"USE_API_CLIENT_DISCONNECTED_TRIGGER",
|
||||
"set_on_client_disconnected_callback",
|
||||
),
|
||||
(
|
||||
CONF_ON_CLIENT_SYNCED,
|
||||
"USE_API_CLIENT_SYNCED_TRIGGER",
|
||||
"set_on_client_synced_callback",
|
||||
),
|
||||
):
|
||||
if (automation_conf := config.get(conf_key)) is not None:
|
||||
cg.add_define(define)
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
callback_method,
|
||||
automation.owning_args(
|
||||
[(cg.StringRef, "client_info"), (cg.StringRef, "client_address")],
|
||||
automation_conf,
|
||||
),
|
||||
automation_conf,
|
||||
)
|
||||
if CONF_ON_CLIENT_CONNECTED in config:
|
||||
cg.add_define("USE_API_CLIENT_CONNECTED_TRIGGER")
|
||||
await automation.build_automation(
|
||||
var.get_client_connected_trigger(),
|
||||
[(cg.std_string, "client_info"), (cg.std_string, "client_address")],
|
||||
config[CONF_ON_CLIENT_CONNECTED],
|
||||
)
|
||||
|
||||
if CONF_ON_CLIENT_DISCONNECTED in config:
|
||||
cg.add_define("USE_API_CLIENT_DISCONNECTED_TRIGGER")
|
||||
await automation.build_automation(
|
||||
var.get_client_disconnected_trigger(),
|
||||
[(cg.std_string, "client_info"), (cg.std_string, "client_address")],
|
||||
config[CONF_ON_CLIENT_DISCONNECTED],
|
||||
)
|
||||
|
||||
if (encryption_config := config.get(CONF_ENCRYPTION, None)) is not None:
|
||||
if key := encryption_config.get(CONF_KEY):
|
||||
|
||||
@@ -251,9 +251,6 @@ void APIConnection::destroy_active_iterator_() {
|
||||
this->iterator_storage_.initial_state.~InitialStateIterator();
|
||||
break;
|
||||
case ActiveIterator::NONE:
|
||||
#ifdef USE_API_CLIENT_SYNCED_TRIGGER
|
||||
case ActiveIterator::CLIENT_SYNCED_PENDING:
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
this->active_iterator_ = ActiveIterator::NONE;
|
||||
@@ -418,25 +415,10 @@ void APIConnection::process_active_iterator_() {
|
||||
} else {
|
||||
this->process_iterator_batch_(this->iterator_storage_.list_entities);
|
||||
}
|
||||
#ifdef USE_API_CLIENT_SYNCED_TRIGGER
|
||||
} else if (this->active_iterator_ == ActiveIterator::CLIENT_SYNCED_PENDING) {
|
||||
// Fired after the sync, not on SubscribeStates, so the subscriptions sent with it are active.
|
||||
// Waits for the batch to drain so a direct send from the automation cannot overtake queued states.
|
||||
// next_close: a DisconnectRequest this pass already answered; remove is only set next loop.
|
||||
if (this->deferred_batch_.empty() && !this->flags_.remove && !this->flags_.next_close) {
|
||||
this->active_iterator_ = ActiveIterator::NONE;
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
this->parent_->get_client_synced_callback().call(StringRef(this->helper_->get_client_name()),
|
||||
StringRef(this->helper_->get_peername_to(peername)));
|
||||
}
|
||||
#endif
|
||||
} else { // INITIAL_STATE
|
||||
if (this->iterator_storage_.initial_state.completed()) {
|
||||
this->destroy_active_iterator_();
|
||||
this->finalize_iterator_sync_();
|
||||
#ifdef USE_API_CLIENT_SYNCED_TRIGGER
|
||||
this->active_iterator_ = ActiveIterator::CLIENT_SYNCED_PENDING;
|
||||
#endif
|
||||
} else {
|
||||
this->process_iterator_batch_(this->iterator_storage_.initial_state);
|
||||
}
|
||||
@@ -1954,8 +1936,8 @@ void APIConnection::complete_authentication_() {
|
||||
#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
|
||||
{
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
this->parent_->get_client_connected_callback().call(StringRef(this->helper_->get_client_name()),
|
||||
StringRef(this->helper_->get_peername_to(peername)));
|
||||
this->parent_->get_client_connected_trigger()->trigger(std::string(this->helper_->get_client_name()),
|
||||
std::string(this->helper_->get_peername_to(peername)));
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_HOMEASSISTANT_TIME
|
||||
|
||||
@@ -296,11 +296,7 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
this->flags_.state_subscription = true;
|
||||
// Start initial state iterator only if no iterator is active
|
||||
// If list_entities is running, we'll start initial_state when it completes
|
||||
if (this->active_iterator_ == ActiveIterator::NONE
|
||||
#ifdef USE_API_CLIENT_SYNCED_TRIGGER
|
||||
|| this->active_iterator_ == ActiveIterator::CLIENT_SYNCED_PENDING
|
||||
#endif
|
||||
) {
|
||||
if (this->active_iterator_ == ActiveIterator::NONE) {
|
||||
this->begin_iterator_(ActiveIterator::INITIAL_STATE);
|
||||
}
|
||||
}
|
||||
@@ -646,15 +642,7 @@ class APIConnection final : public APIServerConnectionBase {
|
||||
// Group 2: Iterator union (saves ~16 bytes vs separate iterators)
|
||||
// These iterators are never active simultaneously - list_entities runs to completion
|
||||
// before initial_state begins, so we use a union with explicit construction/destruction.
|
||||
enum class ActiveIterator : uint8_t {
|
||||
NONE,
|
||||
LIST_ENTITIES,
|
||||
INITIAL_STATE,
|
||||
#ifdef USE_API_CLIENT_SYNCED_TRIGGER
|
||||
// Initial states sent, on_client_synced not fired yet; uses no iterator storage
|
||||
CLIENT_SYNCED_PENDING,
|
||||
#endif
|
||||
};
|
||||
enum class ActiveIterator : uint8_t { NONE, LIST_ENTITIES, INITIAL_STATE };
|
||||
|
||||
union IteratorUnion {
|
||||
ListEntitiesIterator list_entities;
|
||||
|
||||
@@ -257,11 +257,9 @@ void APIServer::remove_client_(uint8_t client_index) {
|
||||
|
||||
#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
|
||||
// Save client info before closing socket and removal for the trigger
|
||||
char client_name[CLIENT_INFO_NAME_MAX_LEN];
|
||||
strncpy(client_name, client->get_name(), sizeof(client_name) - 1);
|
||||
client_name[sizeof(client_name) - 1] = '\0';
|
||||
char peername[socket::SOCKADDR_STR_LEN];
|
||||
client->get_peername_to(peername);
|
||||
char peername_buf[socket::SOCKADDR_STR_LEN];
|
||||
std::string client_name(client->get_name());
|
||||
std::string client_peername(client->get_peername_to(peername_buf));
|
||||
#endif
|
||||
|
||||
// Read before the swap-and-reset below destroys the connection
|
||||
@@ -304,7 +302,7 @@ void APIServer::remove_client_(uint8_t client_index) {
|
||||
|
||||
#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
|
||||
// Fire trigger after client is removed so api.connected reflects the true state
|
||||
this->client_disconnected_callback_.call(StringRef(client_name), StringRef(peername));
|
||||
this->client_disconnected_trigger_.trigger(client_name, client_peername);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -277,27 +277,11 @@ class APIServer final : public Component
|
||||
const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
|
||||
#endif
|
||||
|
||||
#if defined(USE_API_CLIENT_CONNECTED_TRIGGER) || defined(USE_API_CLIENT_DISCONNECTED_TRIGGER) || \
|
||||
defined(USE_API_CLIENT_SYNCED_TRIGGER)
|
||||
/// Called with the client name and address, which are only valid during the call
|
||||
using ClientCallback = Callback<void(StringRef, StringRef)>;
|
||||
#endif
|
||||
#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
|
||||
template<typename F> void set_on_client_connected_callback(F &&callback) {
|
||||
this->client_connected_callback_ = ClientCallback::create(std::forward<F>(callback));
|
||||
}
|
||||
const ClientCallback &get_client_connected_callback() const { return this->client_connected_callback_; }
|
||||
Trigger<std::string, std::string> *get_client_connected_trigger() { return &this->client_connected_trigger_; }
|
||||
#endif
|
||||
#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
|
||||
template<typename F> void set_on_client_disconnected_callback(F &&callback) {
|
||||
this->client_disconnected_callback_ = ClientCallback::create(std::forward<F>(callback));
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_API_CLIENT_SYNCED_TRIGGER
|
||||
template<typename F> void set_on_client_synced_callback(F &&callback) {
|
||||
this->client_synced_callback_ = ClientCallback::create(std::forward<F>(callback));
|
||||
}
|
||||
const ClientCallback &get_client_synced_callback() const { return this->client_synced_callback_; }
|
||||
Trigger<std::string, std::string> *get_client_disconnected_trigger() { return &this->client_disconnected_trigger_; }
|
||||
#endif
|
||||
|
||||
protected:
|
||||
@@ -359,13 +343,10 @@ class APIServer final : public Component
|
||||
// Pointers and pointer-like types first (4 bytes each)
|
||||
socket::ListenSocket *socket_{nullptr};
|
||||
#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
|
||||
ClientCallback client_connected_callback_;
|
||||
Trigger<std::string, std::string> client_connected_trigger_;
|
||||
#endif
|
||||
#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
|
||||
ClientCallback client_disconnected_callback_;
|
||||
#endif
|
||||
#ifdef USE_API_CLIENT_SYNCED_TRIGGER
|
||||
ClientCallback client_synced_callback_;
|
||||
Trigger<std::string, std::string> client_disconnected_trigger_;
|
||||
#endif
|
||||
|
||||
// 4-byte aligned types
|
||||
|
||||
@@ -27,13 +27,13 @@ CONF_ACCELERATION_WAIT_TIME = "acceleration_wait_time"
|
||||
CONF_OVERSHOOT_DURATION = "overshoot_duration"
|
||||
CONF_OBSTACLE_ROLLBACK = "obstacle_rollback"
|
||||
|
||||
endstop_ns = cg.esphome_ns.namespace("feedback")
|
||||
FeedbackCover = endstop_ns.class_("FeedbackCover", cover.Cover, cg.Component)
|
||||
feedback_ns = cg.esphome_ns.namespace("feedback")
|
||||
FeedbackCover = feedback_ns.class_("FeedbackCover", cover.Cover, cg.Component)
|
||||
|
||||
|
||||
def validate_infer_endstop(config: ConfigType) -> ConfigType:
|
||||
if config[CONF_INFER_ENDSTOP_FROM_MOVEMENT] is True:
|
||||
if config[CONF_HAS_BUILT_IN_ENDSTOP] is False:
|
||||
if config[CONF_INFER_ENDSTOP_FROM_MOVEMENT]:
|
||||
if not config[CONF_HAS_BUILT_IN_ENDSTOP]:
|
||||
raise cv.Invalid(
|
||||
f"{CONF_INFER_ENDSTOP_FROM_MOVEMENT} can only be set if {CONF_HAS_BUILT_IN_ENDSTOP} is also set"
|
||||
)
|
||||
@@ -119,58 +119,52 @@ async def to_code(config: ConfigType) -> None:
|
||||
var = await cover.new_cover(config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
# STOP
|
||||
await automation.build_automation(
|
||||
var.get_stop_trigger(), [], config[CONF_STOP_ACTION]
|
||||
)
|
||||
|
||||
# OPEN
|
||||
await automation.build_automation(
|
||||
var.get_open_trigger(), [], config[CONF_OPEN_ACTION]
|
||||
)
|
||||
cg.add(var.set_open_duration(config[CONF_OPEN_DURATION]))
|
||||
await cg.add_variable(var.set_open_endstop, config.get(CONF_OPEN_ENDSTOP))
|
||||
await cg.add_variable(var.set_open_sensor, config.get(CONF_OPEN_SENSOR))
|
||||
await cg.add_variable(
|
||||
var.set_open_obstacle_sensor, config.get(CONF_OPEN_OBSTACLE_SENSOR)
|
||||
)
|
||||
|
||||
# CLOSE
|
||||
await automation.build_automation(
|
||||
var.get_close_trigger(), [], config[CONF_CLOSE_ACTION]
|
||||
)
|
||||
cg.add(var.set_open_duration(config[CONF_OPEN_DURATION]))
|
||||
cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION]))
|
||||
await cg.add_variable(var.set_close_endstop, config.get(CONF_CLOSE_ENDSTOP))
|
||||
await cg.add_variable(var.set_close_sensor, config.get(CONF_CLOSE_SENSOR))
|
||||
await cg.add_variable(
|
||||
var.set_close_obstacle_sensor, config.get(CONF_CLOSE_OBSTACLE_SENSOR)
|
||||
)
|
||||
|
||||
# OTHER
|
||||
if CONF_OPEN_ENDSTOP in config or CONF_CLOSE_ENDSTOP in config:
|
||||
cg.add_define("USE_FEEDBACK_COVER_ENDSTOP")
|
||||
await cg.add_variable(var.set_open_endstop, config.get(CONF_OPEN_ENDSTOP))
|
||||
await cg.add_variable(var.set_close_endstop, config.get(CONF_CLOSE_ENDSTOP))
|
||||
# validation requires both movement sensors or neither
|
||||
if CONF_OPEN_SENSOR in config:
|
||||
cg.add_define("USE_FEEDBACK_COVER_MOVEMENT")
|
||||
await cg.add_variable(var.set_open_sensor, config[CONF_OPEN_SENSOR])
|
||||
await cg.add_variable(var.set_close_sensor, config[CONF_CLOSE_SENSOR])
|
||||
cg.add(var.set_infer_endstop(config[CONF_INFER_ENDSTOP_FROM_MOVEMENT]))
|
||||
if CONF_OPEN_OBSTACLE_SENSOR in config or CONF_CLOSE_OBSTACLE_SENSOR in config:
|
||||
cg.add_define("USE_FEEDBACK_COVER_OBSTACLE")
|
||||
await cg.add_variable(
|
||||
var.set_open_obstacle_sensor, config.get(CONF_OPEN_OBSTACLE_SENSOR)
|
||||
)
|
||||
await cg.add_variable(
|
||||
var.set_close_obstacle_sensor, config.get(CONF_CLOSE_OBSTACLE_SENSOR)
|
||||
)
|
||||
cg.add(var.set_obstacle_rollback(config[CONF_OBSTACLE_ROLLBACK]))
|
||||
|
||||
cg.add_optional(var.set_max_duration, config.get(CONF_MAX_DURATION))
|
||||
|
||||
cg.add(var.set_has_built_in_endstop(config[CONF_HAS_BUILT_IN_ENDSTOP]))
|
||||
|
||||
if CONF_ASSUMED_STATE in config:
|
||||
cg.add(var.set_assumed_state(config[CONF_ASSUMED_STATE]))
|
||||
else:
|
||||
cg.add(
|
||||
var.set_assumed_state(
|
||||
not (
|
||||
(CONF_CLOSE_ENDSTOP in config and CONF_OPEN_ENDSTOP in config)
|
||||
or config[CONF_INFER_ENDSTOP_FROM_MOVEMENT]
|
||||
)
|
||||
)
|
||||
assumed_state = config.get(CONF_ASSUMED_STATE)
|
||||
if assumed_state is None:
|
||||
assumed_state = not (
|
||||
(CONF_CLOSE_ENDSTOP in config and CONF_OPEN_ENDSTOP in config)
|
||||
or config[CONF_INFER_ENDSTOP_FROM_MOVEMENT]
|
||||
)
|
||||
|
||||
cg.add(var.set_assumed_state(assumed_state))
|
||||
cg.add(var.set_update_interval(config[CONF_UPDATE_INTERVAL]))
|
||||
cg.add(var.set_infer_endstop(config[CONF_INFER_ENDSTOP_FROM_MOVEMENT]))
|
||||
if CONF_DIRECTION_CHANGE_WAIT_TIME in config:
|
||||
cg.add(
|
||||
var.set_direction_change_waittime(config[CONF_DIRECTION_CHANGE_WAIT_TIME])
|
||||
)
|
||||
cg.add_optional(
|
||||
var.set_direction_change_waittime, config.get(CONF_DIRECTION_CHANGE_WAIT_TIME)
|
||||
)
|
||||
cg.add(var.set_acceleration_wait_time(config[CONF_ACCELERATION_WAIT_TIME]))
|
||||
if (overshoot := config[CONF_OVERSHOOT_DURATION]).total_milliseconds:
|
||||
cg.add_define("USE_FEEDBACK_COVER_OVERSHOOT")
|
||||
cg.add(var.set_overshoot_duration(overshoot))
|
||||
cg.add(var.set_obstacle_rollback(config[CONF_OBSTACLE_ROLLBACK]))
|
||||
@@ -22,15 +22,15 @@ void FeedbackCover::setup() {
|
||||
}
|
||||
this->current_operation = COVER_OPERATION_IDLE;
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
// if available, get position from endstop sensors
|
||||
#ifdef USE_FEEDBACK_COVER_ENDSTOP
|
||||
if (this->open_endstop_ != nullptr && this->open_endstop_->state) {
|
||||
this->position = COVER_OPEN;
|
||||
} else if (this->close_endstop_ != nullptr && this->close_endstop_->state) {
|
||||
this->position = COVER_CLOSED;
|
||||
}
|
||||
#endif
|
||||
|
||||
// if available, get moving state from sensors
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
if (this->open_feedback_ != nullptr && this->open_feedback_->state) {
|
||||
this->current_operation = COVER_OPERATION_OPENING;
|
||||
} else if (this->close_feedback_ != nullptr && this->close_feedback_->state) {
|
||||
@@ -51,30 +51,36 @@ CoverTraits FeedbackCover::get_traits() {
|
||||
}
|
||||
|
||||
void FeedbackCover::dump_config() {
|
||||
LOG_COVER("", "Endstop Cover", this);
|
||||
ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f);
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
LOG_COVER("", "Feedback Cover", this);
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Open Duration: %.1fs\n"
|
||||
" Close Duration: %.1fs",
|
||||
this->open_duration_ / 1e3f, this->close_duration_ / 1e3f);
|
||||
#ifdef USE_FEEDBACK_COVER_ENDSTOP
|
||||
LOG_BINARY_SENSOR(" ", "Open Endstop", this->open_endstop_);
|
||||
LOG_BINARY_SENSOR(" ", "Open Feedback", this->open_feedback_);
|
||||
LOG_BINARY_SENSOR(" ", "Open Obstacle", this->open_obstacle_);
|
||||
#endif
|
||||
ESP_LOGCONFIG(TAG, " Close Duration: %.1fs", this->close_duration_ / 1e3f);
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
LOG_BINARY_SENSOR(" ", "Close Endstop", this->close_endstop_);
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
LOG_BINARY_SENSOR(" ", "Open Feedback", this->open_feedback_);
|
||||
LOG_BINARY_SENSOR(" ", "Close Feedback", this->close_feedback_);
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
LOG_BINARY_SENSOR(" ", "Open Obstacle", this->open_obstacle_);
|
||||
LOG_BINARY_SENSOR(" ", "Close Obstacle", this->close_obstacle_);
|
||||
#endif
|
||||
if (this->has_built_in_endstop_) {
|
||||
ESP_LOGCONFIG(TAG, " Has builtin endstop: YES");
|
||||
}
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
if (this->infer_endstop_) {
|
||||
ESP_LOGCONFIG(TAG, " Infer endstop from movement: YES");
|
||||
}
|
||||
#endif
|
||||
if (this->max_duration_ < UINT32_MAX) {
|
||||
ESP_LOGCONFIG(TAG, " Max Duration: %.1fs", this->max_duration_ / 1e3f);
|
||||
}
|
||||
if (this->direction_change_waittime_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Direction change wait time: %.1fs", *this->direction_change_waittime_ / 1e3f);
|
||||
if (this->direction_change_waittime_ != UINT32_MAX) {
|
||||
ESP_LOGCONFIG(TAG, " Direction change wait time: %.1fs", this->direction_change_waittime_ / 1e3f);
|
||||
}
|
||||
if (this->acceleration_wait_time_) {
|
||||
ESP_LOGCONFIG(TAG, " Acceleration wait time: %.1fs", this->acceleration_wait_time_ / 1e3f);
|
||||
@@ -84,60 +90,51 @@ void FeedbackCover::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Overshoot duration: %.1fs", this->overshoot_duration_ / 1e3f);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (this->obstacle_rollback_ && (this->open_obstacle_ != nullptr || this->close_obstacle_ != nullptr)) {
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
if (this->obstacle_rollback_) {
|
||||
ESP_LOGCONFIG(TAG, " Obstacle rollback: %.1f%%", this->obstacle_rollback_ * 100);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
void FeedbackCover::set_open_sensor(binary_sensor::BinarySensor *open_feedback) {
|
||||
this->open_feedback_ = open_feedback;
|
||||
|
||||
// setup callbacks to react to sensor changes
|
||||
open_feedback->add_on_state_callback([this](bool state) {
|
||||
ESP_LOGD(TAG, "'%s' - Open feedback '%s'.", LOG_STR_ARG(this->get_log_name()),
|
||||
state ? LOG_STR_LITERAL("STARTED") : LOG_STR_LITERAL("ENDED"));
|
||||
this->recompute_position_();
|
||||
if (!state && this->infer_endstop_ && this->current_trigger_operation_ == COVER_OPERATION_OPENING) {
|
||||
this->endstop_reached_(true);
|
||||
}
|
||||
this->set_current_operation_(state ? COVER_OPERATION_OPENING : COVER_OPERATION_IDLE, false);
|
||||
});
|
||||
open_feedback->add_on_state_callback([this](bool state) { this->on_feedback_(true, state); });
|
||||
}
|
||||
|
||||
void FeedbackCover::set_close_sensor(binary_sensor::BinarySensor *close_feedback) {
|
||||
this->close_feedback_ = close_feedback;
|
||||
|
||||
close_feedback->add_on_state_callback([this](bool state) {
|
||||
ESP_LOGD(TAG, "'%s' - Close feedback '%s'.", LOG_STR_ARG(this->get_log_name()),
|
||||
state ? LOG_STR_LITERAL("STARTED") : LOG_STR_LITERAL("ENDED"));
|
||||
this->recompute_position_();
|
||||
if (!state && this->infer_endstop_ && this->current_trigger_operation_ == COVER_OPERATION_CLOSING) {
|
||||
this->endstop_reached_(false);
|
||||
}
|
||||
|
||||
this->set_current_operation_(state ? COVER_OPERATION_CLOSING : COVER_OPERATION_IDLE, false);
|
||||
});
|
||||
close_feedback->add_on_state_callback([this](bool state) { this->on_feedback_(false, state); });
|
||||
}
|
||||
|
||||
void FeedbackCover::on_feedback_(bool open, bool state) {
|
||||
const CoverOperation operation = open ? COVER_OPERATION_OPENING : COVER_OPERATION_CLOSING;
|
||||
ESP_LOGD(TAG, "'%s' - %s feedback '%s'.", LOG_STR_ARG(this->get_log_name()),
|
||||
open ? LOG_STR_LITERAL("Open") : LOG_STR_LITERAL("Close"),
|
||||
state ? LOG_STR_LITERAL("STARTED") : LOG_STR_LITERAL("ENDED"));
|
||||
this->recompute_position_();
|
||||
if (!state && this->infer_endstop_ && this->current_trigger_operation_ == operation) {
|
||||
this->endstop_reached_(open);
|
||||
}
|
||||
this->set_current_operation_(state ? operation : COVER_OPERATION_IDLE, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_FEEDBACK_COVER_ENDSTOP
|
||||
void FeedbackCover::set_open_endstop(binary_sensor::BinarySensor *open_endstop) {
|
||||
this->open_endstop_ = open_endstop;
|
||||
open_endstop->add_on_state_callback([this](bool state) {
|
||||
if (state) {
|
||||
if (state)
|
||||
this->endstop_reached_(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void FeedbackCover::set_close_endstop(binary_sensor::BinarySensor *close_endstop) {
|
||||
this->close_endstop_ = close_endstop;
|
||||
close_endstop->add_on_state_callback([this](bool state) {
|
||||
if (state) {
|
||||
if (state)
|
||||
this->endstop_reached_(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
#endif
|
||||
@@ -147,22 +144,22 @@ void FeedbackCover::endstop_reached_(bool open_endstop) {
|
||||
|
||||
this->position = open_endstop ? COVER_OPEN : COVER_CLOSED;
|
||||
|
||||
// only act if endstop activated while moving in the right direction, in case we are coming back
|
||||
// from a position slightly past the endpoint
|
||||
// Only when moving towards it; the cover may be returning from just past it
|
||||
if (this->current_trigger_operation_ == (open_endstop ? COVER_OPERATION_OPENING : COVER_OPERATION_CLOSING)) {
|
||||
float dur = (now - this->start_dir_time_) / 1e3f;
|
||||
ESP_LOGD(TAG, "'%s' - %s endstop reached. Took %.1fs.", LOG_STR_ARG(this->get_log_name()),
|
||||
open_endstop ? LOG_STR_LITERAL("Open") : LOG_STR_LITERAL("Close"), dur);
|
||||
|
||||
// if there is no external mechanism, stop the cover
|
||||
if (!this->has_built_in_endstop_) {
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
} else {
|
||||
this->set_current_operation_(COVER_OPERATION_IDLE, true);
|
||||
}
|
||||
// Without movement sensors the stop above already published
|
||||
if (!this->has_movement_sensors_())
|
||||
return;
|
||||
}
|
||||
|
||||
// always sync position and publish
|
||||
this->publish_state();
|
||||
this->last_publish_time_ = now;
|
||||
}
|
||||
@@ -170,69 +167,54 @@ void FeedbackCover::endstop_reached_(bool open_endstop) {
|
||||
void FeedbackCover::set_current_operation_(cover::CoverOperation operation, bool is_triggered) {
|
||||
if (is_triggered) {
|
||||
this->current_trigger_operation_ = operation;
|
||||
// With movement sensors they set the actual operation
|
||||
if (this->has_movement_sensors_())
|
||||
return;
|
||||
}
|
||||
|
||||
// if it is setting the actual operation (not triggered one) or
|
||||
// if we don't have moving sensor, we operate in optimistic mode, assuming actions take place immediately
|
||||
// thus, triggered operation always sets current operation.
|
||||
// otherwise, current operation comes from sensor, and may differ from requested operation
|
||||
// this might be from delays or complex actions, or because the movement was not trigger by the component
|
||||
// but initiated externally
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (!is_triggered || (this->open_feedback_ == nullptr || this->close_feedback_ == nullptr))
|
||||
#endif
|
||||
{
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
this->current_operation = operation;
|
||||
this->start_dir_time_ = this->last_recompute_time_ = now;
|
||||
this->publish_state();
|
||||
this->last_publish_time_ = now;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void FeedbackCover::set_close_obstacle_sensor(binary_sensor::BinarySensor *close_obstacle) {
|
||||
this->close_obstacle_ = close_obstacle;
|
||||
|
||||
close_obstacle->add_on_state_callback([this](bool state) {
|
||||
if (state && (this->current_operation == COVER_OPERATION_CLOSING ||
|
||||
this->current_trigger_operation_ == COVER_OPERATION_CLOSING)) {
|
||||
ESP_LOGD(TAG, "'%s' - Close obstacle detected.", LOG_STR_ARG(this->get_log_name()));
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
|
||||
if (this->obstacle_rollback_) {
|
||||
this->target_position_ = clamp(this->position + this->obstacle_rollback_, COVER_CLOSED, COVER_OPEN);
|
||||
this->start_direction_(COVER_OPERATION_OPENING);
|
||||
}
|
||||
}
|
||||
});
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
this->current_operation = operation;
|
||||
if (operation != COVER_OPERATION_IDLE)
|
||||
this->enable_loop();
|
||||
this->start_dir_time_ = this->last_recompute_time_ = now;
|
||||
this->publish_state();
|
||||
this->last_publish_time_ = now;
|
||||
}
|
||||
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
void FeedbackCover::set_open_obstacle_sensor(binary_sensor::BinarySensor *open_obstacle) {
|
||||
this->open_obstacle_ = open_obstacle;
|
||||
open_obstacle->add_on_state_callback([this](bool state) { this->on_obstacle_(true, state); });
|
||||
}
|
||||
|
||||
open_obstacle->add_on_state_callback([this](bool state) {
|
||||
if (state && (this->current_operation == COVER_OPERATION_OPENING ||
|
||||
this->current_trigger_operation_ == COVER_OPERATION_OPENING)) {
|
||||
ESP_LOGD(TAG, "'%s' - Open obstacle detected.", LOG_STR_ARG(this->get_log_name()));
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
void FeedbackCover::set_close_obstacle_sensor(binary_sensor::BinarySensor *close_obstacle) {
|
||||
this->close_obstacle_ = close_obstacle;
|
||||
close_obstacle->add_on_state_callback([this](bool state) { this->on_obstacle_(false, state); });
|
||||
}
|
||||
|
||||
if (this->obstacle_rollback_) {
|
||||
this->target_position_ = clamp(this->position - this->obstacle_rollback_, COVER_CLOSED, COVER_OPEN);
|
||||
this->start_direction_(COVER_OPERATION_CLOSING);
|
||||
}
|
||||
}
|
||||
});
|
||||
void FeedbackCover::on_obstacle_(bool open, bool state) {
|
||||
const CoverOperation operation = open ? COVER_OPERATION_OPENING : COVER_OPERATION_CLOSING;
|
||||
if (!state || (this->current_operation != operation && this->current_trigger_operation_ != operation))
|
||||
return;
|
||||
ESP_LOGD(TAG, "'%s' - %s obstacle detected.", LOG_STR_ARG(this->get_log_name()),
|
||||
open ? LOG_STR_LITERAL("Open") : LOG_STR_LITERAL("Close"));
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
|
||||
if (this->obstacle_rollback_) {
|
||||
const float rollback = open ? -this->obstacle_rollback_ : this->obstacle_rollback_;
|
||||
this->target_position_ = clamp(this->position + rollback, COVER_CLOSED, COVER_OPEN);
|
||||
this->start_direction_(open ? COVER_OPERATION_CLOSING : COVER_OPERATION_OPENING);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void FeedbackCover::loop() {
|
||||
if (this->current_operation == COVER_OPERATION_IDLE)
|
||||
if (this->current_operation == COVER_OPERATION_IDLE) {
|
||||
this->disable_loop();
|
||||
return;
|
||||
}
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
|
||||
// Recompute position every loop cycle
|
||||
this->recompute_position_();
|
||||
|
||||
// if we initiated the move, check if we reached position or max time
|
||||
@@ -264,9 +246,7 @@ void FeedbackCover::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
// update current position at requested interval, regardless of who started the movement
|
||||
// so that we also update UI if there was an external movement
|
||||
// don't save intermediate positions
|
||||
// Publish intermediate positions without saving them
|
||||
if (now - this->last_publish_time_ > this->update_interval_) {
|
||||
this->publish_state(false);
|
||||
this->last_publish_time_ = now;
|
||||
@@ -274,11 +254,9 @@ void FeedbackCover::loop() {
|
||||
}
|
||||
|
||||
void FeedbackCover::control(const CoverCall &call) {
|
||||
// stop action logic
|
||||
if (call.get_stop()) {
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
} else if (call.get_toggle().has_value()) {
|
||||
// toggle action logic: OPEN - STOP - CLOSE
|
||||
if (this->current_trigger_operation_ != COVER_OPERATION_IDLE) {
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
} else {
|
||||
@@ -294,28 +272,15 @@ void FeedbackCover::control(const CoverCall &call) {
|
||||
auto pos_opt = call.get_position();
|
||||
if (!pos_opt.has_value())
|
||||
return;
|
||||
// go to position action
|
||||
auto pos = *pos_opt;
|
||||
if (pos == this->position) {
|
||||
// already at target,
|
||||
|
||||
// for covers with built in end stop, if we don't have sensors we should send the command again
|
||||
// to make sure the assumed state is not wrong
|
||||
if (this->has_built_in_endstop_ && ((pos == COVER_OPEN
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
&& this->open_endstop_ == nullptr
|
||||
#endif
|
||||
&& !this->infer_endstop_) ||
|
||||
(pos == COVER_CLOSED
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
&& this->close_endstop_ == nullptr
|
||||
#endif
|
||||
&& !this->infer_endstop_))) {
|
||||
// Resend in case the assumed state is wrong
|
||||
if (this->has_built_in_endstop_ &&
|
||||
((pos == COVER_OPEN && !this->has_endstop_(true)) || (pos == COVER_CLOSED && !this->has_endstop_(false)))) {
|
||||
this->target_position_ = pos;
|
||||
this->start_direction_(pos == COVER_CLOSED ? COVER_OPERATION_CLOSING : COVER_OPERATION_OPENING);
|
||||
} else if (this->current_operation != COVER_OPERATION_IDLE ||
|
||||
this->current_trigger_operation_ != COVER_OPERATION_IDLE) {
|
||||
// if we are moving, stop
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
}
|
||||
} else {
|
||||
@@ -326,7 +291,7 @@ void FeedbackCover::control(const CoverCall &call) {
|
||||
}
|
||||
|
||||
void FeedbackCover::stop_prev_trigger_() {
|
||||
if (this->direction_change_waittime_.has_value()) {
|
||||
if (this->direction_change_waittime_ != UINT32_MAX) {
|
||||
this->cancel_timeout(DIRECTION_CHANGE_TIMEOUT_ID);
|
||||
}
|
||||
if (this->prev_command_trigger_ != nullptr) {
|
||||
@@ -336,9 +301,6 @@ void FeedbackCover::stop_prev_trigger_() {
|
||||
}
|
||||
|
||||
bool FeedbackCover::is_at_target_() const {
|
||||
// if initiated externally, current operation might be different from
|
||||
// operation that was triggered, thus evaluate position against what was asked
|
||||
|
||||
switch (this->current_trigger_operation_) {
|
||||
case COVER_OPERATION_OPENING:
|
||||
return this->position >= this->target_position_;
|
||||
@@ -350,10 +312,10 @@ bool FeedbackCover::is_at_target_() const {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void FeedbackCover::start_direction_(CoverOperation dir) {
|
||||
Trigger<> *trig;
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
binary_sensor::BinarySensor *obstacle{nullptr};
|
||||
#endif
|
||||
|
||||
@@ -369,14 +331,14 @@ void FeedbackCover::start_direction_(CoverOperation dir) {
|
||||
case COVER_OPERATION_OPENING:
|
||||
this->last_operation_ = dir;
|
||||
trig = &this->open_trigger_;
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
obstacle = this->open_obstacle_;
|
||||
#endif
|
||||
break;
|
||||
case COVER_OPERATION_CLOSING:
|
||||
this->last_operation_ = dir;
|
||||
trig = &this->close_trigger_;
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
obstacle = this->close_obstacle_;
|
||||
#endif
|
||||
break;
|
||||
@@ -386,9 +348,8 @@ void FeedbackCover::start_direction_(CoverOperation dir) {
|
||||
|
||||
this->stop_prev_trigger_();
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
// check if there is an obstacle to start the new operation -> abort without any change
|
||||
// the case when an obstacle appears while moving is handled in the callback
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
// An obstacle already present aborts the start
|
||||
if (obstacle != nullptr && obstacle->state) {
|
||||
ESP_LOGD(TAG, "'%s' - %s obstacle detected. Action not started.", LOG_STR_ARG(this->get_log_name()),
|
||||
dir == COVER_OPERATION_OPENING ? LOG_STR_LITERAL("Open") : LOG_STR_LITERAL("Close"));
|
||||
@@ -396,14 +357,12 @@ void FeedbackCover::start_direction_(CoverOperation dir) {
|
||||
}
|
||||
#endif
|
||||
|
||||
// if we are moving and need to move in the opposite direction
|
||||
// check if we have a wait time
|
||||
if (this->direction_change_waittime_.has_value() && dir != COVER_OPERATION_IDLE &&
|
||||
if (this->direction_change_waittime_ != UINT32_MAX && dir != COVER_OPERATION_IDLE &&
|
||||
this->current_operation != COVER_OPERATION_IDLE && dir != this->current_operation) {
|
||||
const uint32_t waittime = *this->direction_change_waittime_;
|
||||
ESP_LOGD(TAG, "'%s' - Reversing direction.", LOG_STR_ARG(this->get_log_name()));
|
||||
this->start_direction_(COVER_OPERATION_IDLE);
|
||||
this->set_timeout(DIRECTION_CHANGE_TIMEOUT_ID, waittime, [this, dir]() { this->start_direction_(dir); });
|
||||
this->set_timeout(DIRECTION_CHANGE_TIMEOUT_ID, this->direction_change_waittime_,
|
||||
[this, dir]() { this->start_direction_(dir); });
|
||||
} else {
|
||||
this->set_current_operation_(dir, true);
|
||||
this->prev_command_trigger_ = trig;
|
||||
@@ -425,42 +384,24 @@ void FeedbackCover::recompute_position_() {
|
||||
float min_pos;
|
||||
float max_pos;
|
||||
|
||||
// endstop sensors update position from their callbacks, and sets the fully open/close value
|
||||
// If we have endstop, estimation never reaches the fully open/closed state.
|
||||
// but if movement continues past corresponding endstop (inertia), keep the fully open/close state
|
||||
|
||||
// With an endstop the estimate stops short of the end; the endstop callback sets it
|
||||
switch (this->current_operation) {
|
||||
case COVER_OPERATION_OPENING:
|
||||
dir = 1.0f;
|
||||
action_dur = this->open_duration_;
|
||||
min_pos = COVER_CLOSED;
|
||||
max_pos = (
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
this->open_endstop_ != nullptr ||
|
||||
#endif
|
||||
this->infer_endstop_) &&
|
||||
this->position < COVER_OPEN
|
||||
? 0.99f
|
||||
: COVER_OPEN;
|
||||
max_pos = this->has_endstop_(true) && this->position < COVER_OPEN ? 0.99f : COVER_OPEN;
|
||||
break;
|
||||
case COVER_OPERATION_CLOSING:
|
||||
dir = -1.0f;
|
||||
action_dur = this->close_duration_;
|
||||
min_pos = (
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
this->close_endstop_ != nullptr ||
|
||||
#endif
|
||||
this->infer_endstop_) &&
|
||||
this->position > COVER_CLOSED
|
||||
? 0.01f
|
||||
: COVER_CLOSED;
|
||||
min_pos = this->has_endstop_(false) && this->position > COVER_CLOSED ? 0.01f : COVER_CLOSED;
|
||||
max_pos = COVER_OPEN;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// check if we have an acceleration_wait_time, and remove from position computation
|
||||
if (now - this->start_dir_time_ > this->acceleration_wait_time_) {
|
||||
uint32_t accel_end_time = this->start_dir_time_ + this->acceleration_wait_time_;
|
||||
uint32_t effective_start;
|
||||
|
||||
@@ -20,13 +20,19 @@ class FeedbackCover final : public cover::Cover, public Component {
|
||||
Trigger<> *get_close_trigger() { return &this->close_trigger_; }
|
||||
Trigger<> *get_stop_trigger() { return &this->stop_trigger_; }
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#ifdef USE_FEEDBACK_COVER_ENDSTOP
|
||||
void set_open_endstop(binary_sensor::BinarySensor *open_endstop);
|
||||
void set_open_sensor(binary_sensor::BinarySensor *open_feedback);
|
||||
void set_open_obstacle_sensor(binary_sensor::BinarySensor *open_obstacle);
|
||||
void set_close_endstop(binary_sensor::BinarySensor *close_endstop);
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
void set_open_sensor(binary_sensor::BinarySensor *open_feedback);
|
||||
void set_close_sensor(binary_sensor::BinarySensor *close_feedback);
|
||||
void set_infer_endstop(bool infer_endstop) { this->infer_endstop_ = infer_endstop; }
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
void set_open_obstacle_sensor(binary_sensor::BinarySensor *open_obstacle);
|
||||
void set_close_obstacle_sensor(binary_sensor::BinarySensor *close_obstacle);
|
||||
void set_obstacle_rollback(float obstacle_rollback) { this->obstacle_rollback_ = obstacle_rollback; }
|
||||
#endif
|
||||
void set_open_duration(uint32_t duration) { this->open_duration_ = duration; }
|
||||
void set_close_duration(uint32_t duration) { this->close_duration_ = duration; }
|
||||
@@ -36,9 +42,7 @@ class FeedbackCover final : public cover::Cover, public Component {
|
||||
#ifdef USE_FEEDBACK_COVER_OVERSHOOT
|
||||
void set_overshoot_duration(uint32_t overshoot_duration) { this->overshoot_duration_ = overshoot_duration; }
|
||||
#endif
|
||||
void set_obstacle_rollback(float obstacle_rollback) { this->obstacle_rollback_ = obstacle_rollback; }
|
||||
void set_update_interval(uint32_t interval) { this->update_interval_ = interval; }
|
||||
void set_infer_endstop(bool infer_endstop) { this->infer_endstop_ = infer_endstop; }
|
||||
void set_direction_change_waittime(uint32_t waittime) { this->direction_change_waittime_ = waittime; }
|
||||
void set_acceleration_wait_time(uint32_t waittime) { this->acceleration_wait_time_ = waittime; }
|
||||
|
||||
@@ -53,15 +57,43 @@ class FeedbackCover final : public cover::Cover, public Component {
|
||||
void endstop_reached_(bool open_endstop);
|
||||
void recompute_position_();
|
||||
void set_current_operation_(cover::CoverOperation operation, bool is_triggered);
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
void on_feedback_(bool open, bool state);
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
void on_obstacle_(bool open, bool state);
|
||||
#endif
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
bool has_endstop_(bool open) const {
|
||||
#ifdef USE_FEEDBACK_COVER_ENDSTOP
|
||||
if ((open ? this->open_endstop_ : this->close_endstop_) != nullptr)
|
||||
return true;
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
return this->infer_endstop_;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
bool has_movement_sensors_() const {
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
return this->open_feedback_ != nullptr && this->close_feedback_ != nullptr;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_FEEDBACK_COVER_ENDSTOP
|
||||
binary_sensor::BinarySensor *open_endstop_{nullptr};
|
||||
binary_sensor::BinarySensor *close_endstop_{nullptr};
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
binary_sensor::BinarySensor *open_feedback_{nullptr};
|
||||
binary_sensor::BinarySensor *close_feedback_{nullptr};
|
||||
#endif
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
binary_sensor::BinarySensor *open_obstacle_{nullptr};
|
||||
binary_sensor::BinarySensor *close_obstacle_{nullptr};
|
||||
|
||||
#endif
|
||||
Trigger<> open_trigger_;
|
||||
Trigger<> close_trigger_;
|
||||
@@ -73,12 +105,13 @@ class FeedbackCover final : public cover::Cover, public Component {
|
||||
#ifdef USE_FEEDBACK_COVER_OVERSHOOT
|
||||
uint32_t overshoot_duration_{0};
|
||||
#endif
|
||||
optional<uint32_t> direction_change_waittime_{};
|
||||
uint32_t direction_change_waittime_{UINT32_MAX};
|
||||
uint32_t acceleration_wait_time_{0};
|
||||
bool has_built_in_endstop_{false};
|
||||
bool assumed_state_{false};
|
||||
#ifdef USE_FEEDBACK_COVER_MOVEMENT
|
||||
bool infer_endstop_{false};
|
||||
float obstacle_rollback_{0};
|
||||
#endif
|
||||
|
||||
cover::CoverOperation last_operation_{cover::COVER_OPERATION_OPENING};
|
||||
cover::CoverOperation current_trigger_operation_{cover::COVER_OPERATION_IDLE};
|
||||
@@ -90,6 +123,9 @@ class FeedbackCover final : public cover::Cover, public Component {
|
||||
uint32_t start_overshoot_time_{0};
|
||||
#endif
|
||||
float target_position_{0};
|
||||
#ifdef USE_FEEDBACK_COVER_OBSTACLE
|
||||
float obstacle_rollback_{0};
|
||||
#endif
|
||||
uint32_t update_interval_{1000};
|
||||
};
|
||||
|
||||
|
||||
@@ -109,6 +109,9 @@
|
||||
#define USE_EZSP_PROXY_TAP
|
||||
#define USE_FAN
|
||||
#define USE_FONT
|
||||
#define USE_FEEDBACK_COVER_ENDSTOP
|
||||
#define USE_FEEDBACK_COVER_MOVEMENT
|
||||
#define USE_FEEDBACK_COVER_OBSTACLE
|
||||
#define USE_FEEDBACK_COVER_OVERSHOOT
|
||||
#define USE_GPIO_BINARY_SENSOR_INTERRUPT
|
||||
#define USE_GPIO_SWITCH_INTERLOCK
|
||||
@@ -401,7 +404,6 @@
|
||||
#define USE_API
|
||||
#define USE_API_CLIENT_CONNECTED_TRIGGER
|
||||
#define USE_API_CLIENT_DISCONNECTED_TRIGGER
|
||||
#define USE_API_CLIENT_SYNCED_TRIGGER
|
||||
#define USE_API_HOMEASSISTANT_ACTION_RESPONSES
|
||||
#define USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
|
||||
#define USE_API_HOMEASSISTANT_SERVICES
|
||||
|
||||
@@ -98,16 +98,6 @@ esphome:
|
||||
api:
|
||||
port: 8000
|
||||
reboot_timeout: 0min
|
||||
on_client_connected:
|
||||
# Deferring actions get owned copies of the arguments
|
||||
- delay: 1s
|
||||
- logger.log:
|
||||
format: "Client %s connected from %s"
|
||||
args: [client_info.c_str(), client_address.c_str()]
|
||||
on_client_synced:
|
||||
- logger.log:
|
||||
format: "Client %s synced from %s"
|
||||
args: [client_info.c_str(), client_address.c_str()]
|
||||
actions:
|
||||
- action: hello_world
|
||||
description: Log a greeting
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# No endstop, movement or obstacle sensors, so their code is compiled out
|
||||
cover:
|
||||
- platform: feedback
|
||||
id: feedback_minimal
|
||||
name: Minimal Feedback Cover
|
||||
open_action:
|
||||
- logger.log: open
|
||||
open_duration: 10s
|
||||
close_action:
|
||||
- logger.log: close
|
||||
close_duration: 10s
|
||||
stop_action:
|
||||
- logger.log: stop
|
||||
@@ -1,28 +0,0 @@
|
||||
esphome:
|
||||
name: api-client-synced
|
||||
|
||||
host:
|
||||
|
||||
api:
|
||||
on_client_synced:
|
||||
- logger.log: "Synced trigger entered"
|
||||
- if:
|
||||
condition:
|
||||
api.connected:
|
||||
state_subscription_only: true
|
||||
then:
|
||||
- logger.log: "Synced trigger fired"
|
||||
# Home Assistant subscribes to actions in the same packet as states
|
||||
- homeassistant.event:
|
||||
event: esphome.client_synced
|
||||
|
||||
switch:
|
||||
# A known state at boot, so the test can check it is sent before the trigger
|
||||
- platform: template
|
||||
id: ready_switch
|
||||
name: "Ready switch"
|
||||
optimistic: true
|
||||
restore_mode: ALWAYS_ON
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
@@ -0,0 +1,69 @@
|
||||
esphome:
|
||||
name: feedback-loop-wake
|
||||
|
||||
host:
|
||||
api:
|
||||
batch_delay: 0ms
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
# Stand in for a motor started outside ESPHome
|
||||
switch:
|
||||
- platform: template
|
||||
name: "External Open"
|
||||
id: external_open
|
||||
optimistic: true
|
||||
- platform: template
|
||||
name: "External Close"
|
||||
id: external_close
|
||||
optimistic: true
|
||||
|
||||
binary_sensor:
|
||||
- platform: template
|
||||
id: open_moving
|
||||
internal: true
|
||||
condition:
|
||||
switch.is_on: external_open
|
||||
- platform: template
|
||||
id: close_moving
|
||||
internal: true
|
||||
condition:
|
||||
switch.is_on: external_close
|
||||
|
||||
cover:
|
||||
- platform: feedback
|
||||
name: "Time Cover"
|
||||
open_action:
|
||||
- logger.log: "time_cover open"
|
||||
close_action:
|
||||
- logger.log: "time_cover close"
|
||||
stop_action:
|
||||
- logger.log: "time_cover stop"
|
||||
open_duration: 2s
|
||||
close_duration: 2s
|
||||
|
||||
- platform: feedback
|
||||
name: "Max Duration Cover"
|
||||
open_action:
|
||||
- logger.log: "max_cover open"
|
||||
close_action:
|
||||
- logger.log: "max_cover close"
|
||||
stop_action:
|
||||
- logger.log: "max_cover stop"
|
||||
open_duration: 3s
|
||||
close_duration: 3s
|
||||
max_duration: 1s
|
||||
|
||||
- platform: feedback
|
||||
name: "Sensor Cover"
|
||||
open_action:
|
||||
- logger.log: "sensor_cover open"
|
||||
close_action:
|
||||
- logger.log: "sensor_cover close"
|
||||
stop_action:
|
||||
- logger.log: "sensor_cover stop"
|
||||
open_duration: 4s
|
||||
close_duration: 4s
|
||||
open_sensor: open_moving
|
||||
close_sensor: close_moving
|
||||
update_interval: 200ms
|
||||
@@ -1,56 +0,0 @@
|
||||
"""Integration test for the api on_client_synced trigger."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from aioesphomeapi import EntityState, HomeassistantServiceCall, SwitchState
|
||||
import pytest
|
||||
|
||||
from .log_utils import LineWaiter
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_client_synced(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""The trigger fires after the initial states, with the action subscription active."""
|
||||
waiter = LineWaiter()
|
||||
calls: asyncio.Queue[HomeassistantServiceCall] = asyncio.Queue()
|
||||
# Everything the client receives, in order
|
||||
received: list[EntityState | HomeassistantServiceCall] = []
|
||||
|
||||
def on_service_call(call: HomeassistantServiceCall) -> None:
|
||||
received.append(call)
|
||||
calls.put_nowait(call)
|
||||
|
||||
async with (
|
||||
run_compiled(yaml_config, line_callback=waiter.callback),
|
||||
api_client_connected() as client,
|
||||
):
|
||||
await client.device_info()
|
||||
assert waiter.count("Synced trigger entered") == 0
|
||||
|
||||
client.subscribe_home_assistant_states_and_services(
|
||||
on_state=received.append,
|
||||
on_service_call=on_service_call,
|
||||
on_state_sub=lambda *_: None,
|
||||
)
|
||||
await waiter.wait_for_each(
|
||||
"Synced trigger entered", "Synced trigger fired", timeout=5.0
|
||||
)
|
||||
call = await asyncio.wait_for(calls.get(), timeout=5.0)
|
||||
assert call.service == "esphome.client_synced"
|
||||
assert call.is_event
|
||||
# The initial switch state arrives before the trigger's event
|
||||
synced_at = received.index(call)
|
||||
switch_states = [
|
||||
i
|
||||
for i, item in enumerate(received[:synced_at])
|
||||
if isinstance(item, SwitchState)
|
||||
]
|
||||
assert switch_states, received
|
||||
assert received[switch_states[0]].state is True
|
||||
@@ -0,0 +1,100 @@
|
||||
"""Integration test that every way a feedback cover starts moving wakes its idle loop."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Awaitable
|
||||
|
||||
from aioesphomeapi import CoverInfo, CoverOperation, CoverState, EntityState, SwitchInfo
|
||||
import pytest
|
||||
|
||||
from .log_utils import LineWaiter
|
||||
from .state_utils import StateWaiter, require_entity, subscribe_after_initial_states
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
|
||||
def _expect_idle(waiter: StateWaiter, cover: CoverInfo) -> Awaitable[EntityState]:
|
||||
return waiter.expect(
|
||||
lambda s: (
|
||||
isinstance(s, CoverState)
|
||||
and s.key == cover.key
|
||||
and s.current_operation == CoverOperation.IDLE
|
||||
),
|
||||
label=f"{cover.name} IDLE",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_feedback_cover_loop_wake(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""Test that target position, max_duration and movement sensors still work with the loop idled."""
|
||||
lines = LineWaiter()
|
||||
max_stopped = lines.expect("Max duration reached")
|
||||
async with (
|
||||
run_compiled(yaml_config, line_callback=lines.callback),
|
||||
api_client_connected() as client,
|
||||
):
|
||||
sensor_states: list[CoverState] = []
|
||||
entities, _ = await client.list_entities_services()
|
||||
time_cover = require_entity(entities, "time_cover", CoverInfo)
|
||||
max_cover = require_entity(entities, "max_duration_cover", CoverInfo)
|
||||
sensor_cover = require_entity(entities, "sensor_cover", CoverInfo)
|
||||
|
||||
def on_state(state: EntityState) -> None:
|
||||
if isinstance(state, CoverState) and state.key == sensor_cover.key:
|
||||
sensor_states.append(state)
|
||||
|
||||
sub = await subscribe_after_initial_states(client, entities, on_state=on_state)
|
||||
waiter = sub.waiter
|
||||
|
||||
# Positions are restored from the previous run, so pick targets away from them
|
||||
start = sub.initial_states[time_cover.key].position
|
||||
target = 0.2 if start >= 0.5 else 0.8
|
||||
|
||||
# Only loop() stops a time-only cover at its target
|
||||
done = _expect_idle(waiter, time_cover)
|
||||
client.cover_command(key=time_cover.key, position=target)
|
||||
state = await done
|
||||
assert state.position == pytest.approx(target, abs=0.05)
|
||||
|
||||
# Stop mid move, then a new command must move it again and stop at the target
|
||||
stopped = _expect_idle(waiter, time_cover)
|
||||
client.cover_command(key=time_cover.key, position=start)
|
||||
await asyncio.sleep(0.3)
|
||||
client.cover_command(key=time_cover.key, stop=True)
|
||||
mid = (await stopped).position
|
||||
assert mid != pytest.approx(target, abs=0.05)
|
||||
done = _expect_idle(waiter, time_cover)
|
||||
client.cover_command(key=time_cover.key, position=target)
|
||||
state = await done
|
||||
assert state.position == pytest.approx(target, abs=0.05)
|
||||
|
||||
# 1s max_duration against a 3s travel
|
||||
max_start = sub.initial_states[max_cover.key].position
|
||||
max_target = 0.0 if max_start >= 0.5 else 1.0
|
||||
done = _expect_idle(waiter, max_cover)
|
||||
client.cover_command(key=max_cover.key, position=max_target)
|
||||
state = await done
|
||||
await asyncio.wait_for(max_stopped, 1.0)
|
||||
assert abs(state.position - max_start) == pytest.approx(1 / 3, abs=0.1)
|
||||
|
||||
# Moved by the movement sensor alone, no command
|
||||
sensor_start = sub.initial_states[sensor_cover.key].position
|
||||
switch_name = "external_close" if sensor_start >= 0.5 else "external_open"
|
||||
switch = require_entity(entities, switch_name, SwitchInfo)
|
||||
sensor_states.clear()
|
||||
client.switch_command(switch.key, True)
|
||||
await asyncio.sleep(1.2)
|
||||
settled = _expect_idle(waiter, sensor_cover)
|
||||
client.switch_command(switch.key, False)
|
||||
await settled
|
||||
moving = {
|
||||
round(s.position, 3)
|
||||
for s in sensor_states
|
||||
if s.current_operation != CoverOperation.IDLE
|
||||
}
|
||||
# One state from the sensor callback, the rest are loop() updates every 200ms
|
||||
assert len(moving) >= 4, sensor_states
|
||||
Reference in new issue
Block a user