Skip to content

Commit 5987547

Browse files
committed
ntfy Binding
Signed-off-by: Christian Kittel <ckittel@gmx.de>
1 parent fa7b662 commit 5987547

36 files changed

Lines changed: 3263 additions & 18 deletions

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
/bundles/org.openhab.binding.nikohomecontrol/ @mherwege
285285
/bundles/org.openhab.binding.nobohub/ @espenaf
286286
/bundles/org.openhab.binding.novafinedust/ @t2000
287+
/bundles/org.openhab.binding.ntfy/ @EvilPingu
287288
/bundles/org.openhab.binding.ntp/ @marcelrv
288289
/bundles/org.openhab.binding.nuki/ @janvyb
289290
/bundles/org.openhab.binding.nuvo/ @mlobstein

bom/openhab-addons/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,11 @@
14011401
<artifactId>org.openhab.binding.novafinedust</artifactId>
14021402
<version>${project.version}</version>
14031403
</dependency>
1404+
<dependency>
1405+
<groupId>org.openhab.addons.bundles</groupId>
1406+
<artifactId>org.openhab.binding.ntfy</artifactId>
1407+
<version>${project.version}</version>
1408+
</dependency>
14041409
<dependency>
14051410
<groupId>org.openhab.addons.bundles</groupId>
14061411
<artifactId>org.openhab.binding.ntp</artifactId>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This content is produced and maintained by the openHAB project.
2+
3+
* Project home: https://www.openhab.org
4+
5+
== Declared Project Licenses
6+
7+
This program and the accompanying materials are made available under the terms
8+
of the Eclipse Public License 2.0 which is available at
9+
https://www.eclipse.org/legal/epl-2.0/.
10+
11+
== Source Code
12+
13+
https://github.com/openhab/openhab-addons
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Ntfy Binding
2+
3+
The Ntfy binding enables openHAB to publish notifications to Ntfy-compatible servers (for example [ntfy.sh](https://ntfy.sh) or a self-hosted ntfy-compatible endpoint).
4+
5+
Ntfy is a simple HTTP-based notification service and message broker; see [ntfy.sh](https://ntfy.sh) for details and public servers.
6+
7+
It is intended for integrations where openHAB should push alert or informational messages to mobile or desktop clients that support the Ntfy protocol. The binding supports basic text messages as well as common rich features supported by the protocol: message priority, tags, icon URLs, attachments, click actions and simple action buttons.
8+
9+
Typical uses include doorbell alerts, security notifications, system health messages, or any automation that should notify users in real time via an Ntfy-compatible notification channel.
10+
11+
## Supported Things
12+
13+
This binding provides the following Thing types. The Thing type IDs match the definitions in `src/main/resources/OH-INF/thing/thing-types.xml`.
14+
15+
- `ntfy:ntfyConnection` (bridge) — Represents a connection to an Ntfy server. Configure the bridge with the server hostname (for example `https://ntfy.sh`) and optional credentials (username/password). The bridge holds shared connection settings (hostname, username, password, connectionTimeout) which are used by topic Things.
16+
- `ntfy:ntfyTopic` (Thing) — Represents a topic/channel on a configured Ntfy server. Each topic Thing must be associated with an `ntfyConnection` bridge and requires the `topicname` configuration parameter.
17+
18+
## Thing Configuration
19+
20+
### `ntfyConnection` Bridge Configuration
21+
22+
| Name | Type | Description | Default | Required | Advanced |
23+
|-------------------|---------|-----------------------------------------|-----------------|----------|----------|
24+
| hostname | text | Hostname or base URL of the ntfy server | [https://ntfy.sh](https://ntfy.sh) | yes | no |
25+
| username | text | Optional username for basic auth | N/A | no | no |
26+
| password | text | Optional password for basic auth | N/A | no | no |
27+
| connectionTimeout | integer | WebSocket / HTTP connection timeout ms | 60000 | no | yes |
28+
29+
Configure the `ntfyConnection` as a bridge to hold shared server and authentication settings. Topic Things reference the bridge to reuse these settings.
30+
31+
### `ntfyTopic` Thing Configuration
32+
33+
| Name | Type | Description | Default | Required | Advanced |
34+
|-----------|------|---------------------------|---------|----------|----------|
35+
| topicname | text | Name of the topic/channel | N/A | yes | no |
36+
37+
## Channels
38+
39+
### `ntfyTopic` Channels
40+
41+
| Channel | Type | Read/Write | Description |
42+
|-------------|----------|------------|---------------------------------------------|
43+
| lastMessage | String | R | Last received message payload (read-only) |
44+
| messageTime | DateTime | R | Timestamp of the last message (read-only) |
45+
46+
## Full Example
47+
48+
Below are examples for textual configuration files showing a bridge (`ntfyConnection`), a topic Thing (`ntfyTopic`), Items bound to the Thing's channels, and a simple sitemap to display the last message and its timestamp.
49+
50+
### Thing Configuration
51+
52+
```things
53+
Bridge ntfy:ntfyConnection:myConn "Ntfy Server" [ hostname="https://ntfy.sh", connectionTimeout=60000 ]
54+
55+
Thing ntfy:ntfyTopic:home "Front Door Notifications" (ntfy:ntfyConnection:myConn) [ topicname="home" ]
56+
```
57+
58+
If your server requires authentication, supply `username` and `password` in the bridge configuration. The `topicname` must be provided for each `ntfyTopic` Thing.
59+
60+
### Item Configuration (items file)
61+
62+
Bind Items to the read-only channels exposed by the topic Thing to show the last received message and its timestamp:
63+
64+
```items
65+
String FrontDoorLastMessage "Front Door Message" { channel="ntfy:ntfyTopic:home:lastMessage" }
66+
DateTime FrontDoorMessageTime "Last message received" { channel="ntfy:ntfyTopic:home:messageTime" }
67+
```
68+
69+
Note: Sending notifications from openHAB to ntfy is done via the binding's Actions from rules (the binding exposes Actions to publish/delete messages). The channels above are read-only and show incoming or last-state values.
70+
71+
### Sitemap Configuration (sitemap file)
72+
73+
Simple sitemap demonstrating how to display the last message and its timestamp:
74+
75+
```sitemap
76+
sitemap notifications label="Notifications"
77+
{
78+
<Frame label="Front Door">
79+
Text item=FrontDoorLastMessage label="Last message [%s]"
80+
Text item=FrontDoorMessageTime label="Received [%1$td.%1$tm.%1$tY %1$tR]"
81+
</Frame>
82+
}
83+
```
84+
85+
## Actions (Rules DSL and JavaScript)
86+
87+
This binding exposes Rule Actions to send and manage messages on a configured topic. Actions are available for use from the Rules DSL and the ECMAScript/JavaScript automation scripts. The actions support a builder-style API to configure a message (message text, priority, tags, icon, attachments, actions, sequence id, ...) and then send it.
88+
89+
Important: Always check that the returned actions object is not null (the Thing must exist and be handled by the binding) before invoking methods.
90+
91+
### Available action methods
92+
93+
Below is a quick reference of the builder-style methods provided by the binding actions. Use these to construct messages before calling `send()`.
94+
95+
| Method | Parameters | Description | Ntfy docs |
96+
|--------|------------|-------------|----------|
97+
| withMessage | (String message) | Set the main message text. | [publish](https://docs.ntfy.sh/publish/#message-title) |
98+
| withPriority | (int priority) | Set message priority (0-5). | [priority](https://ntfy.sh/docs/publish/#priority) |
99+
| withTag | (String tag) | Add a tag to the message. | [tags](https://ntfy.sh/docs/publish/#tags) |
100+
| withIcon | (String url) | Set an icon URL for the notification. | [icons](https://ntfy.sh/docs/publish/#icons) |
101+
| withAttachment | (String url, String filename) | Attach a file or resource URL with optional filename. | [attachments](https://docs.ntfy.sh/publish/#attach-file-from-a-url) |
102+
| withViewAction | (String label, Boolean clearNotification, String url) | Add a view action (opens URL) with optional clear flag. | [actions](https://docs.ntfy.sh/publish/#open-websiteapp) |
103+
| withCopyAction | (String label, Boolean clearNotification, String value) | Add an action that copies text to clipboard on the client. | [actions](https://docs.ntfy.sh/publish/#copy-to-clipboard) |
104+
| withHttpAction | (String label, Boolean clearNotification, String url, String method, String headers, String body) | Add an HTTP action with optional method, headers and body. | [actions](https://docs.ntfy.sh/publish/#send-http-request) |
105+
| withBroadcastAction | (String label, Boolean clearNotification, String params) | Add a broadcast action to trigger local apps/handlers. | [actions](https://docs.ntfy.sh/publish/#send-android-broadcast) |
106+
| withDelay | (String delay) | Assign a delay. | [delay](https://docs.ntfy.sh/publish/#scheduled-delivery) |
107+
| withSequenceId | (String sequenceId) | Assign a sequence id for later reference (useful for delete/update). | [sequence id](https://docs.ntfy.sh/publish/#updating-deleting-notifications) |
108+
| send | () | Send the built message; returns a message ID string. | [publish](https://ntfy.sh/docs/publish/) |
109+
| send | (String file, String filename, String sequenceId) | Send a file; returns a message ID string. | [publish_local_file](https://docs.ntfy.sh/publish/#attach-local-file) |
110+
| delete | (String sequenceId) | Delete a message previously sent with the given sequence id. | [delete](https://ntfy.sh/docs/publish/#deleting-notifications) |
111+
112+
For more information about ntfy features and the notification format, see the ntfy project documentation: [https://ntfy.sh/docs/](https://ntfy.sh/docs/)
113+
114+
### Rules DSL example
115+
116+
```rules
117+
// Obtain the Thing-specific actions and use the builder to send a message
118+
val bindingActions = getActions("ntfy", "ntfy:ntfyTopic:frontdoor")
119+
if (bindingActions !== null) {
120+
// simple one-liner: send a message
121+
val msgId = bindingActions.withMessage("Someone is at the door").withPriority(4).send()
122+
123+
// or build up a more complex message
124+
bindingActions.withMessage("Doorbell pressed")
125+
.withSequenceId("doorbell-1")
126+
.withTag("door")
127+
.withIcon("https://example.org/icons/door.png")
128+
.withViewAction("Open UI", true, "https://example.org/ui")
129+
.send()
130+
131+
// delete a previously sent message by sequence id
132+
bindingActions.delete("doorbell-1")
133+
}
134+
```
135+
136+
The builder methods available include: withMessage(String), withPriority(int), withTag(String), withIcon(String), withAttachment(String, String), withViewAction(...), withCopyAction(...), withHttpAction(...), withBroadcastAction(...), withSequenceId(String), send(), and delete(String).
137+
138+
### ECMAScript / JavaScript example
139+
140+
In ECMAScript scripts you can obtain the Thing Actions and use the same builder API. The example below assumes the automation engine provides an `actions` helper (typical in openHAB JavaScript environment):
141+
142+
```javascript
143+
// get the Thing actions for the topic Thing
144+
var bindingActions = actions.get("ntfy", "ntfy:ntfyTopic:frontdoor");
145+
if (bindingActions) {
146+
// send a simple message
147+
var id = bindingActions.withMessage("Garage opened").withPriority(3).send();
148+
149+
// build and send a message with extra features
150+
bindingActions.withMessage("Motion detected in garage")
151+
.withSequenceId("garage-1")
152+
.withTag("motion")
153+
.withHttpAction("Open Camera", true, "https://camera/local/snap", "POST", null, null)
154+
.send();
155+
}
156+
```
157+
158+
If your scripting environment exposes a different API to retrieve Thing Actions, adapt the call accordingly. The important part is to obtain the Thing-specific actions object for the binding and then use the builder methods shown above.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.openhab.addons.bundles</groupId>
9+
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
10+
<version>5.2.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>org.openhab.binding.ntfy</artifactId>
14+
15+
<name>openHAB Add-ons :: Bundles :: Ntfy Binding</name>
16+
17+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<features name="org.openhab.binding.ntfy-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.6.0">
3+
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>
4+
5+
<feature name="openhab-binding-ntfy" description="Ntfy Binding" version="${project.version}">
6+
<feature>openhab-runtime-base</feature>
7+
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.ntfy/${project.version}</bundle>
8+
</feature>
9+
</features>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2010-2026 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.binding.ntfy.internal;
14+
15+
import org.eclipse.jdt.annotation.NonNullByDefault;
16+
import org.openhab.core.thing.ThingTypeUID;
17+
18+
/**
19+
* The {@link NtfyBindingConstants} class defines common constants, which are
20+
* used across the whole binding.
21+
*
22+
* @author Christian Kittel - Initial contribution
23+
*/
24+
@NonNullByDefault
25+
public class NtfyBindingConstants {
26+
27+
private static final String BINDING_ID = "ntfy";
28+
29+
// List of all Thing Type UIDs
30+
public static final ThingTypeUID NTFY_CONNECTION_THING = new ThingTypeUID(BINDING_ID, "ntfyConnection");
31+
public static final ThingTypeUID NTFY_TOPIC_THING = new ThingTypeUID(BINDING_ID, "ntfyTopic");
32+
33+
// List of all Channel ids
34+
public static final String CHANNEL_NTFY_LASTMESSAGE = "lastMessage";
35+
public static final String CHANNEL_NTFY_LASTMESSAGETIME = "lastMessageTime";
36+
public static final String CHANNEL_NTFY_LASTMESSAGEID = "lastMessageMessageId";
37+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2010-2026 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.binding.ntfy.internal;
14+
15+
import java.nio.charset.StandardCharsets;
16+
import java.util.Base64;
17+
18+
import org.eclipse.jdt.annotation.NonNullByDefault;
19+
import org.eclipse.jdt.annotation.Nullable;
20+
21+
/**
22+
* The {@link NtfyConnectionConfiguration} class contains fields mapping thing configuration parameters.
23+
*
24+
* @author Christian Kittel - Initial contribution
25+
*/
26+
@NonNullByDefault
27+
public class NtfyConnectionConfiguration {
28+
29+
/**
30+
* Hostname of the ntfy server, e.g. "ntfy.sh"
31+
*/
32+
public String hostname = "";
33+
34+
/**
35+
* Username for basic authentication
36+
*/
37+
public @Nullable String username;
38+
39+
/**
40+
* Password for basic authentication
41+
*/
42+
public @Nullable String password;
43+
44+
/**
45+
* Connection timeout in milliseconds
46+
*/
47+
public long connectionTimeout = 60000;
48+
49+
/**
50+
* Checks whether a Basic Authorization header should be provided based on the configured
51+
* username and password values.
52+
*
53+
* @return {@code true} when both username and password are non-null and non-blank,
54+
* {@code false} otherwise
55+
*/
56+
public boolean isAuthHeaderNeeded() {
57+
return username != null && !username.isBlank() && password != null && !password.isBlank();
58+
}
59+
60+
/**
61+
* Builds the HTTP Basic Authorization header value from the configured username and password.
62+
*
63+
* <p>
64+
* The credentials are combined as "username:password" and Base64-encoded using UTF-8.
65+
* The returned value is prefixed with "Basic ".
66+
*
67+
* @return the full value for the HTTP Authorization header
68+
*/
69+
public String buildAuthHeader() {
70+
String auth = username + ":" + password;
71+
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
72+
String authHeader = "Basic " + encodedAuth;
73+
return authHeader;
74+
}
75+
}

0 commit comments

Comments
 (0)