Suggest folder when importing blueprint and store source_url (#43650)

This commit is contained in:
Paulus Schoutsen 2020-11-26 11:46:59 +01:00 committed by GitHub
parent d4f9c1979f
commit eb3e5cf446
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 10 deletions

View File

@ -41,7 +41,6 @@ COMMUNITY_TOPIC_SCHEMA = vol.Schema(
class ImportedBlueprint: class ImportedBlueprint:
"""Imported blueprint.""" """Imported blueprint."""
url: str
suggested_filename: str suggested_filename: str
raw_data: str raw_data: str
blueprint: Blueprint blueprint: Blueprint
@ -125,7 +124,9 @@ def _extract_blueprint_from_community_topic(
if blueprint is None: if blueprint is None:
return 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( async def fetch_blueprint_from_community_post(
@ -159,18 +160,20 @@ async def fetch_blueprint_from_github_url(
blueprint = Blueprint(data) blueprint = Blueprint(data)
parsed_import_url = yarl.URL(import_url) 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"): if suggested_filename.endswith(".yaml"):
suggested_filename = suggested_filename[:-5] 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: async def fetch_blueprint_from_url(hass: HomeAssistant, url: str) -> ImportedBlueprint:
"""Get a blueprint from a url.""" """Get a blueprint from a url."""
for func in (fetch_blueprint_from_community_post, fetch_blueprint_from_github_url): for func in (fetch_blueprint_from_community_post, fetch_blueprint_from_github_url):
try: 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: except ValueError:
pass pass

View File

@ -79,7 +79,6 @@ async def ws_import_blueprint(hass, connection, msg):
connection.send_result( connection.send_result(
msg["id"], msg["id"],
{ {
"url": imported_blueprint.url,
"suggested_filename": imported_blueprint.suggested_filename, "suggested_filename": imported_blueprint.suggested_filename,
"raw_data": imported_blueprint.raw_data, "raw_data": imported_blueprint.raw_data,
"blueprint": { "blueprint": {

View File

@ -56,7 +56,6 @@ def test_extract_blueprint_from_community_topic(community_post):
"http://example.com", json.loads(community_post) "http://example.com", json.loads(community_post)
) )
assert imported_blueprint is not None assert imported_blueprint is not None
assert imported_blueprint.url == "http://example.com"
assert imported_blueprint.blueprint.domain == "automation" assert imported_blueprint.blueprint.domain == "automation"
assert imported_blueprint.blueprint.placeholders == { assert imported_blueprint.blueprint.placeholders == {
"service_to_call", "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.""" """Test extracting blueprint with invalid YAML."""
assert ( assert (
importer._extract_blueprint_from_community_topic( 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", "service_to_call",
"trigger_event", "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( @pytest.mark.parametrize(
@ -135,3 +139,5 @@ async def test_fetch_blueprint_from_github_url(hass, aioclient_mock, url):
"service_to_call", "service_to_call",
"trigger_event", "trigger_event",
} }
assert imported_blueprint.suggested_filename == "balloob/motion_light"
assert imported_blueprint.blueprint.metadata["source_url"] == url

View File

@ -85,14 +85,14 @@ async def test_import_blueprint(hass, aioclient_mock, hass_ws_client):
assert msg["id"] == 5 assert msg["id"] == 5
assert msg["success"] assert msg["success"]
assert msg["result"] == { assert msg["result"] == {
"suggested_filename": "balloob-motion_light", "suggested_filename": "balloob/motion_light",
"url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml",
"raw_data": raw_data, "raw_data": raw_data,
"blueprint": { "blueprint": {
"metadata": { "metadata": {
"domain": "automation", "domain": "automation",
"input": {"service_to_call": None, "trigger_event": None}, "input": {"service_to_call": None, "trigger_event": None},
"name": "Call service based on event", "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, "validation_errors": None,