Add attachments to simplepush (#81033)

* Add attachments

* Fix looking for attachment keywords in values

* Improve attachment input format

* Implement better approach to attachment parsing

* Make ruff happy

* Adjust attachment format and implementation according to comment from emontnemery
This commit is contained in:
Timm Schäuble 2023-03-31 14:10:12 +02:00 committed by GitHub
parent 28736e2ce4
commit 2e26b6e0cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View file

@ -6,6 +6,7 @@ DOMAIN: Final = "simplepush"
DEFAULT_NAME: Final = "simplepush"
DATA_HASS_CONFIG: Final = "simplepush_hass_config"
ATTR_ATTACHMENTS: Final = "attachments"
ATTR_ENCRYPTED: Final = "encrypted"
ATTR_EVENT: Final = "event"

View file

@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import ATTR_EVENT, CONF_DEVICE_KEY, CONF_SALT, DOMAIN
from .const import ATTR_ATTACHMENTS, ATTR_EVENT, CONF_DEVICE_KEY, CONF_SALT, DOMAIN
# Configuring Simplepush under the notify has been removed in 2022.9.0
PLATFORM_SCHEMA = BASE_PLATFORM_SCHEMA
@ -61,11 +61,34 @@ class SimplePushNotificationService(BaseNotificationService):
"""Send a message to a Simplepush user."""
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
attachments = None
# event can now be passed in the service data
event = None
if data := kwargs.get(ATTR_DATA):
event = data.get(ATTR_EVENT)
attachments_data = data.get(ATTR_ATTACHMENTS)
if isinstance(attachments_data, list):
attachments = []
for attachment in attachments_data:
if not (
isinstance(attachment, dict)
and (
"image" in attachment
or "video" in attachment
or ("video" in attachment and "thumbnail" in attachment)
)
):
_LOGGER.error("Attachment format is incorrect")
return
if "video" in attachment and "thumbnail" in attachment:
attachments.append(attachment)
elif "video" in attachment:
attachments.append(attachment["video"])
elif "image" in attachment:
attachments.append(attachment["image"])
# use event from config until YAML config is removed
event = event or self._event
@ -77,10 +100,17 @@ class SimplePushNotificationService(BaseNotificationService):
salt=self._salt,
title=title,
message=message,
attachments=attachments,
event=event,
)
else:
send(key=self._device_key, title=title, message=message, event=event)
send(
key=self._device_key,
title=title,
message=message,
attachments=attachments,
event=event,
)
except BadRequest:
_LOGGER.error("Bad request. Title or message are too long")