From eb3e5cf446a486ffb560160735027994b3ee08fe Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 26 Nov 2020 11:46:59 +0100 Subject: [PATCH] Suggest folder when importing blueprint and store source_url (#43650) --- homeassistant/components/blueprint/importer.py | 13 ++++++++----- homeassistant/components/blueprint/websocket_api.py | 1 - tests/components/blueprint/test_importer.py | 10 ++++++++-- tests/components/blueprint/test_websocket_api.py | 4 ++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/blueprint/importer.py b/homeassistant/components/blueprint/importer.py index 0f229ec8a07..f538c0897f0 100644 --- a/homeassistant/components/blueprint/importer.py +++ b/homeassistant/components/blueprint/importer.py @@ -41,7 +41,6 @@ COMMUNITY_TOPIC_SCHEMA = vol.Schema( class ImportedBlueprint: """Imported blueprint.""" - url: str suggested_filename: str raw_data: str blueprint: Blueprint @@ -125,7 +124,9 @@ def _extract_blueprint_from_community_topic( if blueprint is None: return None - return ImportedBlueprint(url, topic["slug"], block_content, blueprint) + return ImportedBlueprint( + f'{post["username"]}/{topic["slug"]}', block_content, blueprint + ) async def fetch_blueprint_from_community_post( @@ -159,18 +160,20 @@ async def fetch_blueprint_from_github_url( blueprint = Blueprint(data) parsed_import_url = yarl.URL(import_url) - suggested_filename = f"{parsed_import_url.parts[1]}-{parsed_import_url.parts[-1]}" + suggested_filename = f"{parsed_import_url.parts[1]}/{parsed_import_url.parts[-1]}" if suggested_filename.endswith(".yaml"): suggested_filename = suggested_filename[:-5] - return ImportedBlueprint(url, suggested_filename, raw_yaml, blueprint) + return ImportedBlueprint(suggested_filename, raw_yaml, blueprint) async def fetch_blueprint_from_url(hass: HomeAssistant, url: str) -> ImportedBlueprint: """Get a blueprint from a url.""" for func in (fetch_blueprint_from_community_post, fetch_blueprint_from_github_url): try: - return await func(hass, url) + imported_bp = await func(hass, url) + imported_bp.blueprint.update_metadata(source_url=url) + return imported_bp except ValueError: pass diff --git a/homeassistant/components/blueprint/websocket_api.py b/homeassistant/components/blueprint/websocket_api.py index 1e6971d9bc8..6968d4530cd 100644 --- a/homeassistant/components/blueprint/websocket_api.py +++ b/homeassistant/components/blueprint/websocket_api.py @@ -79,7 +79,6 @@ async def ws_import_blueprint(hass, connection, msg): connection.send_result( msg["id"], { - "url": imported_blueprint.url, "suggested_filename": imported_blueprint.suggested_filename, "raw_data": imported_blueprint.raw_data, "blueprint": { diff --git a/tests/components/blueprint/test_importer.py b/tests/components/blueprint/test_importer.py index 263f82ab230..3fa3dbab2f1 100644 --- a/tests/components/blueprint/test_importer.py +++ b/tests/components/blueprint/test_importer.py @@ -56,7 +56,6 @@ def test_extract_blueprint_from_community_topic(community_post): "http://example.com", json.loads(community_post) ) assert imported_blueprint is not None - assert imported_blueprint.url == "http://example.com" assert imported_blueprint.blueprint.domain == "automation" assert imported_blueprint.blueprint.placeholders == { "service_to_call", @@ -79,7 +78,7 @@ def test_extract_blueprint_from_community_topic_invalid_yaml(): ) -def test__extract_blueprint_from_community_topic_wrong_lang(): +def test_extract_blueprint_from_community_topic_wrong_lang(): """Test extracting blueprint with invalid YAML.""" assert ( importer._extract_blueprint_from_community_topic( @@ -110,6 +109,11 @@ async def test_fetch_blueprint_from_community_url(hass, aioclient_mock, communit "service_to_call", "trigger_event", } + assert imported_blueprint.suggested_filename == "balloob/test-topic" + assert ( + imported_blueprint.blueprint.metadata["source_url"] + == "https://community.home-assistant.io/t/test-topic/123/2" + ) @pytest.mark.parametrize( @@ -135,3 +139,5 @@ async def test_fetch_blueprint_from_github_url(hass, aioclient_mock, url): "service_to_call", "trigger_event", } + assert imported_blueprint.suggested_filename == "balloob/motion_light" + assert imported_blueprint.blueprint.metadata["source_url"] == url diff --git a/tests/components/blueprint/test_websocket_api.py b/tests/components/blueprint/test_websocket_api.py index 32b7e7748e5..d79463ac3a6 100644 --- a/tests/components/blueprint/test_websocket_api.py +++ b/tests/components/blueprint/test_websocket_api.py @@ -85,14 +85,14 @@ async def test_import_blueprint(hass, aioclient_mock, hass_ws_client): assert msg["id"] == 5 assert msg["success"] assert msg["result"] == { - "suggested_filename": "balloob-motion_light", - "url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml", + "suggested_filename": "balloob/motion_light", "raw_data": raw_data, "blueprint": { "metadata": { "domain": "automation", "input": {"service_to_call": None, "trigger_event": None}, "name": "Call service based on event", + "source_url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml", }, }, "validation_errors": None,