1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-14 23:28:42 +00:00

Use service helper to extract google_photos config entry (#162792)

This commit is contained in:
epenet
2026-02-11 17:24:44 +01:00
committed by GitHub
parent 30f006538d
commit 25d902fd3e
3 changed files with 8 additions and 19 deletions

View File

@@ -18,8 +18,8 @@ from homeassistant.core import (
SupportsResponse,
callback,
)
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, service
from .const import DOMAIN, UPLOAD_SCOPE
from .coordinator import GooglePhotosConfigEntry
@@ -80,15 +80,10 @@ def _read_file_contents(
async def _async_handle_upload(call: ServiceCall) -> ServiceResponse:
"""Generate content from text and optionally images."""
config_entry: GooglePhotosConfigEntry | None = (
call.hass.config_entries.async_get_entry(call.data[CONF_CONFIG_ENTRY_ID])
config_entry: GooglePhotosConfigEntry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[CONF_CONFIG_ENTRY_ID]
)
if not config_entry:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="integration_not_found",
translation_placeholders={"target": DOMAIN},
)
scopes = config_entry.data["token"]["scope"].split(" ")
if UPLOAD_SCOPE not in scopes:
raise HomeAssistantError(

View File

@@ -62,18 +62,12 @@
"filename_is_not_image": {
"message": "`{filename}` is not an image"
},
"integration_not_found": {
"message": "Integration \"{target}\" not found in registry."
},
"missing_upload_permission": {
"message": "Home Assistant was not granted permission to upload to Google Photos"
},
"no_access_to_path": {
"message": "Cannot read {filename}, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`"
},
"not_loaded": {
"message": "{target} is not loaded."
},
"upload_error": {
"message": "Failed to upload content: {message}"
}

View File

@@ -146,7 +146,7 @@ async def test_upload_service_config_entry_not_found(
config_entry: MockConfigEntry,
) -> None:
"""Test upload service call with a config entry that does not exist."""
with pytest.raises(HomeAssistantError, match="not found in registry"):
with pytest.raises(HomeAssistantError, match="service_config_entry_not_found"):
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
@@ -171,12 +171,12 @@ async def test_config_entry_not_loaded(
assert config_entry.state is ConfigEntryState.NOT_LOADED
with pytest.raises(HomeAssistantError, match="not found in registry"):
with pytest.raises(HomeAssistantError, match="service_config_entry_not_loaded"):
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
{
CONF_CONFIG_ENTRY_ID: config_entry.unique_id,
CONF_CONFIG_ENTRY_ID: config_entry.entry_id,
CONF_FILENAME: TEST_FILENAME,
CONF_ALBUM: ALBUM_TITLE,
},