Support password less PI-Hole installations
diff --git a/homeassistant/components/pi_hole/__init__.py b/homeassistant/components/pi_hole/__init__.py
index ba7949c0c30004..b33b94383542fa 100644
--- a/homeassistant/components/pi_hole/__init__.py
+++ b/homeassistant/components/pi_hole/__init__.py
@@ -117,10 +117,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
         entry_data.pop(CONF_STATISTICS_ONLY)
         hass.config_entries.async_update_entry(entry, data=entry_data)
 
-    # start reauth to force api key is present
-    if CONF_API_KEY not in entry.data:
-        raise ConfigEntryAuthFailed
-
     _LOGGER.debug("Setting up %s integration with host %s", DOMAIN, host)
 
     session = async_get_clientsession(hass, verify_tls)
diff --git a/homeassistant/components/pi_hole/config_flow.py b/homeassistant/components/pi_hole/config_flow.py
index 637f906b9eea44..519ef5a76286c4 100644
--- a/homeassistant/components/pi_hole/config_flow.py
+++ b/homeassistant/components/pi_hole/config_flow.py
@@ -56,7 +56,6 @@ async def async_step_user(
                 CONF_LOCATION: user_input[CONF_LOCATION],
                 CONF_SSL: user_input[CONF_SSL],
                 CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL],
-                CONF_API_KEY: user_input[CONF_API_KEY],
             }
 
             self._async_abort_entries_match(
@@ -71,6 +70,9 @@ async def async_step_user(
                     title=user_input[CONF_NAME], data=self._config
                 )
 
+            if CONF_API_KEY in errors:
+                return await self.async_step_api_key()
+
         user_input = user_input or {}
         return self.async_show_form(
             step_id="user",
@@ -80,7 +82,6 @@ async def async_step_user(
                     vol.Required(
                         CONF_PORT, default=user_input.get(CONF_PORT, 80)
                     ): vol.Coerce(int),
-                    vol.Required(CONF_API_KEY): str,
                     vol.Required(
                         CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
                     ): str,
@@ -101,6 +102,25 @@ async def async_step_user(
             errors=errors,
         )
 
+    async def async_step_api_key(
+        self, user_input: dict[str, Any] | None = None
+    ) -> FlowResult:
+        """Handle step to setup API key."""
+        errors = {}
+        if user_input is not None:
+            self._config[CONF_API_KEY] = user_input[CONF_API_KEY]
+            if not (errors := await self._async_try_connect()):
+                return self.async_create_entry(
+                    title=self._config[CONF_NAME],
+                    data=self._config,
+                )
+
+        return self.async_show_form(
+            step_id="api_key",
+            data_schema=vol.Schema({vol.Required(CONF_API_KEY): str}),
+            errors=errors,
+        )
+
     async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
         """Handle a flow initiated by import."""
 
@@ -178,7 +198,7 @@ async def _async_try_connect(self) -> dict[str, str]:
             session,
             location=self._config[CONF_LOCATION],
             tls=self._config[CONF_SSL],
-            api_token=self._config[CONF_API_KEY],
+            api_token=self._config.get(CONF_API_KEY),
         )
         try:
             await pi_hole.get_data()
diff --git a/homeassistant/components/pi_hole/strings.json b/homeassistant/components/pi_hole/strings.json
index 120ab8cb80a5d2..2f04b8fe47e4d2 100644
--- a/homeassistant/components/pi_hole/strings.json
+++ b/homeassistant/components/pi_hole/strings.json
@@ -7,11 +7,15 @@
           "port": "[%key:common::config_flow::data::port%]",
           "name": "[%key:common::config_flow::data::name%]",
           "location": "[%key:common::config_flow::data::location%]",
-          "api_key": "[%key:common::config_flow::data::api_key%]",
           "ssl": "[%key:common::config_flow::data::ssl%]",
           "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
         }
       },
