Strengthen test assertions for configure_entity_ packed values

- Sensor and text_sensor device_class tests now extract and verify the
  packed argument is non-zero instead of just checking call presence
- Remove useless setup_test_environment fixture references in unit tests
This commit is contained in:
J. Nick Koston
2026-03-06 09:43:07 -10:00
parent fd19bb4aa3
commit 69386cce2b
3 changed files with 29 additions and 19 deletions
+13 -2
View File
@@ -1,5 +1,15 @@
"""Tests for the sensor component."""
import re
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_sensor_device_class_set(generate_main):
"""
@@ -10,5 +20,6 @@ def test_sensor_device_class_set(generate_main):
# When
main_cpp = generate_main("tests/component_tests/sensor/test_sensor.yaml")
# Then
assert "s_1->configure_entity_(" in main_cpp
# Then: device_class: voltage means packed value must be non-zero
packed = _extract_packed_value(main_cpp, "s_1")
assert packed != 0
@@ -1,5 +1,15 @@
"""Tests for the text sensor component."""
import re
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_sensor_is_setup(generate_main):
"""
@@ -53,6 +63,9 @@ def test_text_sensor_device_class_set(generate_main):
# When
main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml")
# Then
assert "ts_2->configure_entity_(" in main_cpp
assert "ts_3->configure_entity_(" in main_cpp
# Then: ts_2 has device_class: timestamp, ts_3 has device_class: date
# so their packed values must be non-zero
packed_ts_2 = _extract_packed_value(main_cpp, "ts_2")
assert packed_ts_2 != 0
packed_ts_3 = _extract_packed_value(main_cpp, "ts_3")
assert packed_ts_3 != 0
@@ -309,8 +309,6 @@ def extract_object_id_from_expressions(expressions: list[str]) -> str | None:
async def test_setup_entity_no_duplicates(setup_test_environment: list[str]) -> None:
"""Test setup_entity with unique names."""
setup_test_environment # noqa: F841 - fixture initializes CORE state
# Create mock entities
var1 = MockObj("sensor1")
var2 = MockObj("sensor2")
@@ -344,8 +342,6 @@ async def test_setup_entity_different_platforms(
) -> None:
"""Test that same name on different platforms doesn't conflict."""
setup_test_environment # noqa: F841 - fixture initializes CORE state
# Create mock entities
sensor = MockObj("sensor1")
binary_sensor = MockObj("binary_sensor1")
@@ -392,7 +388,6 @@ async def test_setup_entity_with_devices(
setup_test_environment: list[str], mock_get_variable: dict[ID, MockObj]
) -> None:
"""Test that same name on different devices doesn't conflict."""
setup_test_environment # noqa: F841 - fixture initializes CORE state
# Create mock devices
device1_id = ID("device1", type="Device")
@@ -433,8 +428,6 @@ async def test_setup_entity_with_devices(
async def test_setup_entity_empty_name(setup_test_environment: list[str]) -> None:
"""Test setup_entity with empty entity name."""
setup_test_environment # noqa: F841 - fixture initializes CORE state
var = MockObj("sensor1")
config = {
@@ -455,8 +448,6 @@ async def test_setup_entity_special_characters(
) -> None:
"""Test setup_entity with names containing special characters."""
setup_test_environment # noqa: F841 - fixture initializes CORE state
var = MockObj("sensor1")
config = {
@@ -475,8 +466,6 @@ async def test_setup_entity_special_characters(
async def test_setup_entity_with_icon(setup_test_environment: list[str]) -> None:
"""Test setup_entity sets icon correctly."""
setup_test_environment # noqa: F841 - fixture initializes CORE state
var = MockObj("sensor1")
config = {
@@ -813,7 +802,6 @@ async def test_setup_entity_empty_name_with_mac_suffix(
For empty-name entities, Python passes 0 and C++ calculates the hash
at runtime from friendly_name (bug-for-bug compatibility).
"""
setup_test_environment # noqa: F841 - fixture initializes CORE state
# Set up CORE.config with name_add_mac_suffix enabled
CORE.config = {"name_add_mac_suffix": True}
@@ -844,7 +832,6 @@ async def test_setup_entity_empty_name_with_mac_suffix_no_friendly_name(
at runtime. In this case C++ will hash the empty friendly_name
(bug-for-bug compatibility).
"""
setup_test_environment # noqa: F841 - fixture initializes CORE state
# Set up CORE.config with name_add_mac_suffix enabled
CORE.config = {"name_add_mac_suffix": True}
@@ -874,7 +861,6 @@ async def test_setup_entity_empty_name_no_mac_suffix_no_friendly_name(
For empty-name entities, Python passes 0 and C++ calculates the hash
at runtime from the device name.
"""
setup_test_environment # noqa: F841 - fixture initializes CORE state
# No MAC suffix (either not set or False)
CORE.config = {}