Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f346cea41a | |||
| 93b57878f9 | |||
| fc826d7345 | |||
| 3002acc934 | |||
| 39df1ed0fa | |||
| 6b3a10bd33 | |||
| ec6a9c6dd8 | |||
| aa5a060aef | |||
| 7bea9bbbbb | |||
| 8dc6cfb7c1 | |||
| b4fd722d8a | |||
| 20e6090f65 | |||
| d9ada23f3e | |||
| 276d4a63cb |
@@ -1,64 +1,31 @@
|
|||||||
import logging
|
import logging
|
||||||
import requests
|
from homeassistant.core import HomeAssistant
|
||||||
import voluptuous as vol
|
from .const import DOMAIN
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
|
||||||
from .const import DOMAIN, CONF_PHONE_NUMBER
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: dict):
|
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
|
return True
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry):
|
async def async_setup_entry(hass: HomeAssistant, entry):
|
||||||
"""Set up WhatsApp Bridge from a config 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
|
||||||
|
|
||||||
|
# Forward the setup to the sensor platform to create entities
|
||||||
|
await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
|
||||||
|
|
||||||
# Registriere den Dienst nur einmal
|
# 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)
|
||||||
message = call.data.get("message")
|
|
||||||
recipient = call.data.get("recipient")
|
|
||||||
use_manual = call.data.get("use_manual_number", False)
|
|
||||||
target_number = call.data.get("number")
|
|
||||||
|
|
||||||
entries = hass.config_entries.async_entries(DOMAIN)
|
|
||||||
targets = []
|
|
||||||
|
|
||||||
# 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))
|
|
||||||
|
|
||||||
# 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))
|
|
||||||
|
|
||||||
# Sende-Vorgang
|
|
||||||
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("WhatsApp message successfully sent to %s", num)
|
|
||||||
except Exception as 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
|
return True
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry):
|
async def async_unload_entry(hass: HomeAssistant, entry):
|
||||||
"""Unload a config entry."""
|
"""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
|
||||||
@@ -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.4",
|
"version": "0.1.7",
|
||||||
"iot_class": "local_polling",
|
"iot_class": "local_polling",
|
||||||
"config_flow": true
|
"config_flow": true
|
||||||
}
|
}
|
||||||
44
custom_components/whatsapp_bridge_integration/sensor.py
Normal file
44
custom_components/whatsapp_bridge_integration/sensor.py
Normal 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"
|
||||||
44
custom_components/whatsapp_bridge_integration/services.py
Normal file
44
custom_components/whatsapp_bridge_integration/services.py
Normal 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)
|
||||||
@@ -1,33 +1,21 @@
|
|||||||
send_message:
|
send_message:
|
||||||
name: Send Message
|
name: Send Message
|
||||||
description: Sends a WhatsApp message via the Bahmcloud Bridge.
|
description: Sends a WhatsApp message via the Bridge.
|
||||||
fields:
|
fields:
|
||||||
recipient:
|
recipient:
|
||||||
name: Recipient
|
name: Recipient
|
||||||
description: Select a saved profile or choose "Broadcast to all".
|
description: Select an Account, the Broadcast entity, or type a manual number.
|
||||||
required: true
|
required: true
|
||||||
selector:
|
selector:
|
||||||
select:
|
# Dies zeigt nun "WhatsApp Rene", "WhatsApp Work" UND "WhatsApp Broadcast (All)" an.
|
||||||
options:
|
entity:
|
||||||
- label: "Broadcast to all"
|
filter:
|
||||||
value: "all_accounts"
|
integration: whatsapp_bridge_integration
|
||||||
# Dieser Teil sorgt dafür, dass HA die Namen deiner Accounts anzeigt
|
domain: sensor
|
||||||
custom_value: true
|
|
||||||
message:
|
message:
|
||||||
name: Message
|
name: Message
|
||||||
description: The content of your WhatsApp message.
|
description: Your message text.
|
||||||
required: true
|
required: true
|
||||||
selector:
|
selector:
|
||||||
text:
|
text:
|
||||||
multiline: true
|
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