1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-15 15:20:27 +01:00

[core] make require_framework_version generic (#8412)

This commit is contained in:
tomaszduda23 2025-04-14 00:59:40 +02:00 committed by GitHub
parent 34a4e70cc5
commit 4a9f323d92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 106 additions and 59 deletions

View File

@ -26,7 +26,7 @@ CONFIG_SCHEMA = cv.Schema(
esp32_arduino=cv.Version(0, 0, 0),
esp8266_arduino=cv.Version(0, 0, 0),
rp2040_arduino=cv.Version(0, 0, 0),
bk72xx_libretiny=cv.Version(1, 7, 0),
bk72xx_arduino=cv.Version(1, 7, 0),
),
cv.boolean_false,
),

View File

@ -56,7 +56,6 @@ from esphome.const import (
KEY_CORE,
KEY_FRAMEWORK_VERSION,
KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM,
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_RP2040,
@ -1984,70 +1983,28 @@ def platformio_version_constraint(value):
def require_framework_version(
*,
esp_idf=None,
esp32_arduino=None,
esp8266_arduino=None,
rp2040_arduino=None,
bk72xx_libretiny=None,
host=None,
max_version=False,
extra_message=None,
**kwargs,
):
def validator(value):
core_data = CORE.data[KEY_CORE]
framework = core_data[KEY_TARGET_FRAMEWORK]
if framework == "esp-idf":
if esp_idf is None:
msg = "This feature is incompatible with esp-idf"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
required = esp_idf
elif CORE.is_bk72xx and framework == "arduino":
if bk72xx_libretiny is None:
msg = "This feature is incompatible with BK72XX"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
required = bk72xx_libretiny
elif CORE.is_esp32 and framework == "arduino":
if esp32_arduino is None:
msg = "This feature is incompatible with ESP32 using arduino framework"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
required = esp32_arduino
elif CORE.is_esp8266 and framework == "arduino":
if esp8266_arduino is None:
msg = "This feature is incompatible with ESP8266"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
required = esp8266_arduino
elif CORE.is_rp2040 and framework == "arduino":
if rp2040_arduino is None:
msg = "This feature is incompatible with RP2040"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
required = rp2040_arduino
elif CORE.is_host and framework == "host":
if host is None:
msg = "This feature is incompatible with host platform"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
required = host
else:
raise Invalid(
f"""
Internal Error: require_framework_version does not support this platform configuration
platform: {core_data[KEY_TARGET_PLATFORM]}
framework: {framework}
Please report this issue on GitHub -> https://github.com/esphome/issues/issues/new?template=bug_report.yml.
"""
)
if CORE.is_host and framework == "host":
key = "host"
elif framework == "esp-idf":
key = "esp_idf"
else:
key = CORE.target_platform + "_" + framework
if key not in kwargs:
msg = f"This feature is incompatible with {CORE.target_platform.upper()} using {framework} framework"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
required = kwargs[key]
if max_version:
if core_data[KEY_FRAMEWORK_VERSION] > required:

View File

@ -284,3 +284,93 @@ def test_split_default(framework, platform, variant, full, idf, arduino, simple)
assert schema({}).get("idf") == idf
assert schema({}).get("arduino") == arduino
assert schema({}).get("simple") == simple
@pytest.mark.parametrize(
"framework, platform, message",
[
("esp-idf", PLATFORM_ESP32, "ESP32 using esp-idf framework"),
("arduino", PLATFORM_ESP32, "ESP32 using arduino framework"),
("arduino", PLATFORM_ESP8266, "ESP8266 using arduino framework"),
("arduino", PLATFORM_RP2040, "RP2040 using arduino framework"),
("arduino", PLATFORM_BK72XX, "BK72XX using arduino framework"),
("host", PLATFORM_HOST, "HOST using host framework"),
],
)
def test_require_framework_version(framework, platform, message):
import voluptuous as vol
from esphome.const import (
KEY_CORE,
KEY_FRAMEWORK_VERSION,
KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM,
)
CORE.data[KEY_CORE] = {}
CORE.data[KEY_CORE][KEY_TARGET_PLATFORM] = platform
CORE.data[KEY_CORE][KEY_TARGET_FRAMEWORK] = framework
CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] = config_validation.Version(1, 0, 0)
assert (
config_validation.require_framework_version(
esp_idf=config_validation.Version(0, 5, 0),
esp32_arduino=config_validation.Version(0, 5, 0),
esp8266_arduino=config_validation.Version(0, 5, 0),
rp2040_arduino=config_validation.Version(0, 5, 0),
bk72xx_arduino=config_validation.Version(0, 5, 0),
host=config_validation.Version(0, 5, 0),
extra_message="test 1",
)("test")
== "test"
)
with pytest.raises(
vol.error.Invalid,
match="This feature requires at least framework version 2.0.0. test 2",
):
config_validation.require_framework_version(
esp_idf=config_validation.Version(2, 0, 0),
esp32_arduino=config_validation.Version(2, 0, 0),
esp8266_arduino=config_validation.Version(2, 0, 0),
rp2040_arduino=config_validation.Version(2, 0, 0),
bk72xx_arduino=config_validation.Version(2, 0, 0),
host=config_validation.Version(2, 0, 0),
extra_message="test 2",
)("test")
assert (
config_validation.require_framework_version(
esp_idf=config_validation.Version(1, 5, 0),
esp32_arduino=config_validation.Version(1, 5, 0),
esp8266_arduino=config_validation.Version(1, 5, 0),
rp2040_arduino=config_validation.Version(1, 5, 0),
bk72xx_arduino=config_validation.Version(1, 5, 0),
host=config_validation.Version(1, 5, 0),
max_version=True,
extra_message="test 3",
)("test")
== "test"
)
with pytest.raises(
vol.error.Invalid,
match="This feature requires framework version 0.5.0 or lower. test 4",
):
config_validation.require_framework_version(
esp_idf=config_validation.Version(0, 5, 0),
esp32_arduino=config_validation.Version(0, 5, 0),
esp8266_arduino=config_validation.Version(0, 5, 0),
rp2040_arduino=config_validation.Version(0, 5, 0),
bk72xx_arduino=config_validation.Version(0, 5, 0),
host=config_validation.Version(0, 5, 0),
max_version=True,
extra_message="test 4",
)("test")
with pytest.raises(
vol.error.Invalid, match=f"This feature is incompatible with {message}. test 5"
):
config_validation.require_framework_version(
extra_message="test 5",
)("test")