mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
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:
parent
28736e2ce4
commit
2e26b6e0cc
@ -6,6 +6,7 @@ DOMAIN: Final = "simplepush"
|
|||||||
DEFAULT_NAME: Final = "simplepush"
|
DEFAULT_NAME: Final = "simplepush"
|
||||||
DATA_HASS_CONFIG: Final = "simplepush_hass_config"
|
DATA_HASS_CONFIG: Final = "simplepush_hass_config"
|
||||||
|
|
||||||
|
ATTR_ATTACHMENTS: Final = "attachments"
|
||||||
ATTR_ENCRYPTED: Final = "encrypted"
|
ATTR_ENCRYPTED: Final = "encrypted"
|
||||||
ATTR_EVENT: Final = "event"
|
ATTR_EVENT: Final = "event"
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
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
|
# Configuring Simplepush under the notify has been removed in 2022.9.0
|
||||||
PLATFORM_SCHEMA = BASE_PLATFORM_SCHEMA
|
PLATFORM_SCHEMA = BASE_PLATFORM_SCHEMA
|
||||||
@ -61,11 +61,34 @@ class SimplePushNotificationService(BaseNotificationService):
|
|||||||
"""Send a message to a Simplepush user."""
|
"""Send a message to a Simplepush user."""
|
||||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||||
|
|
||||||
|
attachments = None
|
||||||
# event can now be passed in the service data
|
# event can now be passed in the service data
|
||||||
event = None
|
event = None
|
||||||
if data := kwargs.get(ATTR_DATA):
|
if data := kwargs.get(ATTR_DATA):
|
||||||
event = data.get(ATTR_EVENT)
|
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
|
# use event from config until YAML config is removed
|
||||||
event = event or self._event
|
event = event or self._event
|
||||||
|
|
||||||
@ -77,10 +100,17 @@ class SimplePushNotificationService(BaseNotificationService):
|
|||||||
salt=self._salt,
|
salt=self._salt,
|
||||||
title=title,
|
title=title,
|
||||||
message=message,
|
message=message,
|
||||||
|
attachments=attachments,
|
||||||
event=event,
|
event=event,
|
||||||
)
|
)
|
||||||
else:
|
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:
|
except BadRequest:
|
||||||
_LOGGER.error("Bad request. Title or message are too long")
|
_LOGGER.error("Bad request. Title or message are too long")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user