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:
"""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

View File

@ -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": {

View File

@ -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

View File

@ -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,