23 Commits
0.1.1 ... 0.1.7

Author SHA1 Message Date
f346cea41a custom_components/whatsapp_bridge_integration/manifest.json aktualisiert 2026-04-14 14:02:20 +00:00
93b57878f9 custom_components/whatsapp_bridge_integration/services.yaml aktualisiert 2026-04-14 14:01:07 +00:00
fc826d7345 custom_components/whatsapp_bridge_integration/services.py aktualisiert 2026-04-14 14:00:54 +00:00
3002acc934 custom_components/whatsapp_bridge_integration/sensor.py aktualisiert 2026-04-14 14:00:36 +00:00
39df1ed0fa custom_components/whatsapp_bridge_integration/services.py aktualisiert 2026-04-14 13:59:19 +00:00
6b3a10bd33 custom_components/whatsapp_bridge_integration/services.yaml aktualisiert 2026-04-14 13:59:02 +00:00
ec6a9c6dd8 custom_components/whatsapp_bridge_integration/manifest.json aktualisiert 2026-04-14 13:48:34 +00:00
aa5a060aef custom_components/whatsapp_bridge_integration/services.yaml aktualisiert 2026-04-14 13:47:33 +00:00
7bea9bbbbb custom_components/whatsapp_bridge_integration/manifest.json aktualisiert 2026-04-14 13:43:28 +00:00
8dc6cfb7c1 custom_components/whatsapp_bridge_integration/services.yaml aktualisiert 2026-04-14 13:43:03 +00:00
b4fd722d8a custom_components/whatsapp_bridge_integration/services.yaml aktualisiert 2026-04-14 13:40:46 +00:00
20e6090f65 custom_components/whatsapp_bridge_integration/services.py hinzugefügt 2026-04-14 13:40:31 +00:00
d9ada23f3e custom_components/whatsapp_bridge_integration/sensor.py hinzugefügt 2026-04-14 13:40:10 +00:00
276d4a63cb custom_components/whatsapp_bridge_integration/__init__.py aktualisiert 2026-04-14 13:39:51 +00:00
adfe5dfe9d custom_components/whatsapp_bridge_integration/manifest.json aktualisiert 2026-04-14 13:29:15 +00:00
037cd9c9c7 custom_components/whatsapp_bridge_integration/__init__.py aktualisiert 2026-04-14 13:28:19 +00:00
498c059aa8 custom_components/whatsapp_bridge_integration/manifest.json aktualisiert 2026-04-14 13:24:38 +00:00
dbdf987501 custom_components/whatsapp_bridge_integration/__init__.py aktualisiert 2026-04-14 13:24:24 +00:00
370e33da7c custom_components/whatsapp_bridge_integration/services.yaml aktualisiert 2026-04-14 13:24:10 +00:00
5d5971ece0 custom_components/whatsapp_bridge_integration/manifest.json aktualisiert 2026-04-14 13:15:31 +00:00
8b9aacfefb custom_components/whatsapp_bridge_integration/__init__.py aktualisiert 2026-04-14 13:15:02 +00:00
c0800ce2bb custom_components/whatsapp_bridge_integration/config_flow.py aktualisiert 2026-04-14 13:14:45 +00:00
43b4c747e5 custom_components/whatsapp_bridge_integration/services.yaml hinzugefügt 2026-04-14 13:14:25 +00:00
6 changed files with 133 additions and 54 deletions

View File

