1 """The pi_hole component."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
9 from hole.exceptions
import HoleError
27 from .const
import CONF_STATISTICS_ONLY, DOMAIN, MIN_TIME_BETWEEN_UPDATES
29 _LOGGER = logging.getLogger(__name__)
33 Platform.BINARY_SENSOR,
39 type PiHoleConfigEntry = ConfigEntry[PiHoleData]
44 """Runtime data definition."""
47 coordinator: DataUpdateCoordinator[
None]
51 """Set up Pi-hole entry."""
52 name = entry.data[CONF_NAME]
53 host = entry.data[CONF_HOST]
54 use_tls = entry.data[CONF_SSL]
55 verify_tls = entry.data[CONF_VERIFY_SSL]
56 location = entry.data[CONF_LOCATION]
57 api_key = entry.data.get(CONF_API_KEY,
"")
60 if CONF_STATISTICS_ONLY
in entry.data:
61 entry_data = entry.data.copy()
62 entry_data.pop(CONF_STATISTICS_ONLY)
63 hass.config_entries.async_update_entry(entry, data=entry_data)
65 _LOGGER.debug(
"Setting up %s integration with host %s", DOMAIN, host)
68 "Core Update Available":
"core_update_available",
69 "Web Update Available":
"web_update_available",
70 "FTL Update Available":
"ftl_update_available",
72 "Ads Blocked Today":
"ads_blocked_today",
73 "Ads Percentage Blocked Today":
"ads_percentage_today",
74 "Seen Clients":
"clients_ever_seen",
75 "DNS Queries Today":
"dns_queries_today",
76 "Domains Blocked":
"domains_being_blocked",
77 "DNS Queries Cached":
"queries_cached",
78 "DNS Queries Forwarded":
"queries_forwarded",
79 "DNS Unique Clients":
"unique_clients",
80 "DNS Unique Domains":
"unique_domains",
85 entity_entry: er.RegistryEntry,
86 ) -> dict[str, str] |
None:
87 """Update unique ID of entity entry."""
88 unique_id_parts = entity_entry.unique_id.split(
"/")
89 if len(unique_id_parts) == 2
and unique_id_parts[1]
in name_to_key:
90 name = unique_id_parts[1]
91 new_unique_id = entity_entry.unique_id.replace(name, name_to_key[name])
92 _LOGGER.debug(
"Migrate %s to %s", entity_entry.unique_id, new_unique_id)
93 return {
"new_unique_id": new_unique_id}
97 await er.async_migrate_entries(hass, entry.entry_id, update_unique_id)
108 async
def async_update_data() -> None:
109 """Fetch data from API endpoint."""
112 await api.get_versions()
113 except HoleError
as err:
114 raise UpdateFailed(f
"Failed to communicate with API: {err}")
from err
115 if not isinstance(api.data, dict):
116 raise ConfigEntryAuthFailed
123 update_method=async_update_data,
124 update_interval=MIN_TIME_BETWEEN_UPDATES,
127 await coordinator.async_config_entry_first_refresh()
129 entry.runtime_data =
PiHoleData(api, coordinator)
131 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
137 """Unload Pi-hole entry."""
138 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
dict[str, str]|None update_unique_id(er.RegistryEntry entity_entry, str unique_id)
bool async_setup_entry(HomeAssistant hass, PiHoleConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)