diff --git a/script/scaffold/gather_info.py b/script/scaffold/gather_info.py index 24b2abf710d7..31a8e4fb849b 100644 --- a/script/scaffold/gather_info.py +++ b/script/scaffold/gather_info.py @@ -63,10 +63,16 @@ and has to match the directory name of the integration.""" "oauth2": True, "iot_class": "local_polling", "categories": ["diy_makers"], + "integration_type": "hub", } ) else: - info.update(gather_new_integration(arguments.template == "integration")) + info.update( + gather_new_integration( + arguments.template == "integration", + arguments.template == "config_flow_helper", + ) + ) return Info(**info) @@ -77,7 +83,7 @@ YES_NO = { } -def gather_new_integration(determine_auth: bool) -> Info: +def gather_new_integration(determine_auth: bool, is_helper: bool = False) -> Info: """Gather info about new integration from user.""" fields = { "name": { @@ -118,6 +124,22 @@ More info @ https://contributors.home-assistant.io/core/integrations/manifest/#i ] ], }, + "integration_type": { + "prompt": ( + f"""What is the integration type? + +Valid types are {", ".join(IntegrationType)}. + +More info @ https://contributors.home-assistant.io/core/integrations/manifest/#integration_type +""" + ), + "validators": [ + [ + f"You need to pick one of {', '.join(IntegrationType)}.", + lambda value: value in IntegrationType, + ] + ], + }, } if determine_auth: @@ -140,30 +162,6 @@ More info @ https://contributors.home-assistant.io/core/integrations/manifest/#i "default": "no", **YES_NO, }, - "integration_type": { - "prompt": ( - "What is the integration type?" - "\n\nValid types are " - f"{', '.join(IntegrationType)}." - "\nThis field is recommended and" - " required in some cases." - " To intentionally leave it" - " unset, type 'omit'." - "\n\nMore info @ " - "https://contributors.home-assistant.io" - "/core/integrations/manifest" - "/#integration_type\n" - ), - "validators": [ - [ - "You need to pick one of" - f" {', '.join(IntegrationType)}" - " or 'omit'.", - lambda value: value in IntegrationType or value == "omit", - ] - ], - "converter": lambda value: None if value == "omit" else value, - }, "oauth2": { "prompt": ( "Can the user authenticate the device using OAuth2? (yes/no)" @@ -174,8 +172,14 @@ More info @ https://contributors.home-assistant.io/core/integrations/manifest/#i } ) + if is_helper: + del fields["integration_type"] + info = _gather_info(fields) + if is_helper: + info["integration_type"] = "helper" + if info.get("integration_type") not in ( NO_CATEGORIES_INTEGRATION_TYPES | {IntegrationType.VIRTUAL} ): diff --git a/tests/script/test_scaffold.py b/tests/script/test_scaffold.py index f60530283cff..779b2eb60575 100644 --- a/tests/script/test_scaffold.py +++ b/tests/script/test_scaffold.py @@ -2,15 +2,16 @@ import json from pathlib import Path +from types import SimpleNamespace from unittest.mock import patch import pytest -from script.scaffold.gather_info import gather_new_integration +from script.scaffold.gather_info import gather_info, gather_new_integration from script.scaffold.generate import generate from script.scaffold.model import Info -BASE_ANSWERS = ["Test", "developer", "", "local_polling", "yes", "no"] +BASE_ANSWERS = ["Test", "developer", "", "local_polling"] def test_new_integration_asks_for_categories() -> None: @@ -18,6 +19,8 @@ def test_new_integration_asks_for_categories() -> None: answers = [ *BASE_ANSWERS, "service", + "yes", + "no", "no", "", "not_a_category", @@ -39,13 +42,59 @@ def test_new_integration_without_categories(integration_type: str) -> None: """Test integration types that need no categories are not asked for them.""" with patch( "builtins.input", - side_effect=[*BASE_ANSWERS, integration_type, "no", "energy"], + side_effect=[*BASE_ANSWERS, integration_type, "yes", "no", "no", "energy"], ): info = gather_new_integration(True) assert "categories" not in info +@pytest.mark.parametrize( + ("determine_auth", "auth_answers"), + [(True, ["yes", "no", "no"]), (False, [])], +) +def test_new_integration_requires_integration_type( + determine_auth: bool, auth_answers: list[str] +) -> None: + """Test every new integration gets the type hassfest needs for a config flow.""" + answers = [*BASE_ANSWERS, "", "omit", "service", *auth_answers, "sensors"] + + with patch("builtins.input", side_effect=answers) as mock_input: + info = gather_new_integration(determine_auth) + + assert info["integration_type"] == "service" + assert mock_input.call_count == len(answers) + + +def test_new_helper_integration_is_not_asked_for_integration_type() -> None: + """Test the helper config flow template gets the helper type and categories.""" + answers = [*BASE_ANSWERS, "entity", "helpers"] + arguments = SimpleNamespace( + integration="scaffold_test", develop=False, template="config_flow_helper" + ) + + with patch("builtins.input", side_effect=answers) as mock_input: + info = gather_info(arguments) + + assert info.integration_type == "helper" + assert info.categories == ["helpers"] + assert mock_input.call_count == len(answers) + assert not any( + "What is the integration type?" in call.args[0] + for call in mock_input.call_args_list + ) + + +def test_develop_integration_has_integration_type(tmp_path: Path) -> None: + """Test developer mode sets the type hassfest needs for a config flow.""" + arguments = SimpleNamespace(integration=None, develop=True) + + with patch("script.scaffold.gather_info.COMPONENT_DIR", tmp_path): + info = gather_info(arguments) + + assert info.integration_type == "hub" + + def test_generate_integration_writes_categories(tmp_path: Path) -> None: """Test the generated manifest contains the gathered categories.""" components = tmp_path / "components"