Address NextDNS late review (#75635)

* Init instance attributes

* Remove condition

* Improve typing in tests

* Suggested change
This commit is contained in:
Maciej Bieniek 2022-07-23 00:58:48 +02:00 committed by GitHub
parent 402e533fef
commit cb543a21b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 28 deletions

View file

@ -24,8 +24,8 @@ class NextDnsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize the config flow.""" """Initialize the config flow."""
self.nextdns: NextDns self.nextdns: NextDns | None = None
self.api_key: str self.api_key: str | None = None
async def async_step_user( async def async_step_user(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
@ -63,6 +63,8 @@ class NextDnsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle the profiles step.""" """Handle the profiles step."""
errors: dict[str, str] = {} errors: dict[str, str] = {}
assert self.nextdns is not None
if user_input is not None: if user_input is not None:
profile_name = user_input[CONF_PROFILE_NAME] profile_name = user_input[CONF_PROFILE_NAME]
profile_id = self.nextdns.get_profile_id(profile_name) profile_id = self.nextdns.get_profile_id(profile_name)

View file

@ -57,9 +57,7 @@ SETTINGS = Settings(
) )
async def init_integration( async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
hass: HomeAssistant, add_to_hass: bool = True
) -> MockConfigEntry:
"""Set up the NextDNS integration in Home Assistant.""" """Set up the NextDNS integration in Home Assistant."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -68,9 +66,6 @@ async def init_integration(
data={CONF_API_KEY: "fake_api_key", CONF_PROFILE_ID: "xyz12"}, data={CONF_API_KEY: "fake_api_key", CONF_PROFILE_ID: "xyz12"},
) )
if not add_to_hass:
return entry
with patch( with patch(
"homeassistant.components.nextdns.NextDns.get_profiles", return_value=PROFILES "homeassistant.components.nextdns.NextDns.get_profiles", return_value=PROFILES
), patch( ), patch(

View file

@ -3,13 +3,14 @@ from unittest.mock import patch
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from . import init_integration from . import init_integration
async def test_button(hass): async def test_button(hass: HomeAssistant) -> None:
"""Test states of the button.""" """Test states of the button."""
registry = er.async_get(hass) registry = er.async_get(hass)
@ -24,7 +25,7 @@ async def test_button(hass):
assert entry.unique_id == "xyz12_clear_logs" assert entry.unique_id == "xyz12_clear_logs"
async def test_button_press(hass): async def test_button_press(hass: HomeAssistant) -> None:
"""Test button press.""" """Test button press."""
await init_integration(hass) await init_integration(hass)

View file

@ -13,11 +13,12 @@ from homeassistant.components.nextdns.const import (
) )
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 . import PROFILES, init_integration 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.""" """Test that the user step works."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
@ -52,7 +53,7 @@ async def test_form_create_entry(hass):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"error", "exc,base_error",
[ [
(ApiError("API Error"), "cannot_connect"), (ApiError("API Error"), "cannot_connect"),
(InvalidApiKeyError, "invalid_api_key"), (InvalidApiKeyError, "invalid_api_key"),
@ -60,9 +61,10 @@ async def test_form_create_entry(hass):
(ValueError, "unknown"), (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.""" """Test we handle errors."""
exc, base_error = error
with patch( with patch(
"homeassistant.components.nextdns.NextDns.get_profiles", side_effect=exc "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} 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.""" """Test that errors are shown when duplicates are added."""
entry = await init_integration(hass) await init_integration(hass)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}

View file

@ -1,11 +1,18 @@
"""Test NextDNS diagnostics.""" """Test NextDNS diagnostics."""
from collections.abc import Awaitable, Callable
from aiohttp import ClientSession
from homeassistant.components.diagnostics import REDACTED from homeassistant.components.diagnostics import REDACTED
from homeassistant.core import HomeAssistant
from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.components.nextdns import init_integration 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.""" """Test config entry diagnostics."""
entry = await init_integration(hass) entry = await init_integration(hass)

View file

@ -3,14 +3,17 @@ from unittest.mock import patch
from nextdns import ApiError 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.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 . 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.""" """Test a successful setup entry."""
await init_integration(hass) await init_integration(hass)
@ -20,9 +23,14 @@ async def test_async_setup_entry(hass):
assert state.state == "20.0" 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.""" """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( with patch(
"homeassistant.components.nextdns.NextDns.get_profiles", "homeassistant.components.nextdns.NextDns.get_profiles",
@ -33,7 +41,7 @@ async def test_config_not_ready(hass):
assert entry.state is ConfigEntryState.SETUP_RETRY 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.""" """Test successful unload of entry."""
entry = await init_integration(hass) entry = await init_integration(hass)

View file

@ -11,6 +11,7 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, STATE_UNAVAILABLE 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.helpers import entity_registry as er
from homeassistant.util.dt import utcnow 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 from tests.common import async_fire_time_changed
async def test_sensor(hass): async def test_sensor(hass: HomeAssistant) -> None:
"""Test states of sensors.""" """Test states of sensors."""
registry = er.async_get(hass) registry = er.async_get(hass)
@ -390,7 +391,7 @@ async def test_sensor(hass):
assert entry.unique_id == "xyz12_udp_queries_ratio" 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.""" """Ensure that we mark the entities unavailable correctly when service causes an error."""
registry = er.async_get(hass) registry = er.async_get(hass)

View file

@ -5,12 +5,16 @@ from aiohttp import ClientError
from nextdns.const import API_ENDPOINT from nextdns.const import API_ENDPOINT
from homeassistant.components.nextdns.const import DOMAIN from homeassistant.components.nextdns.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import get_system_health_info 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.""" """Test NextDNS system health."""
aioclient_mock.get(API_ENDPOINT, text="") aioclient_mock.get(API_ENDPOINT, text="")
hass.config.components.add(DOMAIN) hass.config.components.add(DOMAIN)
@ -25,7 +29,9 @@ async def test_nextdns_system_health(hass, aioclient_mock):
assert info == {"can_reach_server": "ok"} 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.""" """Test NextDNS system health."""
aioclient_mock.get(API_ENDPOINT, exc=ClientError) aioclient_mock.get(API_ENDPOINT, exc=ClientError)
hass.config.components.add(DOMAIN) hass.config.components.add(DOMAIN)