mirror of
https://github.com/esphome/esphome.git
synced 2026-05-10 05:37:55 +08:00
[packages] fix support packages: !include mypackages.yaml (#15677)
This commit is contained in:
committed by
Jesse Hills
parent
0c06d78a4f
commit
6d894dd6ee
@@ -321,12 +321,15 @@ def _walk_packages(
|
||||
return config
|
||||
packages = config[CONF_PACKAGES]
|
||||
|
||||
if not isinstance(packages, (dict, list)):
|
||||
raise cv.Invalid(
|
||||
f"Packages must be a key to value mapping or list, got {type(packages)} instead"
|
||||
)
|
||||
|
||||
with cv.prepend_path(CONF_PACKAGES):
|
||||
if isinstance(packages, yaml_util.IncludeFile):
|
||||
# If the packages key is an IncludeFile, resolve it first before processing.
|
||||
packages, _ = resolve_include(packages, [], context, strict_undefined=False)
|
||||
if not isinstance(packages, (dict, list)):
|
||||
raise cv.Invalid(
|
||||
f"Packages must be a key to value mapping or list, got {type(packages)} instead"
|
||||
)
|
||||
|
||||
if not isinstance(packages, dict):
|
||||
_walk_package_list(packages, callback, context)
|
||||
elif (result := _walk_package_dict(packages, callback, context)) is not None:
|
||||
|
||||
@@ -1106,6 +1106,51 @@ def test_packages_invalid_type_raises() -> None:
|
||||
do_packages_pass(config)
|
||||
|
||||
|
||||
@patch("esphome.components.packages.resolve_include")
|
||||
def test_packages_include_file_resolves_to_list(mock_resolve_include) -> None:
|
||||
"""When packages: is an IncludeFile that resolves to a list, it is processed correctly."""
|
||||
include_file = MagicMock(spec=IncludeFile)
|
||||
package_content = {CONF_WIFI: {CONF_SSID: TEST_PACKAGE_WIFI_SSID}}
|
||||
mock_resolve_include.return_value = ([package_content], None)
|
||||
|
||||
config = {CONF_PACKAGES: include_file}
|
||||
result = do_packages_pass(config)
|
||||
result = merge_packages(result)
|
||||
|
||||
assert result == {CONF_WIFI: {CONF_SSID: TEST_PACKAGE_WIFI_SSID}}
|
||||
|
||||
|
||||
@patch("esphome.components.packages.resolve_include")
|
||||
def test_packages_include_file_resolves_to_dict(mock_resolve_include) -> None:
|
||||
"""When packages: is an IncludeFile that resolves to a dict, it is processed correctly."""
|
||||
include_file = MagicMock(spec=IncludeFile)
|
||||
package_content = {CONF_WIFI: {CONF_SSID: TEST_PACKAGE_WIFI_SSID}}
|
||||
mock_resolve_include.return_value = ({"network": package_content}, None)
|
||||
|
||||
config = {CONF_PACKAGES: include_file}
|
||||
result = do_packages_pass(config)
|
||||
result = merge_packages(result)
|
||||
|
||||
assert result == {CONF_WIFI: {CONF_SSID: TEST_PACKAGE_WIFI_SSID}}
|
||||
|
||||
|
||||
@patch("esphome.components.packages.resolve_include")
|
||||
def test_packages_include_file_resolves_to_invalid_type_raises(
|
||||
mock_resolve_include,
|
||||
) -> None:
|
||||
"""When packages: is an IncludeFile that resolves to an invalid type, cv.Invalid is raised."""
|
||||
include_file = MagicMock(spec=IncludeFile)
|
||||
mock_resolve_include.return_value = ("not_a_dict_or_list", None)
|
||||
|
||||
config = {CONF_PACKAGES: include_file}
|
||||
with pytest.raises(
|
||||
cv.Invalid, match="Packages must be a key to value mapping or list"
|
||||
) as exc_info:
|
||||
do_packages_pass(config)
|
||||
|
||||
assert exc_info.value.path == [CONF_PACKAGES]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_package",
|
||||
[
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
wifi:
|
||||
password: pkg_password
|
||||
ssid: main_ssid
|
||||
@@ -0,0 +1,4 @@
|
||||
packages: !include 13-packages_list.yaml
|
||||
|
||||
wifi:
|
||||
ssid: main_ssid
|
||||
@@ -0,0 +1,2 @@
|
||||
- wifi:
|
||||
password: pkg_password
|
||||
@@ -0,0 +1,3 @@
|
||||
wifi:
|
||||
password: pkg_password
|
||||
ssid: main_ssid
|
||||
@@ -0,0 +1,4 @@
|
||||
packages: !include 14-packages_dict.yaml
|
||||
|
||||
wifi:
|
||||
ssid: main_ssid
|
||||
@@ -0,0 +1,3 @@
|
||||
network:
|
||||
wifi:
|
||||
password: pkg_password
|
||||
Reference in New Issue
Block a user