mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use EntityDescription - pi_hole (#54319)
This commit is contained in:
parent
38cb0553f3
commit
d79fc2c506
@ -1,6 +1,9 @@
|
||||
"""Constants for the pi_hole integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components.sensor import SensorEntityDescription
|
||||
from homeassistant.const import PERCENTAGE
|
||||
|
||||
DOMAIN = "pi_hole"
|
||||
@ -25,28 +28,60 @@ MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
|
||||
DATA_KEY_API = "api"
|
||||
DATA_KEY_COORDINATOR = "coordinator"
|
||||
|
||||
SENSOR_DICT = {
|
||||
"ads_blocked_today": ["Ads Blocked Today", "ads", "mdi:close-octagon-outline"],
|
||||
"ads_percentage_today": [
|
||||
"Ads Percentage Blocked Today",
|
||||
PERCENTAGE,
|
||||
"mdi:close-octagon-outline",
|
||||
],
|
||||
"clients_ever_seen": ["Seen Clients", "clients", "mdi:account-outline"],
|
||||
"dns_queries_today": [
|
||||
"DNS Queries Today",
|
||||
"queries",
|
||||
"mdi:comment-question-outline",
|
||||
],
|
||||
"domains_being_blocked": ["Domains Blocked", "domains", "mdi:block-helper"],
|
||||
"queries_cached": ["DNS Queries Cached", "queries", "mdi:comment-question-outline"],
|
||||
"queries_forwarded": [
|
||||
"DNS Queries Forwarded",
|
||||
"queries",
|
||||
"mdi:comment-question-outline",
|
||||
],
|
||||
"unique_clients": ["DNS Unique Clients", "clients", "mdi:account-outline"],
|
||||
"unique_domains": ["DNS Unique Domains", "domains", "mdi:domain"],
|
||||
}
|
||||
|
||||
SENSOR_LIST = list(SENSOR_DICT)
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key="ads_blocked_today",
|
||||
name="Ads Blocked Today",
|
||||
unit_of_measurement="ads",
|
||||
icon="mdi:close-octagon-outline",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="ads_percentage_today",
|
||||
name="Ads Percentage Blocked Today",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
icon="mdi:close-octagon-outline",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="clients_ever_seen",
|
||||
name="Seen Clients",
|
||||
unit_of_measurement="clients",
|
||||
icon="mdi:account-outline",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="dns_queries_today",
|
||||
name="DNS Queries Today",
|
||||
unit_of_measurement="queries",
|
||||
icon="mdi:comment-question-outline",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="domains_being_blocked",
|
||||
name="Domains Blocked",
|
||||
unit_of_measurement="domains",
|
||||
icon="mdi:block-helper",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="queries_cached",
|
||||
name="DNS Queries Cached",
|
||||
unit_of_measurement="queries",
|
||||
icon="mdi:comment-question-outline",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="queries_forwarded",
|
||||
name="DNS Queries Forwarded",
|
||||
unit_of_measurement="queries",
|
||||
icon="mdi:comment-question-outline",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="unique_clients",
|
||||
name="DNS Unique Clients",
|
||||
unit_of_measurement="clients",
|
||||
icon="mdi:account-outline",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="unique_domains",
|
||||
name="DNS Unique Domains",
|
||||
unit_of_measurement="domains",
|
||||
icon="mdi:domain",
|
||||
),
|
||||
)
|
||||
|
@ -5,7 +5,7 @@ from typing import Any
|
||||
|
||||
from hole import Hole
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
@ -18,8 +18,7 @@ from .const import (
|
||||
DATA_KEY_API,
|
||||
DATA_KEY_COORDINATOR,
|
||||
DOMAIN as PIHOLE_DOMAIN,
|
||||
SENSOR_DICT,
|
||||
SENSOR_LIST,
|
||||
SENSOR_TYPES,
|
||||
)
|
||||
|
||||
|
||||
@ -34,10 +33,10 @@ async def async_setup_entry(
|
||||
hole_data[DATA_KEY_API],
|
||||
hole_data[DATA_KEY_COORDINATOR],
|
||||
name,
|
||||
sensor_name,
|
||||
entry.entry_id,
|
||||
description,
|
||||
)
|
||||
for sensor_name in SENSOR_LIST
|
||||
for description in SENSOR_TYPES
|
||||
]
|
||||
async_add_entities(sensors, True)
|
||||
|
||||
@ -50,46 +49,23 @@ class PiHoleSensor(PiHoleEntity, SensorEntity):
|
||||
api: Hole,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
name: str,
|
||||
sensor_name: str,
|
||||
server_unique_id: str,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Pi-hole sensor."""
|
||||
super().__init__(api, coordinator, name, server_unique_id)
|
||||
self.entity_description = description
|
||||
|
||||
self._condition = sensor_name
|
||||
|
||||
variable_info = SENSOR_DICT[sensor_name]
|
||||
self._condition_name = variable_info[0]
|
||||
self._unit_of_measurement = variable_info[1]
|
||||
self._icon = variable_info[2]
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the sensor."""
|
||||
return f"{self._name} {self._condition_name}"
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return the unique id of the sensor."""
|
||||
return f"{self._server_unique_id}/{self._condition_name}"
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit the value is expressed in."""
|
||||
return self._unit_of_measurement
|
||||
self._attr_name = f"{name} {description.name}"
|
||||
self._attr_unique_id = f"{self._server_unique_id}/{description.name}"
|
||||
|
||||
@property
|
||||
def state(self) -> Any:
|
||||
"""Return the state of the device."""
|
||||
try:
|
||||
return round(self.api.data[self._condition], 2)
|
||||
return round(self.api.data[self.entity_description.key], 2)
|
||||
except TypeError:
|
||||
return self.api.data[self._condition]
|
||||
return self.api.data[self.entity_description.key]
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
|
Loading…
x
Reference in New Issue
Block a user