Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 459f5ef1e5 | |||
| 42b2d912c9 | |||
| 2007b020ba | |||
| 2ae706d700 | |||
| 1cc5d7cc7d | |||
| 5d878cfbd0 | |||
| c7ed037e2e | |||
| d26e212c82 |
46
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
46
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
---
|
||||
name: Bug Report
|
||||
about: Report a bug or unexpected behavior
|
||||
title: ''
|
||||
labels: bug
|
||||
assignees: ''
|
||||
---
|
||||
|
||||
## Describe the Bug
|
||||
|
||||
A clear description of what the bug is.
|
||||
|
||||
## Environment
|
||||
|
||||
- **Home Assistant version:**
|
||||
- **Integration version:**
|
||||
- **Immich version:**
|
||||
|
||||
## Steps to Reproduce
|
||||
|
||||
1.
|
||||
2.
|
||||
3.
|
||||
|
||||
## Expected Behavior
|
||||
|
||||
What you expected to happen.
|
||||
|
||||
## Actual Behavior
|
||||
|
||||
What actually happened.
|
||||
|
||||
## Logs
|
||||
|
||||
<details>
|
||||
<summary>Relevant log entries</summary>
|
||||
|
||||
```
|
||||
Paste logs here
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## Additional Context
|
||||
|
||||
Any other context about the problem.
|
||||
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
blank_issues_enabled: true
|
||||
contact_links:
|
||||
- name: Home Assistant Community
|
||||
url: https://community.home-assistant.io/
|
||||
about: Ask questions about Home Assistant
|
||||
- name: Immich Documentation
|
||||
url: https://immich.app/docs
|
||||
about: Immich official documentation
|
||||
27
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
27
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
---
|
||||
name: Feature Request
|
||||
about: Suggest a new feature or enhancement
|
||||
title: ''
|
||||
labels: enhancement
|
||||
assignees: ''
|
||||
---
|
||||
|
||||
## Feature Description
|
||||
|
||||
A clear description of what you would like to see added.
|
||||
|
||||
## Use Case
|
||||
|
||||
Describe the problem this feature would solve or the use case it enables.
|
||||
|
||||
## Proposed Solution
|
||||
|
||||
If you have ideas on how to implement this, describe them here.
|
||||
|
||||
## Alternatives Considered
|
||||
|
||||
Any alternative solutions or features you've considered.
|
||||
|
||||
## Additional Context
|
||||
|
||||
Any other context, screenshots, or examples.
|
||||
20
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
20
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
## Description
|
||||
|
||||
Brief description of the changes.
|
||||
|
||||
## Type of Change
|
||||
|
||||
- [ ] Bug fix
|
||||
- [ ] New feature
|
||||
- [ ] Documentation update
|
||||
- [ ] Other (describe):
|
||||
|
||||
## Testing
|
||||
|
||||
Describe how you tested these changes.
|
||||
|
||||
## Checklist
|
||||
|
||||
- [ ] Code follows project style guidelines
|
||||
- [ ] Changes have been tested locally
|
||||
- [ ] Documentation updated (if applicable)
|
||||
15
CLAUDE.md
15
CLAUDE.md
@@ -14,3 +14,18 @@ Use semantic versioning:
|
||||
- **MAJOR** (x.0.0): Breaking changes
|
||||
- **MINOR** (0.x.0): New features, backward compatible
|
||||
- **PATCH** (0.0.x): Bug fixes, integration documentation updates
|
||||
|
||||
## Documentation Updates
|
||||
|
||||
**IMPORTANT**: Always keep the README.md synchronized with integration changes.
|
||||
|
||||
When modifying the integration interface, you MUST update the corresponding documentation:
|
||||
|
||||
- **Service parameters**: Update parameter tables and examples in README.md
|
||||
- **New events**: Add event documentation with examples and field descriptions
|
||||
- **New entities**: Document entity types, attributes, and usage
|
||||
- **Configuration options**: Update configuration documentation
|
||||
- **Translation files**: Add translations for new parameters/entities in `en.json` and `ru.json`
|
||||
- **services.yaml**: Keep service definitions in sync with implementation
|
||||
|
||||
The README is the primary user-facing documentation and must accurately reflect the current state of the integration.
|
||||
|
||||
49
CONTRIBUTING.md
Normal file
49
CONTRIBUTING.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# Contributing to Immich Album Watcher
|
||||
|
||||
Thank you for your interest in contributing to this Home Assistant integration!
|
||||
|
||||
## Getting Started
|
||||
|
||||
1. Fork the repository
|
||||
2. Clone your fork locally
|
||||
3. Create a new branch for your changes
|
||||
|
||||
## Development Setup
|
||||
|
||||
1. Set up a Home Assistant development environment
|
||||
2. Copy the `custom_components/immich_album_watcher` folder to your HA config
|
||||
3. Restart Home Assistant to load changes
|
||||
|
||||
## Code Style
|
||||
|
||||
- Follow [Home Assistant's development guidelines](https://developers.home-assistant.io/docs/development_guidelines)
|
||||
- Use type hints for all function parameters and return values
|
||||
- Keep code compatible with Python 3.11+
|
||||
|
||||
## Submitting Changes
|
||||
|
||||
1. Test your changes thoroughly
|
||||
2. Update documentation if needed
|
||||
3. Create a pull request with a clear description of changes
|
||||
|
||||
## Reporting Issues
|
||||
|
||||
When reporting bugs, please include:
|
||||
|
||||
- Home Assistant version
|
||||
- Integration version
|
||||
- Immich server version
|
||||
- Relevant log entries
|
||||
- Steps to reproduce
|
||||
|
||||
## Version Numbering
|
||||
|
||||
This project uses semantic versioning:
|
||||
|
||||
- **MAJOR** (x.0.0): Breaking changes
|
||||
- **MINOR** (0.x.0): New features, backward compatible
|
||||
- **PATCH** (0.0.x): Bug fixes
|
||||
|
||||
## Questions?
|
||||
|
||||
Open an issue for any questions about contributing.
|
||||
169
README.md
169
README.md
@@ -32,6 +32,7 @@ A Home Assistant custom integration that monitors [Immich](https://immich.app/)
|
||||
- **Services** - Custom service calls:
|
||||
- `immich_album_watcher.refresh` - Force immediate data refresh
|
||||
- `immich_album_watcher.get_recent_assets` - Get recent assets from an album
|
||||
- `immich_album_watcher.send_telegram_notification` - Send text, photo, video, or media group to Telegram
|
||||
- **Share Link Management** - Button entities to create and delete share links:
|
||||
- Create/delete public (unprotected) share links
|
||||
- Create/delete password-protected share links
|
||||
@@ -69,6 +70,7 @@ A Home Assistant custom integration that monitors [Immich](https://immich.app/)
|
||||
| API Key | Your Immich API key | Required |
|
||||
| Albums | Albums to monitor | Required |
|
||||
| Scan Interval | How often to check for changes (seconds) | 60 |
|
||||
| Telegram Bot Token | Bot token for sending media to Telegram (optional) | - |
|
||||
|
||||
## Entities Created (per album)
|
||||
|
||||
@@ -107,14 +109,129 @@ Get the most recent assets from a specific album (returns response data):
|
||||
|
||||
```yaml
|
||||
service: immich_album_watcher.get_recent_assets
|
||||
target:
|
||||
entity_id: sensor.album_name_asset_count
|
||||
data:
|
||||
album_id: "your-album-id-here"
|
||||
count: 10
|
||||
```
|
||||
|
||||
### Send Telegram Notification
|
||||
|
||||
Send notifications to Telegram. Supports multiple formats:
|
||||
|
||||
- **Text message** - When `urls` is empty or not provided
|
||||
- **Single photo** - When `urls` contains one photo
|
||||
- **Single video** - When `urls` contains one video
|
||||
- **Media group** - When `urls` contains multiple items
|
||||
|
||||
The service downloads media from Immich and uploads it to Telegram, bypassing any CORS restrictions. Large lists of media are automatically split into multiple media groups based on the `max_group_size` parameter (default: 10 items per group).
|
||||
|
||||
**Examples:**
|
||||
|
||||
Text message:
|
||||
|
||||
```yaml
|
||||
service: immich_album_watcher.send_telegram_notification
|
||||
target:
|
||||
entity_id: sensor.album_name_asset_count
|
||||
data:
|
||||
chat_id: "-1001234567890"
|
||||
caption: "Check out the new album!"
|
||||
disable_web_page_preview: true
|
||||
```
|
||||
|
||||
Single photo:
|
||||
|
||||
```yaml
|
||||
service: immich_album_watcher.send_telegram_notification
|
||||
target:
|
||||
entity_id: sensor.album_name_asset_count
|
||||
data:
|
||||
chat_id: "-1001234567890"
|
||||
urls:
|
||||
- url: "https://immich.example.com/api/assets/xxx/thumbnail?key=yyy"
|
||||
type: photo
|
||||
caption: "Beautiful sunset!"
|
||||
```
|
||||
|
||||
Media group:
|
||||
|
||||
```yaml
|
||||
service: immich_album_watcher.send_telegram_notification
|
||||
target:
|
||||
entity_id: sensor.album_name_asset_count
|
||||
data:
|
||||
chat_id: "-1001234567890"
|
||||
urls:
|
||||
- url: "https://immich.example.com/api/assets/xxx/thumbnail?key=yyy"
|
||||
type: photo
|
||||
- url: "https://immich.example.com/api/assets/zzz/video/playback?key=yyy"
|
||||
type: video
|
||||
caption: "New photos from the album!"
|
||||
reply_to_message_id: 123
|
||||
```
|
||||
|
||||
HTML formatting:
|
||||
|
||||
```yaml
|
||||
service: immich_album_watcher.send_telegram_notification
|
||||
target:
|
||||
entity_id: sensor.album_name_asset_count
|
||||
data:
|
||||
chat_id: "-1001234567890"
|
||||
caption: |
|
||||
<b>Album Updated!</b>
|
||||
New photos by <i>{{ trigger.event.data.added_assets[0].asset_owner }}</i>
|
||||
<a href="https://immich.example.com/album">View Album</a>
|
||||
parse_mode: "HTML" # Default, can be omitted
|
||||
```
|
||||
|
||||
Non-blocking mode (fire-and-forget):
|
||||
|
||||
```yaml
|
||||
service: immich_album_watcher.send_telegram_notification
|
||||
target:
|
||||
entity_id: sensor.album_name_asset_count
|
||||
data:
|
||||
chat_id: "-1001234567890"
|
||||
urls:
|
||||
- url: "https://immich.example.com/api/assets/xxx/thumbnail?key=yyy"
|
||||
type: photo
|
||||
caption: "Quick notification"
|
||||
wait_for_response: false # Automation continues immediately
|
||||
```
|
||||
|
||||
| Field | Description | Required |
|
||||
|-------|-------------|----------|
|
||||
| `chat_id` | Telegram chat ID to send to | Yes |
|
||||
| `urls` | List of media items with `url` and `type` (photo/video). Empty for text message. | No |
|
||||
| `bot_token` | Telegram bot token (uses configured token if not provided) | No |
|
||||
| `caption` | For media: caption applied to first item. For text: the message text. Supports HTML formatting by default. | No |
|
||||
| `reply_to_message_id` | Message ID to reply to | No |
|
||||
| `disable_web_page_preview` | Disable link previews in text messages | No |
|
||||
| `parse_mode` | How to parse caption/text. Options: `HTML`, `Markdown`, `MarkdownV2`, or empty string for plain text. Default: `HTML` | No |
|
||||
| `max_group_size` | Maximum media items per group (2-10). Large lists split into multiple groups. Default: 10 | No |
|
||||
| `chunk_delay` | Delay in milliseconds between sending multiple groups (0-60000). Useful for rate limiting. Default: 0 | No |
|
||||
| `wait_for_response` | Wait for Telegram to finish processing. Set to `false` for fire-and-forget (automation continues immediately). Default: `true` | No |
|
||||
|
||||
The service returns a response with `success` status and `message_id` (single message), `message_ids` (media group), or `groups_sent` (number of groups when split). When `wait_for_response` is `false`, the service returns immediately with `{"success": true, "status": "queued"}` while processing continues in the background.
|
||||
|
||||
## Events
|
||||
|
||||
Use these events in your automations:
|
||||
The integration fires multiple event types that you can use in your automations:
|
||||
|
||||
### Available Events
|
||||
|
||||
| Event Type | Description | When Fired |
|
||||
|------------|-------------|------------|
|
||||
| `immich_album_watcher_album_changed` | General album change event | Fired for any album change |
|
||||
| `immich_album_watcher_assets_added` | Assets were added to the album | When new photos/videos are added |
|
||||
| `immich_album_watcher_assets_removed` | Assets were removed from the album | When photos/videos are removed |
|
||||
| `immich_album_watcher_album_renamed` | Album name was changed | When the album is renamed |
|
||||
| `immich_album_watcher_album_deleted` | Album was deleted | When the album is deleted from Immich |
|
||||
| `immich_album_watcher_album_sharing_changed` | Album sharing status changed | When album is shared or unshared |
|
||||
|
||||
### Example Usage
|
||||
|
||||
```yaml
|
||||
automation:
|
||||
@@ -127,21 +244,47 @@ automation:
|
||||
data:
|
||||
title: "New Photos"
|
||||
message: "{{ trigger.event.data.added_count }} new photos in {{ trigger.event.data.album_name }}"
|
||||
|
||||
- alias: "Album renamed"
|
||||
trigger:
|
||||
- platform: event
|
||||
event_type: immich_album_watcher_album_renamed
|
||||
action:
|
||||
- service: notify.mobile_app
|
||||
data:
|
||||
title: "Album Renamed"
|
||||
message: "Album '{{ trigger.event.data.old_name }}' renamed to '{{ trigger.event.data.new_name }}'"
|
||||
|
||||
- alias: "Album deleted"
|
||||
trigger:
|
||||
- platform: event
|
||||
event_type: immich_album_watcher_album_deleted
|
||||
action:
|
||||
- service: notify.mobile_app
|
||||
data:
|
||||
title: "Album Deleted"
|
||||
message: "Album '{{ trigger.event.data.album_name }}' was deleted"
|
||||
```
|
||||
|
||||
### Event Data
|
||||
|
||||
| Field | Description |
|
||||
|-------|-------------|
|
||||
| `album_id` | Album ID |
|
||||
| `album_name` | Album name |
|
||||
| `album_url` | Public URL to view the album (only present if album has a shared link) |
|
||||
| `change_type` | Type of change (assets_added, assets_removed, changed) |
|
||||
| `added_count` | Number of assets added |
|
||||
| `removed_count` | Number of assets removed |
|
||||
| `added_assets` | List of added assets with details (see below) |
|
||||
| `removed_assets` | List of removed asset IDs |
|
||||
| `people` | List of all people detected in the album |
|
||||
| Field | Description | Available In |
|
||||
|-------|-------------|--------------|
|
||||
| `hub_name` | Hub name configured in integration | All events |
|
||||
| `album_id` | Album ID | All events |
|
||||
| `album_name` | Current album name | All events |
|
||||
| `album_url` | Public URL to view the album (only present if album has a shared link) | All events except `album_deleted` |
|
||||
| `change_type` | Type of change (assets_added, assets_removed, album_renamed, album_sharing_changed, changed) | All events except `album_deleted` |
|
||||
| `shared` | Current sharing status of the album | All events except `album_deleted` |
|
||||
| `added_count` | Number of assets added | `album_changed`, `assets_added` |
|
||||
| `removed_count` | Number of assets removed | `album_changed`, `assets_removed` |
|
||||
| `added_assets` | List of added assets with details (see below) | `album_changed`, `assets_added` |
|
||||
| `removed_assets` | List of removed asset IDs | `album_changed`, `assets_removed` |
|
||||
| `people` | List of all people detected in the album | All events except `album_deleted` |
|
||||
| `old_name` | Previous album name | `album_renamed` |
|
||||
| `new_name` | New album name | `album_renamed` |
|
||||
| `old_shared` | Previous sharing status | `album_sharing_changed` |
|
||||
| `new_shared` | New sharing status | `album_sharing_changed` |
|
||||
|
||||
### Added Assets Fields
|
||||
|
||||
|
||||
@@ -82,13 +82,6 @@ class ImmichAlbumNewAssetsSensor(
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if new assets were recently added."""
|
||||
|
||||
@@ -83,13 +83,6 @@ class ImmichCreateShareLinkButton(
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available.
|
||||
@@ -173,13 +166,6 @@ class ImmichDeleteShareLinkButton(
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available.
|
||||
@@ -270,13 +256,6 @@ class ImmichCreateProtectedLinkButton(
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available.
|
||||
@@ -364,13 +343,6 @@ class ImmichDeleteProtectedLinkButton(
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available.
|
||||
|
||||
@@ -78,13 +78,6 @@ class ImmichAlbumThumbnailCamera(
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
|
||||
@@ -27,6 +27,9 @@ DEFAULT_SHARE_PASSWORD: Final = "immich123"
|
||||
EVENT_ALBUM_CHANGED: Final = f"{DOMAIN}_album_changed"
|
||||
EVENT_ASSETS_ADDED: Final = f"{DOMAIN}_assets_added"
|
||||
EVENT_ASSETS_REMOVED: Final = f"{DOMAIN}_assets_removed"
|
||||
EVENT_ALBUM_RENAMED: Final = f"{DOMAIN}_album_renamed"
|
||||
EVENT_ALBUM_DELETED: Final = f"{DOMAIN}_album_deleted"
|
||||
EVENT_ALBUM_SHARING_CHANGED: Final = f"{DOMAIN}_album_sharing_changed"
|
||||
|
||||
# Attributes
|
||||
ATTR_HUB_NAME: Final = "hub_name"
|
||||
@@ -50,6 +53,10 @@ ATTR_THUMBNAIL_URL: Final = "thumbnail_url"
|
||||
ATTR_SHARED: Final = "shared"
|
||||
ATTR_OWNER: Final = "owner"
|
||||
ATTR_PEOPLE: Final = "people"
|
||||
ATTR_OLD_NAME: Final = "old_name"
|
||||
ATTR_NEW_NAME: Final = "new_name"
|
||||
ATTR_OLD_SHARED: Final = "old_shared"
|
||||
ATTR_NEW_SHARED: Final = "new_shared"
|
||||
ATTR_ASSET_TYPE: Final = "asset_type"
|
||||
ATTR_ASSET_FILENAME: Final = "asset_filename"
|
||||
ATTR_ASSET_CREATED: Final = "asset_created"
|
||||
@@ -70,4 +77,4 @@ PLATFORMS: Final = ["sensor", "binary_sensor", "camera", "text", "button"]
|
||||
# Services
|
||||
SERVICE_REFRESH: Final = "refresh"
|
||||
SERVICE_GET_RECENT_ASSETS: Final = "get_recent_assets"
|
||||
SERVICE_SEND_TELEGRAM_MEDIA_GROUP: Final = "send_telegram_media_group"
|
||||
SERVICE_SEND_TELEGRAM_NOTIFICATION: Final = "send_telegram_notification"
|
||||
|
||||
@@ -38,10 +38,18 @@ from .const import (
|
||||
ATTR_PEOPLE,
|
||||
ATTR_REMOVED_ASSETS,
|
||||
ATTR_REMOVED_COUNT,
|
||||
ATTR_OLD_NAME,
|
||||
ATTR_NEW_NAME,
|
||||
ATTR_OLD_SHARED,
|
||||
ATTR_NEW_SHARED,
|
||||
ATTR_SHARED,
|
||||
DOMAIN,
|
||||
EVENT_ALBUM_CHANGED,
|
||||
EVENT_ASSETS_ADDED,
|
||||
EVENT_ASSETS_REMOVED,
|
||||
EVENT_ALBUM_RENAMED,
|
||||
EVENT_ALBUM_DELETED,
|
||||
EVENT_ALBUM_SHARING_CHANGED,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -210,6 +218,10 @@ class AlbumChange:
|
||||
removed_count: int = 0
|
||||
added_assets: list[AssetInfo] = field(default_factory=list)
|
||||
removed_asset_ids: list[str] = field(default_factory=list)
|
||||
old_name: str | None = None
|
||||
new_name: str | None = None
|
||||
old_shared: bool | None = None
|
||||
new_shared: bool | None = None
|
||||
|
||||
|
||||
class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
@@ -510,6 +522,15 @@ class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
) as response:
|
||||
if response.status == 404:
|
||||
_LOGGER.warning("Album %s not found", self._album_id)
|
||||
# Fire album_deleted event if we had previous state (album was deleted)
|
||||
if self._previous_state:
|
||||
event_data = {
|
||||
ATTR_HUB_NAME: self._hub_name,
|
||||
ATTR_ALBUM_ID: self._album_id,
|
||||
ATTR_ALBUM_NAME: self._previous_state.name,
|
||||
}
|
||||
self.hass.bus.async_fire(EVENT_ALBUM_DELETED, event_data)
|
||||
_LOGGER.info("Album '%s' was deleted", self._previous_state.name)
|
||||
return None
|
||||
if response.status != 200:
|
||||
raise UpdateFailed(
|
||||
@@ -599,13 +620,23 @@ class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
added_ids = new_state.asset_ids - old_state.asset_ids
|
||||
removed_ids = old_state.asset_ids - new_state.asset_ids
|
||||
|
||||
if not added_ids and not removed_ids:
|
||||
# Detect metadata changes
|
||||
name_changed = old_state.name != new_state.name
|
||||
sharing_changed = old_state.shared != new_state.shared
|
||||
|
||||
# Return None only if nothing changed at all
|
||||
if not added_ids and not removed_ids and not name_changed and not sharing_changed:
|
||||
return None
|
||||
|
||||
# Determine primary change type
|
||||
change_type = "changed"
|
||||
if added_ids and not removed_ids:
|
||||
if name_changed and not added_ids and not removed_ids and not sharing_changed:
|
||||
change_type = "album_renamed"
|
||||
elif sharing_changed and not added_ids and not removed_ids and not name_changed:
|
||||
change_type = "album_sharing_changed"
|
||||
elif added_ids and not removed_ids and not name_changed and not sharing_changed:
|
||||
change_type = "assets_added"
|
||||
elif removed_ids and not added_ids:
|
||||
elif removed_ids and not added_ids and not name_changed and not sharing_changed:
|
||||
change_type = "assets_removed"
|
||||
|
||||
added_assets = [
|
||||
@@ -620,6 +651,10 @@ class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
removed_count=len(removed_ids),
|
||||
added_assets=added_assets,
|
||||
removed_asset_ids=list(removed_ids),
|
||||
old_name=old_state.name if name_changed else None,
|
||||
new_name=new_state.name if name_changed else None,
|
||||
old_shared=old_state.shared if sharing_changed else None,
|
||||
new_shared=new_state.shared if sharing_changed else None,
|
||||
)
|
||||
|
||||
def _fire_events(self, change: AlbumChange, album: AlbumData) -> None:
|
||||
@@ -658,8 +693,18 @@ class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
ATTR_ADDED_ASSETS: added_assets_detail,
|
||||
ATTR_REMOVED_ASSETS: change.removed_asset_ids,
|
||||
ATTR_PEOPLE: list(album.people),
|
||||
ATTR_SHARED: album.shared,
|
||||
}
|
||||
|
||||
# Add metadata change attributes if applicable
|
||||
if change.old_name is not None:
|
||||
event_data[ATTR_OLD_NAME] = change.old_name
|
||||
event_data[ATTR_NEW_NAME] = change.new_name
|
||||
|
||||
if change.old_shared is not None:
|
||||
event_data[ATTR_OLD_SHARED] = change.old_shared
|
||||
event_data[ATTR_NEW_SHARED] = change.new_shared
|
||||
|
||||
album_url = self.get_any_url()
|
||||
if album_url:
|
||||
event_data[ATTR_ALBUM_URL] = album_url
|
||||
@@ -679,6 +724,24 @@ class ImmichAlbumWatcherCoordinator(DataUpdateCoordinator[AlbumData | None]):
|
||||
if change.removed_count > 0:
|
||||
self.hass.bus.async_fire(EVENT_ASSETS_REMOVED, event_data)
|
||||
|
||||
# Fire specific events for metadata changes
|
||||
if change.old_name is not None:
|
||||
self.hass.bus.async_fire(EVENT_ALBUM_RENAMED, event_data)
|
||||
_LOGGER.info(
|
||||
"Album renamed: '%s' -> '%s'",
|
||||
change.old_name,
|
||||
change.new_name,
|
||||
)
|
||||
|
||||
if change.old_shared is not None:
|
||||
self.hass.bus.async_fire(EVENT_ALBUM_SHARING_CHANGED, event_data)
|
||||
_LOGGER.info(
|
||||
"Album '%s' sharing changed: %s -> %s",
|
||||
change.album_name,
|
||||
change.old_shared,
|
||||
change.new_shared,
|
||||
)
|
||||
|
||||
def get_protected_link_id(self) -> str | None:
|
||||
"""Get the ID of the first protected link."""
|
||||
protected_links = self._get_protected_links()
|
||||
|
||||
@@ -8,5 +8,5 @@
|
||||
"iot_class": "cloud_polling",
|
||||
"issue_tracker": "https://github.com/DolgolyovAlexei/haos-hacs-immich-album-watcher/issues",
|
||||
"requirements": [],
|
||||
"version": "1.4.0"
|
||||
"version": "2.0.0"
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ from .const import (
|
||||
DOMAIN,
|
||||
SERVICE_GET_RECENT_ASSETS,
|
||||
SERVICE_REFRESH,
|
||||
SERVICE_SEND_TELEGRAM_MEDIA_GROUP,
|
||||
SERVICE_SEND_TELEGRAM_NOTIFICATION,
|
||||
)
|
||||
from .coordinator import AlbumData, ImmichAlbumWatcherCoordinator
|
||||
|
||||
@@ -99,16 +99,25 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_SEND_TELEGRAM_MEDIA_GROUP,
|
||||
SERVICE_SEND_TELEGRAM_NOTIFICATION,
|
||||
{
|
||||
vol.Optional("bot_token"): str,
|
||||
vol.Required("chat_id"): vol.Coerce(str),
|
||||
vol.Required("urls"): vol.All(list, vol.Length(min=1, max=10)),
|
||||
vol.Optional("urls"): list,
|
||||
vol.Optional("caption"): str,
|
||||
vol.Optional("reply_to_message_id"): vol.Coerce(int),
|
||||
vol.Optional("disable_web_page_preview"): bool,
|
||||
vol.Optional("parse_mode", default="HTML"): str,
|
||||
vol.Optional("max_group_size", default=10): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=2, max=10)
|
||||
),
|
||||
vol.Optional("chunk_delay", default=0): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=0, max=60000)
|
||||
),
|
||||
vol.Optional("wait_for_response", default=True): bool,
|
||||
},
|
||||
"async_send_telegram_media_group",
|
||||
supports_response=SupportsResponse.ONLY,
|
||||
"async_send_telegram_notification",
|
||||
supports_response=SupportsResponse.OPTIONAL,
|
||||
)
|
||||
|
||||
|
||||
@@ -138,13 +147,6 @@ class ImmichAlbumBaseSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Se
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
@@ -174,19 +176,76 @@ class ImmichAlbumBaseSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Se
|
||||
assets = await self.coordinator.async_get_recent_assets(count)
|
||||
return {"assets": assets}
|
||||
|
||||
async def async_send_telegram_media_group(
|
||||
async def async_send_telegram_notification(
|
||||
self,
|
||||
chat_id: str,
|
||||
urls: list[dict[str, str]],
|
||||
urls: list[dict[str, str]] | None = None,
|
||||
bot_token: str | None = None,
|
||||
caption: str | None = None,
|
||||
reply_to_message_id: int | None = None,
|
||||
disable_web_page_preview: bool | None = None,
|
||||
parse_mode: str = "HTML",
|
||||
max_group_size: int = 10,
|
||||
chunk_delay: int = 0,
|
||||
wait_for_response: bool = True,
|
||||
) -> ServiceResponse:
|
||||
"""Send media URLs to Telegram as a media group.
|
||||
"""Send notification to Telegram.
|
||||
|
||||
Supports:
|
||||
- Empty URLs: sends a simple text message
|
||||
- Single photo: uses sendPhoto API
|
||||
- Single video: uses sendVideo API
|
||||
- Multiple items: uses sendMediaGroup API (splits into multiple groups if needed)
|
||||
|
||||
Each item in urls should be a dict with 'url' and 'type' (photo/video).
|
||||
Downloads media and uploads to Telegram to bypass CORS restrictions.
|
||||
|
||||
If wait_for_response is False, the task will be executed in the background
|
||||
and the service will return immediately.
|
||||
"""
|
||||
# If non-blocking mode, create a background task and return immediately
|
||||
if not wait_for_response:
|
||||
self.hass.async_create_task(
|
||||
self._execute_telegram_notification(
|
||||
chat_id=chat_id,
|
||||
urls=urls,
|
||||
bot_token=bot_token,
|
||||
caption=caption,
|
||||
reply_to_message_id=reply_to_message_id,
|
||||
disable_web_page_preview=disable_web_page_preview,
|
||||
parse_mode=parse_mode,
|
||||
max_group_size=max_group_size,
|
||||
chunk_delay=chunk_delay,
|
||||
)
|
||||
)
|
||||
return {"success": True, "status": "queued", "message": "Notification queued for background processing"}
|
||||
|
||||
# Blocking mode - execute and return result
|
||||
return await self._execute_telegram_notification(
|
||||
chat_id=chat_id,
|
||||
urls=urls,
|
||||
bot_token=bot_token,
|
||||
caption=caption,
|
||||
reply_to_message_id=reply_to_message_id,
|
||||
disable_web_page_preview=disable_web_page_preview,
|
||||
parse_mode=parse_mode,
|
||||
max_group_size=max_group_size,
|
||||
chunk_delay=chunk_delay,
|
||||
)
|
||||
|
||||
async def _execute_telegram_notification(
|
||||
self,
|
||||
chat_id: str,
|
||||
urls: list[dict[str, str]] | None = None,
|
||||
bot_token: str | None = None,
|
||||
caption: str | None = None,
|
||||
reply_to_message_id: int | None = None,
|
||||
disable_web_page_preview: bool | None = None,
|
||||
parse_mode: str = "HTML",
|
||||
max_group_size: int = 10,
|
||||
chunk_delay: int = 0,
|
||||
) -> ServiceResponse:
|
||||
"""Execute the Telegram notification (internal method)."""
|
||||
import json
|
||||
import aiohttp
|
||||
from aiohttp import FormData
|
||||
@@ -202,81 +261,65 @@ class ImmichAlbumBaseSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Se
|
||||
|
||||
session = async_get_clientsession(self.hass)
|
||||
|
||||
# Download all media files
|
||||
media_files: list[tuple[str, bytes, str]] = []
|
||||
for i, item in enumerate(urls):
|
||||
url = item.get("url")
|
||||
media_type = item.get("type", "photo")
|
||||
# Handle empty URLs - send simple text message
|
||||
if not urls:
|
||||
return await self._send_telegram_message(
|
||||
session, token, chat_id, caption or "", reply_to_message_id, disable_web_page_preview, parse_mode
|
||||
)
|
||||
|
||||
if not url:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Missing 'url' in item {i}",
|
||||
}
|
||||
# Handle single photo
|
||||
if len(urls) == 1 and urls[0].get("type", "photo") == "photo":
|
||||
return await self._send_telegram_photo(
|
||||
session, token, chat_id, urls[0].get("url"), caption, reply_to_message_id, parse_mode
|
||||
)
|
||||
|
||||
if media_type not in ("photo", "video"):
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Invalid type '{media_type}' in item {i}. Must be 'photo' or 'video'.",
|
||||
}
|
||||
# Handle single video
|
||||
if len(urls) == 1 and urls[0].get("type") == "video":
|
||||
return await self._send_telegram_video(
|
||||
session, token, chat_id, urls[0].get("url"), caption, reply_to_message_id, parse_mode
|
||||
)
|
||||
|
||||
try:
|
||||
_LOGGER.debug("Downloading media %d from %s", i, url[:80])
|
||||
async with session.get(url) as resp:
|
||||
if resp.status != 200:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Failed to download media {i}: HTTP {resp.status}",
|
||||
}
|
||||
data = await resp.read()
|
||||
ext = "jpg" if media_type == "photo" else "mp4"
|
||||
filename = f"media_{i}.{ext}"
|
||||
media_files.append((media_type, data, filename))
|
||||
_LOGGER.debug("Downloaded media %d: %d bytes", i, len(data))
|
||||
except aiohttp.ClientError as err:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Failed to download media {i}: {err}",
|
||||
}
|
||||
# Handle multiple items - send as media group(s)
|
||||
return await self._send_telegram_media_group(
|
||||
session, token, chat_id, urls, caption, reply_to_message_id, max_group_size, chunk_delay, parse_mode
|
||||
)
|
||||
|
||||
# Build multipart form
|
||||
form = FormData()
|
||||
form.add_field("chat_id", chat_id)
|
||||
async def _send_telegram_message(
|
||||
self,
|
||||
session: Any,
|
||||
token: str,
|
||||
chat_id: str,
|
||||
text: str,
|
||||
reply_to_message_id: int | None = None,
|
||||
disable_web_page_preview: bool | None = None,
|
||||
parse_mode: str = "HTML",
|
||||
) -> ServiceResponse:
|
||||
"""Send a simple text message to Telegram."""
|
||||
import aiohttp
|
||||
|
||||
telegram_url = f"https://api.telegram.org/bot{token}/sendMessage"
|
||||
|
||||
payload: dict[str, Any] = {
|
||||
"chat_id": chat_id,
|
||||
"text": text or "Notification from Home Assistant",
|
||||
"parse_mode": parse_mode,
|
||||
}
|
||||
|
||||
if reply_to_message_id:
|
||||
form.add_field("reply_to_message_id", str(reply_to_message_id))
|
||||
payload["reply_to_message_id"] = reply_to_message_id
|
||||
|
||||
# Build media JSON with attach:// references
|
||||
media_json = []
|
||||
for i, (media_type, data, filename) in enumerate(media_files):
|
||||
attach_name = f"file{i}"
|
||||
media_item: dict[str, Any] = {
|
||||
"type": media_type,
|
||||
"media": f"attach://{attach_name}",
|
||||
}
|
||||
if i == 0 and caption:
|
||||
media_item["caption"] = caption
|
||||
media_json.append(media_item)
|
||||
|
||||
content_type = "image/jpeg" if media_type == "photo" else "video/mp4"
|
||||
form.add_field(attach_name, data, filename=filename, content_type=content_type)
|
||||
|
||||
form.add_field("media", json.dumps(media_json))
|
||||
|
||||
# Send to Telegram
|
||||
telegram_url = f"https://api.telegram.org/bot{token}/sendMediaGroup"
|
||||
if disable_web_page_preview is not None:
|
||||
payload["disable_web_page_preview"] = disable_web_page_preview
|
||||
|
||||
try:
|
||||
_LOGGER.debug("Uploading %d files to Telegram", len(media_files))
|
||||
async with session.post(telegram_url, data=form) as response:
|
||||
_LOGGER.debug("Sending text message to Telegram")
|
||||
async with session.post(telegram_url, json=payload) as response:
|
||||
result = await response.json()
|
||||
_LOGGER.debug("Telegram API response: status=%d, ok=%s", response.status, result.get("ok"))
|
||||
if response.status == 200 and result.get("ok"):
|
||||
return {
|
||||
"success": True,
|
||||
"message_ids": [
|
||||
msg.get("message_id") for msg in result.get("result", [])
|
||||
],
|
||||
"message_id": result.get("result", {}).get("message_id"),
|
||||
}
|
||||
else:
|
||||
_LOGGER.error("Telegram API error: %s", result)
|
||||
@@ -286,9 +329,305 @@ class ImmichAlbumBaseSensor(CoordinatorEntity[ImmichAlbumWatcherCoordinator], Se
|
||||
"error_code": result.get("error_code"),
|
||||
}
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Telegram upload failed: %s", err)
|
||||
_LOGGER.error("Telegram message send failed: %s", err)
|
||||
return {"success": False, "error": str(err)}
|
||||
|
||||
async def _send_telegram_photo(
|
||||
self,
|
||||
session: Any,
|
||||
token: str,
|
||||
chat_id: str,
|
||||
url: str | None,
|
||||
caption: str | None = None,
|
||||
reply_to_message_id: int | None = None,
|
||||
parse_mode: str = "HTML",
|
||||
) -> ServiceResponse:
|
||||
"""Send a single photo to Telegram."""
|
||||
import aiohttp
|
||||
from aiohttp import FormData
|
||||
|
||||
if not url:
|
||||
return {"success": False, "error": "Missing 'url' for photo"}
|
||||
|
||||
try:
|
||||
# Download the photo
|
||||
_LOGGER.debug("Downloading photo from %s", url[:80])
|
||||
async with session.get(url) as resp:
|
||||
if resp.status != 200:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Failed to download photo: HTTP {resp.status}",
|
||||
}
|
||||
data = await resp.read()
|
||||
_LOGGER.debug("Downloaded photo: %d bytes", len(data))
|
||||
|
||||
# Build multipart form
|
||||
form = FormData()
|
||||
form.add_field("chat_id", chat_id)
|
||||
form.add_field("photo", data, filename="photo.jpg", content_type="image/jpeg")
|
||||
form.add_field("parse_mode", parse_mode)
|
||||
|
||||
if caption:
|
||||
form.add_field("caption", caption)
|
||||
|
||||
if reply_to_message_id:
|
||||
form.add_field("reply_to_message_id", str(reply_to_message_id))
|
||||
|
||||
# Send to Telegram
|
||||
telegram_url = f"https://api.telegram.org/bot{token}/sendPhoto"
|
||||
|
||||
_LOGGER.debug("Uploading photo to Telegram")
|
||||
async with session.post(telegram_url, data=form) as response:
|
||||
result = await response.json()
|
||||
_LOGGER.debug("Telegram API response: status=%d, ok=%s", response.status, result.get("ok"))
|
||||
if response.status == 200 and result.get("ok"):
|
||||
return {
|
||||
"success": True,
|
||||
"message_id": result.get("result", {}).get("message_id"),
|
||||
}
|
||||
else:
|
||||
_LOGGER.error("Telegram API error: %s", result)
|
||||
return {
|
||||
"success": False,
|
||||
"error": result.get("description", "Unknown Telegram error"),
|
||||
"error_code": result.get("error_code"),
|
||||
}
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Telegram photo upload failed: %s", err)
|
||||
return {"success": False, "error": str(err)}
|
||||
|
||||
async def _send_telegram_video(
|
||||
self,
|
||||
session: Any,
|
||||
token: str,
|
||||
chat_id: str,
|
||||
url: str | None,
|
||||
caption: str | None = None,
|
||||
reply_to_message_id: int | None = None,
|
||||
parse_mode: str = "HTML",
|
||||
) -> ServiceResponse:
|
||||
"""Send a single video to Telegram."""
|
||||
import aiohttp
|
||||
from aiohttp import FormData
|
||||
|
||||
if not url:
|
||||
return {"success": False, "error": "Missing 'url' for video"}
|
||||
|
||||
try:
|
||||
# Download the video
|
||||
_LOGGER.debug("Downloading video from %s", url[:80])
|
||||
async with session.get(url) as resp:
|
||||
if resp.status != 200:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Failed to download video: HTTP {resp.status}",
|
||||
}
|
||||
data = await resp.read()
|
||||
_LOGGER.debug("Downloaded video: %d bytes", len(data))
|
||||
|
||||
# Build multipart form
|
||||
form = FormData()
|
||||
form.add_field("chat_id", chat_id)
|
||||
form.add_field("video", data, filename="video.mp4", content_type="video/mp4")
|
||||
form.add_field("parse_mode", parse_mode)
|
||||
|
||||
if caption:
|
||||
form.add_field("caption", caption)
|
||||
|
||||
if reply_to_message_id:
|
||||
form.add_field("reply_to_message_id", str(reply_to_message_id))
|
||||
|
||||
# Send to Telegram
|
||||
telegram_url = f"https://api.telegram.org/bot{token}/sendVideo"
|
||||
|
||||
_LOGGER.debug("Uploading video to Telegram")
|
||||
async with session.post(telegram_url, data=form) as response:
|
||||
result = await response.json()
|
||||
_LOGGER.debug("Telegram API response: status=%d, ok=%s", response.status, result.get("ok"))
|
||||
if response.status == 200 and result.get("ok"):
|
||||
return {
|
||||
"success": True,
|
||||
"message_id": result.get("result", {}).get("message_id"),
|
||||
}
|
||||
else:
|
||||
_LOGGER.error("Telegram API error: %s", result)
|
||||
return {
|
||||
"success": False,
|
||||
"error": result.get("description", "Unknown Telegram error"),
|
||||
"error_code": result.get("error_code"),
|
||||
}
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Telegram video upload failed: %s", err)
|
||||
return {"success": False, "error": str(err)}
|
||||
|
||||
async def _send_telegram_media_group(
|
||||
self,
|
||||
session: Any,
|
||||
token: str,
|
||||
chat_id: str,
|
||||
urls: list[dict[str, str]],
|
||||
caption: str | None = None,
|
||||
reply_to_message_id: int | None = None,
|
||||
max_group_size: int = 10,
|
||||
chunk_delay: int = 0,
|
||||
parse_mode: str = "HTML",
|
||||
) -> ServiceResponse:
|
||||
"""Send media URLs to Telegram as media group(s).
|
||||
|
||||
If urls list exceeds max_group_size, splits into multiple media groups.
|
||||
For chunks with single items, uses sendPhoto/sendVideo APIs.
|
||||
Applies chunk_delay (in milliseconds) between groups if specified.
|
||||
"""
|
||||
import json
|
||||
import asyncio
|
||||
import aiohttp
|
||||
from aiohttp import FormData
|
||||
|
||||
# Split URLs into chunks based on max_group_size
|
||||
chunks = [urls[i:i + max_group_size] for i in range(0, len(urls), max_group_size)]
|
||||
all_message_ids = []
|
||||
|
||||
_LOGGER.debug("Sending %d media items in %d chunk(s) of max %d items (delay: %dms)",
|
||||
len(urls), len(chunks), max_group_size, chunk_delay)
|
||||
|
||||
for chunk_idx, chunk in enumerate(chunks):
|
||||
# Add delay before sending subsequent chunks
|
||||
if chunk_idx > 0 and chunk_delay > 0:
|
||||
delay_seconds = chunk_delay / 1000
|
||||
_LOGGER.debug("Waiting %dms (%ss) before sending chunk %d/%d",
|
||||
chunk_delay, delay_seconds, chunk_idx + 1, len(chunks))
|
||||
await asyncio.sleep(delay_seconds)
|
||||
|
||||
# Optimize: Use single-item APIs for chunks with 1 item
|
||||
if len(chunk) == 1:
|
||||
item = chunk[0]
|
||||
media_type = item.get("type", "photo")
|
||||
url = item.get("url")
|
||||
|
||||
# Only apply caption and reply_to to the first chunk
|
||||
chunk_caption = caption if chunk_idx == 0 else None
|
||||
chunk_reply_to = reply_to_message_id if chunk_idx == 0 else None
|
||||
|
||||
if media_type == "photo":
|
||||
_LOGGER.debug("Sending chunk %d/%d as single photo", chunk_idx + 1, len(chunks))
|
||||
result = await self._send_telegram_photo(
|
||||
session, token, chat_id, url, chunk_caption, chunk_reply_to, parse_mode
|
||||
)
|
||||
else: # video
|
||||
_LOGGER.debug("Sending chunk %d/%d as single video", chunk_idx + 1, len(chunks))
|
||||
result = await self._send_telegram_video(
|
||||
session, token, chat_id, url, chunk_caption, chunk_reply_to, parse_mode
|
||||
)
|
||||
|
||||
if not result.get("success"):
|
||||
result["failed_at_chunk"] = chunk_idx + 1
|
||||
return result
|
||||
|
||||
all_message_ids.append(result.get("message_id"))
|
||||
continue
|
||||
# Multi-item chunk: use sendMediaGroup
|
||||
_LOGGER.debug("Sending chunk %d/%d as media group (%d items)", chunk_idx + 1, len(chunks), len(chunk))
|
||||
|
||||
# Download all media files for this chunk
|
||||
media_files: list[tuple[str, bytes, str]] = []
|
||||
for i, item in enumerate(chunk):
|
||||
url = item.get("url")
|
||||
media_type = item.get("type", "photo")
|
||||
|
||||
if not url:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Missing 'url' in item {chunk_idx * max_group_size + i}",
|
||||
}
|
||||
|
||||
if media_type not in ("photo", "video"):
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Invalid type '{media_type}' in item {chunk_idx * max_group_size + i}. Must be 'photo' or 'video'.",
|
||||
}
|
||||
|
||||
try:
|
||||
_LOGGER.debug("Downloading media %d from %s", chunk_idx * max_group_size + i, url[:80])
|
||||
async with session.get(url) as resp:
|
||||
if resp.status != 200:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Failed to download media {chunk_idx * max_group_size + i}: HTTP {resp.status}",
|
||||
}
|
||||
data = await resp.read()
|
||||
ext = "jpg" if media_type == "photo" else "mp4"
|
||||
filename = f"media_{chunk_idx * max_group_size + i}.{ext}"
|
||||
media_files.append((media_type, data, filename))
|
||||
_LOGGER.debug("Downloaded media %d: %d bytes", chunk_idx * max_group_size + i, len(data))
|
||||
except aiohttp.ClientError as err:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Failed to download media {chunk_idx * max_group_size + i}: {err}",
|
||||
}
|
||||
|
||||
# Build multipart form
|
||||
form = FormData()
|
||||
form.add_field("chat_id", chat_id)
|
||||
|
||||
# Only use reply_to_message_id for the first chunk
|
||||
if chunk_idx == 0 and reply_to_message_id:
|
||||
form.add_field("reply_to_message_id", str(reply_to_message_id))
|
||||
|
||||
# Build media JSON with attach:// references
|
||||
media_json = []
|
||||
for i, (media_type, data, filename) in enumerate(media_files):
|
||||
attach_name = f"file{i}"
|
||||
media_item: dict[str, Any] = {
|
||||
"type": media_type,
|
||||
"media": f"attach://{attach_name}",
|
||||
}
|
||||
# Only add caption to the first item of the first chunk
|
||||
if chunk_idx == 0 and i == 0 and caption:
|
||||
media_item["caption"] = caption
|
||||
media_item["parse_mode"] = parse_mode
|
||||
media_json.append(media_item)
|
||||
|
||||
content_type = "image/jpeg" if media_type == "photo" else "video/mp4"
|
||||
form.add_field(attach_name, data, filename=filename, content_type=content_type)
|
||||
|
||||
form.add_field("media", json.dumps(media_json))
|
||||
|
||||
# Send to Telegram
|
||||
telegram_url = f"https://api.telegram.org/bot{token}/sendMediaGroup"
|
||||
|
||||
try:
|
||||
_LOGGER.debug("Uploading media group chunk %d/%d (%d files) to Telegram",
|
||||
chunk_idx + 1, len(chunks), len(media_files))
|
||||
async with session.post(telegram_url, data=form) as response:
|
||||
result = await response.json()
|
||||
_LOGGER.debug("Telegram API response: status=%d, ok=%s", response.status, result.get("ok"))
|
||||
if response.status == 200 and result.get("ok"):
|
||||
chunk_message_ids = [
|
||||
msg.get("message_id") for msg in result.get("result", [])
|
||||
]
|
||||
all_message_ids.extend(chunk_message_ids)
|
||||
else:
|
||||
_LOGGER.error("Telegram API error for chunk %d: %s", chunk_idx + 1, result)
|
||||
return {
|
||||
"success": False,
|
||||
"error": result.get("description", "Unknown Telegram error"),
|
||||
"error_code": result.get("error_code"),
|
||||
"failed_at_chunk": chunk_idx + 1,
|
||||
}
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Telegram upload failed for chunk %d: %s", chunk_idx + 1, err)
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(err),
|
||||
"failed_at_chunk": chunk_idx + 1,
|
||||
}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message_ids": all_message_ids,
|
||||
"chunks_sent": len(chunks),
|
||||
}
|
||||
|
||||
|
||||
class ImmichAlbumIdSensor(ImmichAlbumBaseSensor):
|
||||
"""Sensor exposing the Immich album ID."""
|
||||
|
||||
@@ -25,9 +25,9 @@ get_recent_assets:
|
||||
max: 100
|
||||
mode: slider
|
||||
|
||||
send_telegram_media_group:
|
||||
name: Send Telegram Media Group
|
||||
description: Send specified media URLs to a Telegram chat as a media group.
|
||||
send_telegram_notification:
|
||||
name: Send Telegram Notification
|
||||
description: Send a notification to Telegram (text, photo, video, or media group).
|
||||
target:
|
||||
entity:
|
||||
integration: immich_album_watcher
|
||||
@@ -47,13 +47,13 @@ send_telegram_media_group:
|
||||
text:
|
||||
urls:
|
||||
name: URLs
|
||||
description: List of media URLs to send (max 10). Each item should have 'url' and 'type' (photo/video).
|
||||
required: true
|
||||
description: List of media URLs to send. Each item should have 'url' and 'type' (photo/video). If empty, sends a text message. Large lists are automatically split into multiple media groups.
|
||||
required: false
|
||||
selector:
|
||||
object:
|
||||
caption:
|
||||
name: Caption
|
||||
description: Optional caption for the media group (applied to first item).
|
||||
description: Caption text. For media, applied to first item. For empty URLs, this is the message text.
|
||||
required: false
|
||||
selector:
|
||||
text:
|
||||
@@ -65,3 +65,54 @@ send_telegram_media_group:
|
||||
selector:
|
||||
number:
|
||||
mode: box
|
||||
disable_web_page_preview:
|
||||
name: Disable Web Page Preview
|
||||
description: Disable link previews in text messages.
|
||||
required: false
|
||||
selector:
|
||||
boolean:
|
||||
parse_mode:
|
||||
name: Parse Mode
|
||||
description: How to parse the caption/text. Options are "HTML", "Markdown", "MarkdownV2", or empty string for plain text.
|
||||
required: false
|
||||
default: "HTML"
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- label: "HTML"
|
||||
value: "HTML"
|
||||
- label: "Markdown"
|
||||
value: "Markdown"
|
||||
- label: "MarkdownV2"
|
||||
value: "MarkdownV2"
|
||||
- label: "Plain Text"
|
||||
value: ""
|
||||
max_group_size:
|
||||
name: Max Group Size
|
||||
description: Maximum number of media items per media group (2-10). Large lists will be split into multiple groups.
|
||||
required: false
|
||||
default: 10
|
||||
selector:
|
||||
number:
|
||||
min: 2
|
||||
max: 10
|
||||
mode: slider
|
||||
chunk_delay:
|
||||
name: Chunk Delay
|
||||
description: Delay in milliseconds between sending multiple media groups (0-60000). Useful for rate limiting.
|
||||
required: false
|
||||
default: 0
|
||||
selector:
|
||||
number:
|
||||
min: 0
|
||||
max: 60000
|
||||
step: 100
|
||||
unit_of_measurement: "ms"
|
||||
mode: slider
|
||||
wait_for_response:
|
||||
name: Wait For Response
|
||||
description: Wait for Telegram to finish processing before returning. Set to false for fire-and-forget (automation continues immediately).
|
||||
required: false
|
||||
default: true
|
||||
selector:
|
||||
boolean:
|
||||
|
||||
@@ -79,13 +79,6 @@ class ImmichAlbumProtectedPasswordText(
|
||||
"""Get the album data from coordinator."""
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available.
|
||||
|
||||
@@ -1,58 +1,61 @@
|
||||
{
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"album_id": {
|
||||
"name": "Album ID"
|
||||
},
|
||||
"album_asset_count": {
|
||||
"name": "{album_name}: Asset Count"
|
||||
"name": "Asset Count"
|
||||
},
|
||||
"album_photo_count": {
|
||||
"name": "{album_name}: Photo Count"
|
||||
"name": "Photo Count"
|
||||
},
|
||||
"album_video_count": {
|
||||
"name": "{album_name}: Video Count"
|
||||
"name": "Video Count"
|
||||
},
|
||||
"album_last_updated": {
|
||||
"name": "{album_name}: Last Updated"
|
||||
"name": "Last Updated"
|
||||
},
|
||||
"album_created": {
|
||||
"name": "{album_name}: Created"
|
||||
"name": "Created"
|
||||
},
|
||||
"album_public_url": {
|
||||
"name": "{album_name}: Public URL"
|
||||
"name": "Public URL"
|
||||
},
|
||||
"album_protected_url": {
|
||||
"name": "{album_name}: Protected URL"
|
||||
"name": "Protected URL"
|
||||
},
|
||||
"album_protected_password": {
|
||||
"name": "{album_name}: Protected Password"
|
||||
"name": "Protected Password"
|
||||
}
|
||||
},
|
||||
"binary_sensor": {
|
||||
"album_new_assets": {
|
||||
"name": "{album_name}: New Assets"
|
||||
"name": "New Assets"
|
||||
}
|
||||
},
|
||||
"camera": {
|
||||
"album_thumbnail": {
|
||||
"name": "{album_name}: Thumbnail"
|
||||
"name": "Thumbnail"
|
||||
}
|
||||
},
|
||||
"text": {
|
||||
"album_protected_password_edit": {
|
||||
"name": "{album_name}: Share Password"
|
||||
"name": "Share Password"
|
||||
}
|
||||
},
|
||||
"button": {
|
||||
"create_share_link": {
|
||||
"name": "{album_name}: Create Share Link"
|
||||
"name": "Create Share Link"
|
||||
},
|
||||
"delete_share_link": {
|
||||
"name": "{album_name}: Delete Share Link"
|
||||
"name": "Delete Share Link"
|
||||
},
|
||||
"create_protected_link": {
|
||||
"name": "{album_name}: Create Protected Link"
|
||||
"name": "Create Protected Link"
|
||||
},
|
||||
"delete_protected_link": {
|
||||
"name": "{album_name}: Delete Protected Link"
|
||||
"name": "Delete Protected Link"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -115,10 +118,12 @@
|
||||
"title": "Immich Album Watcher Options",
|
||||
"description": "Configure the polling interval for all albums.",
|
||||
"data": {
|
||||
"scan_interval": "Scan interval (seconds)"
|
||||
"scan_interval": "Scan interval (seconds)",
|
||||
"telegram_bot_token": "Telegram Bot Token"
|
||||
},
|
||||
"data_description": {
|
||||
"scan_interval": "How often to check for album changes (10-3600 seconds)"
|
||||
"scan_interval": "How often to check for album changes (10-3600 seconds)",
|
||||
"telegram_bot_token": "Bot token for sending notifications to Telegram"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,6 +142,52 @@
|
||||
"description": "Number of recent assets to return (1-100)."
|
||||
}
|
||||
}
|
||||
},
|
||||
"send_telegram_notification": {
|
||||
"name": "Send Telegram Notification",
|
||||
"description": "Send a notification to Telegram (text, photo, video, or media group).",
|
||||
"fields": {
|
||||
"bot_token": {
|
||||
"name": "Bot Token",
|
||||
"description": "Telegram bot token (optional if configured in integration options)."
|
||||
},
|
||||
"chat_id": {
|
||||
"name": "Chat ID",
|
||||
"description": "Telegram chat ID to send to."
|
||||
},
|
||||
"urls": {
|
||||
"name": "URLs",
|
||||
"description": "List of media URLs with type (photo/video). If empty, sends a text message. Large lists are automatically split into multiple media groups."
|
||||
},
|
||||
"caption": {
|
||||
"name": "Caption",
|
||||
"description": "Caption text. For media, applied to first item. For empty URLs, this is the message text."
|
||||
},
|
||||
"reply_to_message_id": {
|
||||
"name": "Reply To",
|
||||
"description": "Optional message ID to reply to."
|
||||
},
|
||||
"disable_web_page_preview": {
|
||||
"name": "Disable Web Page Preview",
|
||||
"description": "Disable link previews in text messages."
|
||||
},
|
||||
"parse_mode": {
|
||||
"name": "Parse Mode",
|
||||
"description": "How to parse the caption/text. Options are HTML, Markdown, MarkdownV2, or empty string for plain text."
|
||||
},
|
||||
"max_group_size": {
|
||||
"name": "Max Group Size",
|
||||
"description": "Maximum number of media items per media group (2-10). Large lists will be split into multiple groups."
|
||||
},
|
||||
"chunk_delay": {
|
||||
"name": "Chunk Delay",
|
||||
"description": "Delay in milliseconds between sending multiple media groups (0-60000). Useful for rate limiting."
|
||||
},
|
||||
"wait_for_response": {
|
||||
"name": "Wait For Response",
|
||||
"description": "Wait for Telegram to finish processing before returning. Set to false for fire-and-forget (automation continues immediately)."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,61 @@
|
||||
{
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"album_id": {
|
||||
"name": "ID альбома"
|
||||
},
|
||||
"album_asset_count": {
|
||||
"name": "{album_name}: Число файлов"
|
||||
"name": "Число файлов"
|
||||
},
|
||||
"album_photo_count": {
|
||||
"name": "{album_name}: Число фото"
|
||||
"name": "Число фото"
|
||||
},
|
||||
"album_video_count": {
|
||||
"name": "{album_name}: Число видео"
|
||||
"name": "Число видео"
|
||||
},
|
||||
"album_last_updated": {
|
||||
"name": "{album_name}: Последнее обновление"
|
||||
"name": "Последнее обновление"
|
||||
},
|
||||
"album_created": {
|
||||
"name": "{album_name}: Дата создания"
|
||||
"name": "Дата создания"
|
||||
},
|
||||
"album_public_url": {
|
||||
"name": "{album_name}: Публичная ссылка"
|
||||
"name": "Публичная ссылка"
|
||||
},
|
||||
"album_protected_url": {
|
||||
"name": "{album_name}: Защищённая ссылка"
|
||||
"name": "Защищённая ссылка"
|
||||
},
|
||||
"album_protected_password": {
|
||||
"name": "{album_name}: Пароль ссылки"
|
||||
"name": "Пароль ссылки"
|
||||
}
|
||||
},
|
||||
"binary_sensor": {
|
||||
"album_new_assets": {
|
||||
"name": "{album_name}: Новые файлы"
|
||||
"name": "Новые файлы"
|
||||
}
|
||||
},
|
||||
"camera": {
|
||||
"album_thumbnail": {
|
||||
"name": "{album_name}: Превью"
|
||||
"name": "Превью"
|
||||
}
|
||||
},
|
||||
"text": {
|
||||
"album_protected_password_edit": {
|
||||
"name": "{album_name}: Пароль ссылки"
|
||||
"name": "Пароль ссылки"
|
||||
}
|
||||
},
|
||||
"button": {
|
||||
"create_share_link": {
|
||||
"name": "{album_name}: Создать ссылку"
|
||||
"name": "Создать ссылку"
|
||||
},
|
||||
"delete_share_link": {
|
||||
"name": "{album_name}: Удалить ссылку"
|
||||
"name": "Удалить ссылку"
|
||||
},
|
||||
"create_protected_link": {
|
||||
"name": "{album_name}: Создать защищённую ссылку"
|
||||
"name": "Создать защищённую ссылку"
|
||||
},
|
||||
"delete_protected_link": {
|
||||
"name": "{album_name}: Удалить защищённую ссылку"
|
||||
"name": "Удалить защищённую ссылку"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -115,10 +118,12 @@
|
||||
"title": "Настройки Immich Album Watcher",
|
||||
"description": "Настройте интервал опроса для всех альбомов.",
|
||||
"data": {
|
||||
"scan_interval": "Интервал сканирования (секунды)"
|
||||
"scan_interval": "Интервал сканирования (секунды)",
|
||||
"telegram_bot_token": "Токен Telegram бота"
|
||||
},
|
||||
"data_description": {
|
||||
"scan_interval": "Как часто проверять изменения в альбомах (10-3600 секунд)"
|
||||
"scan_interval": "Как часто проверять изменения в альбомах (10-3600 секунд)",
|
||||
"telegram_bot_token": "Токен бота для отправки уведомлений в Telegram"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,6 +142,52 @@
|
||||
"description": "Количество возвращаемых файлов (1-100)."
|
||||
}
|
||||
}
|
||||
},
|
||||
"send_telegram_notification": {
|
||||
"name": "Отправить уведомление в Telegram",
|
||||
"description": "Отправить уведомление в Telegram (текст, фото, видео или медиа-группу).",
|
||||
"fields": {
|
||||
"bot_token": {
|
||||
"name": "Токен бота",
|
||||
"description": "Токен Telegram бота (необязательно, если настроен в опциях интеграции)."
|
||||
},
|
||||
"chat_id": {
|
||||
"name": "ID чата",
|
||||
"description": "ID чата Telegram для отправки."
|
||||
},
|
||||
"urls": {
|
||||
"name": "URL-адреса",
|
||||
"description": "Список URL медиа-файлов с типом (photo/video). Если пусто, отправляет текстовое сообщение. Большие списки автоматически разделяются на несколько медиа-групп."
|
||||
},
|
||||
"caption": {
|
||||
"name": "Подпись",
|
||||
"description": "Текст подписи. Для медиа применяется к первому элементу. Для пустых URLs это текст сообщения."
|
||||
},
|
||||
"reply_to_message_id": {
|
||||
"name": "Ответ на",
|
||||
"description": "ID сообщения для ответа (необязательно)."
|
||||
},
|
||||
"disable_web_page_preview": {
|
||||
"name": "Отключить предпросмотр ссылок",
|
||||
"description": "Отключить предпросмотр ссылок в текстовых сообщениях."
|
||||
},
|
||||
"parse_mode": {
|
||||
"name": "Режим парсинга",
|
||||
"description": "Как парсить подпись/текст. Варианты: HTML, Markdown, MarkdownV2, или пустая строка для обычного текста."
|
||||
},
|
||||
"max_group_size": {
|
||||
"name": "Макс. размер группы",
|
||||
"description": "Максимальное количество медиа-файлов в одной группе (2-10). Большие списки будут разделены на несколько групп."
|
||||
},
|
||||
"chunk_delay": {
|
||||
"name": "Задержка между группами",
|
||||
"description": "Задержка в миллисекундах между отправкой нескольких медиа-групп (0-60000). Полезно для ограничения скорости."
|
||||
},
|
||||
"wait_for_response": {
|
||||
"name": "Ждать ответа",
|
||||
"description": "Ждать завершения отправки в Telegram перед возвратом. Установите false для фоновой отправки (автоматизация продолжается немедленно)."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user