2 Commits
0.1.3 ... 0.1.4

2 changed files with 37 additions and 9 deletions

View File

@@ -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") message = call.data.get("message")
recipient = call.data.get("recipient") recipient = call.data.get("recipient")
use_manual = call.data.get("use_manual_number", False) 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) 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

View File

@@ -4,7 +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.3", "version": "0.1.4",
"iot_class": "local_polling", "iot_class": "local_polling",
"config_flow": true "config_flow": true
} }