44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
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" |