Address NextDNS late review (#75635)
* Init instance attributes * Remove condition * Improve typing in tests * Suggested change
This commit is contained in:
parent
402e533fef
commit
cb543a21b3
8 changed files with 49 additions and 28 deletions
|
@ -24,8 +24,8 @@ class NextDnsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the config flow."""
|
||||
self.nextdns: NextDns
|
||||
self.api_key: str
|
||||
self.nextdns: NextDns | None = None
|
||||
self.api_key: str | None = None
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
|
@ -63,6 +63,8 @@ class NextDnsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
"""Handle the profiles step."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
assert self.nextdns is not None
|
||||
|
||||
if user_input is not None:
|
||||
profile_name = user_input[CONF_PROFILE_NAME]
|
||||
profile_id = self.nextdns.get_profile_id(profile_name)
|
||||
|
|
|
@ -57,9 +57,7 @@ SETTINGS = Settings(
|
|||
)
|
||||
|
||||
|
||||
async def init_integration(
|
||||
hass: HomeAssistant, add_to_hass: bool = True
|
||||
) -> MockConfigEntry:
|
||||
async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
|
||||
"""Set up the NextDNS integration in Home Assistant."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
|
@ -68,9 +66,6 @@ async def init_integration(
|
|||
data={CONF_API_KEY: "fake_api_key", CONF_PROFILE_ID: "xyz12"},
|
||||
)
|
||||
|
||||
if not add_to_hass:
|
||||
return entry
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nextdns.NextDns.get_profiles", return_value=PROFILES
|
||||
), patch(
|
||||
|
|
|
@ -3,13 +3,14 @@ from unittest.mock import patch
|
|||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import init_integration
|
||||
|
||||
|
||||
async def test_button(hass):
|
||||
async def test_button(hass: HomeAssistant) -> None:
|
||||
"""Test states of the button."""
|
||||
registry = er.async_get(hass)
|
||||
|
||||
|
@ -24,7 +25,7 @@ async def test_button(hass):
|
|||
assert entry.unique_id == "xyz12_clear_logs"
|
||||
|
||||
|
||||
async def test_button_press(hass):
|
||||
async def test_button_press(hass: HomeAssistant) -> None:
|
||||
"""Test button press."""
|
||||
await init_integration(hass)
|
||||
|
||||
|
|
|
@ -13,11 +13,12 @@ from homeassistant.components.nextdns.const import (
|
|||
)
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import PROFILES, init_integration
|
||||
|
||||
|
||||
async def test_form_create_entry(hass):
|
||||
async def test_form_create_entry(hass: HomeAssistant) -> None:
|
||||
"""Test that the user step works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
|
@ -52,7 +53,7 @@ async def test_form_create_entry(hass):
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error",
|
||||
"exc,base_error",
|
||||
[
|
||||
(ApiError("API Error"), "cannot_connect"),
|
||||
(InvalidApiKeyError, "invalid_api_key"),
|
||||
|
@ -60,9 +61,10 @@ async def test_form_create_entry(hass):
|
|||
(ValueError, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_form_errors(hass, error):
|
||||
async def test_form_errors(
|
||||
hass: HomeAssistant, exc: Exception, base_error: str
|
||||
) -> None:
|
||||
"""Test we handle errors."""
|
||||
exc, base_error = error
|
||||
with patch(
|
||||
"homeassistant.components.nextdns.NextDns.get_profiles", side_effect=exc
|
||||
):
|
||||
|
@ -75,10 +77,9 @@ async def test_form_errors(hass, error):
|
|||
assert result["errors"] == {"base": base_error}
|
||||
|
||||
|
||||
async def test_form_already_configured(hass):
|
||||
async def test_form_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
entry = await init_integration(hass)
|
||||
entry.add_to_hass(hass)
|
||||
await init_integration(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
|
|
|
@ -1,11 +1,18 @@
|
|||
"""Test NextDNS diagnostics."""
|
||||
from collections.abc import Awaitable, Callable
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from homeassistant.components.diagnostics import REDACTED
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.components.nextdns import init_integration
|
||||
|
||||
|
||||
async def test_entry_diagnostics(hass, hass_client):
|
||||
async def test_entry_diagnostics(
|
||||
hass: HomeAssistant, hass_client: Callable[..., Awaitable[ClientSession]]
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
entry = await init_integration(hass)
|
||||
|
||||
|
|
|
@ -3,14 +3,17 @@ from unittest.mock import patch
|
|||
|
||||
from nextdns import ApiError
|
||||
|
||||
from homeassistant.components.nextdns.const import DOMAIN
|
||||
from homeassistant.components.nextdns.const import CONF_PROFILE_ID, DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.const import CONF_API_KEY, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import init_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
async def test_async_setup_entry(hass):
|
||||
|
||||
async def test_async_setup_entry(hass: HomeAssistant) -> None:
|
||||
"""Test a successful setup entry."""
|
||||
await init_integration(hass)
|
||||
|
||||
|
@ -20,9 +23,14 @@ async def test_async_setup_entry(hass):
|
|||
assert state.state == "20.0"
|
||||
|
||||
|
||||
async def test_config_not_ready(hass):
|
||||
async def test_config_not_ready(hass: HomeAssistant) -> None:
|
||||
"""Test for setup failure if the connection to the service fails."""
|
||||
entry = await init_integration(hass, add_to_hass=False)
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="Fake Profile",
|
||||
unique_id="xyz12",
|
||||
data={CONF_API_KEY: "fake_api_key", CONF_PROFILE_ID: "xyz12"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nextdns.NextDns.get_profiles",
|
||||
|
@ -33,7 +41,7 @@ async def test_config_not_ready(hass):
|
|||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
async def test_unload_entry(hass: HomeAssistant) -> None:
|
||||
"""Test successful unload of entry."""
|
||||
entry = await init_integration(hass)
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ from homeassistant.components.sensor import (
|
|||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
|
@ -19,7 +20,7 @@ from . import DNSSEC, ENCRYPTION, IP_VERSIONS, PROTOCOLS, STATUS, init_integrati
|
|||
from tests.common import async_fire_time_changed
|
||||
|
||||
|
||||
async def test_sensor(hass):
|
||||
async def test_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test states of sensors."""
|
||||
registry = er.async_get(hass)
|
||||
|
||||
|
@ -390,7 +391,7 @@ async def test_sensor(hass):
|
|||
assert entry.unique_id == "xyz12_udp_queries_ratio"
|
||||
|
||||
|
||||
async def test_availability(hass):
|
||||
async def test_availability(hass: HomeAssistant) -> None:
|
||||
"""Ensure that we mark the entities unavailable correctly when service causes an error."""
|
||||
registry = er.async_get(hass)
|
||||
|
||||
|
|
|
@ -5,12 +5,16 @@ from aiohttp import ClientError
|
|||
from nextdns.const import API_ENDPOINT
|
||||
|
||||
from homeassistant.components.nextdns.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import get_system_health_info
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def test_nextdns_system_health(hass, aioclient_mock):
|
||||
async def test_nextdns_system_health(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test NextDNS system health."""
|
||||
aioclient_mock.get(API_ENDPOINT, text="")
|
||||
hass.config.components.add(DOMAIN)
|
||||
|
@ -25,7 +29,9 @@ async def test_nextdns_system_health(hass, aioclient_mock):
|
|||
assert info == {"can_reach_server": "ok"}
|
||||
|
||||
|
||||
async def test_nextdns_system_health_fail(hass, aioclient_mock):
|
||||
async def test_nextdns_system_health_fail(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test NextDNS system health."""
|
||||
aioclient_mock.get(API_ENDPOINT, exc=ClientError)
|
||||
hass.config.components.add(DOMAIN)
|
||||
|
|
Loading…
Add table
Reference in a new issue