From b1746faa47a6701e67536f3a0a982bce6e3a4541 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 15 May 2024 13:39:07 +0200 Subject: [PATCH] Fix API creation for passwordless pi_hole (#117494) --- homeassistant/components/pi_hole/__init__.py | 2 +- tests/components/pi_hole/test_init.py | 35 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/pi_hole/__init__.py b/homeassistant/components/pi_hole/__init__.py index f892114b26c..922590a5cde 100644 --- a/homeassistant/components/pi_hole/__init__.py +++ b/homeassistant/components/pi_hole/__init__.py @@ -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: diff --git a/tests/components/pi_hole/test_init.py b/tests/components/pi_hole/test_init.py index a58a46680bb..b8d66286c64 100644 --- a/tests/components/pi_hole/test_init.py +++ b/tests/components/pi_hole/test_init.py @@ -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 @@ -12,12 +12,20 @@ from homeassistant.components.pi_hole.const import ( 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, @@ -26,6 +34,29 @@ from . import ( 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()