Move platform related stuff out of const.py in PI-Hole integration (#85237)

move platform related stuff out of const.py
This commit is contained in:
Michael 2023-01-07 12:40:21 +01:00 committed by GitHub
parent ee3ab45012
commit c3716015c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 152 deletions

View file

@ -1,11 +1,17 @@
"""Support for getting status from a Pi-hole system."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from hole import Hole
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
@ -13,12 +19,68 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from . import PiHoleEntity
from .const import (
BINARY_SENSOR_TYPES,
DATA_KEY_API,
DATA_KEY_COORDINATOR,
DOMAIN as PIHOLE_DOMAIN,
PiHoleBinarySensorEntityDescription,
from .const import DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN as PIHOLE_DOMAIN
@dataclass
class RequiredPiHoleBinaryDescription:
"""Represent the required attributes of the PiHole binary description."""
state_value: Callable[[Hole], bool]
@dataclass
class PiHoleBinarySensorEntityDescription(
BinarySensorEntityDescription, RequiredPiHoleBinaryDescription
):
"""Describes PiHole binary sensor entity."""
extra_value: Callable[[Hole], dict[str, Any] | None] = lambda api: None
BINARY_SENSOR_TYPES: tuple[PiHoleBinarySensorEntityDescription, ...] = (
PiHoleBinarySensorEntityDescription(
# Deprecated, scheduled to be removed in 2022.6
key="core_update_available",
name="Core Update Available",
entity_registry_enabled_default=False,
device_class=BinarySensorDeviceClass.UPDATE,
extra_value=lambda api: {
"current_version": api.versions["core_current"],
"latest_version": api.versions["core_latest"],
},
state_value=lambda api: bool(api.versions["core_update"]),
),
PiHoleBinarySensorEntityDescription(
# Deprecated, scheduled to be removed in 2022.6
key="web_update_available",
name="Web Update Available",
entity_registry_enabled_default=False,
device_class=BinarySensorDeviceClass.UPDATE,
extra_value=lambda api: {
"current_version": api.versions["web_current"],
"latest_version": api.versions["web_latest"],
},
state_value=lambda api: bool(api.versions["web_update"]),
),
PiHoleBinarySensorEntityDescription(
# Deprecated, scheduled to be removed in 2022.6
key="ftl_update_available",
name="FTL Update Available",
entity_registry_enabled_default=False,
device_class=BinarySensorDeviceClass.UPDATE,
extra_value=lambda api: {
"current_version": api.versions["FTL_current"],
"latest_version": api.versions["FTL_latest"],
},
state_value=lambda api: bool(api.versions["FTL_update"]),
),
PiHoleBinarySensorEntityDescription(
key="status",
name="Status",
icon="mdi:pi-hole",
state_value=lambda api: bool(api.data.get("status") == "enabled"),
),
)

View file

@ -1,19 +1,5 @@
"""Constants for the pi_hole integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from datetime import timedelta
from typing import Any
from hole import Hole
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntityDescription,
)
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.const import PERCENTAGE
DOMAIN = "pi_hole"
@ -34,130 +20,3 @@ MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
DATA_KEY_API = "api"
DATA_KEY_COORDINATOR = "coordinator"
@dataclass
class PiHoleSensorEntityDescription(SensorEntityDescription):
"""Describes PiHole sensor entity."""
icon: str = "mdi:pi-hole"
SENSOR_TYPES: tuple[PiHoleSensorEntityDescription, ...] = (
PiHoleSensorEntityDescription(
key="ads_blocked_today",
name="Ads Blocked Today",
native_unit_of_measurement="ads",
icon="mdi:close-octagon-outline",
),
PiHoleSensorEntityDescription(
key="ads_percentage_today",
name="Ads Percentage Blocked Today",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:close-octagon-outline",
),
PiHoleSensorEntityDescription(
key="clients_ever_seen",
name="Seen Clients",
native_unit_of_measurement="clients",
icon="mdi:account-outline",
),
PiHoleSensorEntityDescription(
key="dns_queries_today",
name="DNS Queries Today",
native_unit_of_measurement="queries",
icon="mdi:comment-question-outline",
),
PiHoleSensorEntityDescription(
key="domains_being_blocked",
name="Domains Blocked",
native_unit_of_measurement="domains",
icon="mdi:block-helper",
),
PiHoleSensorEntityDescription(
key="queries_cached",
name="DNS Queries Cached",
native_unit_of_measurement="queries",
icon="mdi:comment-question-outline",
),
PiHoleSensorEntityDescription(
key="queries_forwarded",
name="DNS Queries Forwarded",
native_unit_of_measurement="queries",
icon="mdi:comment-question-outline",
),
PiHoleSensorEntityDescription(
key="unique_clients",
name="DNS Unique Clients",
native_unit_of_measurement="clients",
icon="mdi:account-outline",
),
PiHoleSensorEntityDescription(
key="unique_domains",
name="DNS Unique Domains",
native_unit_of_measurement="domains",
icon="mdi:domain",
),
)
@dataclass
class RequiredPiHoleBinaryDescription:
"""Represent the required attributes of the PiHole binary description."""
state_value: Callable[[Hole], bool]
@dataclass
class PiHoleBinarySensorEntityDescription(
BinarySensorEntityDescription, RequiredPiHoleBinaryDescription
):
"""Describes PiHole binary sensor entity."""
extra_value: Callable[[Hole], dict[str, Any] | None] = lambda api: None
BINARY_SENSOR_TYPES: tuple[PiHoleBinarySensorEntityDescription, ...] = (
PiHoleBinarySensorEntityDescription(
# Deprecated, scheduled to be removed in 2022.6
key="core_update_available",
name="Core Update Available",
entity_registry_enabled_default=False,
device_class=BinarySensorDeviceClass.UPDATE,
extra_value=lambda api: {
"current_version": api.versions["core_current"],
"latest_version": api.versions["core_latest"],
},
state_value=lambda api: bool(api.versions["core_update"]),
),
PiHoleBinarySensorEntityDescription(
# Deprecated, scheduled to be removed in 2022.6
key="web_update_available",
name="Web Update Available",
entity_registry_enabled_default=False,
device_class=BinarySensorDeviceClass.UPDATE,
extra_value=lambda api: {
"current_version": api.versions["web_current"],
"latest_version": api.versions["web_latest"],
},
state_value=lambda api: bool(api.versions["web_update"]),
),
PiHoleBinarySensorEntityDescription(
# Deprecated, scheduled to be removed in 2022.6
key="ftl_update_available",
name="FTL Update Available",
entity_registry_enabled_default=False,
device_class=BinarySensorDeviceClass.UPDATE,
extra_value=lambda api: {
"current_version": api.versions["FTL_current"],
"latest_version": api.versions["FTL_latest"],
},
state_value=lambda api: bool(api.versions["FTL_update"]),
),
PiHoleBinarySensorEntityDescription(
key="status",
name="Status",
icon="mdi:pi-hole",
state_value=lambda api: bool(api.data.get("status") == "enabled"),
),
)

