Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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
|
action: whatsapp_bridge_integration.send_message
|
||||||
data:
|
data:
|
||||||
number: "491701234567"
|
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,59 @@
|
|||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import voluptuous as vol
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
from homeassistant.helpers import config_validation as cv
|
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DOMAIN = "whatsapp_bridge_custom"
|
async def async_setup_entry(hass: HomeAssistant, entry):
|
||||||
|
"""Setzt ein Profil (Config Entry) aus der UI-Konfiguration um."""
|
||||||
# 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):
|
# Falls noch nicht geschehen, registrieren wir den globalen Dienst
|
||||||
"""Dienst-Funktion: Nachricht an das Add-on senden."""
|
if not hass.services.has_service(DOMAIN, "send_message"):
|
||||||
number = call.data.get(ATTR_NUMBER)
|
|
||||||
message = call.data.get(ATTR_MESSAGE)
|
|
||||||
|
|
||||||
# Die interne URL zum Add-on (standardmäßig local-whatsapp-bridge)
|
async def handle_send_message(call: ServiceCall):
|
||||||
# Port 3000 haben wir im Dockerfile definiert
|
"""Zentraler Dienst zum Senden von Nachrichten."""
|
||||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
message = call.data.get("message")
|
||||||
|
target_account = call.data.get("account") # Optionaler Filter
|
||||||
try:
|
target_number = call.data.get("number") # Manuelle Nummer
|
||||||
response = requests.post(url, json={
|
|
||||||
"number": number,
|
|
||||||
"message": message
|
|
||||||
}, timeout=10)
|
|
||||||
|
|
||||||
if response.status_code != 200:
|
# Alle installierten Profile durchsuchen
|
||||||
_LOGGER.error("Fehler beim Senden: %s", response.text)
|
entries = hass.config_entries.async_entries(DOMAIN)
|
||||||
except Exception as e:
|
|
||||||
_LOGGER.error("Verbindung zum Add-on fehlgeschlagen: %s", str(e))
|
targets = []
|
||||||
|
|
||||||
|
if target_number:
|
||||||
|
# Fall A: Direkte Nummer wurde im Service-Call mitgegeben
|
||||||
|
targets.append(target_number)
|
||||||
|
elif target_account:
|
||||||
|
# Fall B: Ein spezifisches Profil wurde gewählt
|
||||||
|
for e in entries:
|
||||||
|
if e.title.lower() == target_account.lower():
|
||||||
|
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||||
|
else:
|
||||||
|
# Fall C: Gar kein Ziel? Dann an ALLE Profile senden
|
||||||
|
for e in entries:
|
||||||
|
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||||
|
|
||||||
hass.services.async_register(
|
# Senden an alle ermittelten Ziele
|
||||||
DOMAIN, SERVICE_SEND_MESSAGE, send_whatsapp_msg, schema=SERVICE_SCHEMA
|
for num in targets:
|
||||||
)
|
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||||
|
try:
|
||||||
|
# Wir nutzen hass.async_add_executor_job für synchrone Requests
|
||||||
|
await hass.async_add_executor_job(
|
||||||
|
lambda: requests.post(url, json={
|
||||||
|
"number": num,
|
||||||
|
"message": message
|
||||||
|
}, timeout=10)
|
||||||
|
)
|
||||||
|
_LOGGER.info("WhatsApp an %s gesendet: %s", num, message)
|
||||||
|
except Exception as e:
|
||||||
|
_LOGGER.error("Fehler beim Senden an %s: %s", num, str(e))
|
||||||
|
|
||||||
|
hass.services.async_register(DOMAIN, "send_message", handle_send_message)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def async_unload_entry(hass, entry):
|
||||||
|
"""Profil wieder entfernen."""
|
||||||
return True
|
return True
|
||||||
27
custom_components/whatsapp_bridge_integration/config_flow.py
Normal file
27
custom_components/whatsapp_bridge_integration/config_flow.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
from homeassistant import config_entries
|
||||||
|
import voluptuous as vol
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
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):
|
||||||
|
"""Erster Schritt wenn man auf 'Hinzufügen' klickt."""
|
||||||
|
errors = {}
|
||||||
|
if user_input is not None:
|
||||||
|
# Wir nutzen den Namen als Titel des Eintrags
|
||||||
|
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="Max Mustermann"): 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",
|
"documentation": "https://git.bahmcloud.de/bahmcloud/Whatsapp-Bridge-Integration",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@bahmcloud"],
|
"codeowners": ["@bahmcloud"],
|
||||||
"version": "0.1.0",
|
"version": "0.1.1",
|
||||||
"iot_class": "local_polling"
|
"iot_class": "local_polling",
|
||||||
|
"config_flow": true
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user