Some checks failed
🧪 Integration Testing / 🔧 Test Integration (2023.12.0, 3.11) (push) Successful in 2m11s
🧪 Integration Testing / 🔧 Test Integration (2023.12.0, 3.12) (push) Successful in 2m2s
🧪 Integration Testing / 🔧 Test Integration (2024.1.0, 3.11) (push) Successful in 1m4s
🧪 Integration Testing / 🔧 Test Integration (2024.1.0, 3.12) (push) Successful in 1m19s
🛡️ Code Quality & Security Check / 🔍 Code Quality Analysis (push) Failing after 56s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
"""Switch platform for AdGuard Control Hub integration."""
|
|
import logging
|
|
from homeassistant.components.switch import SwitchEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
from . import AdGuardControlHubCoordinator
|
|
from .api import AdGuardHomeAPI
|
|
from .const import DOMAIN, ICON_PROTECTION, ICON_PROTECTION_OFF, ICON_CLIENT, MANUFACTURER
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback):
|
|
"""Set up AdGuard Control Hub switch platform."""
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
|
|
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
|
|
|
|
entities = []
|
|
# Add global protection switch
|
|
entities.append(AdGuardProtectionSwitch(coordinator, api))
|
|
|
|
# Add client switches
|
|
for client_name in coordinator.clients.keys():
|
|
entities.append(AdGuardClientSwitch(coordinator, api, client_name))
|
|
|
|
async_add_entities(entities)
|
|
|
|
class AdGuardBaseSwitch(CoordinatorEntity, SwitchEntity):
|
|
"""Base class for AdGuard switches."""
|
|
|
|
def __init__(self, coordinator: AdGuardControlHubCoordinator, api: AdGuardHomeAPI):
|
|
super().__init__(coordinator)
|
|
self.api = api
|
|
self._attr_device_info = {
|
|
"identifiers": {(DOMAIN, f"{api.host}:{api.port}")},
|
|
"name": f"AdGuard Control Hub ({api.host})",
|
|
"manufacturer": MANUFACTURER,
|
|
"model": "AdGuard Home",
|
|
}
|
|
|
|
class AdGuardProtectionSwitch(AdGuardBaseSwitch):
|
|
"""Switch to control global AdGuard protection."""
|
|
|
|
def __init__(self, coordinator: AdGuardControlHubCoordinator, api: AdGuardHomeAPI):
|
|
super().__init__(coordinator, api)
|
|
self._attr_unique_id = f"{api.host}_{api.port}_protection"
|
|
self._attr_name = "AdGuard Protection"
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
return self.coordinator.protection_status.get("protection_enabled", False)
|
|
|
|
@property
|
|
def icon(self) -> str:
|
|
return ICON_PROTECTION if self.is_on else ICON_PROTECTION_OFF
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
await self.api.set_protection(True)
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
await self.api.set_protection(False)
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
class AdGuardClientSwitch(AdGuardBaseSwitch):
|
|
"""Switch to control client-specific protection."""
|
|
|
|
def __init__(self, coordinator: AdGuardControlHubCoordinator, api: AdGuardHomeAPI, client_name: str):
|
|
super().__init__(coordinator, api)
|
|
self.client_name = client_name
|
|
self._attr_unique_id = f"{api.host}_{api.port}_client_{client_name}"
|
|
self._attr_name = f"AdGuard {client_name}"
|
|
self._attr_icon = ICON_CLIENT
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
client = self.coordinator.clients.get(self.client_name, {})
|
|
return client.get("filtering_enabled", True)
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
# This would update client settings - simplified for basic functionality
|
|
_LOGGER.info("Would enable protection for %s", self.client_name)
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
# This would update client settings - simplified for basic functionality
|
|
_LOGGER.info("Would disable protection for %s", self.client_name)
|
|
await self.coordinator.async_request_refresh() |