diff --git a/custom_components/whatsapp_bridge_integration/__init__.py b/custom_components/whatsapp_bridge_integration/__init__.py index 340750d..c6b5087 100644 --- a/custom_components/whatsapp_bridge_integration/__init__.py +++ b/custom_components/whatsapp_bridge_integration/__init__.py @@ -1,4 +1,23 @@ -async def handle_send_message(call: ServiceCall): +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): message = call.data.get("message") recipient = call.data.get("recipient") use_manual = call.data.get("use_manual_number", False) @@ -7,21 +26,22 @@ async def handle_send_message(call: ServiceCall): entries = hass.config_entries.async_entries(DOMAIN) targets = [] - # Logik-Prüfung + # 1. Manuelle Nummer Priorität if use_manual and target_number: - # Fall A: Manuelle Nummer targets.append(target_number) + + # 2. Rundruf an alle elif recipient == "all_accounts": - # Fall B: Rundruf for e in entries: targets.append(e.data.get(CONF_PHONE_NUMBER)) + + # 3. Spezifischer Account aus Dropdown elif recipient: - # Fall C: Spezifisches Profil (Recipient Name) for e in entries: if e.title.lower() == recipient.lower(): targets.append(e.data.get(CONF_PHONE_NUMBER)) - # Senden (wie gehabt) + # Sende-Vorgang for num in targets: url = "http://866fd2eb-whatsapp-bridge:3000/send" try: @@ -31,6 +51,14 @@ async def handle_send_message(call: ServiceCall): "message": message }, timeout=10) ) - _LOGGER.info("WhatsApp sent to %s", num) + _LOGGER.info("WhatsApp message successfully sent to %s", num) except Exception as e: - _LOGGER.error("Error sending to %s: %s", num, str(e)) \ No newline at end of file + _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 \ No newline at end of file