custom_components/whatsapp_bridge_integration/__init__.py aktualisiert

This commit is contained in:
2026-04-14 13:28:19 +00:00
parent 498c059aa8
commit 037cd9c9c7

View File

@@ -1,3 +1,22 @@
import logging
import requests
import voluptuous as vol
from homeassistant.core import HomeAssistant, ServiceCall
from .const import DOMAIN, CONF_PHONE_NUMBER
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the WhatsApp Bridge component."""
# Diese Funktion muss existieren, auch wenn sie nur True zurückgibt
return True
async def async_setup_entry(hass: HomeAssistant, entry):
"""Set up WhatsApp Bridge from a config entry."""
# Registriere den Dienst nur einmal
if not hass.services.has_service(DOMAIN, "send_message"):
async def handle_send_message(call: ServiceCall): async def handle_send_message(call: ServiceCall):
message = call.data.get("message") message = call.data.get("message")
recipient = call.data.get("recipient") recipient = call.data.get("recipient")
@@ -7,21 +26,22 @@ async def handle_send_message(call: ServiceCall):
entries = hass.config_entries.async_entries(DOMAIN) entries = hass.config_entries.async_entries(DOMAIN)
targets = [] targets = []
# Logik-Prüfung # 1. Manuelle Nummer Priorität
if use_manual and target_number: if use_manual and target_number:
# Fall A: Manuelle Nummer
targets.append(target_number) targets.append(target_number)
# 2. Rundruf an alle
elif recipient == "all_accounts": elif recipient == "all_accounts":
# Fall B: Rundruf
for e in entries: for e in entries:
targets.append(e.data.get(CONF_PHONE_NUMBER)) targets.append(e.data.get(CONF_PHONE_NUMBER))
# 3. Spezifischer Account aus Dropdown
elif recipient: elif recipient:
# Fall C: Spezifisches Profil (Recipient Name)
for e in entries: for e in entries:
if e.title.lower() == recipient.lower(): if e.title.lower() == recipient.lower():
targets.append(e.data.get(CONF_PHONE_NUMBER)) targets.append(e.data.get(CONF_PHONE_NUMBER))
# Senden (wie gehabt) # Sende-Vorgang
for num in targets: for num in targets:
url = "http://866fd2eb-whatsapp-bridge:3000/send" url = "http://866fd2eb-whatsapp-bridge:3000/send"
try: try:
@@ -31,6 +51,14 @@ async def handle_send_message(call: ServiceCall):
"message": message "message": message
}, timeout=10) }, timeout=10)
) )
_LOGGER.info("WhatsApp sent to %s", num) _LOGGER.info("WhatsApp message successfully sent to %s", num)
except Exception as e: except Exception as e:
_LOGGER.error("Error sending to %s: %s", num, str(e)) _LOGGER.error("Failed to send WhatsApp to %s: %s", num, str(e))
hass.services.async_register(DOMAIN, "send_message", handle_send_message)
return True
async def async_unload_entry(hass: HomeAssistant, entry):
"""Unload a config entry."""
return True