From f85fc7173f99456df5db018a0dbf085f9862ed51 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Tue, 1 Jul 2025 15:39:29 +0200 Subject: [PATCH] Bump Nettigo Air Monitor backend library to version 5.0.0 (#147812) --- homeassistant/components/nam/__init__.py | 9 - homeassistant/components/nam/config_flow.py | 65 +++---- homeassistant/components/nam/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/nam/__init__.py | 5 +- tests/components/nam/test_config_flow.py | 199 +++++++------------- tests/components/nam/test_init.py | 23 +-- 8 files changed, 94 insertions(+), 213 deletions(-) diff --git a/homeassistant/components/nam/__init__.py b/homeassistant/components/nam/__init__.py index d297443c059..03ad5118352 100644 --- a/homeassistant/components/nam/__init__.py +++ b/homeassistant/components/nam/__init__.py @@ -44,15 +44,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: NAMConfigEntry) -> bool: translation_key="device_communication_error", translation_placeholders={"device": entry.title}, ) from err - - try: - await nam.async_check_credentials() - except (ApiError, ClientError) as err: - raise ConfigEntryNotReady( - translation_domain=DOMAIN, - translation_key="device_communication_error", - translation_placeholders={"device": entry.title}, - ) from err except AuthFailedError as err: raise ConfigEntryAuthFailed( translation_domain=DOMAIN, diff --git a/homeassistant/components/nam/config_flow.py b/homeassistant/components/nam/config_flow.py index fa94971e2ef..b90426b66e5 100644 --- a/homeassistant/components/nam/config_flow.py +++ b/homeassistant/components/nam/config_flow.py @@ -3,7 +3,6 @@ from __future__ import annotations from collections.abc import Mapping -from dataclasses import dataclass import logging from typing import Any @@ -26,15 +25,6 @@ from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo from .const import DOMAIN - -@dataclass -class NamConfig: - """NAM device configuration class.""" - - mac_address: str - auth_enabled: bool - - _LOGGER = logging.getLogger(__name__) AUTH_SCHEMA = vol.Schema( @@ -42,29 +32,14 @@ AUTH_SCHEMA = vol.Schema( ) -async def async_get_config(hass: HomeAssistant, host: str) -> NamConfig: - """Get device MAC address and auth_enabled property.""" - websession = async_get_clientsession(hass) - - options = ConnectionOptions(host) - nam = await NettigoAirMonitor.create(websession, options) - - mac = await nam.async_get_mac_address() - - return NamConfig(mac, nam.auth_enabled) - - -async def async_check_credentials( +async def async_get_nam( hass: HomeAssistant, host: str, data: dict[str, Any] -) -> None: - """Check if credentials are valid.""" +) -> NettigoAirMonitor: + """Get NAM client.""" websession = async_get_clientsession(hass) - options = ConnectionOptions(host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD)) - nam = await NettigoAirMonitor.create(websession, options) - - await nam.async_check_credentials() + return await NettigoAirMonitor.create(websession, options) class NAMFlowHandler(ConfigFlow, domain=DOMAIN): @@ -72,8 +47,8 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - _config: NamConfig host: str + auth_enabled: bool = False async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -85,21 +60,20 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): self.host = user_input[CONF_HOST] try: - config = await async_get_config(self.hass, self.host) + nam = await async_get_nam(self.hass, self.host, {}) except (ApiError, ClientConnectorError, TimeoutError): errors["base"] = "cannot_connect" except CannotGetMacError: return self.async_abort(reason="device_unsupported") + except AuthFailedError: + return await self.async_step_credentials() except Exception: _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" else: - await self.async_set_unique_id(format_mac(config.mac_address)) + await self.async_set_unique_id(format_mac(nam.mac)) self._abort_if_unique_id_configured({CONF_HOST: self.host}) - if config.auth_enabled is True: - return await self.async_step_credentials() - return self.async_create_entry( title=self.host, data=user_input, @@ -119,7 +93,7 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): if user_input is not None: try: - await async_check_credentials(self.hass, self.host, user_input) + nam = await async_get_nam(self.hass, self.host, user_input) except AuthFailedError: errors["base"] = "invalid_auth" except (ApiError, ClientConnectorError, TimeoutError): @@ -128,6 +102,9 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" else: + await self.async_set_unique_id(format_mac(nam.mac)) + self._abort_if_unique_id_configured({CONF_HOST: self.host}) + return self.async_create_entry( title=self.host, data={**user_input, CONF_HOST: self.host}, @@ -148,14 +125,16 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): self._async_abort_entries_match({CONF_HOST: self.host}) try: - self._config = await async_get_config(self.hass, self.host) + nam = await async_get_nam(self.hass, self.host, {}) except (ApiError, ClientConnectorError, TimeoutError): return self.async_abort(reason="cannot_connect") except CannotGetMacError: return self.async_abort(reason="device_unsupported") + except AuthFailedError: + self.auth_enabled = True + return await self.async_step_confirm_discovery() - await self.async_set_unique_id(format_mac(self._config.mac_address)) - self._abort_if_unique_id_configured({CONF_HOST: self.host}) + await self.async_set_unique_id(format_mac(nam.mac)) return await self.async_step_confirm_discovery() @@ -171,7 +150,7 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): data={CONF_HOST: self.host}, ) - if self._config.auth_enabled is True: + if self.auth_enabled is True: return await self.async_step_credentials() self._set_confirm_only() @@ -198,7 +177,7 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): if user_input is not None: try: - await async_check_credentials(self.hass, self.host, user_input) + await async_get_nam(self.hass, self.host, user_input) except ( ApiError, AuthFailedError, @@ -228,11 +207,11 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN): if user_input is not None: try: - config = await async_get_config(self.hass, user_input[CONF_HOST]) + nam = await async_get_nam(self.hass, user_input[CONF_HOST], {}) except (ApiError, ClientConnectorError, TimeoutError): errors["base"] = "cannot_connect" else: - await self.async_set_unique_id(format_mac(config.mac_address)) + await self.async_set_unique_id(format_mac(nam.mac)) self._abort_if_unique_id_mismatch(reason="another_device") return self.async_update_reload_and_abort( diff --git a/homeassistant/components/nam/manifest.json b/homeassistant/components/nam/manifest.json index 1c3b9db7a86..4799f657dda 100644 --- a/homeassistant/components/nam/manifest.json +++ b/homeassistant/components/nam/manifest.json @@ -7,7 +7,7 @@ "integration_type": "device", "iot_class": "local_polling", "loggers": ["nettigo_air_monitor"], - "requirements": ["nettigo-air-monitor==4.1.0"], + "requirements": ["nettigo-air-monitor==5.0.0"], "zeroconf": [ { "type": "_http._tcp.local.", diff --git a/requirements_all.txt b/requirements_all.txt index dfa6c4ada1e..048c0f161f5 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1497,7 +1497,7 @@ netdata==1.3.0 netmap==0.7.0.2 # homeassistant.components.nam -nettigo-air-monitor==4.1.0 +nettigo-air-monitor==5.0.0 # homeassistant.components.neurio_energy neurio==0.3.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 6360dc34cd2..ab83d9d3586 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1283,7 +1283,7 @@ nessclient==1.2.0 netmap==0.7.0.2 # homeassistant.components.nam -nettigo-air-monitor==4.1.0 +nettigo-air-monitor==5.0.0 # homeassistant.components.nexia nexia==2.10.0 diff --git a/tests/components/nam/__init__.py b/tests/components/nam/__init__.py index c531d193359..e1063c108e4 100644 --- a/tests/components/nam/__init__.py +++ b/tests/components/nam/__init__.py @@ -33,7 +33,10 @@ async def init_integration( update_response = Mock(json=AsyncMock(return_value=nam_data)) with ( - patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), + patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", + ), patch( "homeassistant.components.nam.NettigoAirMonitor._async_http_request", return_value=update_response, diff --git a/tests/components/nam/test_config_flow.py b/tests/components/nam/test_config_flow.py index 80c6e86f420..e3c2397de77 100644 --- a/tests/components/nam/test_config_flow.py +++ b/tests/components/nam/test_config_flow.py @@ -1,7 +1,8 @@ """Define tests for the Nettigo Air Monitor config flow.""" +from collections.abc import Generator from ipaddress import ip_address -from unittest.mock import patch +from unittest.mock import AsyncMock, patch from nettigo_air_monitor import ApiError, AuthFailedError, CannotGetMacError import pytest @@ -26,11 +27,21 @@ DISCOVERY_INFO = ZeroconfServiceInfo( ) VALID_CONFIG = {"host": "10.10.2.3"} VALID_AUTH = {"username": "fake_username", "password": "fake_password"} -DEVICE_CONFIG = {"www_basicauth_enabled": False} -DEVICE_CONFIG_AUTH = {"www_basicauth_enabled": True} -async def test_form_create_entry_without_auth(hass: HomeAssistant) -> None: +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.nam.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + yield mock_setup_entry + + +async def test_form_create_entry_without_auth( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test that the user step without auth works.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} @@ -39,18 +50,9 @@ async def test_form_create_entry_without_auth(hass: HomeAssistant) -> None: assert result["step_id"] == "user" assert result["errors"] == {} - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), - patch( - "homeassistant.components.nam.async_setup_entry", return_value=True - ) as mock_setup_entry, + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -64,7 +66,9 @@ async def test_form_create_entry_without_auth(hass: HomeAssistant) -> None: assert len(mock_setup_entry.mock_calls) == 1 -async def test_form_create_entry_with_auth(hass: HomeAssistant) -> None: +async def test_form_create_entry_with_auth( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test that the user step with auth works.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} @@ -73,18 +77,9 @@ async def test_form_create_entry_with_auth(hass: HomeAssistant) -> None: assert result["step_id"] == "user" assert result["errors"] == {} - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG_AUTH, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), - patch( - "homeassistant.components.nam.async_setup_entry", return_value=True - ) as mock_setup_entry, + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + side_effect=[AuthFailedError("Authorization has failed"), "aa:bb:cc:dd:ee:ff"], ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -121,23 +116,17 @@ async def test_reauth_successful(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG_AUTH, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=VALID_AUTH, ) - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reauth_successful" + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_successful" async def test_reauth_unsuccessful(hass: HomeAssistant) -> None: @@ -154,7 +143,7 @@ async def test_reauth_unsuccessful(hass: HomeAssistant) -> None: assert result["step_id"] == "reauth_confirm" with patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", side_effect=ApiError("API Error"), ): result = await hass.config_entries.flow.async_configure( @@ -162,8 +151,8 @@ async def test_reauth_unsuccessful(hass: HomeAssistant) -> None: user_input=VALID_AUTH, ) - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reauth_unsuccessful" + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_unsuccessful" @pytest.mark.parametrize( @@ -178,15 +167,9 @@ async def test_reauth_unsuccessful(hass: HomeAssistant) -> None: async def test_form_with_auth_errors(hass: HomeAssistant, error) -> None: """Test we handle errors when auth is required.""" exc, base_error = error - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - side_effect=AuthFailedError("Auth Error"), - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + side_effect=AuthFailedError("Authorization has failed"), ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -198,7 +181,7 @@ async def test_form_with_auth_errors(hass: HomeAssistant, error) -> None: assert result["step_id"] == "credentials" with patch( - "homeassistant.components.nam.NettigoAirMonitor.initialize", + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", side_effect=exc, ): result = await hass.config_entries.flow.async_configure( @@ -236,10 +219,6 @@ async def test_form_errors(hass: HomeAssistant, error) -> None: async def test_form_abort(hass: HomeAssistant) -> None: """Test we handle abort after error.""" with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG, - ), patch( "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", side_effect=CannotGetMacError("Cannot get MAC address from device"), @@ -266,15 +245,9 @@ async def test_form_already_configured(hass: HomeAssistant) -> None: DOMAIN, context={"source": SOURCE_USER} ) - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -288,17 +261,11 @@ async def test_form_already_configured(hass: HomeAssistant) -> None: assert entry.data["host"] == "1.1.1.1" -async def test_zeroconf(hass: HomeAssistant) -> None: +async def test_zeroconf(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: """Test we get the form.""" - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -316,15 +283,8 @@ async def test_zeroconf(hass: HomeAssistant) -> None: assert context["title_placeholders"]["host"] == "10.10.2.3" assert context["confirm_only"] is True - with patch( - "homeassistant.components.nam.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - {}, - ) - await hass.async_block_till_done() + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "10.10.2.3" @@ -332,17 +292,13 @@ async def test_zeroconf(hass: HomeAssistant) -> None: assert len(mock_setup_entry.mock_calls) == 1 -async def test_zeroconf_with_auth(hass: HomeAssistant) -> None: +async def test_zeroconf_with_auth( + hass: HomeAssistant, mock_setup_entry: AsyncMock +) -> None: """Test that the zeroconf step with auth works.""" - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - side_effect=AuthFailedError("Auth Error"), - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + side_effect=AuthFailedError("Auth Error"), ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -360,18 +316,9 @@ async def test_zeroconf_with_auth(hass: HomeAssistant) -> None: assert result["errors"] == {} assert context["title_placeholders"]["host"] == "10.10.2.3" - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG_AUTH, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), - patch( - "homeassistant.components.nam.async_setup_entry", return_value=True - ) as mock_setup_entry, + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -447,15 +394,9 @@ async def test_reconfigure_successful(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reconfigure" - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG_AUTH, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -491,7 +432,7 @@ async def test_reconfigure_not_successful(hass: HomeAssistant) -> None: assert result["step_id"] == "reconfigure" with patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", side_effect=ApiError("API Error"), ): result = await hass.config_entries.flow.async_configure( @@ -503,15 +444,9 @@ async def test_reconfigure_not_successful(hass: HomeAssistant) -> None: assert result["step_id"] == "reconfigure" assert result["errors"] == {"base": "cannot_connect"} - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG_AUTH, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -546,15 +481,9 @@ async def test_reconfigure_not_the_same_device(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reconfigure" - with ( - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - return_value=DEVICE_CONFIG_AUTH, - ), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", - return_value="aa:bb:cc:dd:ee:ff", - ), + with patch( + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", + return_value="aa:bb:cc:dd:ee:ff", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], diff --git a/tests/components/nam/test_init.py b/tests/components/nam/test_init.py index 13bde1432b3..ea61739c008 100644 --- a/tests/components/nam/test_init.py +++ b/tests/components/nam/test_init.py @@ -44,27 +44,6 @@ async def test_config_not_ready(hass: HomeAssistant) -> None: assert entry.state is ConfigEntryState.SETUP_RETRY -async def test_config_not_ready_while_checking_credentials(hass: HomeAssistant) -> None: - """Test for setup failure if the connection fails while checking credentials.""" - entry = MockConfigEntry( - domain=DOMAIN, - title="10.10.2.3", - unique_id="aa:bb:cc:dd:ee:ff", - data={"host": "10.10.2.3"}, - ) - entry.add_to_hass(hass) - - with ( - patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), - patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", - side_effect=ApiError("API Error"), - ), - ): - await hass.config_entries.async_setup(entry.entry_id) - assert entry.state is ConfigEntryState.SETUP_RETRY - - async def test_config_auth_failed(hass: HomeAssistant) -> None: """Test for setup failure if the auth fails.""" entry = MockConfigEntry( @@ -76,7 +55,7 @@ async def test_config_auth_failed(hass: HomeAssistant) -> None: entry.add_to_hass(hass) with patch( - "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", + "homeassistant.components.nam.NettigoAirMonitor.async_get_mac_address", side_effect=AuthFailedError("Authorization has failed"), ): await hass.config_entries.async_setup(entry.entry_id)