Allow overriding blueprints on import (#103340)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
af7155df7a
commit
1cfbdd6a5d
6 changed files with 159 additions and 23 deletions
|
@ -3,6 +3,7 @@ from pathlib import Path
|
|||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -129,6 +130,52 @@ async def test_import_blueprint(
|
|||
},
|
||||
},
|
||||
"validation_errors": None,
|
||||
"exists": False,
|
||||
}
|
||||
|
||||
|
||||
async def test_import_blueprint_update(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
setup_bp,
|
||||
) -> None:
|
||||
"""Test importing blueprints."""
|
||||
raw_data = Path(
|
||||
hass.config.path("blueprints/automation/in_folder/in_folder_blueprint.yaml")
|
||||
).read_text()
|
||||
|
||||
aioclient_mock.get(
|
||||
"https://raw.githubusercontent.com/in_folder/home-assistant-config/main/blueprints/automation/in_folder_blueprint.yaml",
|
||||
text=raw_data,
|
||||
)
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "blueprint/import",
|
||||
"url": "https://github.com/in_folder/home-assistant-config/blob/main/blueprints/automation/in_folder_blueprint.yaml",
|
||||
}
|
||||
)
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg["id"] == 5
|
||||
assert msg["success"]
|
||||
assert msg["result"] == {
|
||||
"suggested_filename": "in_folder/in_folder_blueprint",
|
||||
"raw_data": raw_data,
|
||||
"blueprint": {
|
||||
"metadata": {
|
||||
"domain": "automation",
|
||||
"input": {"action": None, "trigger": None},
|
||||
"name": "In Folder Blueprint",
|
||||
"source_url": "https://github.com/in_folder/home-assistant-config/blob/main/blueprints/automation/in_folder_blueprint.yaml",
|
||||
}
|
||||
},
|
||||
"validation_errors": None,
|
||||
"exists": True,
|
||||
}
|
||||
|
||||
|
||||
|
@ -212,6 +259,42 @@ async def test_save_existing_file(
|
|||
assert msg["error"] == {"code": "already_exists", "message": "File already exists"}
|
||||
|
||||
|
||||
async def test_save_existing_file_override(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
) -> None:
|
||||
"""Test saving blueprints."""
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
with patch("pathlib.Path.write_text") as write_mock:
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 7,
|
||||
"type": "blueprint/save",
|
||||
"path": "test_event_service",
|
||||
"yaml": 'blueprint: {name: "name", domain: "automation"}',
|
||||
"domain": "automation",
|
||||
"source_url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/test_event_service.yaml",
|
||||
"allow_override": True,
|
||||
}
|
||||
)
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg["id"] == 7
|
||||
assert msg["success"]
|
||||
assert msg["result"] == {"overrides_existing": True}
|
||||
assert yaml.safe_load(write_mock.mock_calls[0][1][0]) == {
|
||||
"blueprint": {
|
||||
"name": "name",
|
||||
"domain": "automation",
|
||||
"source_url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/test_event_service.yaml",
|
||||
"input": {},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async def test_save_file_error(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue