Fix add-on discovery and bridge communication
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import logging
|
||||
import async_timeout
|
||||
import aiohttp
|
||||
from datetime import timedelta
|
||||
|
||||
@@ -15,18 +14,14 @@ SCAN_INTERVAL = timedelta(seconds=15)
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
"""Add sensors for WhatsApp accounts, live status, and a global broadcast sensor."""
|
||||
|
||||
api = hass.data[DOMAIN]["api"]
|
||||
|
||||
# Coordinator für den echten Live-Status vom Add-on einrichten
|
||||
async def _async_update_data():
|
||||
url = "http://866fd2eb-whatsapp-bridge:3000/api/status"
|
||||
async with aiohttp.ClientSession() as session:
|
||||
try:
|
||||
async with async_timeout.timeout(5):
|
||||
async with session.get(url) as response:
|
||||
if response.status != 200:
|
||||
raise UpdateFailed(f"API meldet Fehler {response.status}")
|
||||
return await response.json()
|
||||
except Exception as err:
|
||||
raise UpdateFailed(f"Add-on nicht erreichbar: {err}")
|
||||
try:
|
||||
return await api.async_get_status()
|
||||
except (aiohttp.ClientError, TimeoutError, ValueError) as err:
|
||||
raise UpdateFailed(f"Add-on nicht erreichbar: {err}") from err
|
||||
|
||||
coordinator = DataUpdateCoordinator(
|
||||
hass,
|
||||
@@ -36,9 +31,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||
update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
|
||||
try:
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
except UpdateFailed:
|
||||
await coordinator.async_refresh()
|
||||
if not coordinator.last_update_success:
|
||||
_LOGGER.warning("Erster Status-Abruf fehlgeschlagen. Add-on läuft eventuell noch an.")
|
||||
|
||||
# Jeder Account bekommt immer seine eigene Entität
|
||||
@@ -49,6 +43,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||
entities.append(WhatsAppStatusSensor(coordinator))
|
||||
entities.append(WhatsAppBroadcastEntity())
|
||||
hass.data[DOMAIN]["global_entities_registered"] = True
|
||||
hass.data[DOMAIN]["global_entities_entry_id"] = entry.entry_id
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
@@ -129,6 +124,8 @@ class WhatsAppStatusSensor(SensorEntity):
|
||||
return "WAITING_FOR_SCAN"
|
||||
elif data.get("isInitializing"):
|
||||
return "INITIALIZING"
|
||||
elif data.get("reconnectAttempts", 0) > 0:
|
||||
return "RECONNECTING"
|
||||
|
||||
return "UNKNOWN"
|
||||
|
||||
@@ -139,7 +136,10 @@ class WhatsAppStatusSensor(SensorEntity):
|
||||
if data:
|
||||
return {
|
||||
"client_state": data.get("clientState", "UNKNOWN"),
|
||||
"has_qr_code": data.get("hasQr", False)
|
||||
"has_qr_code": data.get("hasQr", False),
|
||||
"is_initializing": data.get("isInitializing", False),
|
||||
"reconnect_attempts": data.get("reconnectAttempts", 0),
|
||||
"last_error": data.get("lastError"),
|
||||
}
|
||||
return {}
|
||||
|
||||
@@ -154,4 +154,4 @@ class WhatsAppStatusSensor(SensorEntity):
|
||||
"""Registrieren beim Live-Coordinator."""
|
||||
self.async_on_remove(
|
||||
self.coordinator.async_add_listener(self.async_write_ha_state)
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user