+      "api_key": {
+        "data": {
+          "api_key": "[%key:common::config_flow::data::api_key%]"
+        }
+      },
       "reauth_confirm": {
         "title": "PI-Hole [%key:common::config_flow::title::reauth%]",
         "description": "Please enter a new api key for PI-Hole at {host}/{location}",
diff --git a/homeassistant/components/pi_hole/translations/en.json b/homeassistant/components/pi_hole/translations/en.json
index 815182731c21d5..940e3950281605 100644
--- a/homeassistant/components/pi_hole/translations/en.json
+++ b/homeassistant/components/pi_hole/translations/en.json
@@ -9,6 +9,11 @@
             "invalid_auth": "Invalid authentication"
         },
         "step": {
+            "api_key": {
+                "data": {
+                    "api_key": "API Key"
+                }
+            },
             "reauth_confirm": {
                 "data": {
                     "api_key": "API Key"
@@ -18,7 +23,6 @@
             },
             "user": {
                 "data": {
-                    "api_key": "API Key",
                     "host": "Host",
                     "location": "Location",
                     "name": "Name",
diff --git a/tests/components/pi_hole/__init__.py b/tests/components/pi_hole/__init__.py
index 49e15391f8c926..8658fc84ffe1ba 100644
--- a/tests/components/pi_hole/__init__.py
+++ b/tests/components/pi_hole/__init__.py
@@ -74,7 +74,6 @@
 CONFIG_FLOW_USER = {
     CONF_HOST: HOST,
     CONF_PORT: PORT,
-    CONF_API_KEY: API_KEY,
     CONF_LOCATION: LOCATION,
     CONF_NAME: NAME,
     CONF_SSL: SSL,
@@ -85,7 +84,7 @@
     CONF_API_KEY: API_KEY,
 }
 
-CONFIG_ENTRY = {
+CONFIG_ENTRY_WITH_API_KEY = {
     CONF_HOST: f"{HOST}:{PORT}",
     CONF_LOCATION: LOCATION,
     CONF_NAME: NAME,
@@ -94,7 +93,15 @@
     CONF_VERIFY_SSL: VERIFY_SSL,
 }
 
-CONFIG_ENTRY_IMPORTED = {**CONFIG_ENTRY, CONF_STATISTICS_ONLY: False}
+CONFIG_ENTRY_WITHOUT_API_KEY = {
+    CONF_HOST: f"{HOST}:{PORT}",
+    CONF_LOCATION: LOCATION,
+    CONF_NAME: NAME,
+    CONF_SSL: SSL,
+    CONF_VERIFY_SSL: VERIFY_SSL,
+}
+
+CONFIG_ENTRY_IMPORTED = {**CONFIG_ENTRY_WITH_API_KEY, CONF_STATISTICS_ONLY: False}
 
 SWITCH_ENTITY_ID = "switch.pi_hole"
 
@@ -128,3 +135,9 @@ def _patch_config_flow_hole(mocked_hole):
     return patch(
         "homeassistant.components.pi_hole.config_flow.Hole", return_value=mocked_hole
     )
+
+
+def _patch_setup_hole():
+    return patch(
+        "homeassistant.components.pi_hole.async_setup_entry", return_value=True
+    )
diff --git a/tests/components/pi_hole/test_config_flow.py b/tests/components/pi_hole/test_config_flow.py
index 65f21418bad194..8c38803de64718 100644
--- a/tests/components/pi_hole/test_config_flow.py
+++ b/tests/components/pi_hole/test_config_flow.py
@@ -1,6 +1,5 @@
 """Test pi_hole config flow."""
 import logging
-from unittest.mock import patch
 
 from homeassistant.components.pi_hole.const import DOMAIN
 from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
@@ -11,30 +10,26 @@
 from . import (
     CONFIG_DATA,
     CONFIG_DATA_DEFAULTS,
-    CONFIG_ENTRY,
     CONFIG_ENTRY_IMPORTED,
+    CONFIG_ENTRY_WITH_API_KEY,
+    CONFIG_ENTRY_WITHOUT_API_KEY,
+    CONFIG_FLOW_API_KEY,
     CONFIG_FLOW_USER,
     NAME,
     ZERO_DATA,
     _create_mocked_hole,
     _patch_config_flow_hole,
     _patch_init_hole,
+    _patch_setup_hole,
 )
 
 from tests.common import MockConfigEntry
 
 
-def _patch_setup():
-    return patch(
-        "homeassistant.components.pi_hole.async_setup_entry",
-        return_value=True,
-    )
-
-
 async def test_flow_import(hass: HomeAssistant, caplog):
     """Test import flow."""
     mocked_hole = _create_mocked_hole()
-    with _patch_config_flow_hole(mocked_hole), _patch_setup():
+    with _patch_config_flow_hole(mocked_hole), _patch_setup_hole():
         result = await hass.config_entries.flow.async_init(
             DOMAIN, context={"source": SOURCE_IMPORT}, data=CONFIG_DATA
         )
@@ -53,7 +48,7 @@ async def test_flow_import(hass: HomeAssistant, caplog):
 async def test_flow_import_invalid(hass: HomeAssistant, caplog):
     """Test import flow with invalid server."""
     mocked_hole = _create_mocked_hole(True)
-    with _patch_config_flow_hole(mocked_hole), _patch_setup():
+    with _patch_config_flow_hole(mocked_hole), _patch_setup_hole():
         result = await hass.config_entries.flow.async_init(
             DOMAIN, context={"source": SOURCE_IMPORT}, data=CONFIG_DATA
         )
@@ -62,10 +57,10 @@ async def test_flow_import_invalid(hass: HomeAssistant, caplog):
         assert len([x for x in caplog.records if x.levelno == logging.ERROR]) == 1
 
 
-async def test_flow_user(hass: HomeAssistant):
-    """Test user initialized flow."""
+async def test_flow_user_with_api_key(hass: HomeAssistant):
+    """Test user initialized flow with api key needed."""
     mocked_hole = _create_mocked_hole(has_data=False)
-    with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
+    with _patch_config_flow_hole(mocked_hole), _patch_setup_hole() as mock_setup:
         result = await hass.config_entries.flow.async_init(
             DOMAIN,
             context={"source": SOURCE_USER},
@@ -79,17 +74,26 @@ async def test_flow_user(hass: HomeAssistant):
             user_input=CONFIG_FLOW_USER,
         )
         assert result["type"] == FlowResultType.FORM
-        assert result["step_id"] == "user"
+        assert result["step_id"] == "api_key"
+        assert result["errors"] == {}
+
+        result = await hass.config_entries.flow.async_configure(
+            result["flow_id"],
+            user_input={CONF_API_KEY: "some_key"},
+        )
+        assert result["type"] == FlowResultType.FORM
+        assert result["step_id"] == "api_key"
         assert result["errors"] == {CONF_API_KEY: "invalid_auth"}
 
         mocked_hole.data = ZERO_DATA
         result = await hass.config_entries.flow.async_configure(
             result["flow_id"],
-            user_input=CONFIG_FLOW_USER,
+            user_input=CONFIG_FLOW_API_KEY,
         )
         assert result["type"] == FlowResultType.CREATE_ENTRY
         assert result["title"] == NAME
-        assert result["data"] == CONFIG_ENTRY
+        assert result["data"] == CONFIG_ENTRY_WITH_API_KEY
+        mock_setup.assert_called_once()
 
         # duplicated server
         result = await hass.config_entries.flow.async_init(
@@ -101,10 +105,32 @@ async def test_flow_user(hass: HomeAssistant):
         assert result["reason"] == "already_configured"
 
 
-async def test_flow_user_invalid(hass):
+async def test_flow_user_without_api_key(hass: HomeAssistant):
+    """Test user initialized flow without api key needed."""
+    mocked_hole = _create_mocked_hole()
+    with _patch_config_flow_hole(mocked_hole), _patch_setup_hole() as mock_setup:
+        result = await hass.config_entries.flow.async_init(
+            DOMAIN,
+            context={"source": SOURCE_USER},
+        )
+        assert result["type"] == FlowResultType.FORM
+        assert result["step_id"] == "user"
+        assert result["errors"] == {}
+
+        result = await hass.config_entries.flow.async_configure(
+            result["flow_id"],
+            user_input=CONFIG_FLOW_USER,
+        )
+        assert result["type"] == FlowResultType.CREATE_ENTRY
+        assert result["title"] == NAME
+        assert result["data"] == CONFIG_ENTRY_WITHOUT_API_KEY
+        mock_setup.assert_called_once()
+
+
+async def test_flow_user_invalid(hass: HomeAssistant):
     """Test user initialized flow with invalid server."""
     mocked_hole = _create_mocked_hole(True)
-    with _patch_config_flow_hole(mocked_hole), _patch_setup():
+    with _patch_config_flow_hole(mocked_hole):
         result = await hass.config_entries.flow.async_init(
             DOMAIN, context={"source": SOURCE_USER}, data=CONFIG_FLOW_USER
         )
diff --git a/tests/components/pi_hole/test_init.py b/tests/components/pi_hole/test_init.py
index 75d9dd27aeea2a..b863898db25003 100644
--- a/tests/components/pi_hole/test_init.py
+++ b/tests/components/pi_hole/test_init.py
@@ -10,8 +10,7 @@
     SERVICE_DISABLE,
     SERVICE_DISABLE_ATTR_DURATION,
 )
-from homeassistant.config_entries import ConfigEntryState
-from homeassistant.const import ATTR_ENTITY_ID, CONF_API_KEY, CONF_HOST
+from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST
 from homeassistant.core import HomeAssistant
 from homeassistant.setup import async_setup_component
 
@@ -200,15 +199,3 @@ async def test_remove_obsolete(hass: HomeAssistant):
     with _patch_init_hole(mocked_hole):
         assert await hass.config_entries.async_setup(entry.entry_id)
         assert CONF_STATISTICS_ONLY not in entry.data
-
-
-async def test_missing_api_key(hass: HomeAssistant):
-    """Tests start reauth flow if api key is missing."""
-    mocked_hole = _create_mocked_hole()
-    data = CONFIG_DATA_DEFAULTS.copy()
-    data.pop(CONF_API_KEY)
-    entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=data)
-    entry.add_to_hass(hass)
-    with _patch_init_hole(mocked_hole):
-        assert not await hass.config_entries.async_setup(entry.entry_id)
-        assert entry.state == ConfigEntryState.SETUP_ERROR

<!--
  You are amazing! Thanks for contributing to our project!
  Please, DO NOT DELETE ANY TEXT from this template! (unless instructed).
-->
## Proposed change
<!--
  Describe the big picture of your changes here to communicate to the
  maintainers why we should accept this pull request. If it fixes a bug
  or resolves a feature request, be sure to link to that issue in the
  additional information section.
-->
With this, the config flow does recognize if an API key is needed or not.

---

This is targeted against `rc` because of https://github.com/home-assistant/core/pull/85264#issuecomment-1374997094
Another PR, based on and targeted to `dev` will follow in a few moments (#86184)

## Type of change
<!--
  What type of change does your PR introduce to Home Assistant?
  NOTE: Please, check only 1! box!
  If your PR requires multiple boxes to be checked, you'll most likely need to
  split it into multiple PRs. This makes things easier and faster to code review.
-->

- [ ] Dependency upgrade
- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [ ] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests

## Additional information
<!--
  Details are important, and help maintainers processing your PR.
  Please be sure to fill out additional details, if applicable.
-->

- This PR fixes or closes issue: fixes #86152
- This PR is related to issue: 
- Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/25827

## Checklist
<!--
  Put an `x` in the boxes that apply. You can also fill these out after
  creating the PR. If you're unsure about any of them, don't hesitate to ask.
  We're here to help! This is simply a reminder of what we are going to look
  for before merging your code.
-->

- [ ] The code change is tested and works locally.
- [ ] Local tests pass. **Your PR cannot be merged unless tests pass**
- [ ] There is no commented out code in this PR.
- [ ] I have followed the [development checklist][dev-checklist]
- [ ] The code has been formatted using Black (`black --fast homeassistant tests`)
- [ ] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

- [ ] Documentation added/updated for [www.home-assistant.io][docs-repository]

If the code communicates with devices, web services, or third-party tools:

- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.  
      Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.  
      Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to `.coveragerc`.

<!--
  This project is very active and we have a high turnover of pull requests.

  Unfortunately, the number of incoming pull requests is higher than what our
  reviewers can review and merge so there is a long backlog of pull requests
  waiting for review. You can help here!
  
  By reviewing another pull request, you will help raise the code quality of
  that pull request and the final review will be faster. This way the general
  pace of pull request reviews will go up and your wait time will go down.
  
  When picking a pull request to review, try to choose one that hasn't yet
  been reviewed.

  Thanks for helping out!
-->

To help with the load of incoming pull requests:

- [ ] I have reviewed two other [open pull requests][prs] in this repository.

[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure

<!--
  Thank you for contributing <3

  Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/en/development_checklist.html
[manifest-docs]: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
[quality-scale]: https://developers.home-assistant.io/docs/en/next/integration_quality_scale_index.html
[docs-repository]: https://github.com/home-assistant/home-assistant.io
