custom_components/whatsapp_bridge_integration/__init__.py aktualisiert
This commit is contained in:
@@ -1,34 +1,27 @@
|
||||
import logging
|
||||
import requests
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry):
|
||||
"""Set up a profile from a config entry."""
|
||||
|
||||
if not hass.services.has_service(DOMAIN, "send_message"):
|
||||
|
||||
async def handle_send_message(call: ServiceCall):
|
||||
message = call.data.get("message")
|
||||
target_account = call.data.get("account")
|
||||
recipient = call.data.get("recipient")
|
||||
use_manual = call.data.get("use_manual_number", False)
|
||||
target_number = call.data.get("number")
|
||||
all_accounts = call.data.get("all_accounts", False)
|
||||
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
targets = []
|
||||
|
||||
if all_accounts:
|
||||
# Logik-Prüfung
|
||||
if use_manual and target_number:
|
||||
# Fall A: Manuelle Nummer
|
||||
targets.append(target_number)
|
||||
elif recipient == "all_accounts":
|
||||
# Fall B: Rundruf
|
||||
for e in entries:
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
elif target_number:
|
||||
targets.append(target_number)
|
||||
elif target_account:
|
||||
elif recipient:
|
||||
# Fall C: Spezifisches Profil (Recipient Name)
|
||||
for e in entries:
|
||||
if e.title.lower() == target_account.lower():
|
||||
if e.title.lower() == recipient.lower():
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
|
||||
# Senden (wie gehabt)
|
||||
for num in targets:
|
||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||
try:
|
||||
@@ -40,11 +33,4 @@ async def async_setup_entry(hass: HomeAssistant, entry):
|
||||
)
|
||||
_LOGGER.info("WhatsApp sent to %s", num)
|
||||
except Exception as e:
|
||||
_LOGGER.error("Failed to send to %s: %s", num, str(e))
|
||||
|
||||
hass.services.async_register(DOMAIN, "send_message", handle_send_message)
|
||||
|
||||
return True
|
||||
|
||||
async def async_unload_entry(hass, entry):
|
||||
return True
|
||||
_LOGGER.error("Error sending to %s: %s", num, str(e))
|
||||
Reference in New Issue
Block a user