166
custom_components/adguard_hub/binary_sensor.py
Normal file
166
custom_components/adguard_hub/binary_sensor.py
Normal file
@@ -0,0 +1,166 @@
|
||||
"""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),
|
||||
AdGuardFilteringBinarySensor(coordinator, api),
|
||||
AdGuardSafeBrowsingBinarySensor(coordinator, api),
|
||||
AdGuardParentalControlBinarySensor(coordinator, api),
|
||||
AdGuardSafeSearchBinarySensor(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"),
|
||||
"http_port": status.get("http_port", "N/A"),
|
||||
"version": status.get("version", "N/A"),
|
||||
"running": status.get("running", False),
|
||||
}
|
||||
|
||||
|
||||
class AdGuardFilteringBinarySensor(AdGuardBaseBinarySensor):
|
||||
"""Binary sensor to show DNS filtering 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}_filtering_enabled"
|
||||
self._attr_name = "AdGuard DNS Filtering"
|
||||
self._attr_device_class = BinarySensorDeviceClass.RUNNING
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if DNS filtering is enabled."""
|
||||
return self.coordinator.protection_status.get("filtering_enabled", False)
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon for the binary sensor."""
|
||||
return "mdi:dns" if self.is_on else "mdi:dns-off"
|
||||
|
||||
|
||||
class AdGuardSafeBrowsingBinarySensor(AdGuardBaseBinarySensor):
|
||||
"""Binary sensor to show Safe Browsing 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}_safebrowsing_enabled"
|
||||
self._attr_name = "AdGuard Safe Browsing"
|
||||
self._attr_device_class = BinarySensorDeviceClass.SAFETY
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if Safe Browsing is enabled."""
|
||||
return self.coordinator.protection_status.get("safebrowsing_enabled", False)
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon for the binary sensor."""
|
||||
return "mdi:security" if self.is_on else "mdi:security-off"
|
||||
|
||||
|
||||
class AdGuardParentalControlBinarySensor(AdGuardBaseBinarySensor):
|
||||
"""Binary sensor to show Parental Control 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}_parental_enabled"
|
||||
self._attr_name = "AdGuard Parental Control"
|
||||
self._attr_device_class = BinarySensorDeviceClass.SAFETY
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if Parental Control is enabled."""
|
||||
return self.coordinator.protection_status.get("parental_enabled", False)
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon for the binary sensor."""
|
||||
return "mdi:account-child" if self.is_on else "mdi:account-child-outline"
|
||||
|
||||
|
||||
class AdGuardSafeSearchBinarySensor(AdGuardBaseBinarySensor):
|
||||
"""Binary sensor to show Safe Search 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}_safesearch_enabled"
|
||||
self._attr_name = "AdGuard Safe Search"
|
||||
self._attr_device_class = BinarySensorDeviceClass.SAFETY
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if Safe Search is enabled."""
|
||||
return self.coordinator.protection_status.get("safesearch_enabled", False)
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon for the binary sensor."""
|
||||
return "mdi:search-web" if self.is_on else "mdi:web-off"
|
Reference in New Issue
Block a user