diff --git a/homeassistant/components/forked_daapd/config_flow.py b/homeassistant/components/forked_daapd/config_flow.py index 16ebc1f82f7..86bc39c05ed 100644 --- a/homeassistant/components/forked_daapd/config_flow.py +++ b/homeassistant/components/forked_daapd/config_flow.py @@ -6,8 +6,10 @@ from pyforked_daapd import ForkedDaapdAPI import voluptuous as vol from homeassistant import config_entries +from homeassistant.components import zeroconf from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_PORT from homeassistant.core import callback +from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import ( @@ -153,35 +155,41 @@ class ForkedDaapdFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): step_id="user", data_schema=vol.Schema(DATA_SCHEMA_DICT), errors={} ) - async def async_step_zeroconf(self, discovery_info): + async def async_step_zeroconf( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Prepare configuration for a discovered forked-daapd device.""" version_num = 0 - if discovery_info.get("properties") and discovery_info["properties"].get( - "Machine Name" - ): + if discovery_info.get(zeroconf.ATTR_PROPERTIES) and discovery_info[ + zeroconf.ATTR_PROPERTIES + ].get("Machine Name"): with suppress(ValueError): version_num = int( - discovery_info["properties"].get("mtd-version", "0").split(".")[0] + discovery_info[zeroconf.ATTR_PROPERTIES] + .get("mtd-version", "0") + .split(".")[0] ) if version_num < 27: return self.async_abort(reason="not_forked_daapd") - await self.async_set_unique_id(discovery_info["properties"]["Machine Name"]) + await self.async_set_unique_id( + discovery_info[zeroconf.ATTR_PROPERTIES]["Machine Name"] + ) self._abort_if_unique_id_configured() # Update title and abort if we already have an entry for this host for entry in self._async_current_entries(): - if entry.data.get(CONF_HOST) != discovery_info["host"]: + if entry.data.get(CONF_HOST) != discovery_info[zeroconf.ATTR_HOST]: continue self.hass.config_entries.async_update_entry( entry, - title=discovery_info["properties"]["Machine Name"], + title=discovery_info[zeroconf.ATTR_PROPERTIES]["Machine Name"], ) return self.async_abort(reason="already_configured") zeroconf_data = { - CONF_HOST: discovery_info["host"], - CONF_PORT: int(discovery_info["port"]), - CONF_NAME: discovery_info["properties"]["Machine Name"], + CONF_HOST: discovery_info[zeroconf.ATTR_HOST], + CONF_PORT: discovery_info[zeroconf.ATTR_PORT], + CONF_NAME: discovery_info[zeroconf.ATTR_PROPERTIES]["Machine Name"], } self.discovery_schema = vol.Schema(fill_in_schema_dict(zeroconf_data)) self.context.update({"title_placeholders": zeroconf_data}) diff --git a/tests/components/forked_daapd/test_config_flow.py b/tests/components/forked_daapd/test_config_flow.py index 668f1be0a4f..c8b82c7a2e9 100644 --- a/tests/components/forked_daapd/test_config_flow.py +++ b/tests/components/forked_daapd/test_config_flow.py @@ -4,6 +4,7 @@ from unittest.mock import AsyncMock, patch import pytest from homeassistant import data_entry_flow +from homeassistant.components import zeroconf from homeassistant.components.forked_daapd.const import ( CONF_LIBRESPOT_JAVA_PORT, CONF_MAX_PLAYLISTS, @@ -98,11 +99,11 @@ async def test_zeroconf_updates_title(hass, config_entry): MockConfigEntry(domain=DOMAIN, data={CONF_HOST: "different host"}).add_to_hass(hass) config_entry.add_to_hass(hass) assert len(hass.config_entries.async_entries(DOMAIN)) == 2 - discovery_info = { - "host": "192.168.1.1", - "port": 23, - "properties": {"mtd-version": "27.0", "Machine Name": "zeroconf_test"}, - } + discovery_info = zeroconf.ZeroconfServiceInfo( + host="192.168.1.1", + port=23, + properties={"mtd-version": "27.0", "Machine Name": "zeroconf_test"}, + ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info ) @@ -129,40 +130,40 @@ async def test_config_flow_no_websocket(hass, config_entry): async def test_config_flow_zeroconf_invalid(hass): """Test that an invalid zeroconf entry doesn't work.""" # test with no discovery properties - discovery_info = {"host": "127.0.0.1", "port": 23} + discovery_info = zeroconf.ZeroconfServiceInfo(host="127.0.0.1", port=23) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info ) # doesn't create the entry, tries to show form but gets abort assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "not_forked_daapd" # test with forked-daapd version < 27 - discovery_info = { - "host": "127.0.0.1", - "port": 23, - "properties": {"mtd-version": "26.3", "Machine Name": "forked-daapd"}, - } + discovery_info = zeroconf.ZeroconfServiceInfo( + host="127.0.0.1", + port=23, + properties={"mtd-version": "26.3", "Machine Name": "forked-daapd"}, + ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info ) # doesn't create the entry, tries to show form but gets abort assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "not_forked_daapd" # test with verbose mtd-version from Firefly - discovery_info = { - "host": "127.0.0.1", - "port": 23, - "properties": {"mtd-version": "0.2.4.1", "Machine Name": "firefly"}, - } + discovery_info = zeroconf.ZeroconfServiceInfo( + host="127.0.0.1", + port=23, + properties={"mtd-version": "0.2.4.1", "Machine Name": "firefly"}, + ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info ) # doesn't create the entry, tries to show form but gets abort assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "not_forked_daapd" # test with svn mtd-version from Firefly - discovery_info = { - "host": "127.0.0.1", - "port": 23, - "properties": {"mtd-version": "svn-1676", "Machine Name": "firefly"}, - } + discovery_info = zeroconf.ZeroconfServiceInfo( + host="127.0.0.1", + port=23, + properties={"mtd-version": "svn-1676", "Machine Name": "firefly"}, + ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info ) # doesn't create the entry, tries to show form but gets abort @@ -172,15 +173,15 @@ async def test_config_flow_zeroconf_invalid(hass): async def test_config_flow_zeroconf_valid(hass): """Test that a valid zeroconf entry works.""" - discovery_info = { - "host": "192.168.1.1", - "port": 23, - "properties": { + discovery_info = zeroconf.ZeroconfServiceInfo( + host="192.168.1.1", + port=23, + properties={ "mtd-version": "27.0", "Machine Name": "zeroconf_test", "Machine ID": "5E55EEFF", }, - } + ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info )