1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00

Add websocket type hints in blueprint (#80531)

This commit is contained in:
epenet 2022-10-19 04:23:17 +02:00 committed by GitHub
parent 971ac015e7
commit c0be1d9fad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 18 deletions

View File

@ -51,7 +51,7 @@ class Blueprint:
def __init__(
self,
data: dict,
data: dict[str, Any],
*,
path: str | None = None,
expected_domain: str | None = None,

View File

@ -1,6 +1,8 @@
"""Websocket API for blueprint."""
from __future__ import annotations
from typing import Any, cast
import async_timeout
import voluptuous as vol
@ -31,12 +33,14 @@ def async_setup(hass: HomeAssistant):
}
)
@websocket_api.async_response
async def ws_list_blueprints(hass, connection, msg):
async def ws_list_blueprints(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""List available blueprints."""
domain_blueprints: dict[str, models.DomainBlueprints] | None = hass.data.get(
DOMAIN, {}
)
results = {}
domain_blueprints: dict[str, models.DomainBlueprints] = hass.data.get(DOMAIN, {})
results: dict[str, Any] = {}
if msg["domain"] not in domain_blueprints:
connection.send_result(msg["id"], results)
@ -62,7 +66,11 @@ async def ws_list_blueprints(hass, connection, msg):
}
)
@websocket_api.async_response
async def ws_import_blueprint(hass, connection, msg):
async def ws_import_blueprint(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Import a blueprint."""
async with async_timeout.timeout(10):
imported_blueprint = await importer.fetch_blueprint_from_url(hass, msg["url"])
@ -96,15 +104,17 @@ async def ws_import_blueprint(hass, connection, msg):
}
)
@websocket_api.async_response
async def ws_save_blueprint(hass, connection, msg):
async def ws_save_blueprint(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Save a blueprint."""
path = msg["path"]
domain = msg["domain"]
domain_blueprints: dict[str, models.DomainBlueprints] | None = hass.data.get(
DOMAIN, {}
)
domain_blueprints: dict[str, models.DomainBlueprints] = hass.data.get(DOMAIN, {})
if domain not in domain_blueprints:
connection.send_error(
@ -112,9 +122,8 @@ async def ws_save_blueprint(hass, connection, msg):
)
try:
blueprint = models.Blueprint(
yaml.parse_yaml(msg["yaml"]), expected_domain=domain
)
yaml_data = cast(dict[str, Any], yaml.parse_yaml(msg["yaml"]))
blueprint = models.Blueprint(yaml_data, expected_domain=domain)
if "source_url" in msg:
blueprint.update_metadata(source_url=msg["source_url"])
except HomeAssistantError as err:
@ -143,15 +152,17 @@ async def ws_save_blueprint(hass, connection, msg):
}
)
@websocket_api.async_response
async def ws_delete_blueprint(hass, connection, msg):
async def ws_delete_blueprint(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Delete a blueprint."""
path = msg["path"]
domain = msg["domain"]
domain_blueprints: dict[str, models.DomainBlueprints] | None = hass.data.get(
DOMAIN, {}
)
domain_blueprints: dict[str, models.DomainBlueprints] = hass.data.get(DOMAIN, {})
if domain not in domain_blueprints:
connection.send_error(