Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 498c059aa8 | |||
| dbdf987501 | |||
| 370e33da7c | |||
| 5d5971ece0 | |||
| 8b9aacfefb | |||
| c0800ce2bb | |||
| 43b4c747e5 |
@@ -1,59 +1,36 @@
|
|||||||
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):
|
|
||||||
"""Setzt ein Profil (Config Entry) aus der UI-Konfiguration um."""
|
|
||||||
|
|
||||||
# Falls noch nicht geschehen, registrieren wir den globalen Dienst
|
|
||||||
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")
|
message = call.data.get("message")
|
||||||
target_account = call.data.get("account") # Optionaler Filter
|
recipient = call.data.get("recipient")
|
||||||
target_number = call.data.get("number") # Manuelle Nummer
|
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)
|
entries = hass.config_entries.async_entries(DOMAIN)
|
||||||
|
|
||||||
targets = []
|
targets = []
|
||||||
|
|
||||||
if target_number:
|
# Logik-Prüfung
|
||||||
# Fall A: Direkte Nummer wurde im Service-Call mitgegeben
|
if use_manual and target_number:
|
||||||
|
# Fall A: Manuelle Nummer
|
||||||
targets.append(target_number)
|
targets.append(target_number)
|
||||||
elif target_account:
|
elif recipient == "all_accounts":
|
||||||
# Fall B: Ein spezifisches Profil wurde gewählt
|
# Fall B: Rundruf
|
||||||
for e in entries:
|
for e in entries:
|
||||||
if e.title.lower() == target_account.lower():
|
|
||||||
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
targets.append(e.data.get(CONF_PHONE_NUMBER))
|
||||||
else:
|
elif recipient:
|
||||||
# Fall C: Gar kein Ziel? Dann an ALLE Profile senden
|
# Fall C: Spezifisches Profil (Recipient Name)
|
||||||
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))
|
||||||
|
|
||||||
# Senden an alle ermittelten Ziele
|
# 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:
|
||||||
# Wir nutzen hass.async_add_executor_job für synchrone Requests
|
|
||||||
await hass.async_add_executor_job(
|
await hass.async_add_executor_job(
|
||||||
lambda: requests.post(url, json={
|
lambda: requests.post(url, json={
|
||||||
"number": num,
|
"number": num,
|
||||||
"message": message
|
"message": message
|
||||||
}, timeout=10)
|
}, timeout=10)
|
||||||
)
|
)
|
||||||
_LOGGER.info("WhatsApp an %s gesendet: %s", num, message)
|
_LOGGER.info("WhatsApp sent to %s", num)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("Fehler beim Senden an %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):
|
|
||||||
"""Profil wieder entfernen."""
|
|
||||||
return True
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||||
|
|
||||||
class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
@@ -8,10 +7,9 @@ class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
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 = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
# Wir nutzen den Namen als Titel des Eintrags
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input[CONF_ACCOUNT_NAME],
|
title=user_input[CONF_ACCOUNT_NAME],
|
||||||
data=user_input
|
data=user_input
|
||||||
@@ -20,7 +18,7 @@ class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=vol.Schema({
|
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,
|
vol.Required(CONF_PHONE_NUMBER, default="491701234567"): str,
|
||||||
}),
|
}),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"documentation": "https://git.bahmcloud.de/bahmcloud/Whatsapp-Bridge-Integration",
|
"documentation": "https://git.bahmcloud.de/bahmcloud/Whatsapp-Bridge-Integration",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@bahmcloud"],
|
"codeowners": ["@bahmcloud"],
|
||||||
"version": "0.1.1",
|
"version": "0.1.3",
|
||||||
"iot_class": "local_polling",
|
"iot_class": "local_polling",
|
||||||
"config_flow": true
|
"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