-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Add Apprise notification integration #26868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+242
−0
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cc8b4f0
Added apprise notification component
caronc 4cd785a
flake-8 fixes; black formatting + import merged to 1 line
caronc c94e2f6
pylint issues resolved
caronc 71cab55
added github name to manifest.json
caronc ba3ba13
import moved to top as per code review request
caronc dd20556
manifest formatting to avoid failing ci
caronc e064234
.coveragerc updated to include apprise
caronc ef4f7e1
removed block for written tests
caronc 693667a
more test coverage
caronc ffa6e45
formatting as per code review
caronc 5e25965
tests converted to async style as per code review
caronc 5d54fbe
increased coverage
caronc 8e199bd
bumped version of apprise to 0.8.1
caronc bbf85b1
test that mocked entries are called
caronc 2fd8264
added tests for hass.service loading
caronc 6b06d3f
support tags for those who identify the TARGET option
caronc 3d1cd2a
renamed variable as per code review
caronc 16b5ace
'assert not' used instead of 'is False'
caronc 8a584e2
added period (in case linter isn't happy)
caronc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """The apprise component.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "domain": "apprise", | ||
| "name": "Apprise", | ||
| "documentation": "https://www.home-assistant.io/components/apprise", | ||
| "requirements": [ | ||
| "apprise==0.8.1" | ||
| ], | ||
| "dependencies": [], | ||
| "codeowners": [ | ||
| "@caronc" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| """Apprise platform for notify component.""" | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import apprise | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| from homeassistant.components.notify import ( | ||
| ATTR_TITLE, | ||
| ATTR_TITLE_DEFAULT, | ||
| PLATFORM_SCHEMA, | ||
| BaseNotificationService, | ||
| ) | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_FILE = "config" | ||
| CONF_URL = "url" | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( | ||
| { | ||
| vol.Optional(CONF_URL): vol.All(cv.ensure_list, [str]), | ||
| vol.Optional(CONF_FILE): cv.string, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def get_service(hass, config, discovery_info=None): | ||
| """Get the Apprise notification service.""" | ||
|
|
||
| # Create our object | ||
| a_obj = apprise.Apprise() | ||
|
|
||
| if config.get(CONF_FILE): | ||
| # Sourced from a Configuration File | ||
| a_config = apprise.AppriseConfig() | ||
| if not a_config.add(config[CONF_FILE]): | ||
| _LOGGER.error("Invalid Apprise config url provided") | ||
| return None | ||
|
|
||
| if not a_obj.add(a_config): | ||
| _LOGGER.error("Invalid Apprise config url provided") | ||
| return None | ||
|
|
||
| if config.get(CONF_URL): | ||
| # Ordered list of URLs | ||
| if not a_obj.add(config[CONF_URL]): | ||
| _LOGGER.error("Invalid Apprise URL(s) supplied") | ||
| return None | ||
|
|
||
| return AppriseNotificationService(a_obj) | ||
|
|
||
|
|
||
| class AppriseNotificationService(BaseNotificationService): | ||
| """Implement the notification service for Apprise.""" | ||
|
|
||
| def __init__(self, a_obj): | ||
| """Initialize the service.""" | ||
| self.apprise = a_obj | ||
|
|
||
| def send_message(self, message="", **kwargs): | ||
| """Send a message to a specified target.""" | ||
| title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) | ||
| self.apprise.notify(body=message, title=title) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the apprise component.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """The tests for the apprise notification platform.""" | ||
| from unittest.mock import patch | ||
|
|
||
| from homeassistant.setup import async_setup_component | ||
|
|
||
| BASE_COMPONENT = "notify" | ||
|
|
||
|
|
||
| async def test_apprise_config_load_fail01(hass): | ||
| """Test apprise configuration failures 1.""" | ||
|
|
||
| config = { | ||
| BASE_COMPONENT: {"name": "test", "platform": "apprise", "config": "/path/"} | ||
| } | ||
|
|
||
| with patch("apprise.AppriseConfig.add", return_value=False): | ||
| assert await async_setup_component(hass, BASE_COMPONENT, config) | ||
| await hass.async_block_till_done() | ||
|
|
||
|
MartinHjelmare marked this conversation as resolved.
|
||
|
|
||
| async def test_apprise_config_load_fail02(hass): | ||
| """Test apprise configuration failures 2.""" | ||
|
|
||
| config = { | ||
| BASE_COMPONENT: {"name": "test", "platform": "apprise", "config": "/path/"} | ||
| } | ||
|
|
||
| with patch("apprise.Apprise.add", return_value=False): | ||
| with patch("apprise.AppriseConfig.add", return_value=True): | ||
| assert await async_setup_component(hass, BASE_COMPONENT, config) | ||
| await hass.async_block_till_done() | ||
|
|
||
|
MartinHjelmare marked this conversation as resolved.
|
||
|
|
||
| async def test_apprise_config_load_okay(hass, tmp_path): | ||
| """Test apprise configuration failures.""" | ||
|
|
||
| # Test cases where our URL is invalid | ||
| d = tmp_path / "apprise-config" | ||
| d.mkdir() | ||
| f = d / "apprise" | ||
| f.write_text("mailto://user:pass@example.com/") | ||
|
|
||
| config = {BASE_COMPONENT: {"name": "test", "platform": "apprise", "config": str(f)}} | ||
|
|
||
| assert await async_setup_component(hass, BASE_COMPONENT, config) | ||
| await hass.async_block_till_done() | ||
|
|
||
|
|
||
| async def test_apprise_url_load_fail(hass): | ||
| """Test apprise url failure.""" | ||
|
|
||
| config = { | ||
| BASE_COMPONENT: { | ||
| "name": "test", | ||
| "platform": "apprise", | ||
| "url": "mailto://user:pass@example.com", | ||
| } | ||
| } | ||
| with patch("apprise.Apprise.add", return_value=False): | ||
|
MartinHjelmare marked this conversation as resolved.
|
||
| assert await async_setup_component(hass, BASE_COMPONENT, config) | ||
| await hass.async_block_till_done() | ||
|
|
||
|
|
||
| async def test_apprise_notification(hass): | ||
| """Test apprise notification.""" | ||
|
|
||
| config = { | ||
| BASE_COMPONENT: { | ||
| "name": "test", | ||
| "platform": "apprise", | ||
| "url": "mailto://user:pass@example.com", | ||
| } | ||
| } | ||
|
|
||
| # Our Message | ||
| data = {"title": "Test Title", "message": "Test Message"} | ||
|
|
||
| with patch("apprise.Apprise") as mock_apprise: | ||
| mock_apprise.notify.return_value = True | ||
| assert await async_setup_component(hass, BASE_COMPONENT, config) | ||
| await hass.async_block_till_done() | ||
|
|
||
| # Test the call to our underlining notify() call | ||
| await hass.services.async_call(BASE_COMPONENT, "test", data) | ||
| await hass.async_block_till_done() | ||
|
MartinHjelmare marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.