custom_components/whatsapp_bridge_custom/__init__.py hinzugefügt

This commit is contained in:
2026-04-14 12:20:01 +00:00
parent 0883e41f56
commit b984a0f3e1

View File

@@ -0,0 +1,47 @@
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)
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
)
return True