diff --git a/esphome/components/wifi/__init__.py b/esphome/components/wifi/__init__.py index 316d4321403..bad57fc4810 100644 --- a/esphome/components/wifi/__init__.py +++ b/esphome/components/wifi/__init__.py @@ -76,11 +76,17 @@ def variant_has_wifi(variant: str) -> bool: Variants without a native PHY (ESP32-H2, ESP32-P4) need the ``esp32_hosted`` co-processor to use ``wifi:``. + Case-insensitive on *variant* so external callers can pass either + the upstream uppercase form (e.g. ``"ESP32H2"`` from + ``const.VARIANT_ESP32H2``) or a lowercase form their own enum + surfaces (e.g. ``"esp32h2"`` from device-builder's + ``Esp32Variant``). Both classify identically. + Used by device-builder (esphome/device-builder) to decide whether its basic-setup wizard emits a ``wifi:`` block — please keep the signature stable. """ - return variant not in NO_WIFI_VARIANTS + return variant.upper() not in NO_WIFI_VARIANTS _WIFI_FIRST_PLATFORMS: frozenset[str] = frozenset( diff --git a/tests/unit_tests/components/test_wifi.py b/tests/unit_tests/components/test_wifi.py index e93ae4b5036..71a14d78177 100644 --- a/tests/unit_tests/components/test_wifi.py +++ b/tests/unit_tests/components/test_wifi.py @@ -10,27 +10,44 @@ from esphome.const import Platform @pytest.mark.parametrize( "variant", [ + # Upstream's canonical uppercase form. const.VARIANT_ESP32, const.VARIANT_ESP32S2, const.VARIANT_ESP32S3, const.VARIANT_ESP32C3, const.VARIANT_ESP32C6, + # Lowercase form external callers (e.g. device-builder's + # ``Esp32Variant`` StrEnum) surface. + "esp32", + "esp32s3", + "esp32c3", + # Mixed-case — defence in depth against future callers that + # pull the value off some other serialisation. + "Esp32", ], ) def test_variant_has_wifi_for_native_phy_variants(variant: str) -> None: - """Variants with a native WiFi PHY → True.""" + """Variants with a native WiFi PHY → True, case-insensitive.""" assert variant_has_wifi(variant) is True @pytest.mark.parametrize( "variant", [ + # Upstream's canonical uppercase form. const.VARIANT_ESP32H2, const.VARIANT_ESP32P4, + # Lowercase form external callers (e.g. device-builder's + # ``Esp32Variant`` StrEnum) surface. + "esp32h2", + "esp32p4", + # Mixed-case — defence in depth against future callers that + # pull the value off some other serialisation. + "Esp32H2", ], ) def test_variant_has_wifi_for_no_phy_variants(variant: str) -> None: - """Variants that need ``esp32_hosted`` → False.""" + """Variants that need ``esp32_hosted`` → False, case-insensitive.""" assert variant_has_wifi(variant) is False @@ -44,6 +61,18 @@ def test_has_native_wifi_dispatches_esp32_to_variant_check() -> None: ) +def test_has_native_wifi_esp32_variant_case_insensitive() -> None: + """has_native_wifi accepts lowercase variant input. + + External callers (device-builder's wizard, etc.) may surface + variant strings from their own enums that don't match upstream's + uppercase convention. The dispatcher should classify them + identically. + """ + assert has_native_wifi(platform=Platform.ESP32, variant="esp32h2") is False + assert has_native_wifi(platform=Platform.ESP32, variant="esp32c3") is True + + def test_has_native_wifi_dispatches_rp2040_to_board_check() -> None: """RP2040 platform routes through ``rp2040.board_id_has_wifi``.""" assert has_native_wifi(platform=Platform.RP2040, board="rpipicow") is True