mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Add Immich integration (#145125)
* add immich integration * bump aioimmich==0.3.1 * rework to require an url as input and pare it afterwards * fix doc strings * remove name attribute from deviceinfo as it is default behaviour * add translated uom for count sensors * explicitly pass in the config_entry in coordinator * fix url in strings to uppercase * use data_updates attribute instead of data * remove left over * match entries only by host * remove quotes * import SOURCE_USER directly, instead of config_entries * split happy and sad flow tests * remove unneccessary async_block_till_done() calls * replace url example by "full URL" * bump aioimmich==0.4.0 * bump aioimmich==0.5.0 * allow multiple users for same immich instance * Fix tests * limit entities when user has no admin rights * Fix tests * Fix tests --------- Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
@ -270,6 +270,7 @@ homeassistant.components.image_processing.*
|
||||
homeassistant.components.image_upload.*
|
||||
homeassistant.components.imap.*
|
||||
homeassistant.components.imgw_pib.*
|
||||
homeassistant.components.immich.*
|
||||
homeassistant.components.incomfort.*
|
||||
homeassistant.components.input_button.*
|
||||
homeassistant.components.input_select.*
|
||||
|
2
CODEOWNERS
generated
2
CODEOWNERS
generated
@ -710,6 +710,8 @@ build.json @home-assistant/supervisor
|
||||
/tests/components/imeon_inverter/ @Imeon-Energy
|
||||
/homeassistant/components/imgw_pib/ @bieniu
|
||||
/tests/components/imgw_pib/ @bieniu
|
||||
/homeassistant/components/immich/ @mib1185
|
||||
/tests/components/immich/ @mib1185
|
||||
/homeassistant/components/improv_ble/ @emontnemery
|
||||
/tests/components/improv_ble/ @emontnemery
|
||||
/homeassistant/components/incomfort/ @jbouwh
|
||||
|
56
homeassistant/components/immich/__init__.py
Normal file
56
homeassistant/components/immich/__init__.py
Normal file
@ -0,0 +1,56 @@
|
||||
"""The Immich integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from aioimmich import Immich
|
||||
from aioimmich.const import CONNECT_ERRORS
|
||||
from aioimmich.exceptions import ImmichUnauthorizedError
|
||||
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_VERIFY_SSL,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .coordinator import ImmichConfigEntry, ImmichDataUpdateCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ImmichConfigEntry) -> bool:
|
||||
"""Set up Immich from a config entry."""
|
||||
|
||||
session = async_get_clientsession(hass, entry.data[CONF_VERIFY_SSL])
|
||||
immich = Immich(
|
||||
session,
|
||||
entry.data[CONF_API_KEY],
|
||||
entry.data[CONF_HOST],
|
||||
entry.data[CONF_PORT],
|
||||
entry.data[CONF_SSL],
|
||||
)
|
||||
|
||||
try:
|
||||
user_info = await immich.users.async_get_my_user()
|
||||
except ImmichUnauthorizedError as err:
|
||||
raise ConfigEntryAuthFailed from err
|
||||
except CONNECT_ERRORS as err:
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
coordinator = ImmichDataUpdateCoordinator(hass, entry, immich, user_info.is_admin)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ImmichConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
174
homeassistant/components/immich/config_flow.py
Normal file
174
homeassistant/components/immich/config_flow.py
Normal file
@ -0,0 +1,174 @@
|
||||
"""Config flow for the Immich integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from aioimmich import Immich
|
||||
from aioimmich.const import CONNECT_ERRORS
|
||||
from aioimmich.exceptions import ImmichUnauthorizedError
|
||||
from aioimmich.users.models import ImmichUser
|
||||
import voluptuous as vol
|
||||
from yarl import URL
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_URL,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.selector import (
|
||||
TextSelector,
|
||||
TextSelectorConfig,
|
||||
TextSelectorType,
|
||||
)
|
||||
|
||||
from .const import DEFAULT_VERIFY_SSL, DOMAIN
|
||||
|
||||
|
||||
class InvalidUrl(HomeAssistantError):
|
||||
"""Error to indicate invalid URL."""
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
STEP_USER_DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_URL): TextSelector(
|
||||
config=TextSelectorConfig(type=TextSelectorType.URL)
|
||||
),
|
||||
vol.Required(CONF_API_KEY): TextSelector(
|
||||
config=TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
||||
),
|
||||
vol.Required(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): bool,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _parse_url(url: str) -> tuple[str, int, bool]:
|
||||
"""Parse the URL and return host, port, and ssl."""
|
||||
parsed_url = URL(url)
|
||||
if (
|
||||
(host := parsed_url.host) is None
|
||||
or (port := parsed_url.port) is None
|
||||
or (scheme := parsed_url.scheme) is None
|
||||
):
|
||||
raise InvalidUrl
|
||||
return host, port, scheme == "https"
|
||||
|
||||
|
||||
async def check_user_info(
|
||||
hass: HomeAssistant, host: str, port: int, ssl: bool, verify_ssl: bool, api_key: str
|
||||
) -> ImmichUser:
|
||||
"""Test connection and fetch own user info."""
|
||||
session = async_get_clientsession(hass, verify_ssl)
|
||||
immich = Immich(session, api_key, host, port, ssl)
|
||||
return await immich.users.async_get_my_user()
|
||||
|
||||
|
||||
class ImmichConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Immich."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
_name: str
|
||||
_current_data: Mapping[str, Any]
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle the initial step."""
|
||||
errors: dict[str, str] = {}
|
||||
if user_input is not None:
|
||||
try:
|
||||
(host, port, ssl) = _parse_url(user_input[CONF_URL])
|
||||
except InvalidUrl:
|
||||
errors[CONF_URL] = "invalid_url"
|
||||
else:
|
||||
try:
|
||||
my_user_info = await check_user_info(
|
||||
self.hass,
|
||||
host,
|
||||
port,
|
||||
ssl,
|
||||
user_input[CONF_VERIFY_SSL],
|
||||
user_input[CONF_API_KEY],
|
||||
)
|
||||
except ImmichUnauthorizedError:
|
||||
errors["base"] = "invalid_auth"
|
||||
except CONNECT_ERRORS:
|
||||
errors["base"] = "cannot_connect"
|
||||
except Exception:
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
await self.async_set_unique_id(my_user_info.user_id)
|
||||
self._abort_if_unique_id_configured()
|
||||
return self.async_create_entry(
|
||||
title=my_user_info.name,
|
||||
data={
|
||||
CONF_HOST: host,
|
||||
CONF_PORT: port,
|
||||
CONF_SSL: ssl,
|
||||
CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL],
|
||||
CONF_API_KEY: user_input[CONF_API_KEY],
|
||||
},
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
||||
)
|
||||
|
||||
async def async_step_reauth(
|
||||
self, entry_data: Mapping[str, Any]
|
||||
) -> ConfigFlowResult:
|
||||
"""Trigger a reauthentication flow."""
|
||||
self._current_data = entry_data
|
||||
self._name = entry_data[CONF_HOST]
|
||||
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle reauthorization flow."""
|
||||
errors = {}
|
||||
|
||||
if user_input is not None:
|
||||
try:
|
||||
my_user_info = await check_user_info(
|
||||
self.hass,
|
||||
self._current_data[CONF_HOST],
|
||||
self._current_data[CONF_PORT],
|
||||
self._current_data[CONF_SSL],
|
||||
self._current_data[CONF_VERIFY_SSL],
|
||||
user_input[CONF_API_KEY],
|
||||
)
|
||||
except ImmichUnauthorizedError:
|
||||
errors["base"] = "invalid_auth"
|
||||
except CONNECT_ERRORS:
|
||||
errors["base"] = "cannot_connect"
|
||||
except Exception:
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
await self.async_set_unique_id(my_user_info.user_id)
|
||||
self._abort_if_unique_id_mismatch()
|
||||
return self.async_update_reload_and_abort(
|
||||
self._get_reauth_entry(), data_updates=user_input
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
data_schema=vol.Schema({vol.Required(CONF_API_KEY): str}),
|
||||
description_placeholders={"name": self._name},
|
||||
errors=errors,
|
||||
)
|
7
homeassistant/components/immich/const.py
Normal file
7
homeassistant/components/immich/const.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""Constants for the Immich integration."""
|
||||
|
||||
DOMAIN = "immich"
|
||||
|
||||
DEFAULT_PORT = 2283
|
||||
DEFAULT_USE_SSL = False
|
||||
DEFAULT_VERIFY_SSL = False
|
74
homeassistant/components/immich/coordinator.py
Normal file
74
homeassistant/components/immich/coordinator.py
Normal file
@ -0,0 +1,74 @@
|
||||
"""Coordinator for the Immich integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from aioimmich import Immich
|
||||
from aioimmich.const import CONNECT_ERRORS
|
||||
from aioimmich.exceptions import ImmichUnauthorizedError
|
||||
from aioimmich.server.models import (
|
||||
ImmichServerAbout,
|
||||
ImmichServerStatistics,
|
||||
ImmichServerStorage,
|
||||
)
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ImmichData:
|
||||
"""Data class for storing data from the API."""
|
||||
|
||||
server_about: ImmichServerAbout
|
||||
server_storage: ImmichServerStorage
|
||||
server_usage: ImmichServerStatistics | None
|
||||
|
||||
|
||||
type ImmichConfigEntry = ConfigEntry[ImmichDataUpdateCoordinator]
|
||||
|
||||
|
||||
class ImmichDataUpdateCoordinator(DataUpdateCoordinator[ImmichData]):
|
||||
"""Class to manage fetching IMGW-PIB data API."""
|
||||
|
||||
config_entry: ImmichConfigEntry
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, entry: ConfigEntry, api: Immich, is_admin: bool
|
||||
) -> None:
|
||||
"""Initialize the data update coordinator."""
|
||||
self.api = api
|
||||
self.is_admin = is_admin
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name=DOMAIN,
|
||||
update_interval=timedelta(seconds=60),
|
||||
)
|
||||
|
||||
async def _async_update_data(self) -> ImmichData:
|
||||
"""Update data via internal method."""
|
||||
try:
|
||||
server_about = await self.api.server.async_get_about_info()
|
||||
server_storage = await self.api.server.async_get_storage_info()
|
||||
server_usage = (
|
||||
await self.api.server.async_get_server_statistics()
|
||||
if self.is_admin
|
||||
else None
|
||||
)
|
||||
except ImmichUnauthorizedError as err:
|
||||
raise ConfigEntryAuthFailed from err
|
||||
except CONNECT_ERRORS as err:
|
||||
raise UpdateFailed from err
|
||||
|
||||
return ImmichData(server_about, server_storage, server_usage)
|
27
homeassistant/components/immich/entity.py
Normal file
27
homeassistant/components/immich/entity.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""Base entity for the Immich integration."""
|
||||
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import ImmichDataUpdateCoordinator
|
||||
|
||||
|
||||
class ImmichEntity(CoordinatorEntity[ImmichDataUpdateCoordinator]):
|
||||
"""Define immich base entity."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: ImmichDataUpdateCoordinator,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, coordinator.config_entry.entry_id)},
|
||||
manufacturer="Immich",
|
||||
sw_version=coordinator.data.server_about.version,
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
)
|
15
homeassistant/components/immich/icons.json
Normal file
15
homeassistant/components/immich/icons.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"disk_usage": {
|
||||
"default": "mdi:database"
|
||||
},
|
||||
"photos_count": {
|
||||
"default": "mdi:file-image"
|
||||
},
|
||||
"videos_count": {
|
||||
"default": "mdi:file-video"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
homeassistant/components/immich/manifest.json
Normal file
11
homeassistant/components/immich/manifest.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"domain": "immich",
|
||||
"name": "Immich",
|
||||
"codeowners": ["@mib1185"],
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/immich",
|
||||
"iot_class": "local_polling",
|
||||
"loggers": ["aioimmich"],
|
||||
"quality_scale": "silver",
|
||||
"requirements": ["aioimmich==0.5.0"]
|
||||
}
|
76
homeassistant/components/immich/quality_scale.yaml
Normal file
76
homeassistant/components/immich/quality_scale.yaml
Normal file
@ -0,0 +1,76 @@
|
||||
rules:
|
||||
# Bronze
|
||||
action-setup:
|
||||
status: done
|
||||
comment: No integration specific actions
|
||||
appropriate-polling: done
|
||||
brands: done
|
||||
common-modules: done
|
||||
config-flow-test-coverage: done
|
||||
config-flow: done
|
||||
dependency-transparency: done
|
||||
docs-actions:
|
||||
status: done
|
||||
comment: No integration specific actions
|
||||
docs-high-level-description: done
|
||||
docs-installation-instructions: done
|
||||
docs-removal-instructions: done
|
||||
entity-event-setup: done
|
||||
entity-unique-id: done
|
||||
has-entity-name: done
|
||||
runtime-data: done
|
||||
test-before-configure: done
|
||||
test-before-setup: done
|
||||
unique-config-entry: done
|
||||
|
||||
# Silver
|
||||
action-exceptions:
|
||||
status: done
|
||||
comment: No integration specific actions
|
||||
config-entry-unloading: done
|
||||
docs-configuration-parameters: done
|
||||
docs-installation-parameters: done
|
||||
entity-unavailable: done
|
||||
integration-owner: done
|
||||
log-when-unavailable: done
|
||||
parallel-updates: done
|
||||
reauthentication-flow: done
|
||||
test-coverage: done
|
||||
|
||||
# Gold
|
||||
devices: done
|
||||
diagnostics: todo
|
||||
discovery-update-info:
|
||||
status: exempt
|
||||
comment: Service can't be discovered
|
||||
discovery:
|
||||
status: exempt
|
||||
comment: Service can't be discovered
|
||||
docs-data-update: todo
|
||||
docs-examples: todo
|
||||
docs-known-limitations: todo
|
||||
docs-supported-devices: todo
|
||||
docs-supported-functions: todo
|
||||
docs-troubleshooting: todo
|
||||
docs-use-cases: todo
|
||||
dynamic-devices:
|
||||
status: exempt
|
||||
comment: Only one device entry per config entry
|
||||
entity-category: todo
|
||||
entity-device-class: done
|
||||
entity-disabled-by-default: done
|
||||
entity-translations: done
|
||||
exception-translations: todo
|
||||
icon-translations: done
|
||||
reconfiguration-flow: todo
|
||||
repair-issues:
|
||||
status: exempt
|
||||
comment: No repair issues needed
|
||||
stale-devices:
|
||||
status: exempt
|
||||
comment: Only one device entry per config entry
|
||||
|
||||
# Platinum
|
||||
async-dependency: done
|
||||
inject-websession: done
|
||||
strict-typing: done
|
147
homeassistant/components/immich/sensor.py
Normal file
147
homeassistant/components/immich/sensor.py
Normal file
@ -0,0 +1,147 @@
|
||||
"""Sensor platform for the Immich integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import PERCENTAGE, UnitOfInformation
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .coordinator import ImmichConfigEntry, ImmichData, ImmichDataUpdateCoordinator
|
||||
from .entity import ImmichEntity
|
||||
|
||||
# Coordinator is used to centralize the data updates
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class ImmichSensorEntityDescription(SensorEntityDescription):
|
||||
"""Immich sensor entity description."""
|
||||
|
||||
value: Callable[[ImmichData], StateType]
|
||||
is_suitable: Callable[[ImmichData], bool] = lambda _: True
|
||||
|
||||
|
||||
SENSOR_TYPES: tuple[ImmichSensorEntityDescription, ...] = (
|
||||
ImmichSensorEntityDescription(
|
||||
key="disk_size",
|
||||
translation_key="disk_size",
|
||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||
suggested_display_precision=1,
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda data: data.server_storage.disk_size_raw,
|
||||
),
|
||||
ImmichSensorEntityDescription(
|
||||
key="disk_available",
|
||||
translation_key="disk_available",
|
||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||
suggested_display_precision=1,
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda data: data.server_storage.disk_available_raw,
|
||||
),
|
||||
ImmichSensorEntityDescription(
|
||||
key="disk_use",
|
||||
translation_key="disk_use",
|
||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||
suggested_display_precision=1,
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda data: data.server_storage.disk_use_raw,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
ImmichSensorEntityDescription(
|
||||
key="disk_usage",
|
||||
translation_key="disk_usage",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda data: data.server_storage.disk_usage_percentage,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
ImmichSensorEntityDescription(
|
||||
key="photos_count",
|
||||
translation_key="photos_count",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda data: data.server_usage.photos if data.server_usage else None,
|
||||
is_suitable=lambda data: data.server_usage is not None,
|
||||
),
|
||||
ImmichSensorEntityDescription(
|
||||
key="videos_count",
|
||||
translation_key="videos_count",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda data: data.server_usage.videos if data.server_usage else None,
|
||||
is_suitable=lambda data: data.server_usage is not None,
|
||||
),
|
||||
ImmichSensorEntityDescription(
|
||||
key="usage_by_photos",
|
||||
translation_key="usage_by_photos",
|
||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||
suggested_display_precision=1,
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda d: d.server_usage.usage_photos if d.server_usage else None,
|
||||
is_suitable=lambda data: data.server_usage is not None,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
ImmichSensorEntityDescription(
|
||||
key="usage_by_videos",
|
||||
translation_key="usage_by_videos",
|
||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||
suggested_unit_of_measurement=UnitOfInformation.GIBIBYTES,
|
||||
suggested_display_precision=1,
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda d: d.server_usage.usage_videos if d.server_usage else None,
|
||||
is_suitable=lambda data: data.server_usage is not None,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ImmichConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Add immich server state sensors."""
|
||||
coordinator = entry.runtime_data
|
||||
async_add_entities(
|
||||
ImmichSensorEntity(coordinator, description)
|
||||
for description in SENSOR_TYPES
|
||||
if description.is_suitable(coordinator.data)
|
||||
)
|
||||
|
||||
|
||||
class ImmichSensorEntity(ImmichEntity, SensorEntity):
|
||||
"""Define Immich sensor entity."""
|
||||
|
||||
entity_description: ImmichSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: ImmichDataUpdateCoordinator,
|
||||
description: ImmichSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = f"{coordinator.config_entry.unique_id}_{description.key}"
|
||||
self.entity_description = description
|
||||
|
||||
@property
|
||||
def native_value(self) -> StateType:
|
||||
"""Return the value reported by the sensor."""
|
||||
return self.entity_description.value(self.coordinator.data)
|
73
homeassistant/components/immich/strings.json
Normal file
73
homeassistant/components/immich/strings.json
Normal file
@ -0,0 +1,73 @@
|
||||
{
|
||||
"common": {
|
||||
"data_desc_url": "The full URL of your immich instance.",
|
||||
"data_desc_api_key": "API key to connect to your immich instance.",
|
||||
"data_desc_ssl_verify": "Whether to verify the SSL certificate when SSL encryption is used to connect to your immich instance."
|
||||
},
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"data": {
|
||||
"url": "[%key:common::config_flow::data::url%]",
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
|
||||
},
|
||||
"data_description": {
|
||||
"url": "[%key:component::immich::common::data_desc_url%]",
|
||||
"api_key": "[%key:component::immich::common::data_desc_api_key%]",
|
||||
"verify_ssl": "[%key:component::immich::common::data_desc_ssl_verify%]"
|
||||
}
|
||||
},
|
||||
"reauth_confirm": {
|
||||
"description": "Update the API key for {name}.",
|
||||
"data": {
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||
},
|
||||
"data_description": {
|
||||
"api_key": "[%key:component::immich::common::data_desc_api_key%]"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
||||
"invalid_url": "The provided URL is invalid.",
|
||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||
},
|
||||
"abort": {
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||
"unique_id_mismatch": "The provided API key does not match the configured user.",
|
||||
"already_configured": "This user is already configured for this immich instance."
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"disk_size": {
|
||||
"name": "Disk size"
|
||||
},
|
||||
"disk_available": {
|
||||
"name": "Disk available"
|
||||
},
|
||||
"disk_use": {
|
||||
"name": "Disk used"
|
||||
},
|
||||
"disk_usage": {
|
||||
"name": "Disk usage"
|
||||
},
|
||||
"photos_count": {
|
||||
"name": "Photos count",
|
||||
"unit_of_measurement": "photos"
|
||||
},
|
||||
"videos_count": {
|
||||
"name": "Videos count",
|
||||
"unit_of_measurement": "videos"
|
||||
},
|
||||
"usage_by_photos": {
|
||||
"name": "Disk used by photos"
|
||||
},
|
||||
"usage_by_videos": {
|
||||
"name": "Disk used by videos"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
homeassistant/generated/config_flows.py
generated
1
homeassistant/generated/config_flows.py
generated
@ -288,6 +288,7 @@ FLOWS = {
|
||||
"imap",
|
||||
"imeon_inverter",
|
||||
"imgw_pib",
|
||||
"immich",
|
||||
"improv_ble",
|
||||
"incomfort",
|
||||
"inkbird",
|
||||
|
@ -2959,6 +2959,12 @@
|
||||
"config_flow": true,
|
||||
"iot_class": "cloud_polling"
|
||||
},
|
||||
"immich": {
|
||||
"name": "Immich",
|
||||
"integration_type": "hub",
|
||||
"config_flow": true,
|
||||
"iot_class": "local_polling"
|
||||
},
|
||||
"improv_ble": {
|
||||
"name": "Improv via BLE",
|
||||
"integration_type": "device",
|
||||
|
10
mypy.ini
generated
10
mypy.ini
generated
@ -2456,6 +2456,16 @@ disallow_untyped_defs = true
|
||||
warn_return_any = true
|
||||
warn_unreachable = true
|
||||
|
||||
[mypy-homeassistant.components.immich.*]
|
||||
check_untyped_defs = true
|
||||
disallow_incomplete_defs = true
|
||||
disallow_subclassing_any = true
|
||||
disallow_untyped_calls = true
|
||||
disallow_untyped_decorators = true
|
||||
disallow_untyped_defs = true
|
||||
warn_return_any = true
|
||||
warn_unreachable = true
|
||||
|
||||
[mypy-homeassistant.components.incomfort.*]
|
||||
check_untyped_defs = true
|
||||
disallow_incomplete_defs = true
|
||||
|
3
requirements_all.txt
generated
3
requirements_all.txt
generated
@ -276,6 +276,9 @@ aiohue==4.7.4
|
||||
# homeassistant.components.imap
|
||||
aioimaplib==2.0.1
|
||||
|
||||
# homeassistant.components.immich
|
||||
aioimmich==0.5.0
|
||||
|
||||
# homeassistant.components.apache_kafka
|
||||
aiokafka==0.10.0
|
||||
|
||||
|
3
requirements_test_all.txt
generated
3
requirements_test_all.txt
generated
@ -261,6 +261,9 @@ aiohue==4.7.4
|
||||
# homeassistant.components.imap
|
||||
aioimaplib==2.0.1
|
||||
|
||||
# homeassistant.components.immich
|
||||
aioimmich==0.5.0
|
||||
|
||||
# homeassistant.components.apache_kafka
|
||||
aiokafka==0.10.0
|
||||
|
||||
|
13
tests/components/immich/__init__.py
Normal file
13
tests/components/immich/__init__.py
Normal file
@ -0,0 +1,13 @@
|
||||
"""Tests for the Immich integration."""
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
|
||||
"""Fixture for setting up the component."""
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
136
tests/components/immich/conftest.py
Normal file
136
tests/components/immich/conftest.py
Normal file
@ -0,0 +1,136 @@
|
||||
"""Common fixtures for the Immich tests."""
|
||||
|
||||
from collections.abc import AsyncGenerator, Generator
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aioimmich import ImmichServer, ImmichUsers
|
||||
from aioimmich.server.models import (
|
||||
ImmichServerAbout,
|
||||
ImmichServerStatistics,
|
||||
ImmichServerStorage,
|
||||
)
|
||||
from aioimmich.users.models import AvatarColor, ImmichUser, UserStatus
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.immich.const import DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.immich.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock a config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_HOST: "localhost",
|
||||
CONF_PORT: 80,
|
||||
CONF_SSL: False,
|
||||
CONF_API_KEY: "api_key",
|
||||
CONF_VERIFY_SSL: True,
|
||||
},
|
||||
unique_id="e7ef5713-9dab-4bd4-b899-715b0ca4379e",
|
||||
title="Someone",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_immich_server() -> AsyncMock:
|
||||
"""Mock the Immich server."""
|
||||
mock = AsyncMock(spec=ImmichServer)
|
||||
mock.async_get_about_info.return_value = ImmichServerAbout(
|
||||
"v1.132.3",
|
||||
"some_url",
|
||||
False,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
mock.async_get_storage_info.return_value = ImmichServerStorage(
|
||||
"294.2 GiB",
|
||||
"142.9 GiB",
|
||||
"136.3 GiB",
|
||||
315926315008,
|
||||
153400434688,
|
||||
146402975744,
|
||||
48.56,
|
||||
)
|
||||
mock.async_get_server_statistics.return_value = ImmichServerStatistics(
|
||||
27038, 1836, 119525451912, 54291170551, 65234281361
|
||||
)
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_immich_user() -> AsyncMock:
|
||||
"""Mock the Immich server."""
|
||||
mock = AsyncMock(spec=ImmichUsers)
|
||||
mock.async_get_my_user.return_value = ImmichUser(
|
||||
"e7ef5713-9dab-4bd4-b899-715b0ca4379e",
|
||||
"user@immich.local",
|
||||
"user",
|
||||
"",
|
||||
AvatarColor.PRIMARY,
|
||||
datetime.fromisoformat("2025-05-11T10:07:46.866Z"),
|
||||
"user",
|
||||
False,
|
||||
True,
|
||||
datetime.fromisoformat("2025-05-11T10:07:46.866Z"),
|
||||
None,
|
||||
None,
|
||||
"",
|
||||
None,
|
||||
None,
|
||||
UserStatus.ACTIVE,
|
||||
)
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_immich(
|
||||
mock_immich_server: AsyncMock, mock_immich_user: AsyncMock
|
||||
) -> AsyncGenerator[AsyncMock]:
|
||||
"""Mock the Immich API."""
|
||||
with (
|
||||
patch("homeassistant.components.immich.Immich", autospec=True) as mock_immich,
|
||||
patch("homeassistant.components.immich.config_flow.Immich", new=mock_immich),
|
||||
):
|
||||
client = mock_immich.return_value
|
||||
client.server = mock_immich_server
|
||||
client.users = mock_immich_user
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_non_admin_immich(mock_immich: AsyncMock) -> AsyncMock:
|
||||
"""Mock the Immich API."""
|
||||
mock_immich.users.async_get_my_user.return_value.is_admin = False
|
||||
return mock_immich
|
24
tests/components/immich/const.py
Normal file
24
tests/components/immich/const.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""Constants for the Immich integration tests."""
|
||||
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_URL,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
|
||||
MOCK_USER_DATA = {
|
||||
CONF_URL: "http://localhost",
|
||||
CONF_API_KEY: "abcdef0123456789",
|
||||
CONF_VERIFY_SSL: False,
|
||||
}
|
||||
|
||||
MOCK_CONFIG_ENTRY_DATA = {
|
||||
CONF_HOST: "localhost",
|
||||
CONF_API_KEY: "abcdef0123456789",
|
||||
CONF_PORT: 80,
|
||||
CONF_SSL: False,
|
||||
CONF_VERIFY_SSL: False,
|
||||
}
|
444
tests/components/immich/snapshots/test_sensor.ambr
Normal file
444
tests/components/immich/snapshots/test_sensor.ambr
Normal file
@ -0,0 +1,444 @@
|
||||
# serializer version: 1
|
||||
# name: test_sensors[sensor.someone_disk_available-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_disk_available',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 1,
|
||||
}),
|
||||
'sensor.private': dict({
|
||||
'suggested_unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.DATA_SIZE: 'data_size'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Disk available',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'disk_available',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_disk_available',
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_available-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'data_size',
|
||||
'friendly_name': 'Someone Disk available',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_disk_available',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '136.34839630127',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_size-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_disk_size',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 1,
|
||||
}),
|
||||
'sensor.private': dict({
|
||||
'suggested_unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.DATA_SIZE: 'data_size'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Disk size',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'disk_size',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_disk_size',
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_size-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'data_size',
|
||||
'friendly_name': 'Someone Disk size',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_disk_size',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '294.229309082031',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_usage-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_disk_usage',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Disk usage',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'disk_usage',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_disk_usage',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_usage-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Someone Disk usage',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_disk_usage',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '48.56',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_used-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_disk_used',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 1,
|
||||
}),
|
||||
'sensor.private': dict({
|
||||
'suggested_unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.DATA_SIZE: 'data_size'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Disk used',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'disk_use',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_disk_use',
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_used-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'data_size',
|
||||
'friendly_name': 'Someone Disk used',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_disk_used',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '142.865287780762',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_used_by_photos-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_disk_used_by_photos',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 1,
|
||||
}),
|
||||
'sensor.private': dict({
|
||||
'suggested_unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.DATA_SIZE: 'data_size'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Disk used by photos',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'usage_by_photos',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_usage_by_photos',
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_used_by_photos-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'data_size',
|
||||
'friendly_name': 'Someone Disk used by photos',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_disk_used_by_photos',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '50.5625927364454',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_used_by_videos-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_disk_used_by_videos',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 1,
|
||||
}),
|
||||
'sensor.private': dict({
|
||||
'suggested_unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.DATA_SIZE: 'data_size'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Disk used by videos',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'usage_by_videos',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_usage_by_videos',
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_disk_used_by_videos-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'data_size',
|
||||
'friendly_name': 'Someone Disk used by videos',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': <UnitOfInformation.GIBIBYTES: 'GiB'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_disk_used_by_videos',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '60.754158870317',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_photos_count-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_photos_count',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Photos count',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'photos_count',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_photos_count',
|
||||
'unit_of_measurement': 'photos',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_photos_count-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Someone Photos count',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': 'photos',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_photos_count',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '27038',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_videos_count-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.someone_videos_count',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Videos count',
|
||||
'platform': 'immich',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'videos_count',
|
||||
'unique_id': 'e7ef5713-9dab-4bd4-b899-715b0ca4379e_videos_count',
|
||||
'unit_of_measurement': 'videos',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.someone_videos_count-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Someone Videos count',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': 'videos',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.someone_videos_count',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1836',
|
||||
})
|
||||
# ---
|
244
tests/components/immich/test_config_flow.py
Normal file
244
tests/components/immich/test_config_flow.py
Normal file
@ -0,0 +1,244 @@
|
||||
"""Test the Immich config flow."""
|
||||
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
from aiohttp import ClientError
|
||||
from aioimmich.exceptions import ImmichUnauthorizedError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.immich.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY, CONF_URL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .const import MOCK_CONFIG_ENTRY_DATA, MOCK_USER_DATA
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_step_user(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_immich: Mock
|
||||
) -> None:
|
||||
"""Test a user initiated config flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "user"
|
||||
assert result["data"] == MOCK_CONFIG_ENTRY_DATA
|
||||
assert result["result"].unique_id == "e7ef5713-9dab-4bd4-b899-715b0ca4379e"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
[
|
||||
(
|
||||
ImmichUnauthorizedError(
|
||||
{
|
||||
"message": "Invalid API key",
|
||||
"error": "Unauthenticated",
|
||||
"statusCode": 401,
|
||||
"correlationId": "abcdefg",
|
||||
}
|
||||
),
|
||||
"invalid_auth",
|
||||
),
|
||||
(ClientError, "cannot_connect"),
|
||||
(Exception, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_step_user_error_handling(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_immich: Mock,
|
||||
exception: Exception,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test a user initiated config flow with errors."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
mock_immich.users.async_get_my_user.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_DATA,
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
mock_immich.users.async_get_my_user.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_step_user_invalid_url(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_immich: Mock
|
||||
) -> None:
|
||||
"""Test a user initiated config flow with errors."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{**MOCK_USER_DATA, CONF_URL: "hts://invalid"},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {CONF_URL: "invalid_url"}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_user_already_configured(
|
||||
hass: HomeAssistant, mock_immich: Mock, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test starting a flow by user when already configured."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_DATA,
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_reauth_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test reauthentication flow."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_API_KEY: "other_fake_api_key",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert mock_config_entry.data[CONF_API_KEY] == "other_fake_api_key"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
[
|
||||
(
|
||||
ImmichUnauthorizedError(
|
||||
{
|
||||
"message": "Invalid API key",
|
||||
"error": "Unauthenticated",
|
||||
"statusCode": 401,
|
||||
"correlationId": "abcdefg",
|
||||
}
|
||||
),
|
||||
"invalid_auth",
|
||||
),
|
||||
(ClientError, "cannot_connect"),
|
||||
(Exception, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_reauth_flow_error_handling(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
exception: Exception,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test reauthentication flow with errors."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
mock_immich.users.async_get_my_user.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_API_KEY: "other_fake_api_key",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
mock_immich.users.async_get_my_user.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_API_KEY: "other_fake_api_key",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert mock_config_entry.data[CONF_API_KEY] == "other_fake_api_key"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reauth_flow_mismatch(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test reauthentication flow with mis-matching unique id."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
mock_immich.users.async_get_my_user.return_value.user_id = "other_user_id"
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_API_KEY: "other_fake_api_key",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "unique_id_mismatch"
|
45
tests/components/immich/test_sensor.py
Normal file
45
tests/components/immich/test_sensor.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""Test the Immich sensor platform."""
|
||||
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sensors(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the Immich sensor platform."""
|
||||
|
||||
with patch("homeassistant.components.immich.PLATFORMS", [Platform.SENSOR]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
async def test_admin_sensors(
|
||||
hass: HomeAssistant,
|
||||
mock_non_admin_immich: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the integration doesn't create admin sensors if not admin."""
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get("sensor.mock_title_photos_count") is None
|
||||
assert hass.states.get("sensor.mock_title_videos_count") is None
|
||||
assert hass.states.get("sensor.mock_title_disk_used_by_photos") is None
|
||||
assert hass.states.get("sensor.mock_title_disk_used_by_videos") is None
|
Reference in New Issue
Block a user