Skip to content

Commit dace1ce

Browse files
committed
Yandex.Cloud SDK version constraints workaround
1 parent 180913c commit dace1ce

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

custom_components/yandexgpt_conversation/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from homeassistant.helpers import config_validation as cv
1616
from homeassistant.helpers import selector
1717
from homeassistant.helpers.typing import ConfigType
18-
from yandex_cloud_ml_sdk import AsyncYCloudML
18+
19+
from .sdk_workaround import is_latest_sdk_installed, upgrade_sdk
1920

2021
from .const import (ATTR_FILENAME, ATTR_PROMPT, ATTR_SEED,
2122
CONF_ENABLE_SERVER_DATA_LOGGING, CONF_FOLDER_ID,
@@ -73,6 +74,11 @@ async def render_image(call: ServiceCall) -> ServiceResponse:
7374
LOGGER.error("Error during image generation: %s", str(err), exc_info=True)
7475
raise HomeAssistantError(f"Image generation failed: {str(err)}") from err
7576

77+
if not is_latest_sdk_installed():
78+
upgrade_success = upgrade_sdk()
79+
if not upgrade_success:
80+
return False
81+
7682
hass.services.async_register(
7783
DOMAIN,
7884
SERVICE_GENERATE_IMAGE,
@@ -99,6 +105,8 @@ async def render_image(call: ServiceCall) -> ServiceResponse:
99105

100106
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
101107
"""Set up YandexGPT from a config entry."""
108+
from yandex_cloud_ml_sdk import AsyncYCloudML
109+
102110
settings = {**entry.data, **entry.options}
103111

104112
entry.runtime_data = AsyncYCloudML(

custom_components/yandexgpt_conversation/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"iot_class": "cloud_polling",
1111
"issue_tracker": "https://github.com/black-roland/homeassistant-yandexgpt/issues",
1212
"loggers": ["yandexgpt_conversation"],
13-
"requirements": ["yandex-cloud-ml-sdk==0.13.1", "yandexcloud==0.353.0"],
13+
"requirements": [],
1414
"version": "1.4.2"
1515
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Workaround for the Yandex.Cloud SDK dependencies BS 💩."""
2+
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
7+
from homeassistant.util import package as pkg_util
8+
from .const import LOGGER
9+
10+
# Details:
11+
# https://github.com/black-roland/homeassistant-yandexgpt/issues/32
12+
#
13+
# The history of "we don't give a 💩":
14+
# https://github.com/yandex-cloud/python-sdk/issues?q=is%3Aissue%20dependencies%20OR%20requirements%20OR%20constraints
15+
REQUIRED_PACKAGES = [
16+
"yandex-cloud-ml-sdk==0.13.1",
17+
"yandexcloud==0.353.0"
18+
]
19+
20+
21+
def is_latest_sdk_installed() -> bool:
22+
"""Check if the required versions of SDKs are installed."""
23+
try:
24+
for package in REQUIRED_PACKAGES:
25+
if not pkg_util.is_installed(package):
26+
LOGGER.debug("Package %s is not installed or not in the required version.", package)
27+
return False
28+
29+
LOGGER.debug("All required Yandex Cloud SDK packages are installed in correct versions.")
30+
return True
31+
except Exception as e:
32+
LOGGER.warning("Error checking SDK package installation status: %s", e)
33+
return False
34+
35+
36+
def upgrade_sdk() -> bool:
37+
"""Use pkg_util to install the required versions of the SDKs."""
38+
39+
# Empty args to avoid setting any package constraints
40+
_pip_kwargs = {}
41+
42+
success = True
43+
for package in REQUIRED_PACKAGES:
44+
try:
45+
LOGGER.info("Attempting to install/upgrade Yandex Cloud SDK package: %s", package)
46+
47+
install_success = pkg_util.install_package(package, **_pip_kwargs)
48+
if not install_success:
49+
LOGGER.error("Failed to install/upgrade Yandex Cloud SDK package: %s", package)
50+
success = False
51+
continue
52+
53+
LOGGER.info("Successfully installed/upgraded Yandex Cloud SDK package: %s", package)
54+
except Exception as e:
55+
LOGGER.exception("Exception occurred while installing/upgrading package %s: %s", package, e)
56+
success = False
57+
return success

0 commit comments

Comments
 (0)