diff --git a/homeassistant/components/pi_hole/__init__.py b/homeassistant/components/pi_hole/__init__.py index f211d646c0b..f73b7156d3e 100644 --- a/homeassistant/components/pi_hole/__init__.py +++ b/homeassistant/components/pi_hole/__init__.py @@ -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) diff --git a/homeassistant/components/pi_hole/config_flow.py b/homeassistant/components/pi_hole/config_flow.py index da994b74e6d..327ce32847e 100644 --- a/homeassistant/components/pi_hole/config_flow.py +++ b/homeassistant/components/pi_hole/config_flow.py @@ -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( diff --git a/homeassistant/components/pi_hole/sensor.py b/homeassistant/components/pi_hole/sensor.py index aa79805cc2d..844b03acf7c 100644 --- a/homeassistant/components/pi_hole/sensor.py +++ b/homeassistant/components/pi_hole/sensor.py @@ -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) diff --git a/tests/components/pi_hole/__init__.py b/tests/components/pi_hole/__init__.py index 36ee963a16f..c20f22ac58d 100644 --- a/tests/components/pi_hole/__init__.py +++ b/tests/components/pi_hole/__init__.py @@ -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" diff --git a/tests/components/pi_hole/test_config_flow.py b/tests/components/pi_hole/test_config_flow.py index e92a845ce1e..e79f65b406e 100644 --- a/tests/components/pi_hole/test_config_flow.py +++ b/tests/components/pi_hole/test_config_flow.py @@ -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): diff --git a/tests/components/pi_hole/test_init.py b/tests/components/pi_hole/test_init.py index b4cc11529d9..94170e967d4 100644 --- a/tests/components/pi_hole/test_init.py +++ b/tests/components/pi_hole/test_init.py @@ -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: