forked from home-assistant/core
Compare commits
3 Commits
immich/add
...
immich/add
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b26f011a36 | ||
|
|
debc51687f | ||
|
|
b38fcf3100 |
@@ -16,13 +16,25 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import ImmichConfigEntry, ImmichDataUpdateCoordinator
|
||||
from .services import async_setup_services
|
||||
|
||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up immich integration."""
|
||||
await async_setup_services(hass)
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ImmichConfigEntry) -> bool:
|
||||
"""Set up Immich from a config entry."""
|
||||
|
||||
|
||||
@@ -11,5 +11,10 @@
|
||||
"default": "mdi:file-video"
|
||||
}
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
"upload_file": {
|
||||
"service": "mdi:upload"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
107
homeassistant/components/immich/services.py
Normal file
107
homeassistant/components/immich/services.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""Services for the Immich integration."""
|
||||
|
||||
import logging
|
||||
import os.path
|
||||
|
||||
from aioimmich.exceptions import ImmichError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import ImmichDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SERVICE_UPLOAD_FILE = "upload_file"
|
||||
SERVICE_SCHEMA_UPLOAD_FILE = vol.Schema(
|
||||
{
|
||||
vol.Required("config_entry_id"): str,
|
||||
vol.Required("file"): str,
|
||||
vol.Optional("album_id"): str,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def _async_upload_file(service_call: ServiceCall) -> None:
|
||||
"""Call immich upload file service."""
|
||||
_LOGGER.debug(
|
||||
"Executing service %s with arguments %s",
|
||||
service_call.service,
|
||||
service_call.data,
|
||||
)
|
||||
hass = service_call.hass
|
||||
target_entry = hass.config_entries.async_get_entry(
|
||||
service_call.data["config_entry_id"]
|
||||
)
|
||||
target_file = service_call.data["file"]
|
||||
|
||||
if not target_entry:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="config_entry_not_found",
|
||||
translation_placeholders={"service": service_call.service},
|
||||
)
|
||||
|
||||
if target_entry.state is not ConfigEntryState.LOADED:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="config_entry_not_loaded",
|
||||
translation_placeholders={"service": service_call.service},
|
||||
)
|
||||
|
||||
if not os.path.isfile(target_file):
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="file_not_found",
|
||||
translation_placeholders={
|
||||
"service": service_call.service,
|
||||
"file": target_file,
|
||||
},
|
||||
)
|
||||
|
||||
coordinator: ImmichDataUpdateCoordinator = target_entry.runtime_data
|
||||
|
||||
if target_album := service_call.data.get("album_id"):
|
||||
try:
|
||||
await coordinator.api.albums.async_get_album_info(target_album, True)
|
||||
except ImmichError as ex:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="album_not_found",
|
||||
translation_placeholders={
|
||||
"service": service_call.service,
|
||||
"album_id": target_album,
|
||||
"error": str(ex),
|
||||
},
|
||||
) from ex
|
||||
|
||||
try:
|
||||
upload_result = await coordinator.api.assets.async_upload_asset(target_file)
|
||||
if target_album:
|
||||
await coordinator.api.albums.async_add_assets_to_album(
|
||||
target_album, [upload_result.asset_id]
|
||||
)
|
||||
except ImmichError as ex:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="upload_failed",
|
||||
translation_placeholders={
|
||||
"service": service_call.service,
|
||||
"file": target_file,
|
||||
"error": str(ex),
|
||||
},
|
||||
) from ex
|
||||
|
||||
|
||||
async def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Set up services for immich integration."""
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
_async_upload_file,
|
||||
SERVICE_SCHEMA_UPLOAD_FILE,
|
||||
)
|
||||
15
homeassistant/components/immich/services.yaml
Normal file
15
homeassistant/components/immich/services.yaml
Normal file
@@ -0,0 +1,15 @@
|
||||
upload_file:
|
||||
fields:
|
||||
config_entry_id:
|
||||
required: true
|
||||
selector:
|
||||
config_entry:
|
||||
integration: immich
|
||||
file:
|
||||
required: true
|
||||
selector:
|
||||
text:
|
||||
album_id:
|
||||
required: false
|
||||
selector:
|
||||
text:
|
||||
@@ -69,5 +69,42 @@
|
||||
"name": "Disk used by videos"
|
||||
}
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
"upload_file": {
|
||||
"name": "Upload file",
|
||||
"description": "Upload a file to your Immich instance.",
|
||||
"fields": {
|
||||
"config_entry_id": {
|
||||
"name": "Immich instance",
|
||||
"description": "Select the Immich instance where to upload the file."
|
||||
},
|
||||
"file": {
|
||||
"name": "File",
|
||||
"description": "The path to the file to be uploaded."
|
||||
},
|
||||
"album_id": {
|
||||
"name": "Album id",
|
||||
"description": "The album where to put the file in after upload."
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"config_entry_not_found": {
|
||||
"message": "Failed to perform action \"{service}\". Config entry not found"
|
||||
},
|
||||
"config_entry_not_loaded": {
|
||||
"message": "Failed to perform action \"{service}\". Config entry not loaded."
|
||||
},
|
||||
"file_not_found": {
|
||||
"message": "Failed to perform action \"{service}\". File `{file}` not found."
|
||||
},
|
||||
"album_not_found": {
|
||||
"message": "Failed to perform action \"{service}\". Album with id `{album_id}` not found ({error})."
|
||||
},
|
||||
"upload_failed": {
|
||||
"message": "Failed to perform action \"{service}\". Upload of file `{file}` failed ({error})."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ from collections.abc import AsyncGenerator, Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aioimmich import ImmichAlbums, ImmichAssests, ImmichServer, ImmichUsers
|
||||
from aioimmich.albums.models import ImmichAddAssetsToAlbumResponse
|
||||
from aioimmich.assets.models import ImmichAssetUploadResponse
|
||||
from aioimmich.server.models import (
|
||||
ImmichServerAbout,
|
||||
ImmichServerStatistics,
|
||||
@@ -61,6 +63,12 @@ def mock_immich_albums() -> AsyncMock:
|
||||
mock = AsyncMock(spec=ImmichAlbums)
|
||||
mock.async_get_all_albums.return_value = [MOCK_ALBUM_WITHOUT_ASSETS]
|
||||
mock.async_get_album_info.return_value = MOCK_ALBUM_WITH_ASSETS
|
||||
mock.async_add_assets_to_album.return_value = [
|
||||
ImmichAddAssetsToAlbumResponse.from_dict(
|
||||
{"id": "abcdef-0123456789", "success": True}
|
||||
)
|
||||
]
|
||||
|
||||
return mock
|
||||
|
||||
|
||||
@@ -70,6 +78,9 @@ def mock_immich_assets() -> AsyncMock:
|
||||
mock = AsyncMock(spec=ImmichAssests)
|
||||
mock.async_view_asset.return_value = b"xxxx"
|
||||
mock.async_play_video_stream.return_value = MockStreamReaderChunked(b"xxxx")
|
||||
mock.async_upload_asset.return_value = ImmichAssetUploadResponse.from_dict(
|
||||
{"id": "abcdef-0123456789", "status": "created"}
|
||||
)
|
||||
return mock
|
||||
|
||||
|
||||
|
||||
267
tests/components/immich/test_services.py
Normal file
267
tests/components/immich/test_services.py
Normal file
@@ -0,0 +1,267 @@
|
||||
"""Test the Immich services."""
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock
|
||||
|
||||
from aioimmich.exceptions import ImmichError, ImmichNotFoundError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.immich.const import DOMAIN
|
||||
from homeassistant.components.immich.services import SERVICE_UPLOAD_FILE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_setup_services(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test setup of immich services."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
services = hass.services.async_services_for_domain(DOMAIN)
|
||||
assert services
|
||||
assert SERVICE_UPLOAD_FILE in services
|
||||
|
||||
|
||||
async def test_upload_file(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test upload_file service."""
|
||||
test_file = tmp_path / "image.png"
|
||||
test_file.write_bytes(b"abcdef")
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": mock_config_entry.entry_id,
|
||||
"file": test_file.as_posix(),
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_immich.assets.async_upload_asset.assert_called_with(test_file.as_posix())
|
||||
mock_immich.albums.async_get_album_info.assert_not_called()
|
||||
mock_immich.albums.async_add_assets_to_album.assert_not_called()
|
||||
|
||||
|
||||
async def test_upload_file_to_album(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test upload_file service with target album_id."""
|
||||
test_file = tmp_path / "image.png"
|
||||
test_file.write_bytes(b"abcdef")
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": mock_config_entry.entry_id,
|
||||
"file": test_file.as_posix(),
|
||||
"album_id": "721e1a4b-aa12-441e-8d3b-5ac7ab283bb6",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_immich.assets.async_upload_asset.assert_called_with(test_file.as_posix())
|
||||
mock_immich.albums.async_get_album_info.assert_called_with(
|
||||
"721e1a4b-aa12-441e-8d3b-5ac7ab283bb6", True
|
||||
)
|
||||
mock_immich.albums.async_add_assets_to_album.assert_called_with(
|
||||
"721e1a4b-aa12-441e-8d3b-5ac7ab283bb6", ["abcdef-0123456789"]
|
||||
)
|
||||
|
||||
|
||||
async def test_upload_file_config_entry_not_found(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test upload_file service raising config_entry_not_found."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match='Failed to perform action "upload_file". Config entry not found',
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": "unknown_entry",
|
||||
"file": "blabla",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_upload_file_config_entry_not_loaded(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test upload_file service raising config_entry_not_loaded."""
|
||||
mock_config_entry.disabled_by = er.RegistryEntryDisabler.USER
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match='Failed to perform action "upload_file". Config entry not loaded',
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": mock_config_entry.entry_id,
|
||||
"file": "blabla",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_upload_file_file_not_found(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test upload_file service raising file_not_found."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match='Failed to perform action "upload_file". File `not_existing.file` not found',
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": mock_config_entry.entry_id,
|
||||
"file": "not_existing.file",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_upload_file_album_not_found(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test upload_file service raising album_not_found."""
|
||||
test_file = tmp_path / "image.png"
|
||||
test_file.write_bytes(b"abcdef")
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
mock_immich.albums.async_get_album_info.side_effect = ImmichNotFoundError(
|
||||
{
|
||||
"message": "Not found or no album.read access",
|
||||
"error": "Bad Request",
|
||||
"statusCode": 400,
|
||||
"correlationId": "nyzxjkno",
|
||||
}
|
||||
)
|
||||
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match='Failed to perform action "upload_file". Album with id `721e1a4b-aa12-441e-8d3b-5ac7ab283bb6` not found',
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": mock_config_entry.entry_id,
|
||||
"file": test_file.as_posix(),
|
||||
"album_id": "721e1a4b-aa12-441e-8d3b-5ac7ab283bb6",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_upload_file_upload_failed(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test upload_file service raising upload_failed."""
|
||||
test_file = tmp_path / "image.png"
|
||||
test_file.write_bytes(b"abcdef")
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
mock_immich.assets.async_upload_asset.side_effect = ImmichError(
|
||||
{
|
||||
"message": "Boom! Upload failed",
|
||||
"error": "Bad Request",
|
||||
"statusCode": 400,
|
||||
"correlationId": "nyzxjkno",
|
||||
}
|
||||
)
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match=f'Failed to perform action "upload_file". Upload of file `{test_file.as_posix()}` failed',
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": mock_config_entry.entry_id,
|
||||
"file": test_file.as_posix(),
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_upload_file_to_album_upload_failed(
|
||||
hass: HomeAssistant,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test upload_file service with target album_id raising upload_failed."""
|
||||
test_file = tmp_path / "image.png"
|
||||
test_file.write_bytes(b"abcdef")
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
mock_immich.albums.async_add_assets_to_album.side_effect = ImmichError(
|
||||
{
|
||||
"message": "Boom! Add to album failed.",
|
||||
"error": "Bad Request",
|
||||
"statusCode": 400,
|
||||
"correlationId": "nyzxjkno",
|
||||
}
|
||||
)
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match=f'Failed to perform action "upload_file". Upload of file `{test_file.as_posix()}` failed',
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_UPLOAD_FILE,
|
||||
{
|
||||
"config_entry_id": mock_config_entry.entry_id,
|
||||
"file": test_file.as_posix(),
|
||||
"album_id": "721e1a4b-aa12-441e-8d3b-5ac7ab283bb6",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
Reference in New Issue
Block a user