From c704d9780454504b6325ff91932b8cb4f66f48eb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Mar 2026 10:54:06 -1000 Subject: [PATCH] Strengthen internal flag assertions in component tests All internal-flag tests now extract the packed value and verify bit 24 is set/clear for the correct entities, instead of just checking configure_entity_() call presence. --- .../binary_sensor/test_binary_sensor.py | 19 +++++++++++++---- tests/component_tests/button/test_button.py | 20 +++++++++++++----- tests/component_tests/text/test_text.py | 21 ++++++++++++++----- .../text_sensor/test_text_sensor.py | 9 ++++---- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/tests/component_tests/binary_sensor/test_binary_sensor.py b/tests/component_tests/binary_sensor/test_binary_sensor.py index 2667e90dda..e029fa19bb 100644 --- a/tests/component_tests/binary_sensor/test_binary_sensor.py +++ b/tests/component_tests/binary_sensor/test_binary_sensor.py @@ -1,5 +1,17 @@ """Tests for the binary sensor component.""" +import re + +_INTERNAL_BIT = 1 << 24 + + +def _extract_packed_value(main_cpp, var_name): + """Extract the third (packed) argument from a configure_entity_ call.""" + pattern = rf"{re.escape(var_name)}->configure_entity_\([^,]+,\s*\w+,\s*(\d+)\)" + match = re.search(pattern, main_cpp) + assert match, f"configure_entity_ call not found for {var_name}" + return int(match.group(1)) + def test_binary_sensor_is_setup(generate_main): """ @@ -44,10 +56,9 @@ def test_binary_sensor_config_value_internal_set(generate_main): "tests/component_tests/binary_sensor/test_binary_sensor.yaml" ) - # Then - # internal flag is now packed into configure_entity_() third argument (bit 24) - assert "bs_1->configure_entity_(" in main_cpp - assert "bs_2->configure_entity_(" in main_cpp + # Then: bs_1 has internal: true, bs_2 has internal: false + assert _extract_packed_value(main_cpp, "bs_1") & _INTERNAL_BIT != 0 + assert _extract_packed_value(main_cpp, "bs_2") & _INTERNAL_BIT == 0 def test_binary_sensor_config_value_use_raw_set(generate_main): diff --git a/tests/component_tests/button/test_button.py b/tests/component_tests/button/test_button.py index cd767dd65b..6973ec0f64 100644 --- a/tests/component_tests/button/test_button.py +++ b/tests/component_tests/button/test_button.py @@ -1,5 +1,17 @@ """Tests for the button component""" +import re + +_INTERNAL_BIT = 1 << 24 + + +def _extract_packed_value(main_cpp, var_name): + """Extract the third (packed) argument from a configure_entity_ call.""" + pattern = rf"{re.escape(var_name)}->configure_entity_\([^,]+,\s*\w+,\s*(\d+)\)" + match = re.search(pattern, main_cpp) + assert match, f"configure_entity_ call not found for {var_name}" + return int(match.group(1)) + def test_button_is_setup(generate_main): """ @@ -39,8 +51,6 @@ def test_button_config_value_internal_set(generate_main): # When main_cpp = generate_main("tests/component_tests/button/test_button.yaml") - # Then - # internal flag is packed into configure_entity_() third argument (bit 24) - # wol_1 has internal: true → bit 24 set → packed value 16777216 - assert "wol_1->configure_entity_(" in main_cpp - assert "wol_2->configure_entity_(" in main_cpp + # Then: wol_1 has internal: true, wol_2 has internal: false + assert _extract_packed_value(main_cpp, "wol_1") & _INTERNAL_BIT != 0 + assert _extract_packed_value(main_cpp, "wol_2") & _INTERNAL_BIT == 0 diff --git a/tests/component_tests/text/test_text.py b/tests/component_tests/text/test_text.py index bad7ad3a33..31c66d8784 100644 --- a/tests/component_tests/text/test_text.py +++ b/tests/component_tests/text/test_text.py @@ -1,4 +1,16 @@ -"""Tests for the binary sensor component.""" +"""Tests for the text component.""" + +import re + +_INTERNAL_BIT = 1 << 24 + + +def _extract_packed_value(main_cpp, var_name): + """Extract the third (packed) argument from a configure_entity_ call.""" + pattern = rf"{re.escape(var_name)}->configure_entity_\([^,]+,\s*\w+,\s*(\d+)\)" + match = re.search(pattern, main_cpp) + assert match, f"configure_entity_ call not found for {var_name}" + return int(match.group(1)) def test_text_is_setup(generate_main): @@ -37,10 +49,9 @@ def test_text_config_value_internal_set(generate_main): # 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 + # Then: it_2 has internal: false, it_3 has internal: true + assert _extract_packed_value(main_cpp, "it_2") & _INTERNAL_BIT == 0 + assert _extract_packed_value(main_cpp, "it_3") & _INTERNAL_BIT != 0 def test_text_config_value_mode_set(generate_main): diff --git a/tests/component_tests/text_sensor/test_text_sensor.py b/tests/component_tests/text_sensor/test_text_sensor.py index 21a273b947..2d77756b2c 100644 --- a/tests/component_tests/text_sensor/test_text_sensor.py +++ b/tests/component_tests/text_sensor/test_text_sensor.py @@ -2,6 +2,8 @@ import re +_INTERNAL_BIT = 1 << 24 + def _extract_packed_value(main_cpp, var_name): """Extract the third (packed) argument from a configure_entity_ call.""" @@ -49,10 +51,9 @@ def test_text_sensor_config_value_internal_set(generate_main): # When main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml") - # Then - # internal flag is now packed into configure_entity_() third argument (bit 24) - assert "ts_2->configure_entity_(" in main_cpp - assert "ts_3->configure_entity_(" in main_cpp + # Then: ts_2 has internal: true, ts_3 has internal: false + assert _extract_packed_value(main_cpp, "ts_2") & _INTERNAL_BIT != 0 + assert _extract_packed_value(main_cpp, "ts_3") & _INTERNAL_BIT == 0 def test_text_sensor_device_class_set(generate_main):