Skip to content

Commit

Permalink
Fix API creation for passwordless pi_hole (#117494)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored and frenck committed May 17, 2024
1 parent 615ae78 commit b1746fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/pi_hole/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
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)
api_key = entry.data.get(CONF_API_KEY, "")

# remove obsolet CONF_STATISTICS_ONLY from entry.data
if CONF_STATISTICS_ONLY in entry.data:
Expand Down
35 changes: 33 additions & 2 deletions tests/components/pi_hole/test_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test pi_hole component."""

import logging
from unittest.mock import AsyncMock
from unittest.mock import ANY, AsyncMock

from hole.exceptions import HoleError
import pytest
Expand All @@ -12,12 +12,20 @@
SERVICE_DISABLE,
SERVICE_DISABLE_ATTR_DURATION,
)
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 . import (
API_KEY,
CONFIG_DATA,
CONFIG_DATA_DEFAULTS,
CONFIG_ENTRY_WITHOUT_API_KEY,
SWITCH_ENTITY_ID,
_create_mocked_hole,
_patch_init_hole,
Expand All @@ -26,6 +34,29 @@
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:
"""Tests component setup with default config."""
mocked_hole = _create_mocked_hole()
Expand Down

0 comments on commit b1746fa

Please sign in to comment.