Fix API creation for passwordless pi_hole (#117494)

This commit is contained in:
Erik Montnemery 2024-05-15 13:39:07 +02:00 committed by GitHub
parent 6bd3648c77
commit 6ecc0ec3a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 3 deletions

View file

@ -60,7 +60,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bo
use_tls = entry.data[CONF_SSL] use_tls = entry.data[CONF_SSL]
verify_tls = entry.data[CONF_VERIFY_SSL] verify_tls = entry.data[CONF_VERIFY_SSL]
location = entry.data[CONF_LOCATION] location = entry.data[CONF_LOCATION]
api_key = entry.data.get(CONF_API_KEY) api_key = entry.data.get(CONF_API_KEY, "")
# remove obsolet CONF_STATISTICS_ONLY from entry.data # remove obsolet CONF_STATISTICS_ONLY from entry.data
if CONF_STATISTICS_ONLY in entry.data: if CONF_STATISTICS_ONLY in entry.data:

View file

@ -1,7 +1,7 @@
"""Test pi_hole component.""" """Test pi_hole component."""
import logging import logging
from unittest.mock import AsyncMock from unittest.mock import ANY, AsyncMock
from hole.exceptions import HoleError from hole.exceptions import HoleError
import pytest import pytest
@ -14,12 +14,20 @@ from homeassistant.components.pi_hole.const import (
SERVICE_DISABLE_ATTR_DURATION, SERVICE_DISABLE_ATTR_DURATION,
) )
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST, CONF_NAME from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_HOST,
CONF_LOCATION,
CONF_NAME,
CONF_SSL,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import ( from . import (
API_KEY,
CONFIG_DATA, CONFIG_DATA,
CONFIG_DATA_DEFAULTS, CONFIG_DATA_DEFAULTS,
CONFIG_ENTRY_WITHOUT_API_KEY,
SWITCH_ENTITY_ID, SWITCH_ENTITY_ID,
_create_mocked_hole, _create_mocked_hole,
_patch_init_hole, _patch_init_hole,
@ -28,6 +36,29 @@ from . import (
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.mark.parametrize(
("config_entry_data", "expected_api_token"),
[(CONFIG_DATA_DEFAULTS, API_KEY), (CONFIG_ENTRY_WITHOUT_API_KEY, "")],
)
async def test_setup_api(
hass: HomeAssistant, config_entry_data: dict, expected_api_token: str
) -> None:
"""Tests the API object is created with the expected parameters."""
mocked_hole = _create_mocked_hole()
config_entry_data = {**config_entry_data, CONF_STATISTICS_ONLY: True}
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=config_entry_data)
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(
config_entry_data[CONF_HOST],
ANY,
api_token=expected_api_token,
location=config_entry_data[CONF_LOCATION],
tls=config_entry_data[CONF_SSL],
)
async def test_setup_with_defaults(hass: HomeAssistant) -> None: async def test_setup_with_defaults(hass: HomeAssistant) -> None:
"""Tests component setup with default config.""" """Tests component setup with default config."""
mocked_hole = _create_mocked_hole() mocked_hole = _create_mocked_hole()