Fix error in Squeezebox DHCP discovery flow (#50771)

* Map data from dhcp to squeezebox discovery flow

* Add tests for squeezebox dhcp discovery
This commit is contained in:
Raj Laud 2021-05-26 03:30:15 -05:00 committed by GitHub
parent 5ee0df29d4
commit b3607343fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View file

@ -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)

View file

@ -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(