@@ -1,59 +1,31 @@
import logging import logging
import requests from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, ServiceCall from .const import DOMAIN
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: dict):
return True
async def async_setup_entry(hass: HomeAssistant, entry): async def async_setup_entry(hass: HomeAssistant, entry):
"""Setzt ein Profil (Config Entry) aus der UI-Konfiguration um.""" """Set up entry and forward to sensor platform."""
# Lege die Daten im hass-Objekt ab
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = entry.data
# Falls noch nicht geschehen, registrieren wir den globalen Dienst # Forward the setup to the sensor platform to create entities
await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
# Registriere den Service (nur beim ersten Mal)
if not hass.services.has_service(DOMAIN, "send_message"): if not hass.services.has_service(DOMAIN, "send_message"):
from .services import async_setup_services
async def handle_send_message(call: ServiceCall): await async_setup_services(hass)
"""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
# Alle installierten Profile durchsuchen
entries = hass.config_entries.async_entries(DOMAIN)
targets = []
if target_number:
# Fall A: Direkte Nummer wurde im Service-Call mitgegeben
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
for e in entries:
targets.append(e.data.get(CONF_PHONE_NUMBER))
# Senden an alle ermittelten Ziele
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)
except Exception as e:
_LOGGER.error("Fehler beim Senden an %s: %s", num, str(e))
hass.services.async_register(DOMAIN, "send_message", handle_send_message)
return True return True
async def async_unload_entry(hass, entry): async def async_unload_entry(hass: HomeAssistant, entry):
"""Profil wieder entfernen.""" """Unload a config entry."""
return True unload_ok = await hass.config_entries.async_unload_platforms(entry, ["sensor"])
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@@ -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,

View File

@@ -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.7",
"iot_class": "local_polling", "iot_class": "local_polling",
"config_flow": true "config_flow": true
} }

View File

@@ -0,0 +1,44 @@
from homeassistant.components.sensor import SensorEntity
from .const import DOMAIN
async def async_setup_entry(hass, entry, async_add_entities):
"""Add sensors for WhatsApp accounts and a global broadcast sensor."""
entities = [WhatsAppAccountEntity(entry)]
# Wir prüfen, ob die Broadcast-Entität schon existiert
# Falls nicht, legen wir sie einmalig an
if not hass.data[DOMAIN].get("broadcast_registered"):
entities.append(WhatsAppBroadcastEntity())
hass.data[DOMAIN]["broadcast_registered"] = True
async_add_entities(entities, True)
class WhatsAppAccountEntity(SensorEntity):
"""Individual Account (e.g., Rene, Work)."""
def __init__(self, entry):
self._entry = entry
self._attr_name = f"WhatsApp {entry.title}"
self._attr_unique_id = f"{entry.entry_id}_status"
self._attr_icon = "mdi:whatsapp"
self._attr_extra_state_attributes = {
"phone_number": entry.data.get("phone_number"),
"is_broadcast": False
}
@property
def state(self):
return "Ready"
class WhatsAppBroadcastEntity(SensorEntity):
"""Virtual 'All Accounts' Entity."""
def __init__(self):
self._attr_name = "WhatsApp Broadcast (All)"
self._attr_unique_id = "whatsapp_bridge_broadcast_all"
self._attr_icon = "mdi:whatsapp-arrow-up"
self._attr_extra_state_attributes = {
"is_broadcast": True
}
@property
def state(self):
return "Global"

View File

@@ -0,0 +1,44 @@
import requests
import logging
from homeassistant.core import HomeAssistant, ServiceCall
from .const import DOMAIN, CONF_PHONE_NUMBER
_LOGGER = logging.getLogger(__name__)
async def async_setup_services(hass: HomeAssistant):
async def handle_send_message(call: ServiceCall):
message = call.data.get("message")
recipient = call.data.get("recipient")
targets = []
entries = hass.config_entries.async_entries(DOMAIN)
# 1. Prüfen, ob es eine Entity-ID ist
if recipient.startswith("sensor."):
state = hass.states.get(recipient)
if state:
# Ist es die 'All Accounts' Entität?
if state.attributes.get("is_broadcast"):
for e in entries:
targets.append(e.data.get(CONF_PHONE_NUMBER))
else:
# Es ist ein einzelner Account
targets.append(state.attributes.get("phone_number"))
# 2. Fallback: Manuelle Nummer (wenn keine sensor-ID übergeben wurde)
else:
targets.append(recipient)
# 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)
)
_LOGGER.info("WhatsApp sent to %s", num)
except Exception as e:
_LOGGER.error("Error sending to %s: %s", num, str(e))
hass.services.async_register(DOMAIN, "send_message", handle_send_message)

View File

@@ -0,0 +1,21 @@
send_message:
name: Send Message
description: Sends a WhatsApp message via the Bridge.
fields:
recipient:
name: Recipient
description: Select an Account, the Broadcast entity, or type a manual number.
required: true
selector:
# Dies zeigt nun "WhatsApp Rene", "WhatsApp Work" UND "WhatsApp Broadcast (All)" an.
entity:
filter:
integration: whatsapp_bridge_integration
domain: sensor
message:
name: Message
description: Your message text.
required: true
selector:
text:
multiline: true