mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Add entity descriptions in AdGuard Home sensors (#75179)
This commit is contained in:
parent
72906bf154
commit
d004adf833
@ -138,8 +138,8 @@ class AdGuardHomeEntity(Entity):
|
|||||||
self,
|
self,
|
||||||
adguard: AdGuardHome,
|
adguard: AdGuardHome,
|
||||||
entry: ConfigEntry,
|
entry: ConfigEntry,
|
||||||
name: str,
|
name: str | None,
|
||||||
icon: str,
|
icon: str | None,
|
||||||
enabled_default: bool = True,
|
enabled_default: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the AdGuard Home entity."""
|
"""Initialize the AdGuard Home entity."""
|
||||||
@ -151,12 +151,12 @@ class AdGuardHomeEntity(Entity):
|
|||||||
self.adguard = adguard
|
self.adguard = adguard
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str | None:
|
||||||
"""Return the name of the entity."""
|
"""Return the name of the entity."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str | None:
|
||||||
"""Return the mdi icon of the entity."""
|
"""Return the mdi icon of the entity."""
|
||||||
return self._icon
|
return self._icon
|
||||||
|
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
"""Support for AdGuard Home sensors."""
|
"""Support for AdGuard Home sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable, Coroutine
|
||||||
|
from dataclasses import dataclass
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from adguardhome import AdGuardHome, AdGuardHomeConnectionError
|
from adguardhome import AdGuardHome, AdGuardHomeConnectionError
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import PERCENTAGE, TIME_MILLISECONDS
|
from homeassistant.const import PERCENTAGE, TIME_MILLISECONDS
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -19,6 +22,81 @@ SCAN_INTERVAL = timedelta(seconds=300)
|
|||||||
PARALLEL_UPDATES = 4
|
PARALLEL_UPDATES = 4
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AdGuardHomeEntityDescriptionMixin:
|
||||||
|
"""Mixin for required keys."""
|
||||||
|
|
||||||
|
value_fn: Callable[[AdGuardHome], Callable[[], Coroutine[Any, Any, int | float]]]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AdGuardHomeEntityDescription(
|
||||||
|
SensorEntityDescription, AdGuardHomeEntityDescriptionMixin
|
||||||
|
):
|
||||||
|
"""Describes AdGuard Home sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
|
SENSORS: tuple[AdGuardHomeEntityDescription, ...] = (
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="dns_queries",
|
||||||
|
name="DNS queries",
|
||||||
|
icon="mdi:magnify",
|
||||||
|
native_unit_of_measurement="queries",
|
||||||
|
value_fn=lambda adguard: adguard.stats.dns_queries,
|
||||||
|
),
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="blocked_filtering",
|
||||||
|
name="DNS queries blocked",
|
||||||
|
icon="mdi:magnify-close",
|
||||||
|
native_unit_of_measurement="queries",
|
||||||
|
value_fn=lambda adguard: adguard.stats.blocked_filtering,
|
||||||
|
),
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="blocked_percentage",
|
||||||
|
name="DNS queries blocked ratio",
|
||||||
|
icon="mdi:magnify-close",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
value_fn=lambda adguard: adguard.stats.blocked_percentage,
|
||||||
|
),
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="blocked_parental",
|
||||||
|
name="Parental control blocked",
|
||||||
|
icon="mdi:human-male-girl",
|
||||||
|
native_unit_of_measurement="requests",
|
||||||
|
value_fn=lambda adguard: adguard.stats.replaced_parental,
|
||||||
|
),
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="blocked_safebrowsing",
|
||||||
|
name="Safe browsing blocked",
|
||||||
|
icon="mdi:shield-half-full",
|
||||||
|
native_unit_of_measurement="requests",
|
||||||
|
value_fn=lambda adguard: adguard.stats.replaced_safebrowsing,
|
||||||
|
),
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="enforced_safesearch",
|
||||||
|
name="Safe searches enforced",
|
||||||
|
icon="mdi:shield-search",
|
||||||
|
native_unit_of_measurement="requests",
|
||||||
|
value_fn=lambda adguard: adguard.stats.replaced_safesearch,
|
||||||
|
),
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="average_speed",
|
||||||
|
name="Average processing speed",
|
||||||
|
icon="mdi:speedometer",
|
||||||
|
native_unit_of_measurement=TIME_MILLISECONDS,
|
||||||
|
value_fn=lambda adguard: adguard.stats.avg_processing_time,
|
||||||
|
),
|
||||||
|
AdGuardHomeEntityDescription(
|
||||||
|
key="rules_count",
|
||||||
|
name="Rules count",
|
||||||
|
icon="mdi:counter",
|
||||||
|
native_unit_of_measurement="rules",
|
||||||
|
value_fn=lambda adguard: adguard.stats.avg_processing_time,
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: ConfigEntry,
|
||||||
@ -34,215 +112,47 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id][DATA_ADGUARD_VERSION] = version
|
hass.data[DOMAIN][entry.entry_id][DATA_ADGUARD_VERSION] = version
|
||||||
|
|
||||||
sensors = [
|
async_add_entities(
|
||||||
AdGuardHomeDNSQueriesSensor(adguard, entry),
|
[AdGuardHomeSensor(adguard, entry, description) for description in SENSORS],
|
||||||
AdGuardHomeBlockedFilteringSensor(adguard, entry),
|
True,
|
||||||
AdGuardHomePercentageBlockedSensor(adguard, entry),
|
)
|
||||||
AdGuardHomeReplacedParentalSensor(adguard, entry),
|
|
||||||
AdGuardHomeReplacedSafeBrowsingSensor(adguard, entry),
|
|
||||||
AdGuardHomeReplacedSafeSearchSensor(adguard, entry),
|
|
||||||
AdGuardHomeAverageProcessingTimeSensor(adguard, entry),
|
|
||||||
AdGuardHomeRulesCountSensor(adguard, entry),
|
|
||||||
]
|
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
|
||||||
|
|
||||||
|
|
||||||
class AdGuardHomeSensor(AdGuardHomeDeviceEntity, SensorEntity):
|
class AdGuardHomeSensor(AdGuardHomeDeviceEntity, SensorEntity):
|
||||||
"""Defines a AdGuard Home sensor."""
|
"""Defines a AdGuard Home sensor."""
|
||||||
|
|
||||||
|
entity_description: AdGuardHomeEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
adguard: AdGuardHome,
|
adguard: AdGuardHome,
|
||||||
entry: ConfigEntry,
|
entry: ConfigEntry,
|
||||||
name: str,
|
description: AdGuardHomeEntityDescription,
|
||||||
icon: str,
|
|
||||||
measurement: str,
|
|
||||||
unit_of_measurement: str,
|
|
||||||
enabled_default: bool = True,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize AdGuard Home sensor."""
|
"""Initialize AdGuard Home sensor."""
|
||||||
self._state: int | str | None = None
|
self.entity_description = description
|
||||||
self._unit_of_measurement = unit_of_measurement
|
|
||||||
self.measurement = measurement
|
|
||||||
|
|
||||||
super().__init__(adguard, entry, name, icon, enabled_default)
|
self._attr_unique_id = "_".join(
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str:
|
|
||||||
"""Return the unique ID for this sensor."""
|
|
||||||
return "_".join(
|
|
||||||
[
|
[
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
self.adguard.host,
|
adguard.host,
|
||||||
str(self.adguard.port),
|
str(adguard.port),
|
||||||
"sensor",
|
"sensor",
|
||||||
self.measurement,
|
description.key,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self) -> int | str | None:
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self) -> str | None:
|
|
||||||
"""Return the unit this state is expressed in."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
|
|
||||||
class AdGuardHomeDNSQueriesSensor(AdGuardHomeSensor):
|
|
||||||
"""Defines a AdGuard Home DNS Queries sensor."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
adguard,
|
adguard,
|
||||||
entry,
|
entry,
|
||||||
"DNS queries",
|
description.name,
|
||||||
"mdi:magnify",
|
description.icon,
|
||||||
"dns_queries",
|
description.entity_registry_enabled_default,
|
||||||
"queries",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _adguard_update(self) -> None:
|
async def _adguard_update(self) -> None:
|
||||||
"""Update AdGuard Home entity."""
|
"""Update AdGuard Home entity."""
|
||||||
self._state = await self.adguard.stats.dns_queries()
|
value = await self.entity_description.value_fn(self.adguard)()
|
||||||
|
self._attr_native_value = value
|
||||||
|
if isinstance(value, float):
|
||||||
class AdGuardHomeBlockedFilteringSensor(AdGuardHomeSensor):
|
self._attr_native_value = f"{value:.2f}"
|
||||||
"""Defines a AdGuard Home blocked by filtering sensor."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
|
||||||
adguard,
|
|
||||||
entry,
|
|
||||||
"DNS queries blocked",
|
|
||||||
"mdi:magnify-close",
|
|
||||||
"blocked_filtering",
|
|
||||||
"queries",
|
|
||||||
enabled_default=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
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."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
|
||||||
adguard,
|
|
||||||
entry,
|
|
||||||
"DNS queries blocked ratio",
|
|
||||||
"mdi:magnify-close",
|
|
||||||
"blocked_percentage",
|
|
||||||
PERCENTAGE,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _adguard_update(self) -> None:
|
|
||||||
"""Update AdGuard Home entity."""
|
|
||||||
percentage = await self.adguard.stats.blocked_percentage()
|
|
||||||
self._state = f"{percentage:.2f}"
|
|
||||||
|
|
||||||
|
|
||||||
class AdGuardHomeReplacedParentalSensor(AdGuardHomeSensor):
|
|
||||||
"""Defines a AdGuard Home replaced by parental control sensor."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
|
||||||
adguard,
|
|
||||||
entry,
|
|
||||||
"Parental control blocked",
|
|
||||||
"mdi:human-male-girl",
|
|
||||||
"blocked_parental",
|
|
||||||
"requests",
|
|
||||||
)
|
|
||||||
|
|
||||||
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."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
|
||||||
adguard,
|
|
||||||
entry,
|
|
||||||
"Safe browsing blocked",
|
|
||||||
"mdi:shield-half-full",
|
|
||||||
"blocked_safebrowsing",
|
|
||||||
"requests",
|
|
||||||
)
|
|
||||||
|
|
||||||
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."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
|
||||||
adguard,
|
|
||||||
entry,
|
|
||||||
"Safe searches enforced",
|
|
||||||
"mdi:shield-search",
|
|
||||||
"enforced_safesearch",
|
|
||||||
"requests",
|
|
||||||
)
|
|
||||||
|
|
||||||
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."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
|
||||||
adguard,
|
|
||||||
entry,
|
|
||||||
"Average processing speed",
|
|
||||||
"mdi:speedometer",
|
|
||||||
"average_speed",
|
|
||||||
TIME_MILLISECONDS,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _adguard_update(self) -> None:
|
|
||||||
"""Update AdGuard Home entity."""
|
|
||||||
average = await self.adguard.stats.avg_processing_time()
|
|
||||||
self._state = f"{average:.2f}"
|
|
||||||
|
|
||||||
|
|
||||||
class AdGuardHomeRulesCountSensor(AdGuardHomeSensor):
|
|
||||||
"""Defines a AdGuard Home rules count sensor."""
|
|
||||||
|
|
||||||
def __init__(self, adguard: AdGuardHome, entry: ConfigEntry) -> None:
|
|
||||||
"""Initialize AdGuard Home sensor."""
|
|
||||||
super().__init__(
|
|
||||||
adguard,
|
|
||||||
entry,
|
|
||||||
"Rules count",
|
|
||||||
"mdi:counter",
|
|
||||||
"rules_count",
|
|
||||||
"rules",
|
|
||||||
enabled_default=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _adguard_update(self) -> None:
|
|
||||||
"""Update AdGuard Home entity."""
|
|
||||||
self._state = await self.adguard.filtering.rules_count(allowlist=False)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user