mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 10:12:52 +00:00
[emontx] Add pause/resume actions to let another component take over the UART (#18756)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
5cdefa910f
commit
36fbf5bf22
@@ -5,3 +5,21 @@ button:
|
||||
- emontx.send_command:
|
||||
id: test_emontx
|
||||
command: !lambda return "l";
|
||||
- platform: template
|
||||
name: Pause emontx
|
||||
on_press:
|
||||
- if:
|
||||
condition:
|
||||
emontx.is_paused: test_emontx
|
||||
then:
|
||||
- emontx.resume: test_emontx
|
||||
else:
|
||||
- emontx.pause: test_emontx
|
||||
- platform: template
|
||||
name: Pause emontx explicit id
|
||||
on_press:
|
||||
- emontx.pause:
|
||||
id: test_emontx
|
||||
- delay: 1s
|
||||
- emontx.resume:
|
||||
id: test_emontx
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -91,4 +92,70 @@ TEST_F(EmonTxTest, OverflowAcrossLoopCalls) {
|
||||
EXPECT_EQ(this->lines_[0], "MSG:3");
|
||||
}
|
||||
|
||||
TEST_F(EmonTxTest, LoopLeavesBytesUntouchedWhenPaused) {
|
||||
this->uart_.push_rx("{\"P1\":123}\n");
|
||||
this->sut_.set_paused(true);
|
||||
this->sut_.loop();
|
||||
|
||||
EXPECT_EQ(this->uart_.available(), std::strlen("{\"P1\":123}\n"));
|
||||
EXPECT_TRUE(this->lines_.empty());
|
||||
}
|
||||
|
||||
TEST_F(EmonTxTest, SendCommandIsSuppressedWhilePaused) {
|
||||
this->sut_.set_paused(true);
|
||||
this->sut_.send_command("x");
|
||||
EXPECT_TRUE(this->uart_.tx.empty());
|
||||
|
||||
this->sut_.set_paused(false);
|
||||
this->sut_.send_command("x");
|
||||
EXPECT_EQ(std::string(this->uart_.tx.begin(), this->uart_.tx.end()), "x\n");
|
||||
}
|
||||
|
||||
// Neither the part before the pause nor the part after it may reach on_data
|
||||
TEST_F(EmonTxTest, ResumeDiscardsInterruptedLine) {
|
||||
this->uart_.push_rx("{\"P1\":4");
|
||||
this->sut_.loop();
|
||||
this->sut_.set_paused(true);
|
||||
this->sut_.loop();
|
||||
this->sut_.set_paused(false);
|
||||
|
||||
this->uart_.push_rx("56}\n");
|
||||
this->sut_.loop();
|
||||
EXPECT_TRUE(this->lines_.empty());
|
||||
|
||||
this->uart_.push_rx("{\"P1\":789}\n");
|
||||
this->sut_.loop();
|
||||
ASSERT_EQ(this->lines_.size(), 1u);
|
||||
EXPECT_EQ(this->lines_[0], "{\"P1\":789}");
|
||||
}
|
||||
|
||||
TEST_F(EmonTxTest, ResumeDropsBacklogReceivedWhilePaused) {
|
||||
this->sut_.set_paused(true);
|
||||
this->sut_.loop();
|
||||
this->uart_.push_rx("{\"P1\":1}\n{\"P1\":2}\n{\"P1\":");
|
||||
this->sut_.set_paused(false);
|
||||
|
||||
this->sut_.loop();
|
||||
EXPECT_EQ(this->uart_.available(), 0u);
|
||||
EXPECT_TRUE(this->lines_.empty());
|
||||
|
||||
this->uart_.push_rx("3}\n{\"P1\":4}\n");
|
||||
this->sut_.loop();
|
||||
ASSERT_EQ(this->lines_.size(), 1u);
|
||||
EXPECT_EQ(this->lines_[0], "{\"P1\":4}");
|
||||
}
|
||||
|
||||
TEST_F(EmonTxTest, ResumeAfterCompleteBacklogKeepsNextLine) {
|
||||
this->sut_.set_paused(true);
|
||||
this->sut_.loop();
|
||||
this->uart_.push_rx("{\"P1\":1}\n");
|
||||
this->sut_.set_paused(false);
|
||||
|
||||
this->sut_.loop();
|
||||
this->uart_.push_rx("{\"P1\":2}\n");
|
||||
this->sut_.loop();
|
||||
ASSERT_EQ(this->lines_.size(), 1u);
|
||||
EXPECT_EQ(this->lines_[0], "{\"P1\":2}");
|
||||
}
|
||||
|
||||
} // namespace esphome::emontx::testing
|
||||
|
||||
Reference in New Issue
Block a user