From 20e6090f65eab5d8ad685909f6214e7de1a4c194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Bachmann?= Date: Tue, 14 Apr 2026 13:40:31 +0000 Subject: [PATCH] =?UTF-8?q?custom=5Fcomponents/whatsapp=5Fbridge=5Fintegra?= =?UTF-8?q?tion/services.py=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whatsapp_bridge_integration/services.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 custom_components/whatsapp_bridge_integration/services.py diff --git a/custom_components/whatsapp_bridge_integration/services.py b/custom_components/whatsapp_bridge_integration/services.py new file mode 100644 index 0000000..7a8e159 --- /dev/null +++ b/custom_components/whatsapp_bridge_integration/services.py @@ -0,0 +1,43 @@ +import requests +import logging +from homeassistant.core import HomeAssistant, ServiceCall +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +async def async_setup_services(hass: HomeAssistant): + async def handle_send_message(call: ServiceCall): + message = call.data.get("message") + target_entity = call.data.get("recipient") + + targets = [] + + # 1. Fall: Broadcast an alle + if target_entity == "all": + entities = hass.states.async_all("sensor") + for entity in entities: + if entity.entity_id.startswith("sensor.whatsapp_"): + targets.append(entity.attributes.get("phone_number")) + + # 2. Fall: Einzelne Entity ausgewählt + else: + state = hass.states.get(target_entity) + if state: + num = state.attributes.get("phone_number") + if num: + targets.append(num) + else: + # Fallback für manuelle Eingabe in YAML + targets.append(target_entity) + + for num in targets: + 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) + ) + _LOGGER.info("Sent to %s", num) + except Exception as e: + _LOGGER.error("Error: %s", str(e)) + + hass.services.async_register(DOMAIN, "send_message", handle_send_message) \ No newline at end of file