Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 498c059aa8 | |||
| dbdf987501 | |||
| 370e33da7c | |||
| 5d5971ece0 | |||
| 8b9aacfefb | |||
| c0800ce2bb | |||
| 43b4c747e5 | |||
| 61f36c7db7 | |||
| 9981adf5ac | |||
| 6b927b7e20 | |||
| 5122d59535 | |||
| f7240b4eb3 |
31
README.md
31
README.md
@@ -30,4 +30,33 @@ You can call the service directly in your automations:
|
||||
action: whatsapp_bridge_integration.send_message
|
||||
data:
|
||||
number: "491701234567"
|
||||
message: "The washing machine is finished! 🧺"
|
||||
message: "The washing machine is finished! 🧺"
|
||||
```
|
||||
|
||||
### Use in Node-RED
|
||||
Add a Call Service (or Action) node.
|
||||
|
||||
Domain: whatsapp_bridge_integration
|
||||
|
||||
Service: send_message
|
||||
|
||||
Data:
|
||||
|
||||
```JSON
|
||||
{
|
||||
"number": "491701234567",
|
||||
"message": "{{payload}}"
|
||||
}
|
||||
```
|
||||
|
||||
## 👥 Multi-Account Support (Coming Soon)
|
||||
We are currently working on a UI-based configuration flow so you can save "Account Aliases" (like "Dad", "Work", "Security") and select them from a dropdown menu without typing the phone number every time.
|
||||
|
||||
##⚠️ Important Notes
|
||||
Phone Numbers: Always use the international format without + or 00 (e.g., 49... for Germany).
|
||||
|
||||
Local Network: The integration communicates locally with the Add-on on port 3000.
|
||||
|
||||
Developed by Bahmcloud
|
||||
|
||||
---
|
||||
@@ -1,47 +1,36 @@
|
||||
import logging
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "whatsapp_bridge_custom"
|
||||
|
||||
# Definition der Service-Parameter
|
||||
SERVICE_SEND_MESSAGE = "send_message"
|
||||
ATTR_NUMBER = "number"
|
||||
ATTR_MESSAGE = "message"
|
||||
|
||||
SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Required(ATTR_NUMBER): cv.string,
|
||||
vol.Required(ATTR_MESSAGE): cv.string,
|
||||
})
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the WhatsApp Bridge integration."""
|
||||
|
||||
def send_whatsapp_msg(call):
|
||||
"""Dienst-Funktion: Nachricht an das Add-on senden."""
|
||||
number = call.data.get(ATTR_NUMBER)
|
||||
message = call.data.get(ATTR_MESSAGE)
|
||||
|
||||
# Die interne URL zum Add-on (standardmäßig local-whatsapp-bridge)
|
||||
# Port 3000 haben wir im Dockerfile definiert
|
||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||
|
||||
try:
|
||||
response = requests.post(url, json={
|
||||
"number": number,
|
||||
"message": message
|
||||
}, timeout=10)
|
||||
async def handle_send_message(call: ServiceCall):
|
||||
message = call.data.get("message")
|
||||
recipient = call.data.get("recipient")
|
||||
use_manual = call.data.get("use_manual_number", False)
|
||||
target_number = call.data.get("number")
|
||||
|
||||
if response.status_code != 200:
|
||||
_LOGGER.error("Fehler beim Senden: %s", response.text)
|
||||
except Exception as e:
|
||||
_LOGGER.error("Verbindung zum Add-on fehlgeschlagen: %s", str(e))
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
targets = []
|
||||
|
||||
# Logik-Prüfung
|
||||
if use_manual and target_number:
|
||||
# Fall A: Manuelle Nummer
|
||||
targets.append(target_number)
|
||||
elif recipient == "all_accounts":
|
||||
# Fall B: Rundruf
|
||||
for e in entries:
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
elif recipient:
|
||||
# Fall C: Spezifisches Profil (Recipient Name)
|
||||
for e in entries:
|
||||
if e.title.lower() == recipient.lower():
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_SEND_MESSAGE, send_whatsapp_msg, schema=SERVICE_SCHEMA
|
||||
)
|
||||
|
||||
return True
|
||||
# Senden (wie gehabt)
|
||||
for num in targets:
|
||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||
try:
|
||||
await hass.async_add_executor_job(
|
||||
lambda: requests.post(url, json={
|
||||
"number": num,
|
||||
"message": message
|
||||
}, timeout=10)
|
||||
)
|
||||
_LOGGER.info("WhatsApp sent to %s", num)
|
||||
except Exception as e:
|
||||
_LOGGER.error("Error sending to %s: %s", num, str(e))
|
||||
25
custom_components/whatsapp_bridge_integration/config_flow.py
Normal file
25
custom_components/whatsapp_bridge_integration/config_flow.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from homeassistant import config_entries
|
||||
import voluptuous as vol
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||
|
||||
class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for WhatsApp Bridge."""
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""First step when adding the integration via UI."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
title=user_input[CONF_ACCOUNT_NAME],
|
||||
data=user_input
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({
|
||||
vol.Required(CONF_ACCOUNT_NAME, default="John Doe"): str,
|
||||
vol.Required(CONF_PHONE_NUMBER, default="491701234567"): str,
|
||||
}),
|
||||
errors=errors,
|
||||
)
|
||||
3
custom_components/whatsapp_bridge_integration/const.py
Normal file
3
custom_components/whatsapp_bridge_integration/const.py
Normal file
@@ -0,0 +1,3 @@
|
||||
DOMAIN = "whatsapp_bridge_integration"
|
||||
CONF_PHONE_NUMBER = "phone_number"
|
||||
CONF_ACCOUNT_NAME = "account_name"
|
||||
@@ -4,6 +4,7 @@
|
||||
"documentation": "https://git.bahmcloud.de/bahmcloud/Whatsapp-Bridge-Integration",
|
||||
"dependencies": [],
|
||||
"codeowners": ["@bahmcloud"],
|
||||
"version": "0.1.0",
|
||||
"iot_class": "local_polling"
|
||||
"version": "0.1.3",
|
||||
"iot_class": "local_polling",
|
||||
"config_flow": true
|
||||
}
|
||||
33
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
33
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
@@ -0,0 +1,33 @@
|
||||
send_message:
|
||||
name: Send Message
|
||||
description: Sends a WhatsApp message via the Bahmcloud Bridge.
|
||||
fields:
|
||||
recipient:
|
||||
name: Recipient
|
||||
description: Select a saved profile or choose "Broadcast to all".
|
||||
required: true
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- label: "Broadcast to all"
|
||||
value: "all_accounts"
|
||||
# Dieser Teil sorgt dafür, dass HA die Namen deiner Accounts anzeigt
|
||||
custom_value: true
|
||||
message:
|
||||
name: Message
|
||||
description: The content of your WhatsApp message.
|
||||
required: true
|
||||
selector:
|
||||
text:
|
||||
multiline: true
|
||||
use_manual_number:
|
||||
name: Use Manual Number
|
||||
description: Enable this to ignore the recipient and use the number field below.
|
||||
default: false
|
||||
selector:
|
||||
boolean:
|
||||
number:
|
||||
name: Manual Number
|
||||
description: "Direct phone number (e.g., 491701234567)."
|
||||
selector:
|
||||
text:
|
||||
Reference in New Issue
Block a user