2019-04-03 17:40:03 +02:00
|
|
|
"""Support for getting statistical data from a Pi-hole system."""
|
2021-06-22 12:50:50 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from hole import Hole
|
2016-09-09 17:10:46 +02:00
|
|
|
|
2023-01-07 12:40:21 +01:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2021-06-22 12:50:50 +03:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-01-07 12:40:21 +01:00
|
|
|
from homeassistant.const import CONF_NAME, PERCENTAGE
|
2021-06-22 12:50:50 +03:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-03-21 11:40:06 +01:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-06-22 12:50:50 +03:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2020-07-18 14:19:01 +08:00
|
|
|
from . import PiHoleEntity
|
2023-01-08 13:25:37 +01:00
|
|
|
from .const import DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN as PIHOLE_DOMAIN
|
2023-01-07 12:40:21 +01:00
|
|
|
|
2023-01-07 14:06:33 +01:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="ads_blocked_today",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="ads_blocked_today",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="ads",
|
|
|
|
icon="mdi:close-octagon-outline",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="ads_percentage_today",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="ads_percentage_today",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
icon="mdi:close-octagon-outline",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="clients_ever_seen",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="clients_ever_seen",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="clients",
|
|
|
|
icon="mdi:account-outline",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="dns_queries_today",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="dns_queries_today",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="queries",
|
|
|
|
icon="mdi:comment-question-outline",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="domains_being_blocked",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="domains_being_blocked",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="domains",
|
|
|
|
icon="mdi:block-helper",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="queries_cached",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="queries_cached",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="queries",
|
|
|
|
icon="mdi:comment-question-outline",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="queries_forwarded",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="queries_forwarded",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="queries",
|
|
|
|
icon="mdi:comment-question-outline",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="unique_clients",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="unique_clients",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="clients",
|
|
|
|
icon="mdi:account-outline",
|
|
|
|
),
|
2023-01-07 14:06:33 +01:00
|
|
|
SensorEntityDescription(
|
2023-01-07 12:40:21 +01:00
|
|
|
key="unique_domains",
|
2023-04-03 14:00:27 +02:00
|
|
|
translation_key="unique_domains",
|
2023-01-07 12:40:21 +01:00
|
|
|
native_unit_of_measurement="domains",
|
|
|
|
icon="mdi:domain",
|
|
|
|
),
|
2019-09-03 16:18:06 -07:00
|
|
|
)
|
2018-06-04 18:49:26 +02:00
|
|
|
|
2016-09-09 17:10:46 +02:00
|
|
|
|
2021-06-22 12:50:50 +03:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-06-22 18:47:37 -07:00
|
|
|
"""Set up the Pi-hole sensor."""
|
|
|
|
name = entry.data[CONF_NAME]
|
2020-07-18 14:19:01 +08:00
|
|
|
hole_data = hass.data[PIHOLE_DOMAIN][entry.entry_id]
|
2020-05-13 06:25:06 -07:00
|
|
|
sensors = [
|
2020-06-22 18:47:37 -07:00
|
|
|
PiHoleSensor(
|
|
|
|
hole_data[DATA_KEY_API],
|
|
|
|
hole_data[DATA_KEY_COORDINATOR],
|
|
|
|
name,
|
|
|
|
entry.entry_id,
|
2021-08-09 23:38:58 +02:00
|
|
|
description,
|
2020-06-22 18:47:37 -07:00
|
|
|
)
|
2021-08-09 23:38:58 +02:00
|
|
|
for description in SENSOR_TYPES
|
2020-05-13 06:25:06 -07:00
|
|
|
]
|
2018-08-24 16:37:30 +02:00
|
|
|
async_add_entities(sensors, True)
|
2016-09-09 17:10:46 +02:00
|
|
|
|
|
|
|
|
2021-03-22 19:46:46 +01:00
|
|
|
class PiHoleSensor(PiHoleEntity, SensorEntity):
|
2018-06-04 18:49:26 +02:00
|
|
|
"""Representation of a Pi-hole sensor."""
|
2016-09-09 17:10:46 +02:00
|
|
|
|
2023-01-07 14:06:33 +01:00
|
|
|
entity_description: SensorEntityDescription
|
2023-04-03 14:00:27 +02:00
|
|
|
_attr_has_entity_name = True
|
2021-08-10 18:57:25 +02:00
|
|
|
|
2021-06-22 12:50:50 +03:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
api: Hole,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
name: str,
|
|
|
|
server_unique_id: str,
|
2023-01-07 14:06:33 +01:00
|
|
|
description: SensorEntityDescription,
|
2021-06-22 12:50:50 +03:00
|
|
|
) -> None:
|
2018-06-04 18:49:26 +02:00
|
|
|
"""Initialize a Pi-hole sensor."""
|
2020-07-18 14:19:01 +08:00
|
|
|
super().__init__(api, coordinator, name, server_unique_id)
|
2021-08-09 23:38:58 +02:00
|
|
|
self.entity_description = description
|
2020-07-18 14:19:01 +08:00
|
|
|
|
2023-04-03 14:00:27 +02:00
|
|
|
self._attr_unique_id = f"{self._server_unique_id}/{description.key}"
|
2016-09-09 17:10:46 +02:00
|
|
|
|
|
|
|
@property
|
2023-03-21 11:40:06 +01:00
|
|
|
def native_value(self) -> StateType:
|
2016-09-09 17:10:46 +02:00
|
|
|
"""Return the state of the device."""
|
2017-07-17 13:21:41 -04:00
|
|
|
try:
|
2023-03-21 11:40:06 +01:00
|
|
|
return round(self.api.data[self.entity_description.key], 2) # type: ignore[no-any-return]
|
2017-07-17 13:21:41 -04:00
|
|
|
except TypeError:
|
2023-03-21 11:40:06 +01:00
|
|
|
return self.api.data[self.entity_description.key] # type: ignore[no-any-return]
|