47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
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 |