Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d5971ece0 | |||
| 8b9aacfefb | |||
| c0800ce2bb | |||
| 43b4c747e5 | |||
| 61f36c7db7 | |||
| 9981adf5ac | |||
| 6b927b7e20 | |||
| 5122d59535 | |||
| f7240b4eb3 |
29
README.md
29
README.md
@@ -31,3 +31,32 @@ action: whatsapp_bridge_integration.send_message
|
||||
data:
|
||||
number: "491701234567"
|
||||
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,50 @@
|
||||
import logging
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "whatsapp_bridge_custom"
|
||||
async def async_setup_entry(hass: HomeAssistant, entry):
|
||||
"""Set up a profile from a config entry."""
|
||||
|
||||
# Definition der Service-Parameter
|
||||
SERVICE_SEND_MESSAGE = "send_message"
|
||||
ATTR_NUMBER = "number"
|
||||
ATTR_MESSAGE = "message"
|
||||
if not hass.services.has_service(DOMAIN, "send_message"):
|
||||
|
||||
SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Required(ATTR_NUMBER): cv.string,
|
||||
vol.Required(ATTR_MESSAGE): cv.string,
|
||||
})
|
||||
async def handle_send_message(call: ServiceCall):
|
||||
message = call.data.get("message")
|
||||
target_account = call.data.get("account")
|
||||
target_number = call.data.get("number")
|
||||
all_accounts = call.data.get("all_accounts", False)
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the WhatsApp Bridge integration."""
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
targets = []
|
||||
|
||||
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)
|
||||
if all_accounts:
|
||||
for e in entries:
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
elif target_number:
|
||||
targets.append(target_number)
|
||||
elif target_account:
|
||||
for e in entries:
|
||||
if e.title.lower() == target_account.lower():
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
|
||||
# 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"
|
||||
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("Failed to send to %s: %s", num, str(e))
|
||||
|
||||
try:
|
||||
response = requests.post(url, json={
|
||||
"number": number,
|
||||
"message": message
|
||||
}, timeout=10)
|
||||
|
||||
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))
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_SEND_MESSAGE, send_whatsapp_msg, schema=SERVICE_SCHEMA
|
||||
)
|
||||
hass.services.async_register(DOMAIN, "send_message", handle_send_message)
|
||||
|
||||
return True
|
||||
|
||||
async def async_unload_entry(hass, entry):
|
||||
return True
|
||||
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.2",
|
||||
"iot_class": "local_polling",
|
||||
"config_flow": true
|
||||
}
|
||||
30
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
30
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
@@ -0,0 +1,30 @@
|
||||
send_message:
|
||||
name: Send Message
|
||||
description: Sends a WhatsApp message via the Bahmcloud Bridge.
|
||||
fields:
|
||||
account:
|
||||
name: Account Profile
|
||||
description: The name of a saved profile (e.g., Max). Leave empty if using a manual number.
|
||||
example: "Max"
|
||||
selector:
|
||||
text:
|
||||
number:
|
||||
name: Manual Number
|
||||
description: "Direct phone number (Format: 491701234567). Overwrites the Account field."
|
||||
example: "491701234567"
|
||||
selector:
|
||||
text:
|
||||
message:
|
||||
name: Message
|
||||
description: The content of your WhatsApp message.
|
||||
required: true
|
||||
example: "The garage door is open!"
|
||||
selector:
|
||||
text:
|
||||
multiline: true
|
||||
all_accounts:
|
||||
name: Send to All
|
||||
description: If enabled, the message is sent to all registered profiles.
|
||||
default: false
|
||||
selector:
|
||||
boolean:
|
||||
Reference in New Issue
Block a user