custom_components/whatsapp_bridge_integration/services.py aktualisiert
This commit is contained in:
@@ -11,42 +11,33 @@ async def async_setup_services(hass: HomeAssistant):
|
|||||||
recipient = call.data.get("recipient")
|
recipient = call.data.get("recipient")
|
||||||
|
|
||||||
targets = []
|
targets = []
|
||||||
|
|
||||||
# 1. Fall: Broadcast (Wenn "all" getippt wurde)
|
|
||||||
if recipient == "all":
|
|
||||||
entries = hass.config_entries.async_entries(DOMAIN)
|
entries = hass.config_entries.async_entries(DOMAIN)
|
||||||
for e in entries:
|
|
||||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
|
||||||
|
|
||||||
# 2. Fall: Eine Entity wurde ausgewählt (z.B. sensor.whatsapp_rene)
|
# 1. Prüfen, ob es eine Entity-ID ist
|
||||||
elif recipient.startswith("sensor."):
|
if recipient.startswith("sensor."):
|
||||||
state = hass.states.get(recipient)
|
state = hass.states.get(recipient)
|
||||||
if state and "phone_number" in state.attributes:
|
if state:
|
||||||
targets.append(state.attributes.get("phone_number"))
|
# Ist es die 'All Accounts' Entität?
|
||||||
else:
|
if state.attributes.get("is_broadcast"):
|
||||||
_LOGGER.error("Could not find phone number for entity: %s", recipient)
|
|
||||||
|
|
||||||
# 3. Fall: Manuelle Nummer oder Name
|
|
||||||
else:
|
|
||||||
# Wir prüfen sicherheitshalber, ob es ein Account-Name ist
|
|
||||||
entries = hass.config_entries.async_entries(DOMAIN)
|
|
||||||
account_found = False
|
|
||||||
for e in entries:
|
for e in entries:
|
||||||
if e.title.lower() == recipient.lower():
|
|
||||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||||
account_found = True
|
else:
|
||||||
break
|
# Es ist ein einzelner Account
|
||||||
|
targets.append(state.attributes.get("phone_number"))
|
||||||
|
|
||||||
if not account_found:
|
# 2. Fallback: Manuelle Nummer (wenn keine sensor-ID übergeben wurde)
|
||||||
|
else:
|
||||||
targets.append(recipient)
|
targets.append(recipient)
|
||||||
|
|
||||||
# Senden
|
# Senden
|
||||||
for num in targets:
|
for num in targets:
|
||||||
|
if not num: continue
|
||||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||||
try:
|
try:
|
||||||
await hass.async_add_executor_job(
|
await hass.async_add_executor_job(
|
||||||
lambda: requests.post(url, json={"number": num, "message": message}, timeout=10)
|
lambda: requests.post(url, json={"number": num, "message": message}, timeout=10)
|
||||||
)
|
)
|
||||||
|
_LOGGER.info("WhatsApp sent to %s", num)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("Error sending to %s: %s", num, str(e))
|
_LOGGER.error("Error sending to %s: %s", num, str(e))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user