2019-06-02 05:13:14 +00:00
|
|
|
"""Support for AdGuard Home sensors."""
|
2021-03-06 22:19:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-06-02 05:13:14 +00:00
|
|
|
from datetime import timedelta
|
2021-03-06 22:19:03 +00:00
|
|
|
from typing import Callable
|
2019-06-02 05:13:14 +00:00
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
from adguardhome import AdGuardHome, AdGuardHomeConnectionError
|
2019-06-02 05:13:14 +00:00
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2019-06-02 05:13:14 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-09-05 19:09:14 +00:00
|
|
|
from homeassistant.const import PERCENTAGE, TIME_MILLISECONDS
|
2021-03-06 22:19:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-06-02 05:13:14 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2021-03-06 22:19:03 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from . import AdGuardHomeDeviceEntity
|
|
|
|
from .const import DATA_ADGUARD_CLIENT, DATA_ADGUARD_VERION, DOMAIN
|
2019-06-02 05:13:14 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=300)
|
|
|
|
PARALLEL_UPDATES = 4
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-03-06 22:19:03 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[list[Entity], bool], None],
|
2019-06-02 05:13:14 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up AdGuard Home sensor based on a config entry."""
|
|
|
|
adguard = hass.data[DOMAIN][DATA_ADGUARD_CLIENT]
|
|
|
|
|
|
|
|
try:
|
|
|
|
version = await adguard.version()
|
|
|
|
except AdGuardHomeConnectionError as exception:
|
|
|
|
raise PlatformNotReady from exception
|
|
|
|
|
|
|
|
hass.data[DOMAIN][DATA_ADGUARD_VERION] = version
|
|
|
|
|
|
|
|
sensors = [
|
|
|
|
AdGuardHomeDNSQueriesSensor(adguard),
|
|
|
|
AdGuardHomeBlockedFilteringSensor(adguard),
|
|
|
|
AdGuardHomePercentageBlockedSensor(adguard),
|
|
|
|
AdGuardHomeReplacedParentalSensor(adguard),
|
|
|
|
AdGuardHomeReplacedSafeBrowsingSensor(adguard),
|
|
|
|
AdGuardHomeReplacedSafeSearchSensor(adguard),
|
|
|
|
AdGuardHomeAverageProcessingTimeSensor(adguard),
|
|
|
|
AdGuardHomeRulesCountSensor(adguard),
|
|
|
|
]
|
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
|
|
|
|
|
|
|
|
2021-03-22 19:05:13 +00:00
|
|
|
class AdGuardHomeSensor(AdGuardHomeDeviceEntity, SensorEntity):
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Defines a AdGuard Home sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
2020-01-23 23:50:59 +00:00
|
|
|
self,
|
2021-03-06 22:19:03 +00:00
|
|
|
adguard: AdGuardHome,
|
2020-01-23 23:50:59 +00:00
|
|
|
name: str,
|
|
|
|
icon: str,
|
|
|
|
measurement: str,
|
|
|
|
unit_of_measurement: str,
|
|
|
|
enabled_default: bool = True,
|
2019-06-02 05:13:14 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = unit_of_measurement
|
|
|
|
self.measurement = measurement
|
|
|
|
|
2020-01-23 23:50:59 +00:00
|
|
|
super().__init__(adguard, name, icon, enabled_default)
|
2019-06-02 05:13:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "_".join(
|
2019-06-02 05:13:14 +00:00
|
|
|
[
|
|
|
|
DOMAIN,
|
|
|
|
self.adguard.host,
|
|
|
|
str(self.adguard.port),
|
2019-07-31 19:25:30 +00:00
|
|
|
"sensor",
|
2019-06-02 05:13:14 +00:00
|
|
|
self.measurement,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-03-06 22:19:03 +00:00
|
|
|
def state(self) -> str | None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
2021-03-06 22:19:03 +00:00
|
|
|
def unit_of_measurement(self) -> str | None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Return the unit this state is expressed in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomeDNSQueriesSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home DNS Queries sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
2019-07-31 19:25:30 +00:00
|
|
|
adguard, "AdGuard DNS Queries", "mdi:magnify", "dns_queries", "queries"
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
|
|
|
self._state = await self.adguard.stats.dns_queries()
|
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomeBlockedFilteringSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home blocked by filtering sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
|
|
|
adguard,
|
2019-07-31 19:25:30 +00:00
|
|
|
"AdGuard DNS Queries Blocked",
|
|
|
|
"mdi:magnify-close",
|
|
|
|
"blocked_filtering",
|
|
|
|
"queries",
|
2020-01-23 23:50:59 +00:00
|
|
|
enabled_default=False,
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
|
|
|
self._state = await self.adguard.stats.blocked_filtering()
|
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomePercentageBlockedSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home blocked percentage sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
|
|
|
adguard,
|
2019-07-31 19:25:30 +00:00
|
|
|
"AdGuard DNS Queries Blocked Ratio",
|
|
|
|
"mdi:magnify-close",
|
|
|
|
"blocked_percentage",
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
|
|
|
percentage = await self.adguard.stats.blocked_percentage()
|
2019-09-03 14:11:36 +00:00
|
|
|
self._state = f"{percentage:.2f}"
|
2019-06-02 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomeReplacedParentalSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home replaced by parental control sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
|
|
|
adguard,
|
2019-07-31 19:25:30 +00:00
|
|
|
"AdGuard Parental Control Blocked",
|
|
|
|
"mdi:human-male-girl",
|
|
|
|
"blocked_parental",
|
|
|
|
"requests",
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
|
|
|
self._state = await self.adguard.stats.replaced_parental()
|
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomeReplacedSafeBrowsingSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home replaced by safe browsing sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
|
|
|
adguard,
|
2019-07-31 19:25:30 +00:00
|
|
|
"AdGuard Safe Browsing Blocked",
|
|
|
|
"mdi:shield-half-full",
|
|
|
|
"blocked_safebrowsing",
|
|
|
|
"requests",
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
|
|
|
self._state = await self.adguard.stats.replaced_safebrowsing()
|
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomeReplacedSafeSearchSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home replaced by safe search sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
|
|
|
adguard,
|
2019-12-24 13:14:58 +00:00
|
|
|
"AdGuard Safe Searches Enforced",
|
2019-07-31 19:25:30 +00:00
|
|
|
"mdi:shield-search",
|
|
|
|
"enforced_safesearch",
|
|
|
|
"requests",
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
|
|
|
self._state = await self.adguard.stats.replaced_safesearch()
|
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomeAverageProcessingTimeSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home average processing time sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
|
|
|
adguard,
|
2019-07-31 19:25:30 +00:00
|
|
|
"AdGuard Average Processing Speed",
|
|
|
|
"mdi:speedometer",
|
|
|
|
"average_speed",
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_MILLISECONDS,
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
|
|
|
average = await self.adguard.stats.avg_processing_time()
|
2019-09-03 14:11:36 +00:00
|
|
|
self._state = f"{average:.2f}"
|
2019-06-02 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdGuardHomeRulesCountSensor(AdGuardHomeSensor):
|
|
|
|
"""Defines a AdGuard Home rules count sensor."""
|
|
|
|
|
2021-03-06 22:19:03 +00:00
|
|
|
def __init__(self, adguard: AdGuardHome) -> None:
|
2019-06-02 05:13:14 +00:00
|
|
|
"""Initialize AdGuard Home sensor."""
|
|
|
|
super().__init__(
|
2020-01-23 23:50:59 +00:00
|
|
|
adguard,
|
|
|
|
"AdGuard Rules Count",
|
|
|
|
"mdi:counter",
|
|
|
|
"rules_count",
|
|
|
|
"rules",
|
|
|
|
enabled_default=False,
|
2019-06-02 05:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _adguard_update(self) -> None:
|
|
|
|
"""Update AdGuard Home entity."""
|
2021-03-11 23:37:34 +00:00
|
|
|
self._state = await self.adguard.filtering.rules_count(allowlist=False)
|