Files
esphome/tests/components/binary_sensor/raw_callback_test.cpp
T
d1c1f10be8 [binary_sensor] Add on_raw_value trigger (#21126)
Co-authored-by: Philip Poten <philip@poten.at>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-28 10:06:32 -10:00

41 lines
1.3 KiB
C++

#include <gtest/gtest.h>
#include <vector>
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/binary_sensor/filter.h"
namespace esphome::binary_sensor::testing {
class BinarySensorRawCallbackTest : public ::testing::Test {
protected:
void SetUp() override {
this->sensor_.add_on_raw_state_callback([this](bool value) { this->raw_.push_back(value); });
this->sensor_.add_on_state_callback([this](bool value) { this->state_.push_back(value); });
}
BinarySensor sensor_;
std::vector<bool> raw_;
std::vector<bool> state_;
};
TEST_F(BinarySensorRawCallbackTest, FiresOnRepeatedValues) {
this->sensor_.publish_state(true);
this->sensor_.publish_state(true);
this->sensor_.publish_state(false);
this->sensor_.publish_state(false);
this->sensor_.invalidate_state();
EXPECT_EQ(this->raw_, (std::vector<bool>{true, true, false, false}));
EXPECT_EQ(this->state_, (std::vector<bool>{true, false}));
}
TEST_F(BinarySensorRawCallbackTest, FiresBeforeFilters) {
InvertFilter invert;
this->sensor_.add_filter(&invert);
this->sensor_.publish_state(true);
this->sensor_.publish_state(true);
EXPECT_EQ(this->raw_, (std::vector<bool>{true, true}));
EXPECT_EQ(this->state_, (std::vector<bool>{false}));
}
} // namespace esphome::binary_sensor::testing