Skip to content

Commit

Permalink
Check for allowlist_external_dirs
Browse files Browse the repository at this point in the history
Resolves #3.
  • Loading branch information
black-roland committed Nov 7, 2024
1 parent aa52da7 commit d97bc38
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.analysis.typeCheckingMode": "basic"
}
16 changes: 13 additions & 3 deletions custom_components/yandexgpt_conversation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
ServiceResponse,
SupportsResponse,
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import selector
from homeassistant.helpers.typing import ConfigType
from yandex_cloud_ml_sdk import YCloudML

from .const import CONF_FOLDER_ID, DOMAIN
from .const import ATTR_FILENAME, ATTR_PROMPT, ATTR_SEED, CONF_FOLDER_ID, DOMAIN
from .yandexart import YandexArt

SERVICE_GENERATE_IMAGE = "generate_image"
Expand All @@ -39,17 +40,26 @@ def write_image_to_file(base64_img: str, file_name: str) -> str:
async def render_image(call: ServiceCall) -> ServiceResponse:
"""Render an image with YandexART."""

if not hass.config.is_allowed_path(call.data[ATTR_FILENAME]):
raise HomeAssistantError(
f"Cannot write `{call.data[ATTR_FILENAME]}`, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`" # noqa: E501
)

entry_id = call.data["config_entry"]
entry = hass.config_entries.async_get_entry(entry_id)
if entry is None:
raise HomeAssistantError(f"Config entry {entry_id} not found")

settings = {**entry.data, **entry.options}
yandexart = YandexArt(
folder_id=settings[CONF_FOLDER_ID], api_key=settings[CONF_API_KEY]
)
base64_img = await yandexart.generate(call.data["seed"], call.data["prompt"])
base64_img = await yandexart.generate(
call.data[ATTR_SEED], call.data[ATTR_PROMPT]
)

file_name = await hass.async_add_executor_job(
write_image_to_file, base64_img, call.data["file_name"]
write_image_to_file, base64_img, call.data[ATTR_FILENAME]
)

return {"file_name": file_name}
Expand Down
4 changes: 4 additions & 0 deletions custom_components/yandexgpt_conversation/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
Отвечай на вопросы правдиво.
Отвечай простым текстом, без вводных фраз и объяснений. Не усложняй и отвечай по сути.
"""

ATTR_FILENAME = "file_name"
ATTR_SEED = "seed"
ATTR_PROMPT = "prompt"

0 comments on commit d97bc38

Please sign in to comment.