custom_components/whatsapp_bridge_integration/sensor.py aktualisiert
0.2.1
This commit is contained in:
@@ -10,7 +10,6 @@ from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Polling-Intervall (15 Sekunden)
|
||||
SCAN_INTERVAL = timedelta(seconds=15)
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
@@ -37,22 +36,19 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||
update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
|
||||
# Ersten Abruf vor dem Erstellen der Entitäten erzwingen
|
||||
try:
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
except UpdateFailed:
|
||||
_LOGGER.warning("Erster Status-Abruf fehlgeschlagen. Add-on läuft eventuell noch an.")
|
||||
|
||||
# Basis-Entitäten: Account-Sensor und der neue Live-Status-Sensor
|
||||
entities = [
|
||||
WhatsAppAccountEntity(entry),
|
||||
WhatsAppStatusSensor(coordinator, entry)
|
||||
]
|
||||
# Jeder Account bekommt immer seine eigene Entität
|
||||
entities = [WhatsAppAccountEntity(entry)]
|
||||
|
||||
# Prüfen, ob die Broadcast-Entität schon existiert
|
||||
if not hass.data[DOMAIN].get("broadcast_registered"):
|
||||
# Globale Entitäten (Status & Broadcast) NUR EINMALIG für die gesamte Integration anlegen
|
||||
if not hass.data[DOMAIN].get("global_entities_registered"):
|
||||
entities.append(WhatsAppStatusSensor(coordinator))
|
||||
entities.append(WhatsAppBroadcastEntity())
|
||||
hass.data[DOMAIN]["broadcast_registered"] = True
|
||||
hass.data[DOMAIN]["global_entities_registered"] = True
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
@@ -75,14 +71,14 @@ class WhatsAppAccountEntity(SensorEntity):
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Ordnet diesen Sensor dem Hauptgerät zu."""
|
||||
"""Ordnet diesen Sensor seinem spezifischen Personen-Gerät zu."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, "general_gateway")},
|
||||
identifiers={(DOMAIN, self._entry.entry_id)},
|
||||
)
|
||||
|
||||
|
||||
class WhatsAppBroadcastEntity(SensorEntity):
|
||||
"""Virtual 'All Accounts' Entity."""
|
||||
"""Virtual 'All Accounts' Entity (Global, keinem spezifischen Gerät zugeordnet)."""
|
||||
def __init__(self):
|
||||
self._attr_name = "WhatsApp Broadcast (All)"
|
||||
self._attr_unique_id = "whatsapp_bridge_broadcast_all"
|
||||
@@ -95,22 +91,14 @@ class WhatsAppBroadcastEntity(SensorEntity):
|
||||
def state(self):
|
||||
return "Global"
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Ordnet diesen Sensor dem Hauptgerät zu."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, "general_gateway")},
|
||||
)
|
||||
|
||||
|
||||
class WhatsAppStatusSensor(SensorEntity):
|
||||
"""Echter Live-Status-Sensor, gekoppelt an die Add-on API."""
|
||||
"""Echter Live-Status-Sensor (Global, keinem spezifischen Gerät zugeordnet)."""
|
||||
|
||||
def __init__(self, coordinator, entry):
|
||||
def __init__(self, coordinator):
|
||||
self.coordinator = coordinator
|
||||
self._entry = entry
|
||||
self._attr_name = "WhatsApp Bridge Status"
|
||||
self._attr_unique_id = f"{entry.entry_id}_bridge_live_status"
|
||||
self._attr_unique_id = "whatsapp_bridge_live_status_global"
|
||||
self._attr_icon = "mdi:whatsapp"
|
||||
|
||||
@property
|
||||
@@ -148,13 +136,6 @@ class WhatsAppStatusSensor(SensorEntity):
|
||||
}
|
||||
return {}
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Ordnet diesen Sensor dem Hauptgerät zu."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, "general_gateway")},
|
||||
)
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Registrieren beim Live-Coordinator."""
|
||||
self.async_on_remove(
|
||||
|
||||
Reference in New Issue
Block a user