Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for AdGuard Home sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 from datetime import timedelta
8 from typing import Any
9 
10 from adguardhome import AdGuardHome
11 
12 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
13 from homeassistant.const import PERCENTAGE, UnitOfTime
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import AdGuardConfigEntry, AdGuardData
18 from .const import DOMAIN
19 from .entity import AdGuardHomeEntity
20 
21 SCAN_INTERVAL = timedelta(seconds=300)
22 PARALLEL_UPDATES = 4
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Describes AdGuard Home sensor entity."""
28 
29  value_fn: Callable[[AdGuardHome], Coroutine[Any, Any, int | float]]
30 
31 
32 SENSORS: tuple[AdGuardHomeEntityDescription, ...] = (
34  key="dns_queries",
35  translation_key="dns_queries",
36  native_unit_of_measurement="queries",
37  value_fn=lambda adguard: adguard.stats.dns_queries(),
38  ),
40  key="blocked_filtering",
41  translation_key="dns_queries_blocked",
42  native_unit_of_measurement="queries",
43  value_fn=lambda adguard: adguard.stats.blocked_filtering(),
44  ),
46  key="blocked_percentage",
47  translation_key="dns_queries_blocked_ratio",
48  native_unit_of_measurement=PERCENTAGE,
49  value_fn=lambda adguard: adguard.stats.blocked_percentage(),
50  ),
52  key="blocked_parental",
53  translation_key="parental_control_blocked",
54  native_unit_of_measurement="requests",
55  value_fn=lambda adguard: adguard.stats.replaced_parental(),
56  ),
58  key="blocked_safebrowsing",
59  translation_key="safe_browsing_blocked",
60  native_unit_of_measurement="requests",
61  value_fn=lambda adguard: adguard.stats.replaced_safebrowsing(),
62  ),
64  key="enforced_safesearch",
65  translation_key="safe_searches_enforced",
66  native_unit_of_measurement="requests",
67  value_fn=lambda adguard: adguard.stats.replaced_safesearch(),
68  ),
70  key="average_speed",
71  translation_key="average_processing_speed",
72  native_unit_of_measurement=UnitOfTime.MILLISECONDS,
73  value_fn=lambda adguard: adguard.stats.avg_processing_time(),
74  ),
76  key="rules_count",
77  translation_key="rules_count",
78  native_unit_of_measurement="rules",
79  value_fn=lambda adguard: adguard.filtering.rules_count(allowlist=False),
80  entity_registry_enabled_default=False,
81  ),
82 )
83 
84 
86  hass: HomeAssistant,
87  entry: AdGuardConfigEntry,
88  async_add_entities: AddEntitiesCallback,
89 ) -> None:
90  """Set up AdGuard Home sensor based on a config entry."""
91  data = entry.runtime_data
92 
94  [AdGuardHomeSensor(data, entry, description) for description in SENSORS],
95  True,
96  )
97 
98 
100  """Defines a AdGuard Home sensor."""
101 
102  entity_description: AdGuardHomeEntityDescription
103 
104  def __init__(
105  self,
106  data: AdGuardData,
107  entry: AdGuardConfigEntry,
108  description: AdGuardHomeEntityDescription,
109  ) -> None:
110  """Initialize AdGuard Home sensor."""
111  super().__init__(data, entry)
112  self.entity_descriptionentity_description = description
113  self._attr_unique_id_attr_unique_id = "_".join(
114  [
115  DOMAIN,
116  self.adguardadguard.host,
117  str(self.adguardadguard.port),
118  "sensor",
119  description.key,
120  ]
121  )
122 
123  async def _adguard_update(self) -> None:
124  """Update AdGuard Home entity."""
125  value = await self.entity_descriptionentity_description.value_fn(self.adguardadguard)
126  self._attr_native_value_attr_native_value = value
127  if isinstance(value, float):
128  self._attr_native_value_attr_native_value = f"{value:.2f}"
None __init__(self, AdGuardData data, AdGuardConfigEntry entry, AdGuardHomeEntityDescription description)
Definition: sensor.py:109
None async_setup_entry(HomeAssistant hass, AdGuardConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:89