Skip to content

Add attachments to simplepush#81033

Merged
emontnemery merged 6 commits into
home-assistant:devfrom
tymm:add-simplepush-attachments
Mar 31, 2023
Merged

Add attachments to simplepush#81033
emontnemery merged 6 commits into
home-assistant:devfrom
tymm:add-simplepush-attachments

Conversation

@tymm
Copy link
Copy Markdown
Contributor

@tymm tymm commented Oct 26, 2022

Breaking change

Proposed change

This PR adds attachments for the Simplepush component.
Attachments can be pictures, GIFs or video files defined by an URL.

I will update the documentation (https://www.home-assistant.io/integrations/simplepush/), after this PR gets merged. If preferred I can also do it beforehand.

The new functionality can be tested with the following automation:

alias: Test
description: >-
  Send a notification with two attachments
trigger: []
condition: []
action:
  - service: notify.simplepush
    data:
      message: test
      data:
        attachments:
          - image: https://upload.wikimedia.org/wikipedia/commons/e/ee/Sample_abc.jpg
          - video: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4
            thumbnail: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerEscapes.jpg
mode: restart

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @engrbm87, mind taking a look at this pull request as it has been labeled with an integration (simplepush) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of simplepush can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant unassign simplepush Removes the current integration label and assignees on the issue, add the integration domain after the command.

@tymm tymm mentioned this pull request Oct 26, 2022
19 tasks
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Dec 20, 2022
Comment on lines +70 to +82
attachments_data = data.get(ATTR_ATTACHMENTS)
if isinstance(attachments_data, list) and attachments_data:
attachments = []
for attachment in attachments_data:
if "attachment" in attachment and "thumbnail" in attachment:
attachments.append(
{
"video": attachment["attachment"],
"thumbnail": attachment["thumbnail"],
}
)
elif "attachment" in attachment:
attachments.append(attachment["attachment"])
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.

Can't we have the user mention "video" and "thumbnail" instead of "attachment" and "thumbnail" when adding a video under "attachments"? This way you can directly use the "attachments" key.
If I am not mistaken the "attachments" key entered by the user would be like this:

attachments:
    - https://upload.wikimedia.org/wikipedia/commons/e/ee/Sample_abc.jpg
    - video: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4
      thumbnail: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerEscapes.jpg

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.

@engrbm87, thanks a lot for your review!

Your suggestion makes a lot of sense to me.

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.

@engrbm87 I implemented your suggestion in commit 9bc18c3.

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.

Added a comment based on the last commit

Comment on lines +70 to +82
attachments_data = data.get(ATTR_ATTACHMENTS)
if isinstance(attachments_data, list) and attachments_data:
attachments = []
for attachment in attachments_data:
if (
isinstance(attachment, dict)
and "video" in attachment.keys()
and "thumbnail" in attachment.keys()
):
attachments.append(
{
"video": attachment["video"],
"thumbnail": attachment["thumbnail"],
}
)
elif isinstance(attachment, dict) and "video" in attachment.keys():
attachments.append(attachment["video"])
else:
attachments.append(attachment)

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.

@tymm tymm force-pushed the add-simplepush-attachments branch from d753f4e to 4387c8d Compare March 15, 2023 21:45
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Instead of supporting attachments which are either strings or dicts:

        attachments:
          - https://upload.wikimedia.org/wikipedia/commons/e/ee/Sample_abc.jpg
          - video: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4
            thumbnail: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerEscapes.jpg

Just make them dicts always:

        attachments:
          - image: https://upload.wikimedia.org/wikipedia/commons/e/ee/Sample_abc.jpg
          - video: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4
            thumbnail: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerEscapes.jpg

This will simplify the Python implementation and IMHO is also clearer for the user.

@home-assistant home-assistant Bot marked this pull request as draft March 30, 2023 09:35
@home-assistant
Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@tymm tymm marked this pull request as ready for review March 30, 2023 14:49
@home-assistant home-assistant Bot requested a review from emontnemery March 30, 2023 14:49
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

LGTM, can be merged when a documentation PR has been opened

@tymm
Copy link
Copy Markdown
Contributor Author

tymm commented Mar 31, 2023

LGTM, can be merged when a documentation PR has been opened

Thank you for your review!

Here is a link to the documentation PR: home-assistant/home-assistant.io#26813

@emontnemery emontnemery merged commit 2e26b6e into home-assistant:dev Mar 31, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants