5 Commits
0.1.2 ... 0.1.4

3 changed files with 46 additions and 29 deletions

View File

@@ -1,34 +1,47 @@
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):
"""Set up a profile from a config entry."""
"""Set up WhatsApp Bridge from a config entry."""
# Registriere den Dienst nur einmal
if not hass.services.has_service(DOMAIN, "send_message"):
async def handle_send_message(call: ServiceCall):
message = call.data.get("message")
target_account = call.data.get("account")
recipient = call.data.get("recipient")
use_manual = call.data.get("use_manual_number", False)
target_number = call.data.get("number")
all_accounts = call.data.get("all_accounts", False)
entries = hass.config_entries.async_entries(DOMAIN)
targets = []
if all_accounts:
# 1. Manuelle Nummer Priorität
if use_manual and target_number:
targets.append(target_number)
# 2. Rundruf an alle
elif recipient == "all_accounts":
for e in entries:
targets.append(e.data.get(CONF_PHONE_NUMBER))
elif target_number:
targets.append(target_number)
elif target_account:
# 3. Spezifischer Account aus Dropdown
elif recipient:
for e in entries:
if e.title.lower() == target_account.lower():
if e.title.lower() == recipient.lower():
targets.append(e.data.get(CONF_PHONE_NUMBER))
# Sende-Vorgang
for num in targets:
url = "http://866fd2eb-whatsapp-bridge:3000/send"
try:
@@ -38,13 +51,14 @@ async def async_setup_entry(hass: HomeAssistant, entry):
"message": message
}, timeout=10)
)
_LOGGER.info("WhatsApp sent to %s", num)
_LOGGER.info("WhatsApp message successfully sent to %s", num)
except Exception as e:
_LOGGER.error("Failed to send to %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):
async def async_unload_entry(hass: HomeAssistant, entry):
"""Unload a config entry."""
return True

View File

@@ -4,7 +4,7 @@
"documentation": "https://git.bahmcloud.de/bahmcloud/Whatsapp-Bridge-Integration",
"dependencies": [],
"codeowners": ["@bahmcloud"],
"version": "0.1.2",
"version": "0.1.4",
"iot_class": "local_polling",
"config_flow": true
}

View File

@@ -2,29 +2,32 @@ send_message:
name: Send Message
description: Sends a WhatsApp message via the Bahmcloud Bridge.
fields:
account:
name: Account Profile
description: The name of a saved profile (e.g., Max). Leave empty if using a manual number.
example: "Max"
recipient:
name: Recipient
description: Select a saved profile or choose "Broadcast to all".
required: true
selector:
text:
number:
name: Manual Number
description: "Direct phone number (Format: 491701234567). Overwrites the Account field."
example: "491701234567"
selector:
text:
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
example: "The garage door is open!"
selector:
text:
multiline: true
all_accounts:
name: Send to All
description: If enabled, the message is sent to all registered profiles.
use_manual_number:
name: Use Manual Number
description: Enable this to ignore the recipient and use the number field below.
default: false
selector:
boolean:
boolean:
number:
name: Manual Number
description: "Direct phone number (e.g., 491701234567)."
selector:
text: