From c3716015c42d3867a8e6e08464f5f397e9245bbe Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sat, 7 Jan 2023 12:40:21 +0100 Subject: [PATCH] Move platform related stuff out of const.py in PI-Hole integration (#85237) move platform related stuff out of const.py --- .../components/pi_hole/binary_sensor.py | 76 +++++++++- homeassistant/components/pi_hole/const.py | 141 ------------------ homeassistant/components/pi_hole/sensor.py | 72 ++++++++- 3 files changed, 137 insertions(+), 152 deletions(-) diff --git a/homeassistant/components/pi_hole/binary_sensor.py b/homeassistant/components/pi_hole/binary_sensor.py index 7d0d9034fad..4aa391b567f 100644 --- a/homeassistant/components/pi_hole/binary_sensor.py +++ b/homeassistant/components/pi_hole/binary_sensor.py @@ -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"), + ), ) diff --git a/homeassistant/components/pi_hole/const.py b/homeassistant/components/pi_hole/const.py index a9bc5824ad9..1e545ad77db 100644 --- a/homeassistant/components/pi_hole/const.py +++ b/homeassistant/components/pi_hole/const.py @@ -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"), - ), -) diff --git a/homeassistant/components/pi_hole/sensor.py b/homeassistant/components/pi_hole/sensor.py index 0e231868647..c25a3502f78 100644 --- a/homeassistant/components/pi_hole/sensor.py +++ b/homeassistant/components/pi_hole/sensor.py @@ -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", + ), )