custom_components/whatsapp_bridge_integration/sensor.py aktualisiert
This commit is contained in:
@@ -1,22 +1,44 @@
|
|||||||
from homeassistant.components.sensor import SensorEntity
|
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):
|
async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
"""Add a sensor for each WhatsApp account."""
|
"""Add sensors for WhatsApp accounts and a global broadcast sensor."""
|
||||||
async_add_entities([WhatsAppAccountEntity(entry)], True)
|
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):
|
class WhatsAppAccountEntity(SensorEntity):
|
||||||
"""Representation of a WhatsApp Account as an entity."""
|
"""Individual Account (e.g., Rene, Work)."""
|
||||||
def __init__(self, entry):
|
def __init__(self, entry):
|
||||||
self._entry = entry
|
self._entry = entry
|
||||||
self._attr_name = f"WhatsApp {entry.title}"
|
self._attr_name = f"WhatsApp {entry.title}"
|
||||||
self._attr_unique_id = f"{entry.entry_id}_status"
|
self._attr_unique_id = f"{entry.entry_id}_status"
|
||||||
self._attr_icon = "mdi:whatsapp"
|
self._attr_icon = "mdi:whatsapp"
|
||||||
# Wir speichern die Nummer in den Attributen, damit der Service sie findet
|
|
||||||
self._attr_extra_state_attributes = {
|
self._attr_extra_state_attributes = {
|
||||||
"phone_number": entry.data.get("phone_number")
|
"phone_number": entry.data.get("phone_number"),
|
||||||
|
"is_broadcast": False
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
return "Ready"
|
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"
|
||||||
Reference in New Issue
Block a user