Files
project-work/home-assistant/homeassistant/components/pi_hole/__init__.py
2026-11-19 15:15:32 -05:00

139 lines
4.2 KiB
Python

"""The pi_hole component."""
from __future__ import annotations
from dataclasses import dataclass
import logging
from hole import Hole
from hole.exceptions import HoleError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_API_KEY,
CONF_HOST,
CONF_LOCATION,
CONF_NAME,
CONF_SSL,
CONF_VERIFY_SSL,
Platform,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import CONF_STATISTICS_ONLY, DOMAIN, MIN_TIME_BETWEEN_UPDATES
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.SENSOR,
Platform.SWITCH,
Platform.UPDATE,
]
type PiHoleConfigEntry = ConfigEntry[PiHoleData]
@dataclass
class PiHoleData:
"""Runtime data definition."""
api: Hole
coordinator: DataUpdateCoordinator[None]
async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bool:
"""Set up Pi-hole entry."""
name = entry.data[CONF_NAME]
host = entry.data[CONF_HOST]
use_tls = entry.data[CONF_SSL]
verify_tls = entry.data[CONF_VERIFY_SSL]
location = entry.data[CONF_LOCATION]
api_key = entry.data.get(CONF_API_KEY, "")
# remove obsolet CONF_STATISTICS_ONLY from entry.data
if CONF_STATISTICS_ONLY in entry.data:
entry_data = entry.data.copy()
entry_data.pop(CONF_STATISTICS_ONLY)
hass.config_entries.async_update_entry(entry, data=entry_data)
_LOGGER.debug("Setting up %s integration with host %s", DOMAIN, host)
name_to_key = {
"Core Update Available": "core_update_available",
"Web Update Available": "web_update_available",
"FTL Update Available": "ftl_update_available",
"Status": "status",
"Ads Blocked Today": "ads_blocked_today",
"Ads Percentage Blocked Today": "ads_percentage_today",
"Seen Clients": "clients_ever_seen",
"DNS Queries Today": "dns_queries_today",
"Domains Blocked": "domains_being_blocked",
"DNS Queries Cached": "queries_cached",
"DNS Queries Forwarded": "queries_forwarded",
"DNS Unique Clients": "unique_clients",
"DNS Unique Domains": "unique_domains",
}
@callback
def update_unique_id(
entity_entry: er.RegistryEntry,
) -> dict[str, str] | None:
"""Update unique ID of entity entry."""
unique_id_parts = entity_entry.unique_id.split("/")
if len(unique_id_parts) == 2 and unique_id_parts[1] in name_to_key:
name = unique_id_parts[1]
new_unique_id = entity_entry.unique_id.replace(name, name_to_key[name])
_LOGGER.debug("Migrate %s to %s", entity_entry.unique_id, new_unique_id)
return {"new_unique_id": new_unique_id}
return None
await er.async_migrate_entries(hass, entry.entry_id, update_unique_id)
session = async_get_clientsession(hass, verify_tls)
api = Hole(
host,
session,
location=location,
tls=use_tls,
api_token=api_key,
)
async def async_update_data() -> None:
"""Fetch data from API endpoint."""
try:
await api.get_data()
await api.get_versions()
except HoleError as err:
raise UpdateFailed(f"Failed to communicate with API: {err}") from err
if not isinstance(api.data, dict):
raise ConfigEntryAuthFailed
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
config_entry=entry,
name=name,
update_method=async_update_data,
update_interval=MIN_TIME_BETWEEN_UPDATES,
)
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = PiHoleData(api, coordinator)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload Pi-hole entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)