6 Commits
0.1.6 ... 0.1.7

4 changed files with 58 additions and 31 deletions

View File

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

View File

@@ -1,22 +1,44 @@
from homeassistant.components.sensor import SensorEntity
from .const import DOMAIN, CONF_ACCOUNT_NAME
from .const import DOMAIN
async def async_setup_entry(hass, entry, async_add_entities):
"""Add a sensor for each WhatsApp account."""
async_add_entities([WhatsAppAccountEntity(entry)], True)
"""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):
"""Representation of a WhatsApp Account as an entity."""
"""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"
# Wir speichern die Nummer in den Attributen, damit der Service sie findet
self._attr_extra_state_attributes = {
"phone_number": entry.data.get("phone_number")
"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

@@ -1,43 +1,44 @@
import requests
import logging
from homeassistant.core import HomeAssistant, ServiceCall
from .const import DOMAIN
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")
target_entity = call.data.get("recipient")
recipient = call.data.get("recipient")
targets = []
entries = hass.config_entries.async_entries(DOMAIN)
# 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)
# 1. Prüfen, ob es eine Entity-ID ist
if recipient.startswith("sensor."):
state = hass.states.get(recipient)
if state:
num = state.attributes.get("phone_number")
if num:
targets.append(num)
# 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:
# Fallback für manuelle Eingabe in YAML
targets.append(target_entity)
# 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("Sent to %s", num)
_LOGGER.info("WhatsApp sent to %s", num)
except Exception as e:
_LOGGER.error("Error: %s", str(e))
_LOGGER.error("Error sending to %s: %s", num, str(e))
hass.services.async_register(DOMAIN, "send_message", handle_send_message)

View File

@@ -4,13 +4,17 @@ send_message:
fields:
recipient:
name: Recipient
description: Select an account entity or type a manual number.
description: Select an Account, the Broadcast entity, or type a manual number.
required: true
selector:
text: {}
# Dies zeigt nun "WhatsApp Rene", "WhatsApp Work" UND "WhatsApp Broadcast (All)" an.
entity:
filter:
integration: whatsapp_bridge_integration
domain: sensor
message:
name: Message
description: The content of your message.
description: Your message text.
required: true
selector:
text: