Add config flow to pi_hole integration (#35442)
* Add config flow to pi-hole * Add config flow tests * Change PlatformNotReady to ConfigEntryNotReady * Improve config flow * Add @shenxn as codeowner * Use entity_id as unique id * Remove .get with [] for required fields * Remove unique id from config flow * Replace some strings with references * Fix api_key string * Fix service api_key check * Remove unused DuplicatedNameException
This commit is contained in:
1 parent
7147c5306d
commit
ce99fa8c02
11 files changed
+488
-157
No files matched your search
@@ -1 +1,71 @@
|
||||
"""Tests for the pi_hole component."""
|
||||
from hole.exceptions import HoleError
|
||||
|
||||
from homeassistant.components.pi_hole.const import CONF_LOCATION
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
|
||||
from tests.async_mock import AsyncMock, MagicMock, patch
|
||||
|
||||
ZERO_DATA = {
|
||||
"ads_blocked_today": 0,
|
||||
"ads_percentage_today": 0,
|
||||
"clients_ever_seen": 0,
|
||||
"dns_queries_today": 0,
|
||||
"domains_being_blocked": 0,
|
||||
"queries_cached": 0,
|
||||
"queries_forwarded": 0,
|
||||
"status": 0,
|
||||
"unique_clients": 0,
|
||||
"unique_domains": 0,
|
||||
}
|
||||
|
||||
HOST = "1.2.3.4"
|
||||
PORT = 80
|
||||
LOCATION = "location"
|
||||
NAME = "name"
|
||||
API_KEY = "apikey"
|
||||
SSL = False
|
||||
VERIFY_SSL = True
|
||||
|
||||
CONF_DATA = {
|
||||
CONF_HOST: f"{HOST}:{PORT}",
|
||||
CONF_LOCATION: LOCATION,
|
||||
CONF_NAME: NAME,
|
||||
CONF_API_KEY: API_KEY,
|
||||
CONF_SSL: SSL,
|
||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
||||
}
|
||||
|
||||
CONF_CONFIG_FLOW = {
|
||||
CONF_HOST: HOST,
|
||||
CONF_PORT: PORT,
|
||||
CONF_LOCATION: LOCATION,
|
||||
CONF_NAME: NAME,
|
||||
CONF_API_KEY: API_KEY,
|
||||
CONF_SSL: SSL,
|
||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
||||
}
|
||||
|
||||
|
||||
def _create_mocked_hole(raise_exception=False):
|
||||
mocked_hole = MagicMock()
|
||||
type(mocked_hole).get_data = AsyncMock(
|
||||
side_effect=HoleError("") if raise_exception else None
|
||||
)
|
||||
type(mocked_hole).enable = AsyncMock()
|
||||
type(mocked_hole).disable = AsyncMock()
|
||||
mocked_hole.data = ZERO_DATA
|
||||
return mocked_hole
|
||||
|
||||
|
||||
def _patch_config_flow_hole(mocked_hole):
|
||||
return patch(
|
||||
"homeassistant.components.pi_hole.config_flow.Hole", return_value=mocked_hole
|
||||
)
|
||||
@@ -0,0 +1,125 @@
|
||||
"""Test pi_hole config flow."""
|
||||
import copy
|
||||
import logging
|
||||
|
||||
from homeassistant.components.pi_hole.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
|
||||
from . import (
|
||||
CONF_CONFIG_FLOW,
|
||||
CONF_DATA,
|
||||
CONF_HOST,
|
||||
NAME,
|
||||
_create_mocked_hole,
|
||||
_patch_config_flow_hole,
|
||||
)
|
||||
|
||||
from tests.async_mock import patch
|
||||
|
||||
|
||||
def _flow_next(hass, flow_id):
|
||||
return next(
|
||||
flow
|
||||
for flow in hass.config_entries.flow.async_progress()
|
||||
if flow["flow_id"] == flow_id
|
||||
)
|
||||
|
||||
|
||||
def _patch_setup():
|
||||
return patch(
|
||||
"homeassistant.components.pi_hole.async_setup_entry", return_value=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_flow_import(hass, caplog):
|
||||
"""Test import flow."""
|
||||
mocked_hole = _create_mocked_hole()
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=CONF_DATA
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == NAME
|
||||
assert result["data"] == CONF_DATA
|
||||
|
||||
# duplicated server
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=CONF_DATA
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
# duplicated name
|
||||
conf_data = copy.deepcopy(CONF_DATA)
|
||||
conf_data[CONF_HOST] = "4.3.2.1"
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf_data
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "duplicated_name"
|
||||
assert len([x for x in caplog.records if x.levelno == logging.ERROR]) == 1
|
||||
|
||||
|
||||
async def test_flow_import_invalid(hass, caplog):
|
||||
"""Test import flow with invalid server."""
|
||||
mocked_hole = _create_mocked_hole(True)
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=CONF_DATA
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "cannot_connect"
|
||||
assert len([x for x in caplog.records if x.levelno == logging.ERROR]) == 1
|
||||
|
||||
|
||||
async def test_flow_user(hass):
|
||||
"""Test user initialized flow."""
|
||||
mocked_hole = _create_mocked_hole()
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER},
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
_flow_next(hass, result["flow_id"])
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONF_CONFIG_FLOW,
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == NAME
|
||||
assert result["data"] == CONF_DATA
|
||||
|
||||
# duplicated server
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW,
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
# duplicated name
|
||||
conf_data = copy.deepcopy(CONF_CONFIG_FLOW)
|
||||
conf_data[CONF_HOST] = "4.3.2.1"
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf_data
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "duplicated_name"
|
||||
|
||||
|
||||
async def test_flow_user_invalid(hass):
|
||||
"""Test user initialized flow with invalid server."""
|
||||
mocked_hole = _create_mocked_hole(True)
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
@@ -2,29 +2,20 @@
|
||||
|
||||
from homeassistant.components import pi_hole
|
||||
|
||||
from tests.async_mock import AsyncMock, patch
|
||||
from . import _create_mocked_hole, _patch_config_flow_hole
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import async_setup_component
|
||||
|
||||
ZERO_DATA = {
|
||||
"ads_blocked_today": 0,
|
||||
"ads_percentage_today": 0,
|
||||
"clients_ever_seen": 0,
|
||||
"dns_queries_today": 0,
|
||||
"domains_being_blocked": 0,
|
||||
"queries_cached": 0,
|
||||
"queries_forwarded": 0,
|
||||
"status": 0,
|
||||
"unique_clients": 0,
|
||||
"unique_domains": 0,
|
||||
}
|
||||
|
||||
def _patch_init_hole(mocked_hole):
|
||||
return patch("homeassistant.components.pi_hole.Hole", return_value=mocked_hole)
|
||||
|
||||
|
||||
async def test_setup_minimal_config(hass):
|
||||
"""Tests component setup with minimal config."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
_hole.return_value.get_data = AsyncMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
mocked_hole = _create_mocked_hole()
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
||||
assert await async_setup_component(
|
||||
hass, pi_hole.DOMAIN, {pi_hole.DOMAIN: [{"host": "pi.hole"}]}
|
||||
)
|
||||
@@ -78,10 +69,8 @@ async def test_setup_minimal_config(hass):
|
||||
|
||||
async def test_setup_name_config(hass):
|
||||
"""Tests component setup with a custom name."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
_hole.return_value.get_data = AsyncMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
mocked_hole = _create_mocked_hole()
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
pi_hole.DOMAIN,
|
||||
@@ -98,19 +87,15 @@ async def test_setup_name_config(hass):
|
||||
|
||||
async def test_disable_service_call(hass):
|
||||
"""Test disable service call with no Pi-hole named."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
mock_disable = AsyncMock(return_value=None)
|
||||
_hole.return_value.disable = mock_disable
|
||||
_hole.return_value.get_data = AsyncMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
mocked_hole = _create_mocked_hole()
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
pi_hole.DOMAIN,
|
||||
{
|
||||
pi_hole.DOMAIN: [
|
||||
{"host": "pi.hole", "api_key": "1"},
|
||||
{"host": "pi.hole", "name": "Custom", "api_key": "2"},
|
||||
{"host": "pi.hole1", "api_key": "1"},
|
||||
{"host": "pi.hole2", "name": "Custom", "api_key": "2"},
|
||||
]
|
||||
},
|
||||
)
|
||||
@@ -126,24 +111,20 @@ async def test_disable_service_call(hass):
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_disable.call_count == 2
|
||||
assert mocked_hole.disable.call_count == 2
|
||||
|
||||
|
||||
async def test_enable_service_call(hass):
|
||||
"""Test enable service call with no Pi-hole named."""
|
||||
with patch("homeassistant.components.pi_hole.Hole") as _hole:
|
||||
mock_enable = AsyncMock(return_value=None)
|
||||
_hole.return_value.enable = mock_enable
|
||||
_hole.return_value.get_data = AsyncMock(return_value=None)
|
||||
_hole.return_value.data = ZERO_DATA
|
||||
|
||||
mocked_hole = _create_mocked_hole()
|
||||
with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
pi_hole.DOMAIN,
|
||||
{
|
||||
pi_hole.DOMAIN: [
|
||||
{"host": "pi.hole", "api_key": "1"},
|
||||
{"host": "pi.hole", "name": "Custom", "api_key": "2"},
|
||||
{"host": "pi.hole1", "api_key": "1"},
|
||||
{"host": "pi.hole2", "name": "Custom", "api_key": "2"},
|
||||
]
|
||||
},
|
||||
)
|
||||
@@ -156,4 +137,4 @@ async def test_enable_service_call(hass):
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_enable.call_count == 2
|
||||
assert mocked_hole.enable.call_count == 2
|
||||
Reference in new issue
Block a user