Fix importing blueprints from forums with HTML entities (#44098)
This commit is contained in:
parent
9cce6e91f1
commit
d9a86c1145
3 changed files with 391 additions and 440 deletions
|
@ -1,5 +1,6 @@
|
||||||
"""Import logic for blueprint."""
|
"""Import logic for blueprint."""
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
import html
|
||||||
import re
|
import re
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
@ -110,7 +111,7 @@ def _extract_blueprint_from_community_topic(
|
||||||
block_content = block_content.strip()
|
block_content = block_content.strip()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = yaml.parse_yaml(block_content)
|
data = yaml.parse_yaml(html.unescape(block_content))
|
||||||
except HomeAssistantError:
|
except HomeAssistantError:
|
||||||
if block_syntax == "yaml":
|
if block_syntax == "yaml":
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -16,6 +16,70 @@ def community_post():
|
||||||
return load_fixture("blueprint/community_post.json")
|
return load_fixture("blueprint/community_post.json")
|
||||||
|
|
||||||
|
|
||||||
|
COMMUNITY_POST_INPUTS = {
|
||||||
|
"remote": {
|
||||||
|
"name": "Remote",
|
||||||
|
"description": "IKEA remote to use",
|
||||||
|
"selector": {
|
||||||
|
"device": {
|
||||||
|
"integration": "zha",
|
||||||
|
"manufacturer": "IKEA of Sweden",
|
||||||
|
"model": "TRADFRI remote control",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"light": {
|
||||||
|
"name": "Light(s)",
|
||||||
|
"description": "The light(s) to control",
|
||||||
|
"selector": {"target": {"entity": {"domain": "light"}}},
|
||||||
|
},
|
||||||
|
"force_brightness": {
|
||||||
|
"name": "Force turn on brightness",
|
||||||
|
"description": 'Force the brightness to the set level below, when the "on" button on the remote is pushed and lights turn on.\n',
|
||||||
|
"default": False,
|
||||||
|
"selector": {"boolean": {}},
|
||||||
|
},
|
||||||
|
"brightness": {
|
||||||
|
"name": "Brightness",
|
||||||
|
"description": "Brightness of the light(s) when turning on",
|
||||||
|
"default": 50,
|
||||||
|
"selector": {
|
||||||
|
"number": {
|
||||||
|
"min": 0.0,
|
||||||
|
"max": 100.0,
|
||||||
|
"mode": "slider",
|
||||||
|
"step": 1.0,
|
||||||
|
"unit_of_measurement": "%",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"button_left_short": {
|
||||||
|
"name": "Left button - short press",
|
||||||
|
"description": "Action to run on short left button press",
|
||||||
|
"default": [],
|
||||||
|
"selector": {"action": {}},
|
||||||
|
},
|
||||||
|
"button_left_long": {
|
||||||
|
"name": "Left button - long press",
|
||||||
|
"description": "Action to run on long left button press",
|
||||||
|
"default": [],
|
||||||
|
"selector": {"action": {}},
|
||||||
|
},
|
||||||
|
"button_right_short": {
|
||||||
|
"name": "Right button - short press",
|
||||||
|
"description": "Action to run on short right button press",
|
||||||
|
"default": [],
|
||||||
|
"selector": {"action": {}},
|
||||||
|
},
|
||||||
|
"button_right_long": {
|
||||||
|
"name": "Right button - long press",
|
||||||
|
"description": "Action to run on long right button press",
|
||||||
|
"default": [],
|
||||||
|
"selector": {"action": {}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_get_community_post_import_url():
|
def test_get_community_post_import_url():
|
||||||
"""Test variations of generating import forum url."""
|
"""Test variations of generating import forum url."""
|
||||||
assert (
|
assert (
|
||||||
|
@ -57,10 +121,7 @@ def test_extract_blueprint_from_community_topic(community_post):
|
||||||
)
|
)
|
||||||
assert imported_blueprint is not None
|
assert imported_blueprint is not None
|
||||||
assert imported_blueprint.blueprint.domain == "automation"
|
assert imported_blueprint.blueprint.domain == "automation"
|
||||||
assert imported_blueprint.blueprint.inputs == {
|
assert imported_blueprint.blueprint.inputs == COMMUNITY_POST_INPUTS
|
||||||
"service_to_call": None,
|
|
||||||
"trigger_event": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def test_extract_blueprint_from_community_topic_invalid_yaml():
|
def test_extract_blueprint_from_community_topic_invalid_yaml():
|
||||||
|
@ -103,11 +164,11 @@ async def test_fetch_blueprint_from_community_url(hass, aioclient_mock, communit
|
||||||
)
|
)
|
||||||
assert isinstance(imported_blueprint, importer.ImportedBlueprint)
|
assert isinstance(imported_blueprint, importer.ImportedBlueprint)
|
||||||
assert imported_blueprint.blueprint.domain == "automation"
|
assert imported_blueprint.blueprint.domain == "automation"
|
||||||
assert imported_blueprint.blueprint.inputs == {
|
assert imported_blueprint.blueprint.inputs == COMMUNITY_POST_INPUTS
|
||||||
"service_to_call": None,
|
assert (
|
||||||
"trigger_event": None,
|
imported_blueprint.suggested_filename
|
||||||
}
|
== "frenck/zha-ikea-five-button-remote-for-lights"
|
||||||
assert imported_blueprint.suggested_filename == "balloob/test-topic"
|
)
|
||||||
assert (
|
assert (
|
||||||
imported_blueprint.blueprint.metadata["source_url"]
|
imported_blueprint.blueprint.metadata["source_url"]
|
||||||
== "https://community.home-assistant.io/t/test-topic/123/2"
|
== "https://community.home-assistant.io/t/test-topic/123/2"
|
||||||
|
|
655
tests/fixtures/blueprint/community_post.json
vendored
655
tests/fixtures/blueprint/community_post.json
vendored
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue