mirror of
https://github.com/esphome/esphome.git
synced 2026-09-01 10:36:01 +00:00
Move set_internal(), set_disabled_by_default(), set_entity_category(), and set_device() to protected on EntityBase. These were codegen-only setters never intended for runtime use. internal, disabled_by_default, and entity_category are now packed into the existing configure_entity_() uint32 parameter alongside string indices, eliminating up to 3 separate function calls per entity. set_device() is renamed to set_device_() per protected naming convention and remains a separate call (pointer can't be packed). Entity category integer mapping is derived from cv.ENTITY_CATEGORIES to stay in sync with the C++ enum automatically.
87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
"""Tests for the binary sensor component."""
|
|
|
|
|
|
def test_text_is_setup(generate_main):
|
|
"""
|
|
When the binary sensor is set in the yaml file, it should be registered in main
|
|
"""
|
|
# Given
|
|
|
|
# When
|
|
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
|
|
|
# Then
|
|
assert "new template_::TemplateText();" in main_cpp
|
|
assert "App.register_text" in main_cpp
|
|
|
|
|
|
def test_text_sets_mandatory_fields(generate_main):
|
|
"""
|
|
When the mandatory fields are set in the yaml, they should be set in main
|
|
"""
|
|
# Given
|
|
|
|
# When
|
|
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
|
|
|
# Then
|
|
assert 'it_1->configure_entity_("test 1 text",' in main_cpp
|
|
|
|
|
|
def test_text_config_value_internal_set(generate_main):
|
|
"""
|
|
Test that the "internal" config value is correctly set
|
|
"""
|
|
# Given
|
|
|
|
# When
|
|
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
|
|
|
# Then
|
|
# internal flag is now packed into configure_entity_() third argument (bit 24)
|
|
assert "it_2->configure_entity_(" in main_cpp
|
|
assert "it_3->configure_entity_(" in main_cpp
|
|
|
|
|
|
def test_text_config_value_mode_set(generate_main):
|
|
"""
|
|
Test that the "internal" config value is correctly set
|
|
"""
|
|
# Given
|
|
|
|
# When
|
|
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
|
|
|
# Then
|
|
assert "it_1->traits.set_mode(text::TEXT_MODE_TEXT);" in main_cpp
|
|
assert "it_3->traits.set_mode(text::TEXT_MODE_PASSWORD);" in main_cpp
|
|
|
|
|
|
def test_text_config_lamda_is_set(generate_main):
|
|
"""
|
|
Test if lambda is set for lambda mode (optimized with stateless lambda)
|
|
"""
|
|
# Given
|
|
|
|
# When
|
|
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
|
|
|
# Then
|
|
assert "it_4->set_template([]() -> std::optional<std::string> {" in main_cpp
|
|
assert 'return std::string{"Hello"};' in main_cpp
|
|
|
|
|
|
def test_esphome_optional_alias_works(generate_main):
|
|
"""
|
|
Test that esphome::optional alias compiles (backward compatibility)
|
|
"""
|
|
# Given
|
|
|
|
# When
|
|
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
|
|
|
# Then
|
|
# Codegen emits std::optional, but esphome::optional must also work
|
|
# via the using alias in esphome/core/optional.h
|
|
assert "std::optional<std::string>" in main_cpp
|