Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mihov committed Jun 29, 2023
0 parents commit f5bf43f
Show file tree
Hide file tree
Showing 9 changed files with 899 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/hacs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: HACS Action

on:
push:
pull_request:
schedule:
- cron: "0 0 * * *"

jobs:
hacs:
name: HACS Action
runs-on: "ubuntu-latest"
steps:
- name: HACS Action
uses: "hacs/action@main"
with:
category: integration
ignore: brands
14 changes: 14 additions & 0 deletions .github/workflows/hassfest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Validate with hassfest

on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
validate:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v3"
- uses: "home-assistant/actions/hassfest@master"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# ntfy.sh HomeAssistant Notifications

The `ntfy.sh` notification platform enables you to easily send notifications with a custom prioirty and extra information via [ntfy.sh](https://ntfy.sh).

### Installation
The recommended way to install the integration is via [HACS](https://hacs.xyz/). Add this repository to HACS custom integration repositories and install.
If you want to install it manually download the repository as zip and extract it to the `<config_dir>/custom_components/` directory of HomeAssistant.

### Configuration
This integration exposes itself as a [notifications integration](https://www.home-assistant.io/integrations/notify/) and configured by adding the folowing snippet to the `configuration.yaml` file:
```yaml
notify:
- name: "my ntfy"
platform: ntfy
url: <ntfy.sh_url>
token: <ntfy.sh_token>
topic: <ntfy.sh_topic>
icon: <notification_icon_url>
```
Replace `<ntfy.sh_url>`, `<ntfy.sh_token>`, `<ntfy.sh_topic>`, and `<notification_icon_url>` with the url, optional token, optional topic, and optional icon url for your ntfy.sh instance. Token is optional if your instance does not require authentication. Topic is also optional and will default to `homeassistant` if not specified. Topics in the automations have higher priority.

### Usage
This integration accepts the same values as the official ntfy.sh API. For a full list of options that can be added to a notification refer to the [ntfy.sh docs](https://docs.ntfy.sh/publish/#publish-as-json). Here a few examples:

#### Simple text message
```yaml
action:
- service: notify.my_ntfy
data:
message: "This is a test message."
```

#### Message with title and priority
```yaml
action:
- service: notify.my_ntfy
data:
message: "This is a test message."
title: "ntfy Test"
data:
priority: 10
```

#### Message with click event
```yaml
action:
- service: notify.my_ntfy
data:
message: "This is a test message."
title: "ntfy Test"
data:
priority: 10
click: https://www.home-assistant.io/
```

##### Message with image
```yaml
action:
- service: notify.my_gotify
data:
message: "This is a test message."
title: "Gotify Test"
data:
priority: 10
image: https://placekitten.com/400/300
```

##### All available options
```yaml
action:
- service: notify.my_gotify
data:
message: "This is a test message."
title: "Gotify Test"
topic: "override_default_topic"
data:
tags: [tags]
priority: 3
attach: https://placekitten.com/400/300
filename: filename.jpg
click: https://www.home-assistant.io/
actions: [list_of_actions]
icon: https://placekitten.com/400/300
```

### License
The whole project is under the [GPL-3 license](https://www.gnu.org/licenses/gpl-3.0.html).
Empty file.
12 changes: 12 additions & 0 deletions custom_components/ntfy/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"domain": "ntfy",
"name": "ntfy.sh Notifications",
"documentation": "https://github.com/ivanmihov/homeassistant-ntfy.sh",
"issue_tracker": "https://github.com/ivanmihov/homeassistant-ntfy.sh/issues",
"dependencies": [],
"codeowners": ["@ivanmihov"],
"requirements": [],
"integration_type": "service",
"iot_class": "local_push",
"version": "2023.6.1"
}
89 changes: 89 additions & 0 deletions custom_components/ntfy/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import logging
import requests
from typing import Any
import voluptuous as vol
from homeassistant.const import (
CONF_URL,
CONF_TOKEN,
CONF_ICON
)

CONF_TOPIC = 'topic'

import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (
ATTR_TITLE_DEFAULT,
ATTR_TITLE,
ATTR_DATA,
PLATFORM_SCHEMA,
BaseNotificationService,
)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_URL): cv.url,
vol.Optional(CONF_TOKEN): cv.string,
vol.Optional(CONF_TOPIC): cv.string,
vol.Optional(CONF_ICON): cv.url
})
_LOGGER = logging.getLogger(__name__)

def get_service(hass, config, discovery_info=None):
url = config.get(CONF_URL) or 'https://ntfy.sh'
token = config.get(CONF_TOKEN) or ''
topic = config.get(CONF_TOPIC)
icon = config.get(CONF_ICON)

_LOGGER.info('Service created')

return HassAgentNotificationService(hass, url, token, topic, icon)


class HassAgentNotificationService(BaseNotificationService):
def __init__(self, hass, url, token, topic, icon):
self._url = url
self._token = token
self._topic = topic
self._icon = icon
self._hass = hass

# if not self._url.endswith('/'):
# self._url += '/'
# self._url += topic

def send_request(self, url, token, data):
headers = {}
if token != '':
headers = {'Authorization': 'Bearer ' + token}
return requests.post(url, headers=headers, json=data, timeout=10)

async def async_send_message(self, message: str, **kwargs: Any):
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
data = kwargs.get(ATTR_DATA, None)
if data is None:
data = dict()

# Prefer topic in automation
topic = data.get('topic') or self._topic or "homeassistant"
# Prefer icon in automation
icon = data.get('icon') or self._icon or ""

payload = {
'topic': topic,
'message': message,
'title': title,
'tags': data.get('tags', []),
'priority': data.get('priority', 3),
'attach': data.get('attach', "") or data.get('image', ""),
'filename': data.get('filename', ""),
'click': data.get('click', "") or data.get('click_url', ""),
'actions': data.get('actions', []),
'icon': icon,
}

_LOGGER.debug('Sending message to ntfy.sh: %s', payload)

try:
response = await self.hass.async_add_executor_job(self.send_request, self._url, self._token, payload)
response.raise_for_status()
except Exception as ex:
_LOGGER.error('Error while sending ntfy.sh message: %s', ex)
4 changes: 4 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "ntfy.sh Notifications",
"render_readme": true
}

0 comments on commit f5bf43f

Please sign in to comment.