Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bea9bbbbb | |||
| 8dc6cfb7c1 | |||
| b4fd722d8a | |||
| 20e6090f65 | |||
| d9ada23f3e | |||
| 276d4a63cb | |||
| adfe5dfe9d | |||
| 037cd9c9c7 | |||
| 498c059aa8 | |||
| dbdf987501 | |||
| 370e33da7c | |||
| 5d5971ece0 | |||
| 8b9aacfefb | |||
| c0800ce2bb | |||
| 43b4c747e5 | |||
| 61f36c7db7 | |||
| 9981adf5ac | |||
| 6b927b7e20 | |||
| 5122d59535 | |||
| f7240b4eb3 |
29
README.md
29
README.md
@@ -31,3 +31,32 @@ action: whatsapp_bridge_integration.send_message
|
||||
data:
|
||||
number: "491701234567"
|
||||
message: "The washing machine is finished! 🧺"
|
||||
```
|
||||
|
||||
### Use in Node-RED
|
||||
Add a Call Service (or Action) node.
|
||||
|
||||
Domain: whatsapp_bridge_integration
|
||||
|
||||
Service: send_message
|
||||
|
||||
Data:
|
||||
|
||||
```JSON
|
||||
{
|
||||
"number": "491701234567",
|
||||
"message": "{{payload}}"
|
||||
}
|
||||
```
|
||||
|
||||
## 👥 Multi-Account Support (Coming Soon)
|
||||
We are currently working on a UI-based configuration flow so you can save "Account Aliases" (like "Dad", "Work", "Security") and select them from a dropdown menu without typing the phone number every time.
|
||||
|
||||
##⚠️ Important Notes
|
||||
Phone Numbers: Always use the international format without + or 00 (e.g., 49... for Germany).
|
||||
|
||||
Local Network: The integration communicates locally with the Add-on on port 3000.
|
||||
|
||||
Developed by Bahmcloud
|
||||
|
||||
---
|
||||
@@ -1,47 +1,31 @@
|
||||
import logging
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.core import HomeAssistant
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "whatsapp_bridge_custom"
|
||||
async def async_setup(hass: HomeAssistant, config: dict):
|
||||
return True
|
||||
|
||||
# Definition der Service-Parameter
|
||||
SERVICE_SEND_MESSAGE = "send_message"
|
||||
ATTR_NUMBER = "number"
|
||||
ATTR_MESSAGE = "message"
|
||||
async def async_setup_entry(hass: HomeAssistant, entry):
|
||||
"""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
|
||||
|
||||
SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Required(ATTR_NUMBER): cv.string,
|
||||
vol.Required(ATTR_MESSAGE): cv.string,
|
||||
})
|
||||
# Forward the setup to the sensor platform to create entities
|
||||
await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the WhatsApp Bridge integration."""
|
||||
|
||||
def send_whatsapp_msg(call):
|
||||
"""Dienst-Funktion: Nachricht an das Add-on senden."""
|
||||
number = call.data.get(ATTR_NUMBER)
|
||||
message = call.data.get(ATTR_MESSAGE)
|
||||
|
||||
# Die interne URL zum Add-on (standardmäßig local-whatsapp-bridge)
|
||||
# Port 3000 haben wir im Dockerfile definiert
|
||||
url = "http://866fd2eb-whatsapp-bridge:3000/send"
|
||||
|
||||
try:
|
||||
response = requests.post(url, json={
|
||||
"number": number,
|
||||
"message": message
|
||||
}, timeout=10)
|
||||
|
||||
if response.status_code != 200:
|
||||
_LOGGER.error("Fehler beim Senden: %s", response.text)
|
||||
except Exception as e:
|
||||
_LOGGER.error("Verbindung zum Add-on fehlgeschlagen: %s", str(e))
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_SEND_MESSAGE, send_whatsapp_msg, schema=SERVICE_SCHEMA
|
||||
)
|
||||
# Registriere den Service (nur beim ersten Mal)
|
||||
if not hass.services.has_service(DOMAIN, "send_message"):
|
||||
from .services import async_setup_services
|
||||
await async_setup_services(hass)
|
||||
|
||||
return True
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry):
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, ["sensor"])
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return unload_ok
|
||||
25
custom_components/whatsapp_bridge_integration/config_flow.py
Normal file
25
custom_components/whatsapp_bridge_integration/config_flow.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from homeassistant import config_entries
|
||||
import voluptuous as vol
|
||||
from .const import DOMAIN, CONF_PHONE_NUMBER, CONF_ACCOUNT_NAME
|
||||
|
||||
class WhatsAppBridgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for WhatsApp Bridge."""
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""First step when adding the integration via UI."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
title=user_input[CONF_ACCOUNT_NAME],
|
||||
data=user_input
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({
|
||||
vol.Required(CONF_ACCOUNT_NAME, default="John Doe"): str,
|
||||
vol.Required(CONF_PHONE_NUMBER, default="491701234567"): str,
|
||||
}),
|
||||
errors=errors,
|
||||
)
|
||||
3
custom_components/whatsapp_bridge_integration/const.py
Normal file
3
custom_components/whatsapp_bridge_integration/const.py
Normal file
@@ -0,0 +1,3 @@
|
||||
DOMAIN = "whatsapp_bridge_integration"
|
||||
CONF_PHONE_NUMBER = "phone_number"
|
||||
CONF_ACCOUNT_NAME = "account_name"
|
||||
@@ -4,6 +4,7 @@
|
||||
"documentation": "https://git.bahmcloud.de/bahmcloud/Whatsapp-Bridge-Integration",
|
||||
"dependencies": [],
|
||||
"codeowners": ["@bahmcloud"],
|
||||
"version": "0.1.0",
|
||||
"iot_class": "local_polling"
|
||||
"version": "0.1.5",
|
||||
"iot_class": "local_polling",
|
||||
"config_flow": true
|
||||
}
|
||||
22
custom_components/whatsapp_bridge_integration/sensor.py
Normal file
22
custom_components/whatsapp_bridge_integration/sensor.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from .const import DOMAIN, CONF_ACCOUNT_NAME
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
"""Add a sensor for each WhatsApp account."""
|
||||
async_add_entities([WhatsAppAccountEntity(entry)], True)
|
||||
|
||||
class WhatsAppAccountEntity(SensorEntity):
|
||||
"""Representation of a WhatsApp Account as an entity."""
|
||||
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"
|
||||
# Wir speichern die Nummer in den Attributen, damit der Service sie findet
|
||||
self._attr_extra_state_attributes = {
|
||||
"phone_number": entry.data.get("phone_number")
|
||||
}
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return "Ready"
|
||||
43
custom_components/whatsapp_bridge_integration/services.py
Normal file
43
custom_components/whatsapp_bridge_integration/services.py
Normal file
@@ -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)
|
||||
21
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
21
custom_components/whatsapp_bridge_integration/services.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
send_message:
|
||||
name: Send Message
|
||||
description: Sends a WhatsApp message.
|
||||
fields:
|
||||
recipient:
|
||||
name: Recipient
|
||||
description: Select an account or enter a manual number/ID.
|
||||
required: true
|
||||
selector:
|
||||
text: {} # Easy Entry from some custom Numbers
|
||||
entity:
|
||||
filter:
|
||||
integration: whatsapp_bridge_integration
|
||||
domain: sensor
|
||||
message:
|
||||
name: Message
|
||||
description: The content of your message.
|
||||
required: true
|
||||
selector:
|
||||
text:
|
||||
multiline: true
|
||||
Reference in New Issue
Block a user