@@ -1,8 +1,7 @@
|
||||
"""
|
||||
AdGuard Control Hub for Home Assistant.
|
||||
|
||||
Transform your AdGuard Home into a smart network management powerhouse with
|
||||
complete client control, service blocking, and automation capabilities.
|
||||
Transform your AdGuard Home into a smart network management powerhouse.
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
@@ -76,12 +75,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
raise ConfigEntryNotReady(f"Failed to set up platforms: {err}") from err
|
||||
|
||||
# Register services (only once, not per config entry)
|
||||
# Register services (only once)
|
||||
if not hass.services.has_service(DOMAIN, "block_services"):
|
||||
services = AdGuardControlHubServices(hass)
|
||||
services.register_services()
|
||||
|
||||
# Store services instance for cleanup
|
||||
hass.data.setdefault(f"{DOMAIN}_services", services)
|
||||
|
||||
_LOGGER.info("AdGuard Control Hub setup complete for %s:%s",
|
||||
@@ -98,13 +95,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
# Unregister services if this was the last entry
|
||||
if not hass.data[DOMAIN]: # No more entries
|
||||
if not hass.data[DOMAIN]:
|
||||
services = hass.data.get(f"{DOMAIN}_services")
|
||||
if services:
|
||||
services.unregister_services()
|
||||
hass.data.pop(f"{DOMAIN}_services", None)
|
||||
|
||||
# Also clean up the empty domain entry
|
||||
hass.data.pop(DOMAIN, None)
|
||||
|
||||
return unload_ok
|
||||
@@ -129,7 +124,7 @@ class AdGuardControlHubCoordinator(DataUpdateCoordinator):
|
||||
async def _async_update_data(self) -> Dict[str, Any]:
|
||||
"""Fetch data from AdGuard Home."""
|
||||
try:
|
||||
# Fetch all data concurrently for better performance
|
||||
# Fetch all data concurrently
|
||||
tasks = [
|
||||
self.api.get_clients(),
|
||||
self.api.get_statistics(),
|
||||
@@ -139,37 +134,25 @@ class AdGuardControlHubCoordinator(DataUpdateCoordinator):
|
||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||
clients, statistics, status = results
|
||||
|
||||
# Handle any exceptions in individual requests
|
||||
for i, result in enumerate(results):
|
||||
if isinstance(result, Exception):
|
||||
endpoint_names = ["clients", "statistics", "status"]
|
||||
_LOGGER.warning(
|
||||
"Error fetching %s from %s:%s: %s",
|
||||
endpoint_names[i],
|
||||
self.api.host,
|
||||
self.api.port,
|
||||
result
|
||||
)
|
||||
|
||||
# Update stored data (use empty dict if fetch failed)
|
||||
if not isinstance(clients, Exception):
|
||||
self._clients = {
|
||||
client["name"]: client
|
||||
for client in clients.get("clients", [])
|
||||
if client.get("name") # Ensure client has a name
|
||||
if client.get("name")
|
||||
}
|
||||
else:
|
||||
_LOGGER.warning("Failed to update clients data, keeping previous data")
|
||||
_LOGGER.warning("Failed to update clients data: %s", clients)
|
||||
|
||||
if not isinstance(statistics, Exception):
|
||||
self._statistics = statistics
|
||||
else:
|
||||
_LOGGER.warning("Failed to update statistics data, keeping previous data")
|
||||
_LOGGER.warning("Failed to update statistics data: %s", statistics)
|
||||
|
||||
if not isinstance(status, Exception):
|
||||
self._protection_status = status
|
||||
else:
|
||||
_LOGGER.warning("Failed to update status data, keeping previous data")
|
||||
_LOGGER.warning("Failed to update status data: %s", status)
|
||||
|
||||
return {
|
||||
"clients": self._clients,
|
||||
@@ -196,21 +179,3 @@ class AdGuardControlHubCoordinator(DataUpdateCoordinator):
|
||||
def protection_status(self) -> Dict[str, Any]:
|
||||
"""Return protection status data."""
|
||||
return self._protection_status
|
||||
|
||||
def get_client(self, client_name: str) -> Dict[str, Any] | None:
|
||||
"""Get a specific client by name."""
|
||||
return self._clients.get(client_name)
|
||||
|
||||
def has_client(self, client_name: str) -> bool:
|
||||
"""Check if a client exists."""
|
||||
return client_name in self._clients
|
||||
|
||||
@property
|
||||
def client_count(self) -> int:
|
||||
"""Return the number of clients."""
|
||||
return len(self._clients)
|
||||
|
||||
@property
|
||||
def is_protection_enabled(self) -> bool:
|
||||
"""Return True if protection is enabled."""
|
||||
return self._protection_status.get("protection_enabled", False)
|
||||
|
Reference in New Issue
Block a user