View file

@ -1,13 +1,14 @@
"""Support for getting statistical data from a Pi-hole system."""
from __future__ import annotations
from dataclasses import dataclass
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.const import CONF_NAME, PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
@ -18,8 +19,71 @@ from .const import (
DATA_KEY_API,
DATA_KEY_COORDINATOR,
DOMAIN as PIHOLE_DOMAIN,
SENSOR_TYPES,
PiHoleSensorEntityDescription,
)
@dataclass
class PiHoleSensorEntityDescription(SensorEntityDescription):
"""Describes PiHole sensor entity."""
icon: str = "mdi:pi-hole"
SENSOR_TYPES: tuple[PiHoleSensorEntityDescription, ...] = (
PiHoleSensorEntityDescription(
key="ads_blocked_today",
name="Ads Blocked Today",
native_unit_of_measurement="ads",
icon="mdi:close-octagon-outline",
),
PiHoleSensorEntityDescription(
key="ads_percentage_today",
name="Ads Percentage Blocked Today",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:close-octagon-outline",
),
PiHoleSensorEntityDescription(
key="clients_ever_seen",
name="Seen Clients",
native_unit_of_measurement="clients",
icon="mdi:account-outline",
),
PiHoleSensorEntityDescription(
key="dns_queries_today",
name="DNS Queries Today",
native_unit_of_measurement="queries",
icon="mdi:comment-question-outline",
),
PiHoleSensorEntityDescription(
key="domains_being_blocked",
name="Domains Blocked",
native_unit_of_measurement="domains",
icon="mdi:block-helper",
),
PiHoleSensorEntityDescription(
key="queries_cached",
name="DNS Queries Cached",
native_unit_of_measurement="queries",
icon="mdi:comment-question-outline",
),
PiHoleSensorEntityDescription(
key="queries_forwarded",
name="DNS Queries Forwarded",
native_unit_of_measurement="queries",
icon="mdi:comment-question-outline",
),
PiHoleSensorEntityDescription(
key="unique_clients",
name="DNS Unique Clients",
native_unit_of_measurement="clients",
icon="mdi:account-outline",
),
PiHoleSensorEntityDescription(
key="unique_domains",
name="DNS Unique Domains",
native_unit_of_measurement="domains",
icon="mdi:domain",
),
)