Fix add-on discovery and bridge communication
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import requests
|
||||
import logging
|
||||
import aiohttp
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -9,6 +10,8 @@ async def async_setup_services(hass: HomeAssistant):
|
||||
async def handle_send_message(call: ServiceCall):
|
||||
message = call.data.get("message")
|
||||
recipient = call.data.get("recipient")
|
||||
if not recipient or message is None:
|
||||
raise HomeAssistantError("Empfänger und Nachricht sind erforderlich")
|
||||
|
||||
targets = []
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
@@ -29,16 +32,18 @@ async def async_setup_services(hass: HomeAssistant):
|
||||
else:
|
||||
targets.append(recipient)
|
||||
|
||||
api = hass.data[DOMAIN]["api"]
|
||||
|
||||
# Senden
|
||||
for num in targets:
|
||||
if not num: continue
|
||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||
try:
|
||||
await hass.async_add_executor_job(
|
||||
lambda: requests.post(url, json={"number": num, "message": message}, timeout=10)
|
||||
)
|
||||
await api.async_send_message(num, message)
|
||||
_LOGGER.info("WhatsApp sent to %s", num)
|
||||
except Exception as e:
|
||||
_LOGGER.error("Error sending to %s: %s", num, str(e))
|
||||
except (aiohttp.ClientError, TimeoutError, RuntimeError) as err:
|
||||
_LOGGER.error("Error sending to %s: %s", num, err)
|
||||
raise HomeAssistantError(
|
||||
f"WhatsApp-Nachricht an {num} fehlgeschlagen: {err}"
|
||||
) from err
|
||||
|
||||
hass.services.async_register(DOMAIN, "send_message", handle_send_message)
|
||||
hass.services.async_register(DOMAIN, "send_message", handle_send_message)
|
||||
|
||||
Reference in New Issue
Block a user