mirror of
https://github.com/esphome/esphome.git
synced 2026-11-17 18:46:08 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ae9e70946 | ||
|
|
21d3bffd80 | ||
|
|
95692c6736 | ||
|
|
2de3d0fce8 | ||
|
|
8381438203 |
No files matched your search
@@ -104,7 +104,9 @@ void FeedbackCover::set_open_sensor(binary_sensor::BinarySensor *open_feedback)
|
||||
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);
|
||||
// In a reversal the other sensor may already report the new direction
|
||||
if (state || this->current_operation == COVER_OPERATION_OPENING)
|
||||
this->set_current_operation_(state ? COVER_OPERATION_OPENING : COVER_OPERATION_IDLE, false);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -119,7 +121,9 @@ void FeedbackCover::set_close_sensor(binary_sensor::BinarySensor *close_feedback
|
||||
this->endstop_reached_(false);
|
||||
}
|
||||
|
||||
this->set_current_operation_(state ? COVER_OPERATION_CLOSING : COVER_OPERATION_IDLE, false);
|
||||
// In a reversal the other sensor may already report the new direction
|
||||
if (state || this->current_operation == COVER_OPERATION_CLOSING)
|
||||
this->set_current_operation_(state ? COVER_OPERATION_CLOSING : COVER_OPERATION_IDLE, false);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -170,6 +174,10 @@ 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;
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
} else if (operation == this->current_trigger_operation_) {
|
||||
this->awaiting_movement_ = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// if it is setting the actual operation (not triggered one) or
|
||||
@@ -180,7 +188,12 @@ void FeedbackCover::set_current_operation_(cover::CoverOperation operation, bool
|
||||
// but initiated externally
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (!is_triggered || (this->open_feedback_ == nullptr || this->close_feedback_ == nullptr))
|
||||
if (is_triggered && this->open_feedback_ != nullptr && this->close_feedback_ != nullptr) {
|
||||
// max_duration counts from the command, or from the end of a movement being reversed
|
||||
this->awaiting_movement_ = operation != COVER_OPERATION_IDLE && operation != this->current_operation;
|
||||
if (this->awaiting_movement_ && this->current_operation == COVER_OPERATION_IDLE)
|
||||
this->start_dir_time_ = App.get_loop_component_start_time();
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
@@ -228,7 +241,9 @@ void FeedbackCover::set_open_obstacle_sensor(binary_sensor::BinarySensor *open_o
|
||||
#endif
|
||||
|
||||
void FeedbackCover::loop() {
|
||||
if (this->current_operation == COVER_OPERATION_IDLE)
|
||||
const bool moving = this->current_operation != COVER_OPERATION_IDLE;
|
||||
// A command waiting for its movement sensor still needs the max_duration check
|
||||
if (!moving && !this->awaiting_movement_)
|
||||
return;
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
|
||||
@@ -238,7 +253,7 @@ void FeedbackCover::loop() {
|
||||
// if we initiated the move, check if we reached position or max time
|
||||
// (stopping from endstop sensor is handled in callback)
|
||||
if (this->current_trigger_operation_ != COVER_OPERATION_IDLE) {
|
||||
if (this->is_at_target_()) {
|
||||
if (moving && this->is_at_target_()) {
|
||||
const bool at_end = this->target_position_ == COVER_OPEN || this->target_position_ == COVER_CLOSED;
|
||||
if (this->has_built_in_endstop_ && at_end) {
|
||||
// Don't trigger stop, let the cover stop by itself.
|
||||
@@ -267,7 +282,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
|
||||
if (now - this->last_publish_time_ > this->update_interval_) {
|
||||
if (moving && now - this->last_publish_time_ > this->update_interval_) {
|
||||
this->publish_state(false);
|
||||
this->last_publish_time_ = now;
|
||||
}
|
||||
@@ -350,7 +365,7 @@ bool FeedbackCover::is_at_target_() const {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
void FeedbackCover::start_direction_(CoverOperation dir) {
|
||||
void FeedbackCover::start_direction_(CoverOperation dir, bool skip_wait) {
|
||||
Trigger<> *trig;
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
@@ -398,12 +413,13 @@ void FeedbackCover::start_direction_(CoverOperation dir) {
|
||||
|
||||
// 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 (!skip_wait && this->direction_change_waittime_.has_value() && 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); });
|
||||
// The movement sensor may still report the old direction, which would reverse again on every pass
|
||||
this->set_timeout(DIRECTION_CHANGE_TIMEOUT_ID, waittime, [this, dir]() { this->start_direction_(dir, true); });
|
||||
} else {
|
||||
this->set_current_operation_(dir, true);
|
||||
this->prev_command_trigger_ = trig;
|
||||
|
||||
@@ -48,7 +48,7 @@ class FeedbackCover final : public cover::Cover, public Component {
|
||||
void control(const cover::CoverCall &call) override;
|
||||
void stop_prev_trigger_();
|
||||
bool is_at_target_() const;
|
||||
void start_direction_(cover::CoverOperation dir);
|
||||
void start_direction_(cover::CoverOperation dir, bool skip_wait = false);
|
||||
void update_operation_(cover::CoverOperation dir);
|
||||
void endstop_reached_(bool open_endstop);
|
||||
void recompute_position_();
|
||||
@@ -78,6 +78,7 @@ class FeedbackCover final : public cover::Cover, public Component {
|
||||
bool has_built_in_endstop_{false};
|
||||
bool assumed_state_{false};
|
||||
bool infer_endstop_{false};
|
||||
bool awaiting_movement_{false};
|
||||
float obstacle_rollback_{0};
|
||||
|
||||
cover::CoverOperation last_operation_{cover::COVER_OPERATION_OPENING};
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
esphome:
|
||||
name: feedback-cover-movement
|
||||
host:
|
||||
api:
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
switch:
|
||||
- platform: template
|
||||
name: "Reverse Opening"
|
||||
id: reverse_opening
|
||||
optimistic: true
|
||||
- platform: template
|
||||
name: "Reverse Closing"
|
||||
id: reverse_closing
|
||||
optimistic: true
|
||||
- platform: template
|
||||
name: "Limit Opening"
|
||||
id: limit_opening
|
||||
optimistic: true
|
||||
- platform: template
|
||||
name: "Flicker Opening"
|
||||
id: flicker_opening
|
||||
optimistic: true
|
||||
- platform: template
|
||||
name: "Dead Reverse Opening"
|
||||
id: dead_reverse_opening
|
||||
optimistic: true
|
||||
- platform: template
|
||||
name: "Overlap Opening"
|
||||
id: overlap_opening
|
||||
optimistic: true
|
||||
- platform: template
|
||||
name: "Overlap Closing"
|
||||
id: overlap_closing
|
||||
optimistic: true
|
||||
|
||||
binary_sensor:
|
||||
- platform: template
|
||||
id: reverse_open_sensor
|
||||
lambda: return id(reverse_opening).state;
|
||||
- platform: template
|
||||
id: reverse_close_sensor
|
||||
lambda: return id(reverse_closing).state;
|
||||
- platform: template
|
||||
id: limit_open_sensor
|
||||
lambda: return id(limit_opening).state;
|
||||
- platform: template
|
||||
id: limit_close_sensor
|
||||
lambda: return false;
|
||||
- platform: template
|
||||
id: flicker_open_sensor
|
||||
lambda: return id(flicker_opening).state;
|
||||
- platform: template
|
||||
id: flicker_close_sensor
|
||||
lambda: return false;
|
||||
- platform: template
|
||||
id: dead_reverse_open_sensor
|
||||
lambda: return id(dead_reverse_opening).state;
|
||||
- platform: template
|
||||
id: dead_reverse_close_sensor
|
||||
lambda: return false;
|
||||
- platform: template
|
||||
id: overlap_open_sensor
|
||||
lambda: return id(overlap_opening).state;
|
||||
- platform: template
|
||||
id: overlap_close_sensor
|
||||
lambda: return id(overlap_closing).state;
|
||||
- platform: template
|
||||
id: stuck_open_sensor
|
||||
lambda: return false;
|
||||
- platform: template
|
||||
id: stuck_close_sensor
|
||||
lambda: return false;
|
||||
|
||||
cover:
|
||||
- platform: feedback
|
||||
name: "Reverse Cover"
|
||||
open_duration: 6s
|
||||
open_sensor: reverse_open_sensor
|
||||
close_duration: 6s
|
||||
close_sensor: reverse_close_sensor
|
||||
direction_change_wait_time: 0s
|
||||
has_built_in_endstop: true
|
||||
infer_endstop_from_movement: true
|
||||
open_action:
|
||||
- logger.log: "REVERSE_OPEN_ACTION"
|
||||
close_action:
|
||||
- logger.log: "REVERSE_CLOSE_ACTION"
|
||||
stop_action:
|
||||
- logger.log: "REVERSE_STOP_ACTION"
|
||||
- platform: feedback
|
||||
name: "Stuck Cover"
|
||||
open_duration: 6s
|
||||
open_sensor: stuck_open_sensor
|
||||
close_duration: 6s
|
||||
close_sensor: stuck_close_sensor
|
||||
max_duration: 500ms
|
||||
open_action:
|
||||
- logger.log: "STUCK_OPEN_ACTION"
|
||||
close_action:
|
||||
- logger.log: "STUCK_CLOSE_ACTION"
|
||||
stop_action:
|
||||
- logger.log: "STUCK_STOP_ACTION"
|
||||
- platform: feedback
|
||||
name: "Limit Cover"
|
||||
open_duration: 6s
|
||||
open_sensor: limit_open_sensor
|
||||
close_duration: 6s
|
||||
close_sensor: limit_close_sensor
|
||||
max_duration: 1s
|
||||
has_built_in_endstop: true
|
||||
open_action:
|
||||
- logger.log: "LIMIT_OPEN_ACTION"
|
||||
close_action:
|
||||
- logger.log: "LIMIT_CLOSE_ACTION"
|
||||
stop_action:
|
||||
- logger.log: "LIMIT_STOP_ACTION"
|
||||
- platform: feedback
|
||||
name: "Flicker Cover"
|
||||
open_duration: 4s
|
||||
open_sensor: flicker_open_sensor
|
||||
close_duration: 4s
|
||||
close_sensor: flicker_close_sensor
|
||||
open_action:
|
||||
- logger.log: "FLICKER_OPEN_ACTION"
|
||||
close_action:
|
||||
- logger.log: "FLICKER_CLOSE_ACTION"
|
||||
stop_action:
|
||||
- logger.log: "FLICKER_STOP_ACTION"
|
||||
- switch.turn_off: flicker_opening
|
||||
- platform: feedback
|
||||
name: "Dead Reverse Cover"
|
||||
open_duration: 6s
|
||||
open_sensor: dead_reverse_open_sensor
|
||||
close_duration: 6s
|
||||
close_sensor: dead_reverse_close_sensor
|
||||
direction_change_wait_time: 0s
|
||||
max_duration: 500ms
|
||||
open_action:
|
||||
- logger.log: "DEAD_OPEN_ACTION"
|
||||
close_action:
|
||||
- logger.log: "DEAD_CLOSE_ACTION"
|
||||
stop_action:
|
||||
- logger.log: "DEAD_STOP_ACTION"
|
||||
- platform: feedback
|
||||
name: "Overlap Cover"
|
||||
open_duration: 6s
|
||||
open_sensor: overlap_open_sensor
|
||||
close_duration: 6s
|
||||
close_sensor: overlap_close_sensor
|
||||
direction_change_wait_time: 0s
|
||||
max_duration: 1s
|
||||
open_action:
|
||||
- logger.log: "OVERLAP_OPEN_ACTION"
|
||||
close_action:
|
||||
- logger.log: "OVERLAP_CLOSE_ACTION"
|
||||
stop_action:
|
||||
- logger.log: "OVERLAP_STOP_ACTION"
|
||||
@@ -0,0 +1,135 @@
|
||||
"""Integration test for a feedback cover driven by open and close movement sensors."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from aioesphomeapi import CoverInfo, CoverOperation, CoverState, EntityState, SwitchInfo
|
||||
import pytest
|
||||
|
||||
from .state_utils import require_entity
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_feedback_cover_movement_sensors(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""Test max_duration, a self-stopping motor, a flickering sensor and reverses with no wait time,
|
||||
including one where the movement sensors overlap."""
|
||||
lines: list[str] = []
|
||||
|
||||
def count(marker: str) -> int:
|
||||
return sum(marker in line for line in lines)
|
||||
|
||||
async with (
|
||||
run_compiled(yaml_config, line_callback=lines.append),
|
||||
api_client_connected() as client,
|
||||
):
|
||||
entities, _ = await client.list_entities_services()
|
||||
states: dict[int, EntityState] = {}
|
||||
client.subscribe_states(lambda state: states.__setitem__(state.key, state))
|
||||
reverse = require_entity(entities, "reverse_cover", CoverInfo, exact=True)
|
||||
stuck = require_entity(entities, "stuck_cover", CoverInfo, exact=True)
|
||||
opening = require_entity(entities, "reverse_opening", SwitchInfo, exact=True)
|
||||
closing = require_entity(entities, "reverse_closing", SwitchInfo, exact=True)
|
||||
limit = require_entity(entities, "limit_cover", CoverInfo, exact=True)
|
||||
limit_opening = require_entity(
|
||||
entities, "limit_opening", SwitchInfo, exact=True
|
||||
)
|
||||
flicker = require_entity(entities, "flicker_cover", CoverInfo, exact=True)
|
||||
flicker_opening = require_entity(
|
||||
entities, "flicker_opening", SwitchInfo, exact=True
|
||||
)
|
||||
dead = require_entity(entities, "dead_reverse_cover", CoverInfo, exact=True)
|
||||
dead_opening = require_entity(
|
||||
entities, "dead_reverse_opening", SwitchInfo, exact=True
|
||||
)
|
||||
overlap = require_entity(entities, "overlap_cover", CoverInfo, exact=True)
|
||||
overlap_opening = require_entity(
|
||||
entities, "overlap_opening", SwitchInfo, exact=True
|
||||
)
|
||||
overlap_closing = require_entity(
|
||||
entities, "overlap_closing", SwitchInfo, exact=True
|
||||
)
|
||||
|
||||
# The motor never reports movement, so max_duration has to stop it
|
||||
client.cover_command(key=stuck.key, position=1.0)
|
||||
await asyncio.sleep(1.5)
|
||||
assert count("STUCK_OPEN_ACTION") == 1
|
||||
assert count("STUCK_STOP_ACTION") == 1
|
||||
|
||||
# The motor stops itself before the estimate reaches the target; max_duration must not fire later
|
||||
client.cover_command(key=limit.key, position=1.0)
|
||||
await asyncio.sleep(0.3)
|
||||
client.switch_command(key=limit_opening.key, state=True)
|
||||
await asyncio.sleep(0.3)
|
||||
client.switch_command(key=limit_opening.key, state=False)
|
||||
await asyncio.sleep(1.5)
|
||||
assert count("LIMIT_OPEN_ACTION") == 1
|
||||
assert count("LIMIT_STOP_ACTION") == 0
|
||||
limit_state = states[limit.key]
|
||||
assert isinstance(limit_state, CoverState)
|
||||
assert limit_state.current_operation == CoverOperation.IDLE
|
||||
|
||||
# A sensor that drops out briefly mid-move must not lose the stop at the target
|
||||
client.cover_command(key=flicker.key, position=0.75)
|
||||
await asyncio.sleep(0.2)
|
||||
client.switch_command(key=flicker_opening.key, state=True)
|
||||
await asyncio.sleep(0.3)
|
||||
client.switch_command(key=flicker_opening.key, state=False)
|
||||
await asyncio.sleep(0.1)
|
||||
client.switch_command(key=flicker_opening.key, state=True)
|
||||
await asyncio.sleep(1.5)
|
||||
assert count("FLICKER_STOP_ACTION") == 1
|
||||
flicker_state = states[flicker.key]
|
||||
assert isinstance(flicker_state, CoverState)
|
||||
assert flicker_state.current_operation == CoverOperation.IDLE
|
||||
assert flicker_state.position == pytest.approx(0.75, abs=0.05)
|
||||
|
||||
# The reversed motor never starts, so max_duration stops it once the old movement ends
|
||||
client.cover_command(key=dead.key, position=1.0)
|
||||
await asyncio.sleep(0.1)
|
||||
client.switch_command(key=dead_opening.key, state=True)
|
||||
await asyncio.sleep(0.1)
|
||||
client.cover_command(key=dead.key, position=0.0)
|
||||
await asyncio.sleep(0.1)
|
||||
client.switch_command(key=dead_opening.key, state=False)
|
||||
await asyncio.sleep(1.0)
|
||||
assert count("DEAD_CLOSE_ACTION") == 1
|
||||
assert count("DEAD_STOP_ACTION") == 2
|
||||
|
||||
# The close sensor starts before the open sensor ends; max_duration must still stop the close
|
||||
client.cover_command(key=overlap.key, position=1.0)
|
||||
await asyncio.sleep(0.1)
|
||||
client.switch_command(key=overlap_opening.key, state=True)
|
||||
await asyncio.sleep(0.1)
|
||||
client.cover_command(key=overlap.key, position=0.0)
|
||||
await asyncio.sleep(0.1)
|
||||
client.switch_command(key=overlap_closing.key, state=True)
|
||||
await asyncio.sleep(0.1)
|
||||
client.switch_command(key=overlap_opening.key, state=False)
|
||||
await asyncio.sleep(0.2)
|
||||
overlap_state = states[overlap.key]
|
||||
assert isinstance(overlap_state, CoverState)
|
||||
assert overlap_state.current_operation == CoverOperation.IS_CLOSING
|
||||
await asyncio.sleep(1.2)
|
||||
assert count("OVERLAP_CLOSE_ACTION") == 1
|
||||
assert count("OVERLAP_STOP_ACTION") == 2
|
||||
|
||||
# Reverse while the open sensor still reports movement for a while
|
||||
client.cover_command(key=reverse.key, position=1.0)
|
||||
await asyncio.sleep(0.3)
|
||||
client.switch_command(key=opening.key, state=True)
|
||||
await asyncio.sleep(0.5)
|
||||
client.cover_command(key=reverse.key, position=0.0)
|
||||
await asyncio.sleep(0.3)
|
||||
client.switch_command(key=opening.key, state=False)
|
||||
await asyncio.sleep(0.3)
|
||||
client.switch_command(key=closing.key, state=True)
|
||||
await asyncio.sleep(0.3)
|
||||
assert count("REVERSE_OPEN_ACTION") == 1
|
||||
assert count("REVERSE_STOP_ACTION") == 1
|
||||
assert count("REVERSE_CLOSE_ACTION") == 1
|
||||
Reference in new issue
Block a user