78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
"""Binary sensor platform for AdGuard Control Hub integration."""
|
|
import logging
|
|
from typing import Any
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass
|
|
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, MANUFACTURER, ICON_PROTECTION, ICON_PROTECTION_OFF
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up AdGuard Control Hub binary sensor platform."""
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
|
|
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
|
|
|
|
entities = [
|
|
AdGuardProtectionBinarySensor(coordinator, api),
|
|
]
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
class AdGuardBaseBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|
"""Base class for AdGuard binary sensors."""
|
|
|
|
def __init__(self, coordinator: AdGuardControlHubCoordinator, api: AdGuardHomeAPI):
|
|
"""Initialize the binary sensor."""
|
|
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 AdGuardProtectionBinarySensor(AdGuardBaseBinarySensor):
|
|
"""Binary sensor to show AdGuard protection status."""
|
|
|
|
def __init__(self, coordinator: AdGuardControlHubCoordinator, api: AdGuardHomeAPI):
|
|
"""Initialize the binary sensor."""
|
|
super().__init__(coordinator, api)
|
|
self._attr_unique_id = f"{api.host}_{api.port}_protection_enabled"
|
|
self._attr_name = "AdGuard Protection Status"
|
|
self._attr_device_class = BinarySensorDeviceClass.RUNNING
|
|
|
|
@property
|
|
def is_on(self) -> bool | None:
|
|
"""Return true if protection is enabled."""
|
|
return self.coordinator.protection_status.get("protection_enabled", False)
|
|
|
|
@property
|
|
def icon(self) -> str:
|
|
"""Return the icon for the binary sensor."""
|
|
return ICON_PROTECTION if self.is_on else ICON_PROTECTION_OFF
|
|
|
|
@property
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
|
"""Return additional state attributes."""
|
|
status = self.coordinator.protection_status
|
|
return {
|
|
"dns_port": status.get("dns_port", "N/A"),
|
|
"version": status.get("version", "N/A"),
|
|
"running": status.get("running", False),
|
|
}
|