From b3607343fc6bd5a832b79c128d1322a197b77a77 Mon Sep 17 00:00:00 2001 From: Raj Laud <50647620+rajlaud@users.noreply.github.com> Date: Wed, 26 May 2021 03:30:15 -0500 Subject: [PATCH] Fix error in Squeezebox DHCP discovery flow (#50771) * Map data from dhcp to squeezebox discovery flow * Add tests for squeezebox dhcp discovery --- .../components/squeezebox/config_flow.py | 16 +++++++-- .../components/squeezebox/test_config_flow.py | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/squeezebox/config_flow.py b/homeassistant/components/squeezebox/config_flow.py index f81ff505583..1f1c23942db 100644 --- a/homeassistant/components/squeezebox/config_flow.py +++ b/homeassistant/components/squeezebox/config_flow.py @@ -6,6 +6,7 @@ from pysqueezebox import Server, async_discover import voluptuous as vol from homeassistant import config_entries +from homeassistant.components.dhcp import IP_ADDRESS, MAC_ADDRESS from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, @@ -172,10 +173,21 @@ class SqueezeboxConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): await self.async_set_unique_id(discovery_info.pop("uuid")) self._abort_if_unique_id_configured() else: - # attempt to connect to server and determine uuid. will fail if password required + # attempt to connect to server and determine uuid. will fail if + # password required + + if CONF_HOST not in discovery_info and IP_ADDRESS in discovery_info: + discovery_info[CONF_HOST] = discovery_info[IP_ADDRESS] + + if CONF_PORT not in discovery_info: + discovery_info[CONF_PORT] = DEFAULT_PORT + error = await self._validate_input(discovery_info) if error: - await self._async_handle_discovery_without_unique_id() + if MAC_ADDRESS in discovery_info: + await self.async_set_unique_id(discovery_info[MAC_ADDRESS]) + else: + await self._async_handle_discovery_without_unique_id() # update schema with suggested values from discovery self.data_schema = _base_schema(discovery_info) diff --git a/tests/components/squeezebox/test_config_flow.py b/tests/components/squeezebox/test_config_flow.py index cd734e1627c..e740ea671cd 100644 --- a/tests/components/squeezebox/test_config_flow.py +++ b/tests/components/squeezebox/test_config_flow.py @@ -4,6 +4,7 @@ from unittest.mock import patch from pysqueezebox import Server from homeassistant import config_entries +from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS from homeassistant.components.squeezebox.const import DOMAIN from homeassistant.const import ( CONF_HOST, @@ -196,6 +197,41 @@ async def test_discovery_no_uuid(hass): assert result["step_id"] == "edit" +async def test_dhcp_discovery(hass): + """Test we can process discovery from dhcp.""" + with patch( + "pysqueezebox.Server.async_query", + return_value={"uuid": UUID}, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_DHCP}, + data={ + IP_ADDRESS: "1.1.1.1", + MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", + HOSTNAME: "any", + }, + ) + assert result["type"] == RESULT_TYPE_FORM + assert result["step_id"] == "edit" + + +async def test_dhcp_discovery_no_connection(hass): + """Test we can process discovery from dhcp without connecting to squeezebox server.""" + with patch("pysqueezebox.Server.async_query", new=patch_async_query_unauthorized): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_DHCP}, + data={ + IP_ADDRESS: "1.1.1.1", + MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", + HOSTNAME: "any", + }, + ) + assert result["type"] == RESULT_TYPE_FORM + assert result["step_id"] == "edit" + + async def test_import(hass): """Test handling of configuration imported.""" with patch("pysqueezebox.Server.async_query", return_value={"uuid": UUID},), patch(