custom_components/whatsapp_bridge_integration/__init__.py aktualisiert
This commit is contained in:
@@ -1,34 +1,27 @@
|
|||||||
import logging
|
async def handle_send_message(call: ServiceCall):
|
||||||
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")
|
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")
|
target_number = call.data.get("number")
|
||||||
all_accounts = call.data.get("all_accounts", False)
|
|
||||||
|
|
||||||
entries = hass.config_entries.async_entries(DOMAIN)
|
entries = hass.config_entries.async_entries(DOMAIN)
|
||||||
targets = []
|
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:
|
for e in entries:
|
||||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||||
elif target_number:
|
elif recipient:
|
||||||
targets.append(target_number)
|
# Fall C: Spezifisches Profil (Recipient Name)
|
||||||
elif target_account:
|
|
||||||
for e in entries:
|
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))
|
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||||
|
|
||||||
|
# Senden (wie gehabt)
|
||||||
for num in targets:
|
for num in targets:
|
||||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||||
try:
|
try:
|
||||||
@@ -40,11 +33,4 @@ async def async_setup_entry(hass: HomeAssistant, entry):
|
|||||||
)
|
)
|
||||||
_LOGGER.info("WhatsApp sent to %s", num)
|
_LOGGER.info("WhatsApp sent to %s", num)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("Failed to send to %s: %s", num, str(e))
|
_LOGGER.error("Error sending 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
|
|
||||||
Reference in New Issue
Block a user