Make api_version runtime_data in pi_hole (#148238)

This commit is contained in:
Josef Zweck 2025-07-08 11:46:13 +02:00 committed by GitHub
parent f58c76c883
commit 7541e266da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 15 additions and 26 deletions

View File

@ -12,7 +12,6 @@ from hole.exceptions import HoleConnectionError, HoleError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_API_KEY,
CONF_API_VERSION,
CONF_HOST,
CONF_LOCATION,
CONF_NAME,
@ -52,13 +51,13 @@ class PiHoleData:
api: Hole
coordinator: DataUpdateCoordinator[None]
api_version: int
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]
version = entry.data.get(CONF_API_VERSION)
# remove obsolet CONF_STATISTICS_ONLY from entry.data
if CONF_STATISTICS_ONLY in entry.data:
@ -100,15 +99,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bo
await er.async_migrate_entries(hass, entry.entry_id, update_unique_id)
if version is None:
_LOGGER.debug(
"No API version specified, determining Pi-hole API version for %s", host
)
version = await determine_api_version(hass, dict(entry.data))
_LOGGER.debug("Pi-hole API version determined: %s", version)
hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_API_VERSION: version}
)
_LOGGER.debug("Determining Pi-hole API version for %s", host)
version = await determine_api_version(hass, dict(entry.data))
_LOGGER.debug("Pi-hole API version determined: %s", version)
# Once API version 5 is deprecated we should instantiate Hole directly
api = api_by_version(hass, dict(entry.data), version)
@ -151,7 +145,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bo
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = PiHoleData(api, coordinator)
entry.runtime_data = PiHoleData(api, coordinator, version)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

View File

@ -12,7 +12,6 @@ import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import (
CONF_API_KEY,
CONF_API_VERSION,
CONF_HOST,
CONF_LOCATION,
CONF_NAME,
@ -145,7 +144,6 @@ class PiHoleFlowHandler(ConfigFlow, domain=DOMAIN):
try:
await pi_hole.authenticate()
_LOGGER.debug("Success authenticating with pihole API version: %s", 6)
self._config[CONF_API_VERSION] = 6
except HoleError:
_LOGGER.debug("Failed authenticating with pihole API version: %s", 6)
return {CONF_API_KEY: "invalid_auth"}
@ -171,7 +169,6 @@ class PiHoleFlowHandler(ConfigFlow, domain=DOMAIN):
"Success connecting to, but necessarily authenticating with, pihole, API version is: %s",
5,
)
self._config[CONF_API_VERSION] = 5
# the v5 API returns an empty list to unauthenticated requests.
if not isinstance(pi_hole.data, dict):
_LOGGER.debug(

View File

@ -8,7 +8,7 @@ from typing import Any
from hole import Hole
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import CONF_API_VERSION, CONF_NAME, PERCENTAGE
from homeassistant.const import CONF_NAME, PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType
@ -133,7 +133,7 @@ async def async_setup_entry(
description,
)
for description in (
SENSOR_TYPES if entry.data[CONF_API_VERSION] == 5 else SENSOR_TYPES_V6
SENSOR_TYPES if hole_data.api_version == 5 else SENSOR_TYPES_V6
)
]
async_add_entities(sensors, True)

View File

@ -185,7 +185,6 @@ CONFIG_ENTRY_WITH_API_KEY = {
CONF_API_KEY: API_KEY,
CONF_SSL: SSL,
CONF_VERIFY_SSL: VERIFY_SSL,
CONF_API_VERSION: API_VERSION,
}
CONFIG_ENTRY_WITHOUT_API_KEY = {
@ -194,7 +193,6 @@ CONFIG_ENTRY_WITHOUT_API_KEY = {
CONF_NAME: NAME,
CONF_SSL: SSL,
CONF_VERIFY_SSL: VERIFY_SSL,
CONF_API_VERSION: API_VERSION,
}
SWITCH_ENTITY_ID = "switch.pi_hole"

View File

@ -3,7 +3,7 @@
from homeassistant.components import pi_hole
from homeassistant.components.pi_hole.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_API_KEY, CONF_API_VERSION
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -104,7 +104,7 @@ async def test_flow_user_with_api_key_v5(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == NAME
assert result["data"] == {**CONFIG_ENTRY_WITH_API_KEY, CONF_API_VERSION: 5}
assert result["data"] == {**CONFIG_ENTRY_WITH_API_KEY}
mock_setup.assert_called_once()
# duplicated server
@ -148,7 +148,7 @@ async def test_flow_reauth(hass: HomeAssistant) -> None:
mocked_hole = _create_mocked_hole(has_data=False, api_version=5)
entry = MockConfigEntry(
domain=pi_hole.DOMAIN,
data={**CONFIG_DATA_DEFAULTS, CONF_API_VERSION: 5, CONF_API_KEY: "oldkey"},
data={**CONFIG_DATA_DEFAULTS, CONF_API_KEY: "oldkey"},
)
entry.add_to_hass(hass)
with _patch_init_hole(mocked_hole), _patch_config_flow_hole(mocked_hole):

View File

@ -51,7 +51,7 @@ async def test_setup_api_v6(
entry.add_to_hass(hass)
with _patch_init_hole(mocked_hole) as patched_init_hole:
assert await hass.config_entries.async_setup(entry.entry_id)
patched_init_hole.assert_called_once_with(
patched_init_hole.assert_called_with(
host=config_entry_data[CONF_HOST],
session=ANY,
password=expected_api_token,
@ -78,7 +78,7 @@ async def test_setup_api_v5(
entry.add_to_hass(hass)
with _patch_init_hole(mocked_hole) as patched_init_hole:
assert await hass.config_entries.async_setup(entry.entry_id)
patched_init_hole.assert_called_once_with(
patched_init_hole.assert_called_with(
host=config_entry_data[CONF_HOST],
session=ANY,
api_token=expected_api_token,
@ -206,7 +206,7 @@ async def test_setup_without_api_version(hass: HomeAssistant) -> None:
with _patch_init_hole(mocked_hole):
assert await hass.config_entries.async_setup(entry.entry_id)
assert entry.data[CONF_API_VERSION] == 6
assert entry.runtime_data.api_version == 6
mocked_hole = _create_mocked_hole(api_version=5)
config = {**CONFIG_DATA_DEFAULTS}
@ -216,7 +216,7 @@ async def test_setup_without_api_version(hass: HomeAssistant) -> None:
with _patch_init_hole(mocked_hole):
assert await hass.config_entries.async_setup(entry.entry_id)
assert entry.data[CONF_API_VERSION] == 5
assert entry.runtime_data.api_version == 5
async def test_setup_name_config(hass: HomeAssistant) -> None: