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