Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| adfe5dfe9d | |||
| 037cd9c9c7 | |||
| 498c059aa8 | |||
| dbdf987501 | |||
| 370e33da7c | |||
| 5d5971ece0 | |||
| 8b9aacfefb | |||
| c0800ce2bb | |||
| 43b4c747e5 |
@@ -1,59 +1,64 @@
|
||||
import logging
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: dict):
|
||||
"""Set up the WhatsApp Bridge component."""
|
||||
# Diese Funktion muss existieren, auch wenn sie nur True zurückgibt
|
||||
return True
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry):
|
||||
"""Setzt ein Profil (Config Entry) aus der UI-Konfiguration um."""
|
||||
"""Set up WhatsApp Bridge from a config entry."""
|
||||
|
||||
# Falls noch nicht geschehen, registrieren wir den globalen Dienst
|
||||
# Registriere den Dienst nur einmal
|
||||
if not hass.services.has_service(DOMAIN, "send_message"):
|
||||
|
||||
async def handle_send_message(call: ServiceCall):
|
||||
"""Zentraler Dienst zum Senden von Nachrichten."""
|
||||
message = call.data.get("message")
|
||||
target_account = call.data.get("account") # Optionaler Filter
|
||||
target_number = call.data.get("number") # Manuelle Nummer
|
||||
recipient = call.data.get("recipient")
|
||||
use_manual = call.data.get("use_manual_number", False)
|
||||
target_number = call.data.get("number")
|
||||
|
||||
# Alle installierten Profile durchsuchen
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
|
||||
targets = []
|
||||
|
||||
if target_number:
|
||||
# Fall A: Direkte Nummer wurde im Service-Call mitgegeben
|
||||
# 1. Manuelle Nummer Priorität
|
||||
if use_manual and target_number:
|
||||
targets.append(target_number)
|
||||
elif target_account:
|
||||
# Fall B: Ein spezifisches Profil wurde gewählt
|
||||
for e in entries:
|
||||
if e.title.lower() == target_account.lower():
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
else:
|
||||
# Fall C: Gar kein Ziel? Dann an ALLE Profile senden
|
||||
|
||||
# 2. Rundruf an alle
|
||||
elif recipient == "all_accounts":
|
||||
for e in entries:
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
|
||||
# 3. Spezifischer Account aus Dropdown
|
||||
elif recipient:
|
||||
for e in entries:
|
||||
if e.title.lower() == recipient.lower():
|
||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||
|
||||
# Senden an alle ermittelten Ziele
|
||||
# Sende-Vorgang
|
||||
for num in targets:
|
||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||
try:
|
||||
# Wir nutzen hass.async_add_executor_job für synchrone Requests
|
||||
await hass.async_add_executor_job(
|
||||
lambda: requests.post(url, json={
|
||||
"number": num,
|
||||
"message": message
|
||||
}, timeout=10)
|
||||
)
|
||||
_LOGGER.info("WhatsApp an %s gesendet: %s", num, message)
|
||||
_LOGGER.info("WhatsApp message successfully sent to %s", num)
|
||||
except Exception as e:
|
||||
_LOGGER.error("Fehler beim Senden an %s: %s", num, str(e))
|
||||
_LOGGER.error("Failed to send WhatsApp 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):
|
||||
"""Profil wieder entfernen."""
|
||||
async def async_unload_entry(hass: HomeAssistant, entry):
|
||||
"""Unload a config entry."""
|
||||
return True
|
||||
@@ -1,6 +1,5 @@
|
||||
from homeassistant import config_entries
|
||||
import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||
|
||||
class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
@@ -8,10 +7,9 @@ class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Erster Schritt wenn man auf 'Hinzufügen' klickt."""
|
||||
"""First step when adding the integration via UI."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
# Wir nutzen den Namen als Titel des Eintrags
|
||||
return self.async_create_entry(
|
||||
title=user_input[CONF_ACCOUNT_NAME],
|
||||
data=user_input
|
||||
@@ -20,7 +18,7 @@ class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({
|
||||
vol.Required(CONF_ACCOUNT_NAME, default="Max Mustermann"): str,
|
||||
vol.Required(CONF_ACCOUNT_NAME, default="John Doe"): str,
|
||||
vol.Required(CONF_PHONE_NUMBER, default="491701234567"): str,
|
||||
}),
|
||||
errors=errors,
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"documentation": "https://git.bahmcloud.de/bahmcloud/Whatsapp-Bridge-Integration",
|
||||
"dependencies": [],
|
||||
"codeowners": ["@bahmcloud"],
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.4",
|
||||
"iot_class": "local_polling",
|
||||
"config_flow": true
|
||||
}
|
||||
33
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
33
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
@@ -0,0 +1,33 @@
|
||||
send_message:
|
||||
name: Send Message
|
||||
description: Sends a WhatsApp message via the Bahmcloud Bridge.
|
||||
fields:
|
||||
recipient:
|
||||
name: Recipient
|
||||
description: Select a saved profile or choose "Broadcast to all".
|
||||
required: true
|
||||
selector:
|
||||
select:
|
||||
options:
|
||||
- label: "Broadcast to all"
|
||||
value: "all_accounts"
|
||||
# Dieser Teil sorgt dafür, dass HA die Namen deiner Accounts anzeigt
|
||||
custom_value: true
|
||||
message:
|
||||
name: Message
|
||||
description: The content of your WhatsApp message.
|
||||
required: true
|
||||
selector:
|
||||
text:
|
||||
multiline: true
|
||||
use_manual_number:
|
||||
name: Use Manual Number
|
||||
description: Enable this to ignore the recipient and use the number field below.
|
||||
default: false
|
||||
selector:
|
||||
boolean:
|
||||
number:
|
||||
name: Manual Number
|
||||
description: "Direct phone number (e.g., 491701234567)."
|
||||
selector:
|
||||
text:
|
||||
Reference in New Issue
Block a user