diff --git a/homeassistant/components/pi_hole/__init__.py b/homeassistant/components/pi_hole/__init__.py index 05d301b5250..582a4574dc4 100644 --- a/homeassistant/components/pi_hole/__init__.py +++ b/homeassistant/components/pi_hole/__init__.py @@ -60,7 +60,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bo 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 3c8f66a82d0..b5a24a5972b 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 @@ -14,12 +14,20 @@ from homeassistant.components.pi_hole.const import ( SERVICE_DISABLE_ATTR_DURATION, ) 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 . import ( + API_KEY, CONFIG_DATA, CONFIG_DATA_DEFAULTS, + CONFIG_ENTRY_WITHOUT_API_KEY, SWITCH_ENTITY_ID, _create_mocked_hole, _patch_init_hole, @@ -28,6 +36,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()