-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ea7366
commit efb2aec
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 ntfy component.""" |
This file contains 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,15 @@ | ||
{ | ||
"domain": "ntfy", | ||
"name": "ntfy", | ||
"version": "0.2.0", | ||
"config_flow": false, | ||
"documentation": "https://github.com/hbrennhaeuser/homeassistant_integration_ntfy", | ||
"requirements": ["requests==2.25.1"], | ||
"ssdp": [], | ||
"zeroconf": [], | ||
"homekit": {}, | ||
"dependencies": [], | ||
"codeowners": [], | ||
"iot_class": "cloud_push", | ||
"loggers":[] | ||
} |
This file contains 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,107 @@ | ||
"""Ntfy notification service.""" | ||
|
||
from tokenize import String | ||
from homeassistant.components.notify import ( | ||
ATTR_MESSAGE, | ||
ATTR_TITLE, | ||
ATTR_TITLE_DEFAULT, | ||
ATTR_DATA, | ||
PLATFORM_SCHEMA, | ||
BaseNotificationService, | ||
) | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
|
||
from requests.auth import HTTPBasicAuth | ||
import requests | ||
import logging | ||
import voluptuous as vol | ||
|
||
CONF_TOPIC="topic" | ||
|
||
from homeassistant.const import ( | ||
CONF_PASSWORD, | ||
CONF_USERNAME, | ||
CONF_VERIFY_SSL, | ||
CONF_URL, | ||
CONF_AUTHENTICATION, | ||
) | ||
|
||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def get_service(hass,config,discovery_info=None): | ||
return NtfyNotificationService(config) | ||
|
||
|
||
|
||
class NtfyNotificationService(BaseNotificationService): | ||
def __init__ (self, config): | ||
"""Initialize the Ntfy notification service.""" | ||
self.user = config.get(CONF_USERNAME) | ||
_LOGGER.debug("Got User: ",self.user) | ||
self.password = config.get(CONF_PASSWORD) | ||
_LOGGER.debug("Got Password: XXXXXXXXXX") | ||
|
||
self.topic = config.get(CONF_TOPIC) | ||
_LOGGER.debug("Got User: ",self.user) | ||
|
||
self.url = config.get(CONF_URL) | ||
_LOGGER.debug("Got URL: ",self.url) | ||
|
||
self.auth = config.get(CONF_AUTHENTICATION) | ||
_LOGGER.debug("Got Authentication: ",self.auth) | ||
|
||
self.verifyssl = config.get(CONF_VERIFY_SSL) | ||
_LOGGER.debug("Got Verify_SSL: ",self.verifyssl) | ||
|
||
|
||
|
||
def send_message(self, message="", **kwargs): | ||
|
||
title=kwargs.get(ATTR_TITLE,ATTR_TITLE_DEFAULT) | ||
data=kwargs.get(ATTR_DATA,[]) | ||
|
||
|
||
req_data=message | ||
req_headers={ | ||
"Title": title | ||
} | ||
|
||
if "tags" in data: | ||
req_headers["Tags"] = data["tags"] | ||
|
||
|
||
|
||
|
||
if "priority" in data: | ||
schema_click = ['max','urgent','high','default','low','min'] | ||
|
||
if str(data["priority"]) not in schema_click: | ||
raise SyntaxError('Incorrect value for attribute priority given') | ||
|
||
req_headers["Priority"] = data["priority"] | ||
|
||
|
||
|
||
|
||
if "click" in data: | ||
schema_url = vol.Schema(vol.Url()) | ||
|
||
try: | ||
schema_url(data["click"]) | ||
except vol.MultipleInvalid as e: | ||
raise SyntaxError('expected a URL for attribute click') | ||
|
||
req_headers["Click"] = data["click"] | ||
|
||
|
||
req_verify=True | ||
if self.verifyssl == False: | ||
req_verify=False | ||
|
||
if self.auth == True : | ||
requests.post(str(self.url)+'/'+str(self.topic),data=req_data,headers=req_headers,verify=req_verify,auth=HTTPBasicAuth(self.user,self.password)) | ||
else: | ||
requests.post(str(self.url)+'/'+str(self.topic),data=req_data,headers=req_headers,verify=req_verify) |
This file contains 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,51 @@ | ||
# Notify via ntfy.sh | ||
|
||
This custom component allows you to send notifications through [ntfy.sh](https://ntfy.sh/). | ||
This component supports authentication and some additional ntfy-features like tags. | ||
|
||
## Installation | ||
|
||
Copy custom_components/ntfy to config/custom_components/ntfy. | ||
|
||
## Configuration | ||
|
||
Define a new ntfy notification-service in configuration.yaml: | ||
|
||
```yaml | ||
notify: | ||
- name: ntfy_test | ||
platform: ntfy | ||
username: 'user' | ||
password: 'password' | ||
topic: 'test' | ||
url: 'https://ntfy.domain.tld' | ||
authentication: True | ||
verify_ssl: True | ||
``` | ||
Set `authentication` to False to connect to the server anonymously. | ||
|
||
## Usage | ||
|
||
Call the notification service anywhere in homeassistant: | ||
|
||
```yaml | ||
service: notify.ntfy_test | ||
data: | ||
title: Homeassistant Notification | ||
message: Terrace door is open | ||
``` | ||
|
||
Optionally define additional data: | ||
|
||
```yaml | ||
service: notify.ntfy_test | ||
data: | ||
title: Homeassistant Notification | ||
message: Terrace door is open | ||
data: | ||
tags: door | ||
priority: high | ||
``` | ||
|
||
Currently tags, priority and click are supported. |