Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/simplepush/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
DEFAULT_NAME: Final = "simplepush"
DATA_HASS_CONFIG: Final = "simplepush_hass_config"

ATTR_ATTACHMENTS: Final = "attachments"
ATTR_ENCRYPTED: Final = "encrypted"
ATTR_EVENT: Final = "event"

Expand Down
25 changes: 23 additions & 2 deletions homeassistant/components/simplepush/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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
Expand Down Expand Up @@ -61,11 +61,25 @@ def send_message(self, message: str, **kwargs: Any) -> None:
"""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):
for attachment in attachments_data:
if not (
isinstance(attachment, dict)
and "video" in attachment
and "thumbnail" in attachment
) and not isinstance(attachment, str):
_LOGGER.error("Attachment format is incorrect")
return

attachments = attachments_data

Comment on lines +70 to +91

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better approach would be to raise an error if the attachments key values are not as per the documentation. If no errors are raised then simply use attachments=attachments_data.
for lines 85 and 86 I believe a video key should only be provided with a thumbnail. The documentation should mentions this clearly so no need to check for a video only key in the attachments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, makes a lot of sense.

Please see my latest commit for the implementation of your comment.

# use event from config until YAML config is removed
event = event or self._event

Expand All @@ -77,10 +91,17 @@ def send_message(self, message: str, **kwargs: Any) -> None:
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")
Expand Down