mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Make API key mandatory for PI-Hole (#85264)
* add reauth flow * adjust tests * use constant for platforms * remove not needed async_get_entry() * fix typo * user _async_abort_entries_match() * don't use CONF_ prefix for config dicts * sort PLATFORMS * use entry_data in reauth flow
This commit is contained in:
parent
7f2b7340b9
commit
ee3ab45012
@ -16,7 +16,8 @@ from homeassistant.const import (
|
|||||||
CONF_VERIFY_SSL,
|
CONF_VERIFY_SSL,
|
||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
@ -38,6 +39,13 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
|
|
||||||
|
PLATFORMS = [
|
||||||
|
Platform.BINARY_SENSOR,
|
||||||
|
Platform.SENSOR,
|
||||||
|
Platform.SWITCH,
|
||||||
|
Platform.UPDATE,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Pi-hole entry."""
|
"""Set up Pi-hole entry."""
|
||||||
@ -48,11 +56,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
location = entry.data[CONF_LOCATION]
|
location = entry.data[CONF_LOCATION]
|
||||||
api_key = entry.data.get(CONF_API_KEY)
|
api_key = entry.data.get(CONF_API_KEY)
|
||||||
|
|
||||||
# For backward compatibility
|
# remove obsolet CONF_STATISTICS_ONLY from entry.data
|
||||||
if CONF_STATISTICS_ONLY not in entry.data:
|
if CONF_STATISTICS_ONLY in entry.data:
|
||||||
hass.config_entries.async_update_entry(
|
entry_data = entry.data.copy()
|
||||||
entry, data={**entry.data, CONF_STATISTICS_ONLY: not api_key}
|
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)
|
_LOGGER.debug("Setting up %s integration with host %s", DOMAIN, host)
|
||||||
|
|
||||||
@ -72,6 +84,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
await api.get_versions()
|
await api.get_versions()
|
||||||
except HoleError as err:
|
except HoleError as err:
|
||||||
raise UpdateFailed(f"Failed to communicate with API: {err}") from err
|
raise UpdateFailed(f"Failed to communicate with API: {err}") from err
|
||||||
|
if not isinstance(api.data, dict):
|
||||||
|
raise ConfigEntryAuthFailed
|
||||||
|
|
||||||
coordinator = DataUpdateCoordinator(
|
coordinator = DataUpdateCoordinator(
|
||||||
hass,
|
hass,
|
||||||
@ -89,30 +103,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, _async_platforms(entry))
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload Pi-hole entry."""
|
"""Unload Pi-hole entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
entry, _async_platforms(entry)
|
|
||||||
)
|
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def _async_platforms(entry: ConfigEntry) -> list[Platform]:
|
|
||||||
"""Return platforms to be loaded / unloaded."""
|
|
||||||
platforms = [Platform.BINARY_SENSOR, Platform.UPDATE, Platform.SENSOR]
|
|
||||||
if not entry.data[CONF_STATISTICS_ONLY]:
|
|
||||||
platforms.append(Platform.SWITCH)
|
|
||||||
return platforms
|
|
||||||
|
|
||||||
|
|
||||||
class PiHoleEntity(CoordinatorEntity):
|
class PiHoleEntity(CoordinatorEntity):
|
||||||
"""Representation of a Pi-hole entity."""
|
"""Representation of a Pi-hole entity."""
|
||||||
|
|
||||||
|
@ -15,8 +15,6 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|||||||
from . import PiHoleEntity
|
from . import PiHoleEntity
|
||||||
from .const import (
|
from .const import (
|
||||||
BINARY_SENSOR_TYPES,
|
BINARY_SENSOR_TYPES,
|
||||||
BINARY_SENSOR_TYPES_STATISTICS_ONLY,
|
|
||||||
CONF_STATISTICS_ONLY,
|
|
||||||
DATA_KEY_API,
|
DATA_KEY_API,
|
||||||
DATA_KEY_COORDINATOR,
|
DATA_KEY_COORDINATOR,
|
||||||
DOMAIN as PIHOLE_DOMAIN,
|
DOMAIN as PIHOLE_DOMAIN,
|
||||||
@ -42,18 +40,6 @@ async def async_setup_entry(
|
|||||||
for description in BINARY_SENSOR_TYPES
|
for description in BINARY_SENSOR_TYPES
|
||||||
]
|
]
|
||||||
|
|
||||||
if entry.data[CONF_STATISTICS_ONLY]:
|
|
||||||
binary_sensors += [
|
|
||||||
PiHoleBinarySensor(
|
|
||||||
hole_data[DATA_KEY_API],
|
|
||||||
hole_data[DATA_KEY_COORDINATOR],
|
|
||||||
name,
|
|
||||||
entry.entry_id,
|
|
||||||
description,
|
|
||||||
)
|
|
||||||
for description in BINARY_SENSOR_TYPES_STATISTICS_ONLY
|
|
||||||
]
|
|
||||||
|
|
||||||
async_add_entities(binary_sensors, True)
|
async_add_entities(binary_sensors, True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Config flow to configure the Pi-hole integration."""
|
"""Config flow to configure the Pi-hole integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -22,11 +23,9 @@ from homeassistant.data_entry_flow import FlowResult
|
|||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_STATISTICS_ONLY,
|
|
||||||
DEFAULT_LOCATION,
|
DEFAULT_LOCATION,
|
||||||
DEFAULT_NAME,
|
DEFAULT_NAME,
|
||||||
DEFAULT_SSL,
|
DEFAULT_SSL,
|
||||||
DEFAULT_STATISTICS_ONLY,
|
|
||||||
DEFAULT_VERIFY_SSL,
|
DEFAULT_VERIFY_SSL,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
@ -47,59 +46,29 @@ class PiHoleFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Handle a flow initiated by the user."""
|
"""Handle a flow initiated by the user."""
|
||||||
return await self.async_step_init(user_input)
|
|
||||||
|
|
||||||
async def async_step_init(
|
|
||||||
self, user_input: dict[str, Any] | None, is_import: bool = False
|
|
||||||
) -> FlowResult:
|
|
||||||
"""Handle init step of a flow."""
|
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
host = (
|
|
||||||
user_input[CONF_HOST]
|
|
||||||
if is_import
|
|
||||||
else f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}"
|
|
||||||
)
|
|
||||||
name = user_input[CONF_NAME]
|
|
||||||
location = user_input[CONF_LOCATION]
|
|
||||||
tls = user_input[CONF_SSL]
|
|
||||||
verify_tls = user_input[CONF_VERIFY_SSL]
|
|
||||||
endpoint = f"{host}/{location}"
|
|
||||||
|
|
||||||
if await self._async_endpoint_existed(endpoint):
|
|
||||||
return self.async_abort(reason="already_configured")
|
|
||||||
|
|
||||||
try:
|
|
||||||
await self._async_try_connect(host, location, tls, verify_tls)
|
|
||||||
except HoleError as ex:
|
|
||||||
_LOGGER.debug("Connection failed: %s", ex)
|
|
||||||
if is_import:
|
|
||||||
_LOGGER.error("Failed to import: %s", ex)
|
|
||||||
return self.async_abort(reason="cannot_connect")
|
|
||||||
errors["base"] = "cannot_connect"
|
|
||||||
else:
|
|
||||||
self._config = {
|
self._config = {
|
||||||
CONF_HOST: host,
|
CONF_HOST: f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}",
|
||||||
CONF_NAME: name,
|
CONF_NAME: user_input[CONF_NAME],
|
||||||
CONF_LOCATION: location,
|
CONF_LOCATION: user_input[CONF_LOCATION],
|
||||||
CONF_SSL: tls,
|
CONF_SSL: user_input[CONF_SSL],
|
||||||
CONF_VERIFY_SSL: verify_tls,
|
CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL],
|
||||||
|
CONF_API_KEY: user_input[CONF_API_KEY],
|
||||||
|
}
|
||||||
|
|
||||||
|
self._async_abort_entries_match(
|
||||||
|
{
|
||||||
|
CONF_HOST: f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}",
|
||||||
|
CONF_LOCATION: user_input[CONF_LOCATION],
|
||||||
}
|
}
|
||||||
if is_import:
|
|
||||||
api_key = user_input.get(CONF_API_KEY)
|
|
||||||
return self.async_create_entry(
|
|
||||||
title=name,
|
|
||||||
data={
|
|
||||||
**self._config,
|
|
||||||
CONF_STATISTICS_ONLY: api_key is None,
|
|
||||||
CONF_API_KEY: api_key,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
self._config[CONF_STATISTICS_ONLY] = user_input[CONF_STATISTICS_ONLY]
|
|
||||||
if self._config[CONF_STATISTICS_ONLY]:
|
if not (errors := await self._async_try_connect()):
|
||||||
return self.async_create_entry(title=name, data=self._config)
|
return self.async_create_entry(
|
||||||
return await self.async_step_api_key()
|
title=user_input[CONF_NAME], data=self._config
|
||||||
|
)
|
||||||
|
|
||||||
user_input = user_input or {}
|
user_input = user_input or {}
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
@ -110,6 +79,7 @@ class PiHoleFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_PORT, default=user_input.get(CONF_PORT, 80)
|
CONF_PORT, default=user_input.get(CONF_PORT, 80)
|
||||||
): vol.Coerce(int),
|
): vol.Coerce(int),
|
||||||
|
vol.Required(CONF_API_KEY): str,
|
||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
|
CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
|
||||||
): str,
|
): str,
|
||||||
@ -117,12 +87,6 @@ class PiHoleFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
CONF_LOCATION,
|
CONF_LOCATION,
|
||||||
default=user_input.get(CONF_LOCATION, DEFAULT_LOCATION),
|
default=user_input.get(CONF_LOCATION, DEFAULT_LOCATION),
|
||||||
): str,
|
): str,
|
||||||
vol.Required(
|
|
||||||
CONF_STATISTICS_ONLY,
|
|
||||||
default=user_input.get(
|
|
||||||
CONF_STATISTICS_ONLY, DEFAULT_STATISTICS_ONLY
|
|
||||||
),
|
|
||||||
): bool,
|
|
||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_SSL,
|
CONF_SSL,
|
||||||
default=user_input.get(CONF_SSL, DEFAULT_SSL),
|
default=user_input.get(CONF_SSL, DEFAULT_SSL),
|
||||||
@ -136,34 +100,54 @@ class PiHoleFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_api_key(
|
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
||||||
self, user_input: dict[str, Any] | None = None
|
"""Perform reauth upon an API authentication error."""
|
||||||
|
self._config = dict(entry_data)
|
||||||
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
|
async def async_step_reauth_confirm(
|
||||||
|
self,
|
||||||
|
user_input: dict[str, Any] | None = None,
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Handle step to setup API key."""
|
"""Perform reauth confirm upon an API authentication error."""
|
||||||
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
return self.async_create_entry(
|
self._config = {**self._config, CONF_API_KEY: user_input[CONF_API_KEY]}
|
||||||
title=self._config[CONF_NAME],
|
if not (errors := await self._async_try_connect()):
|
||||||
data={
|
entry = self.hass.config_entries.async_get_entry(
|
||||||
**self._config,
|
self.context["entry_id"]
|
||||||
CONF_API_KEY: user_input.get(CONF_API_KEY, ""),
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
assert entry
|
||||||
|
self.hass.config_entries.async_update_entry(entry, data=self._config)
|
||||||
|
self.hass.async_create_task(
|
||||||
|
self.hass.config_entries.async_reload(self.context["entry_id"])
|
||||||
|
)
|
||||||
|
return self.async_abort(reason="reauth_successful")
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="api_key",
|
step_id="reauth_confirm",
|
||||||
data_schema=vol.Schema({vol.Optional(CONF_API_KEY): str}),
|
description_placeholders={
|
||||||
|
CONF_HOST: self._config[CONF_HOST],
|
||||||
|
CONF_LOCATION: self._config[CONF_LOCATION],
|
||||||
|
},
|
||||||
|
data_schema=vol.Schema({vol.Required(CONF_API_KEY): str}),
|
||||||
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_endpoint_existed(self, endpoint: str) -> bool:
|
async def _async_try_connect(self) -> dict[str, str]:
|
||||||
existing_endpoints = [
|
session = async_get_clientsession(self.hass, self._config[CONF_VERIFY_SSL])
|
||||||
f"{entry.data.get(CONF_HOST)}/{entry.data.get(CONF_LOCATION)}"
|
pi_hole = Hole(
|
||||||
for entry in self._async_current_entries()
|
self._config[CONF_HOST],
|
||||||
]
|
session,
|
||||||
return endpoint in existing_endpoints
|
location=self._config[CONF_LOCATION],
|
||||||
|
tls=self._config[CONF_SSL],
|
||||||
async def _async_try_connect(
|
api_token=self._config[CONF_API_KEY],
|
||||||
self, host: str, location: str, tls: bool, verify_tls: bool
|
)
|
||||||
) -> None:
|
try:
|
||||||
session = async_get_clientsession(self.hass, verify_tls)
|
|
||||||
pi_hole = Hole(host, session, location=location, tls=tls)
|
|
||||||
await pi_hole.get_data()
|
await pi_hole.get_data()
|
||||||
|
except HoleError as ex:
|
||||||
|
_LOGGER.debug("Connection failed: %s", ex)
|
||||||
|
return {"base": "cannot_connect"}
|
||||||
|
if not isinstance(pi_hole.data, dict):
|
||||||
|
return {CONF_API_KEY: "invalid_auth"}
|
||||||
|
return {}
|
||||||
|
@ -154,9 +154,6 @@ BINARY_SENSOR_TYPES: tuple[PiHoleBinarySensorEntityDescription, ...] = (
|
|||||||
},
|
},
|
||||||
state_value=lambda api: bool(api.versions["FTL_update"]),
|
state_value=lambda api: bool(api.versions["FTL_update"]),
|
||||||
),
|
),
|
||||||
)
|
|
||||||
|
|
||||||
BINARY_SENSOR_TYPES_STATISTICS_ONLY: tuple[PiHoleBinarySensorEntityDescription, ...] = (
|
|
||||||
PiHoleBinarySensorEntityDescription(
|
PiHoleBinarySensorEntityDescription(
|
||||||
key="status",
|
key="status",
|
||||||
name="Status",
|
name="Status",
|
||||||
|
@ -8,22 +8,25 @@
|
|||||||
"name": "[%key:common::config_flow::data::name%]",
|
"name": "[%key:common::config_flow::data::name%]",
|
||||||
"location": "[%key:common::config_flow::data::location%]",
|
"location": "[%key:common::config_flow::data::location%]",
|
||||||
"api_key": "[%key:common::config_flow::data::api_key%]",
|
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||||
"statistics_only": "Statistics Only",
|
|
||||||
"ssl": "[%key:common::config_flow::data::ssl%]",
|
"ssl": "[%key:common::config_flow::data::ssl%]",
|
||||||
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
|
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"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}",
|
||||||
"data": {
|
"data": {
|
||||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||||
|
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]",
|
||||||
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "Service is already configured"
|
"already_configured": "Service is already configured",
|
||||||
|
"reauth_successful": "Re-authentication was successful"
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "Failed to connect"
|
"cannot_connect": "Failed to connect",
|
||||||
|
"invalid_auth": "Invalid authentication"
|
||||||
},
|
},
|
||||||
"step": {
|
"step": {
|
||||||
"api_key": {
|
"reauth_confirm": {
|
||||||
"data": {
|
"data": {
|
||||||
"api_key": "API Key"
|
"api_key": "API Key"
|
||||||
}
|
},
|
||||||
|
"description": "Please enter a new api key for PI-Hole at {host}/{location}",
|
||||||
|
"title": "PI-Hole Reauthenticate Integration"
|
||||||
},
|
},
|
||||||
"user": {
|
"user": {
|
||||||
"data": {
|
"data": {
|
||||||
@ -20,16 +24,9 @@
|
|||||||
"name": "Name",
|
"name": "Name",
|
||||||
"port": "Port",
|
"port": "Port",
|
||||||
"ssl": "Uses an SSL certificate",
|
"ssl": "Uses an SSL certificate",
|
||||||
"statistics_only": "Statistics Only",
|
|
||||||
"verify_ssl": "Verify SSL certificate"
|
"verify_ssl": "Verify SSL certificate"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"issues": {
|
|
||||||
"deprecated_yaml": {
|
|
||||||
"description": "Configuring PI-Hole using YAML is being removed.\n\nYour existing YAML configuration has been imported into the UI automatically.\n\nRemove the PI-Hole YAML configuration from your configuration.yaml file and restart Home Assistant to fix this issue.",
|
|
||||||
"title": "The PI-Hole YAML configuration is being removed"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,11 +4,9 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
|||||||
from hole.exceptions import HoleError
|
from hole.exceptions import HoleError
|
||||||
|
|
||||||
from homeassistant.components.pi_hole.const import (
|
from homeassistant.components.pi_hole.const import (
|
||||||
CONF_STATISTICS_ONLY,
|
|
||||||
DEFAULT_LOCATION,
|
DEFAULT_LOCATION,
|
||||||
DEFAULT_NAME,
|
DEFAULT_NAME,
|
||||||
DEFAULT_SSL,
|
DEFAULT_SSL,
|
||||||
DEFAULT_STATISTICS_ONLY,
|
|
||||||
DEFAULT_VERIFY_SSL,
|
DEFAULT_VERIFY_SSL,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -54,16 +52,16 @@ API_KEY = "apikey"
|
|||||||
SSL = False
|
SSL = False
|
||||||
VERIFY_SSL = True
|
VERIFY_SSL = True
|
||||||
|
|
||||||
CONF_DATA_DEFAULTS = {
|
CONFIG_DATA_DEFAULTS = {
|
||||||
CONF_HOST: f"{HOST}:{PORT}",
|
CONF_HOST: f"{HOST}:{PORT}",
|
||||||
CONF_LOCATION: DEFAULT_LOCATION,
|
CONF_LOCATION: DEFAULT_LOCATION,
|
||||||
CONF_NAME: DEFAULT_NAME,
|
CONF_NAME: DEFAULT_NAME,
|
||||||
CONF_STATISTICS_ONLY: DEFAULT_STATISTICS_ONLY,
|
|
||||||
CONF_SSL: DEFAULT_SSL,
|
CONF_SSL: DEFAULT_SSL,
|
||||||
CONF_VERIFY_SSL: DEFAULT_VERIFY_SSL,
|
CONF_VERIFY_SSL: DEFAULT_VERIFY_SSL,
|
||||||
|
CONF_API_KEY: API_KEY,
|
||||||
}
|
}
|
||||||
|
|
||||||
CONF_DATA = {
|
CONFIG_DATA = {
|
||||||
CONF_HOST: f"{HOST}:{PORT}",
|
CONF_HOST: f"{HOST}:{PORT}",
|
||||||
CONF_LOCATION: LOCATION,
|
CONF_LOCATION: LOCATION,
|
||||||
CONF_NAME: NAME,
|
CONF_NAME: NAME,
|
||||||
@ -72,25 +70,20 @@ CONF_DATA = {
|
|||||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
CONF_VERIFY_SSL: VERIFY_SSL,
|
||||||
}
|
}
|
||||||
|
|
||||||
CONF_CONFIG_FLOW_USER = {
|
CONFIG_FLOW_USER = {
|
||||||
CONF_HOST: HOST,
|
CONF_HOST: HOST,
|
||||||
CONF_PORT: PORT,
|
CONF_PORT: PORT,
|
||||||
|
CONF_API_KEY: API_KEY,
|
||||||
CONF_LOCATION: LOCATION,
|
CONF_LOCATION: LOCATION,
|
||||||
CONF_NAME: NAME,
|
CONF_NAME: NAME,
|
||||||
CONF_STATISTICS_ONLY: False,
|
|
||||||
CONF_SSL: SSL,
|
CONF_SSL: SSL,
|
||||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
CONF_VERIFY_SSL: VERIFY_SSL,
|
||||||
}
|
}
|
||||||
|
|
||||||
CONF_CONFIG_FLOW_API_KEY = {
|
CONFIG_ENTRY = {
|
||||||
CONF_API_KEY: API_KEY,
|
|
||||||
}
|
|
||||||
|
|
||||||
CONF_CONFIG_ENTRY = {
|
|
||||||
CONF_HOST: f"{HOST}:{PORT}",
|
CONF_HOST: f"{HOST}:{PORT}",
|
||||||
CONF_LOCATION: LOCATION,
|
CONF_LOCATION: LOCATION,
|
||||||
CONF_NAME: NAME,
|
CONF_NAME: NAME,
|
||||||
CONF_STATISTICS_ONLY: False,
|
|
||||||
CONF_API_KEY: API_KEY,
|
CONF_API_KEY: API_KEY,
|
||||||
CONF_SSL: SSL,
|
CONF_SSL: SSL,
|
||||||
CONF_VERIFY_SSL: VERIFY_SSL,
|
CONF_VERIFY_SSL: VERIFY_SSL,
|
||||||
@ -99,7 +92,7 @@ CONF_CONFIG_ENTRY = {
|
|||||||
SWITCH_ENTITY_ID = "switch.pi_hole"
|
SWITCH_ENTITY_ID = "switch.pi_hole"
|
||||||
|
|
||||||
|
|
||||||
def _create_mocked_hole(raise_exception=False, has_versions=True):
|
def _create_mocked_hole(raise_exception=False, has_versions=True, has_data=True):
|
||||||
mocked_hole = MagicMock()
|
mocked_hole = MagicMock()
|
||||||
type(mocked_hole).get_data = AsyncMock(
|
type(mocked_hole).get_data = AsyncMock(
|
||||||
side_effect=HoleError("") if raise_exception else None
|
side_effect=HoleError("") if raise_exception else None
|
||||||
@ -109,7 +102,10 @@ def _create_mocked_hole(raise_exception=False, has_versions=True):
|
|||||||
)
|
)
|
||||||
type(mocked_hole).enable = AsyncMock()
|
type(mocked_hole).enable = AsyncMock()
|
||||||
type(mocked_hole).disable = AsyncMock()
|
type(mocked_hole).disable = AsyncMock()
|
||||||
|
if has_data:
|
||||||
mocked_hole.data = ZERO_DATA
|
mocked_hole.data = ZERO_DATA
|
||||||
|
else:
|
||||||
|
mocked_hole.data = []
|
||||||
if has_versions:
|
if has_versions:
|
||||||
mocked_hole.versions = SAMPLE_VERSIONS
|
mocked_hole.versions = SAMPLE_VERSIONS
|
||||||
else:
|
else:
|
||||||
|
@ -1,31 +1,28 @@
|
|||||||
"""Test pi_hole config flow."""
|
"""Test pi_hole config flow."""
|
||||||
from homeassistant.components.pi_hole.const import CONF_STATISTICS_ONLY, DOMAIN
|
from homeassistant.components import pi_hole
|
||||||
|
from homeassistant.components.pi_hole.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_USER
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
CONF_CONFIG_ENTRY,
|
CONFIG_DATA_DEFAULTS,
|
||||||
CONF_CONFIG_FLOW_API_KEY,
|
CONFIG_ENTRY,
|
||||||
CONF_CONFIG_FLOW_USER,
|
CONFIG_FLOW_USER,
|
||||||
NAME,
|
NAME,
|
||||||
|
ZERO_DATA,
|
||||||
_create_mocked_hole,
|
_create_mocked_hole,
|
||||||
_patch_config_flow_hole,
|
_patch_config_flow_hole,
|
||||||
|
_patch_init_hole,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
def _flow_next(hass: HomeAssistant, flow_id: str):
|
|
||||||
return next(
|
|
||||||
flow
|
|
||||||
for flow in hass.config_entries.flow.async_progress()
|
|
||||||
if flow["flow_id"] == flow_id
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_user(hass: HomeAssistant):
|
async def test_flow_user(hass: HomeAssistant):
|
||||||
"""Test user initialized flow."""
|
"""Test user initialized flow."""
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole(has_data=False)
|
||||||
with _patch_config_flow_hole(mocked_hole):
|
with _patch_config_flow_hole(mocked_hole):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@ -34,69 +31,68 @@ async def test_flow_user(hass: HomeAssistant):
|
|||||||
assert result["type"] == FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
_flow_next(hass, result["flow_id"])
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input=CONF_CONFIG_FLOW_USER,
|
user_input=CONFIG_FLOW_USER,
|
||||||
)
|
)
|
||||||
assert result["type"] == FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == "api_key"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] is None
|
assert result["errors"] == {CONF_API_KEY: "invalid_auth"}
|
||||||
_flow_next(hass, result["flow_id"])
|
|
||||||
|
|
||||||
|
mocked_hole.data = ZERO_DATA
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input=CONF_CONFIG_FLOW_API_KEY,
|
user_input=CONFIG_FLOW_USER,
|
||||||
)
|
)
|
||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == NAME
|
assert result["title"] == NAME
|
||||||
assert result["data"] == CONF_CONFIG_ENTRY
|
assert result["data"] == CONFIG_ENTRY
|
||||||
|
|
||||||
# duplicated server
|
# duplicated server
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
data=CONF_CONFIG_FLOW_USER,
|
data=CONFIG_FLOW_USER,
|
||||||
)
|
)
|
||||||
assert result["type"] == FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_statistics_only(hass: HomeAssistant):
|
|
||||||
"""Test user initialized flow with statistics only."""
|
|
||||||
mocked_hole = _create_mocked_hole()
|
|
||||||
with _patch_config_flow_hole(mocked_hole):
|
|
||||||
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"] == {}
|
|
||||||
_flow_next(hass, result["flow_id"])
|
|
||||||
|
|
||||||
user_input = {**CONF_CONFIG_FLOW_USER}
|
|
||||||
user_input[CONF_STATISTICS_ONLY] = True
|
|
||||||
config_entry_data = {**CONF_CONFIG_ENTRY}
|
|
||||||
config_entry_data[CONF_STATISTICS_ONLY] = True
|
|
||||||
config_entry_data.pop(CONF_API_KEY)
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
user_input=user_input,
|
|
||||||
)
|
|
||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["title"] == NAME
|
|
||||||
assert result["data"] == config_entry_data
|
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_user_invalid(hass: HomeAssistant):
|
async def test_flow_user_invalid(hass: HomeAssistant):
|
||||||
"""Test user initialized flow with invalid server."""
|
"""Test user initialized flow with invalid server."""
|
||||||
mocked_hole = _create_mocked_hole(True)
|
mocked_hole = _create_mocked_hole(True)
|
||||||
with _patch_config_flow_hole(mocked_hole):
|
with _patch_config_flow_hole(mocked_hole):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW_USER
|
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG_FLOW_USER
|
||||||
)
|
)
|
||||||
assert result["type"] == FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] == {"base": "cannot_connect"}
|
assert result["errors"] == {"base": "cannot_connect"}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_flow_reauth(hass: HomeAssistant):
|
||||||
|
"""Test reauth flow."""
|
||||||
|
mocked_hole = _create_mocked_hole(has_data=False)
|
||||||
|
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
with _patch_init_hole(mocked_hole), _patch_config_flow_hole(mocked_hole):
|
||||||
|
assert not await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
|
||||||
|
flows = hass.config_entries.flow.async_progress()
|
||||||
|
|
||||||
|
assert len(flows) == 1
|
||||||
|
assert flows[0]["step_id"] == "reauth_confirm"
|
||||||
|
assert flows[0]["context"]["entry_id"] == entry.entry_id
|
||||||
|
|
||||||
|
mocked_hole.data = ZERO_DATA
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
flows[0]["flow_id"],
|
||||||
|
user_input={CONF_API_KEY: "newkey"},
|
||||||
|
)
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert result["type"] == FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reauth_successful"
|
||||||
|
assert entry.data[CONF_API_KEY] == "newkey"
|
||||||
|
@ -7,28 +7,16 @@ from hole.exceptions import HoleError
|
|||||||
from homeassistant.components import pi_hole, switch
|
from homeassistant.components import pi_hole, switch
|
||||||
from homeassistant.components.pi_hole.const import (
|
from homeassistant.components.pi_hole.const import (
|
||||||
CONF_STATISTICS_ONLY,
|
CONF_STATISTICS_ONLY,
|
||||||
DEFAULT_LOCATION,
|
|
||||||
DEFAULT_NAME,
|
|
||||||
DEFAULT_SSL,
|
|
||||||
DEFAULT_VERIFY_SSL,
|
|
||||||
SERVICE_DISABLE,
|
SERVICE_DISABLE,
|
||||||
SERVICE_DISABLE_ATTR_DURATION,
|
SERVICE_DISABLE_ATTR_DURATION,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
ATTR_ENTITY_ID,
|
from homeassistant.const import ATTR_ENTITY_ID, CONF_API_KEY, CONF_HOST, CONF_NAME
|
||||||
CONF_API_KEY,
|
|
||||||
CONF_HOST,
|
|
||||||
CONF_LOCATION,
|
|
||||||
CONF_NAME,
|
|
||||||
CONF_SSL,
|
|
||||||
CONF_VERIFY_SSL,
|
|
||||||
)
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
CONF_CONFIG_ENTRY,
|
CONFIG_DATA,
|
||||||
CONF_DATA,
|
CONFIG_DATA_DEFAULTS,
|
||||||
CONF_DATA_DEFAULTS,
|
|
||||||
SWITCH_ENTITY_ID,
|
SWITCH_ENTITY_ID,
|
||||||
_create_mocked_hole,
|
_create_mocked_hole,
|
||||||
_patch_init_hole,
|
_patch_init_hole,
|
||||||
@ -40,7 +28,9 @@ from tests.common import MockConfigEntry
|
|||||||
async def test_setup_with_defaults(hass: HomeAssistant):
|
async def test_setup_with_defaults(hass: HomeAssistant):
|
||||||
"""Tests component setup with default config."""
|
"""Tests component setup with default config."""
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole()
|
||||||
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONF_DATA_DEFAULTS)
|
entry = MockConfigEntry(
|
||||||
|
domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_STATISTICS_ONLY: True}
|
||||||
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
with _patch_init_hole(mocked_hole):
|
with _patch_init_hole(mocked_hole):
|
||||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -90,7 +80,7 @@ async def test_setup_name_config(hass: HomeAssistant):
|
|||||||
"""Tests component setup with a custom name."""
|
"""Tests component setup with a custom name."""
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole()
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=pi_hole.DOMAIN, data={**CONF_DATA_DEFAULTS, CONF_NAME: "Custom"}
|
domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_NAME: "Custom"}
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
with _patch_init_hole(mocked_hole):
|
with _patch_init_hole(mocked_hole):
|
||||||
@ -107,7 +97,7 @@ async def test_setup_name_config(hass: HomeAssistant):
|
|||||||
async def test_switch(hass: HomeAssistant, caplog):
|
async def test_switch(hass: HomeAssistant, caplog):
|
||||||
"""Test Pi-hole switch."""
|
"""Test Pi-hole switch."""
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole()
|
||||||
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONF_DATA)
|
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
with _patch_init_hole(mocked_hole):
|
with _patch_init_hole(mocked_hole):
|
||||||
@ -156,12 +146,12 @@ async def test_disable_service_call(hass: HomeAssistant):
|
|||||||
|
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole()
|
||||||
with _patch_init_hole(mocked_hole):
|
with _patch_init_hole(mocked_hole):
|
||||||
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONF_DATA)
|
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=pi_hole.DOMAIN, data={**CONF_DATA_DEFAULTS, CONF_NAME: "Custom"}
|
domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_NAME: "Custom"}
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -177,21 +167,14 @@ async def test_disable_service_call(hass: HomeAssistant):
|
|||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
mocked_hole.disable.assert_called_once_with(1)
|
mocked_hole.disable.assert_called_with(1)
|
||||||
|
|
||||||
|
|
||||||
async def test_unload(hass: HomeAssistant):
|
async def test_unload(hass: HomeAssistant):
|
||||||
"""Test unload entities."""
|
"""Test unload entities."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=pi_hole.DOMAIN,
|
domain=pi_hole.DOMAIN,
|
||||||
data={
|
data={**CONFIG_DATA_DEFAULTS, CONF_HOST: "pi.hole"},
|
||||||
CONF_NAME: DEFAULT_NAME,
|
|
||||||
CONF_HOST: "pi.hole",
|
|
||||||
CONF_LOCATION: DEFAULT_LOCATION,
|
|
||||||
CONF_SSL: DEFAULT_SSL,
|
|
||||||
CONF_VERIFY_SSL: DEFAULT_VERIFY_SSL,
|
|
||||||
CONF_STATISTICS_ONLY: True,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole()
|
||||||
@ -199,38 +182,32 @@ async def test_unload(hass: HomeAssistant):
|
|||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert entry.entry_id in hass.data[pi_hole.DOMAIN]
|
assert entry.entry_id in hass.data[pi_hole.DOMAIN]
|
||||||
|
|
||||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert entry.entry_id not in hass.data[pi_hole.DOMAIN]
|
assert entry.entry_id not in hass.data[pi_hole.DOMAIN]
|
||||||
|
|
||||||
|
|
||||||
async def test_migrate(hass: HomeAssistant):
|
async def test_remove_obsolete(hass: HomeAssistant):
|
||||||
"""Test migrate from old config entry."""
|
"""Test removing obsolete config entry parameters."""
|
||||||
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONF_DATA)
|
|
||||||
entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole()
|
||||||
with _patch_init_hole(mocked_hole):
|
entry = MockConfigEntry(
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_STATISTICS_ONLY: True}
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
|
|
||||||
assert entry.data == CONF_CONFIG_ENTRY
|
|
||||||
|
|
||||||
|
|
||||||
async def test_migrate_statistics_only(hass: HomeAssistant):
|
|
||||||
"""Test migrate from old config entry with statistics only."""
|
|
||||||
conf_data = {**CONF_DATA}
|
|
||||||
conf_data[CONF_API_KEY] = ""
|
|
||||||
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=conf_data)
|
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
mocked_hole = _create_mocked_hole()
|
|
||||||
with _patch_init_hole(mocked_hole):
|
with _patch_init_hole(mocked_hole):
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
assert CONF_STATISTICS_ONLY not in entry.data
|
||||||
|
|
||||||
config_entry_data = {**CONF_CONFIG_ENTRY}
|
|
||||||
config_entry_data[CONF_STATISTICS_ONLY] = True
|
async def test_missing_api_key(hass: HomeAssistant):
|
||||||
config_entry_data[CONF_API_KEY] = ""
|
"""Tests start reauth flow if api key is missing."""
|
||||||
assert entry.data == config_entry_data
|
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
|
||||||
|
@ -4,7 +4,7 @@ from homeassistant.components import pi_hole
|
|||||||
from homeassistant.const import STATE_ON, STATE_UNKNOWN
|
from homeassistant.const import STATE_ON, STATE_UNKNOWN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import CONF_DATA_DEFAULTS, _create_mocked_hole, _patch_init_hole
|
from . import CONFIG_DATA_DEFAULTS, _create_mocked_hole, _patch_init_hole
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -12,7 +12,7 @@ from tests.common import MockConfigEntry
|
|||||||
async def test_update(hass: HomeAssistant):
|
async def test_update(hass: HomeAssistant):
|
||||||
"""Tests update entity."""
|
"""Tests update entity."""
|
||||||
mocked_hole = _create_mocked_hole()
|
mocked_hole = _create_mocked_hole()
|
||||||
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONF_DATA_DEFAULTS)
|
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
with _patch_init_hole(mocked_hole):
|
with _patch_init_hole(mocked_hole):
|
||||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -53,7 +53,7 @@ async def test_update(hass: HomeAssistant):
|
|||||||
async def test_update_no_versions(hass: HomeAssistant):
|
async def test_update_no_versions(hass: HomeAssistant):
|
||||||
"""Tests update entity when no version data available."""
|
"""Tests update entity when no version data available."""
|
||||||
mocked_hole = _create_mocked_hole(has_versions=False)
|
mocked_hole = _create_mocked_hole(has_versions=False)
|
||||||
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONF_DATA_DEFAULTS)
|
entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
with _patch_init_hole(mocked_hole):
|
with _patch_init_hole(mocked_hole):
|
||||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user