Simplify google_photos service actions (#146744)

This commit is contained in:
epenet 2025-06-14 03:57:11 +02:00 committed by GitHub
parent 3d2dca5f0c
commit 56aa809074
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -78,14 +78,10 @@ def _read_file_contents(
return results return results
@callback async def _async_handle_upload(call: ServiceCall) -> ServiceResponse:
def async_setup_services(hass: HomeAssistant) -> None:
"""Register Google Photos services."""
async def async_handle_upload(call: ServiceCall) -> ServiceResponse:
"""Generate content from text and optionally images.""" """Generate content from text and optionally images."""
config_entry: GooglePhotosConfigEntry | None = ( config_entry: GooglePhotosConfigEntry | None = (
hass.config_entries.async_get_entry(call.data[CONF_CONFIG_ENTRY_ID]) call.hass.config_entries.async_get_entry(call.data[CONF_CONFIG_ENTRY_ID])
) )
if not config_entry: if not config_entry:
raise ServiceValidationError( raise ServiceValidationError(
@ -103,8 +99,8 @@ def async_setup_services(hass: HomeAssistant) -> None:
coordinator = config_entry.runtime_data coordinator = config_entry.runtime_data
client_api = coordinator.client client_api = coordinator.client
upload_tasks = [] upload_tasks = []
file_results = await hass.async_add_executor_job( file_results = await call.hass.async_add_executor_job(
_read_file_contents, hass, call.data[CONF_FILENAME] _read_file_contents, call.hass, call.data[CONF_FILENAME]
) )
album = call.data[CONF_ALBUM] album = call.data[CONF_ALBUM]
@ -130,9 +126,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
try: try:
upload_result = await client_api.create_media_items( upload_result = await client_api.create_media_items(
[ [
NewMediaItem( NewMediaItem(SimpleMediaItem(upload_token=upload_result.upload_token))
SimpleMediaItem(upload_token=upload_result.upload_token)
)
for upload_result in upload_results for upload_result in upload_results
], ],
album_id=album_id, album_id=album_id,
@ -154,10 +148,15 @@ def async_setup_services(hass: HomeAssistant) -> None:
} }
return None return None
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Register Google Photos services."""
hass.services.async_register( hass.services.async_register(
DOMAIN, DOMAIN,
UPLOAD_SERVICE, UPLOAD_SERVICE,
async_handle_upload, _async_handle_upload,
schema=UPLOAD_SERVICE_SCHEMA, schema=UPLOAD_SERVICE_SCHEMA,
supports_response=SupportsResponse.OPTIONAL, supports_response=SupportsResponse.OPTIONAL,
) )