-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2628166
commit beaf916
Showing
12 changed files
with
504 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"services": { | ||
"upload": { | ||
"service": "mdi:cloud-upload" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"""Google Photos services.""" | ||
|
||
from __future__ import annotations | ||
|
||
import asyncio | ||
import mimetypes | ||
from pathlib import Path | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_FILENAME | ||
from homeassistant.core import ( | ||
HomeAssistant, | ||
ServiceCall, | ||
ServiceResponse, | ||
SupportsResponse, | ||
) | ||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError | ||
from homeassistant.helpers import config_validation as cv | ||
|
||
from . import api | ||
from .const import DOMAIN | ||
|
||
type GooglePhotosConfigEntry = ConfigEntry[api.AsyncConfigEntryAuth] | ||
|
||
__all__ = [ | ||
"DOMAIN", | ||
] | ||
|
||
CONF_CONFIG_ENTRY_ID = "config_entry_id" | ||
|
||
UPLOAD_SERVICE = "upload" | ||
UPLOAD_SERVICE_SCHEMA = vol.Schema( | ||
{ | ||
vol.Required(CONF_CONFIG_ENTRY_ID): cv.string, | ||
vol.Required(CONF_FILENAME): vol.All(cv.ensure_list, [cv.string]), | ||
} | ||
) | ||
|
||
|
||
def async_register_services(hass: HomeAssistant) -> None: | ||
"""Register Google Photos services.""" | ||
|
||
async def async_handle_upload(call: ServiceCall) -> ServiceResponse: | ||
"""Generate content from text and optionally images.""" | ||
config_entry: GooglePhotosConfigEntry | None = ( | ||
hass.config_entries.async_get_entry(call.data[CONF_CONFIG_ENTRY_ID]) | ||
) | ||
if not config_entry: | ||
raise ServiceValidationError( | ||
translation_domain=DOMAIN, | ||
translation_key="integration_not_found", | ||
translation_placeholders={"target": DOMAIN}, | ||
) | ||
client_api = config_entry.runtime_data | ||
upload_tasks = [] | ||
for filename in call.data[CONF_FILENAME]: | ||
if not hass.config.is_allowed_path(filename): | ||
raise HomeAssistantError( | ||
translation_domain=DOMAIN, | ||
translation_key="no_access_to_path", | ||
translation_placeholders={"filename": filename}, | ||
) | ||
if not Path(filename).exists(): | ||
raise HomeAssistantError( | ||
translation_domain=DOMAIN, | ||
translation_key="filename_does_not_exist", | ||
translation_placeholders={"filename": filename}, | ||
) | ||
mime_type, _ = mimetypes.guess_type(filename) | ||
if mime_type is None or not (mime_type.startswith(("image", "video"))): | ||
raise HomeAssistantError( | ||
translation_domain=DOMAIN, | ||
translation_key="filename_is_not_image", | ||
translation_placeholders={"filename": filename}, | ||
) | ||
filename_path = Path(filename) | ||
content = await hass.async_add_executor_job(filename_path.read_bytes) | ||
upload_tasks.append(client_api.upload_content(content, mime_type)) | ||
upload_tokens = await asyncio.gather(*upload_tasks) | ||
media_ids = await client_api.create_media_items(upload_tokens) | ||
if call.return_response: | ||
return { | ||
"media_items": [{"media_item_id": media_id for media_id in media_ids}] | ||
} | ||
return None | ||
|
||
if not hass.services.has_service(DOMAIN, UPLOAD_SERVICE): | ||
hass.services.async_register( | ||
DOMAIN, | ||
UPLOAD_SERVICE, | ||
async_handle_upload, | ||
schema=UPLOAD_SERVICE_SCHEMA, | ||
supports_response=SupportsResponse.OPTIONAL, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
upload: | ||
fields: | ||
config_entry_id: | ||
required: true | ||
selector: | ||
config_entry: | ||
integration: google_photos | ||
filename: | ||
required: false | ||
selector: | ||
text: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.