From 382efef2e25ce077d139661f34e8e21689c4e97d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sun, 21 Nov 2021 23:33:44 +0100 Subject: [PATCH] Use DhcpServiceInfo in squeezebox (#60100) --- .../components/squeezebox/config_flow.py | 11 +++++-- .../components/squeezebox/test_config_flow.py | 32 +++++++++---------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/squeezebox/config_flow.py b/homeassistant/components/squeezebox/config_flow.py index e7e0a691e85..ac5bf8b580e 100644 --- a/homeassistant/components/squeezebox/config_flow.py +++ b/homeassistant/components/squeezebox/config_flow.py @@ -2,12 +2,13 @@ import asyncio from http import HTTPStatus import logging +from typing import TYPE_CHECKING from pysqueezebox import Server, async_discover import voluptuous as vol from homeassistant import config_entries, data_entry_flow -from homeassistant.components.dhcp import MAC_ADDRESS +from homeassistant.components import dhcp from homeassistant.components.media_player import DOMAIN as MP_DOMAIN from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -184,18 +185,22 @@ class SqueezeboxConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return await self.async_step_edit() - async def async_step_dhcp(self, discovery_info): + async def async_step_dhcp( + self, discovery_info: dhcp.DhcpServiceInfo + ) -> data_entry_flow.FlowResult: """Handle dhcp discovery of a Squeezebox player.""" _LOGGER.debug( "Reached dhcp discovery of a player with info: %s", discovery_info ) - await self.async_set_unique_id(format_mac(discovery_info[MAC_ADDRESS])) + await self.async_set_unique_id(format_mac(discovery_info[dhcp.MAC_ADDRESS])) self._abort_if_unique_id_configured() _LOGGER.debug("Configuring dhcp player with unique id: %s", self.unique_id) registry = async_get(self.hass) + if TYPE_CHECKING: + assert self.unique_id # if we have detected this player, do nothing. if not, there must be a server out there for us to configure, so start the normal user flow (which tries to autodetect server) if registry.async_get_entity_id(MP_DOMAIN, DOMAIN, self.unique_id) is not None: # this player is already known, so do nothing other than mark as configured diff --git a/tests/components/squeezebox/test_config_flow.py b/tests/components/squeezebox/test_config_flow.py index 7f07576427b..df6d27572b8 100644 --- a/tests/components/squeezebox/test_config_flow.py +++ b/tests/components/squeezebox/test_config_flow.py @@ -5,7 +5,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 import dhcp from homeassistant.components.squeezebox.const import DOMAIN from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.data_entry_flow import ( @@ -200,11 +200,11 @@ async def test_dhcp_discovery(hass): 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", - }, + data=dhcp.DhcpServiceInfo( + ip="1.1.1.1", + macaddress="AA:BB:CC:DD:EE:FF", + hostname="any", + ), ) assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "edit" @@ -219,11 +219,11 @@ async def test_dhcp_discovery_no_server_found(hass): 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", - }, + data=dhcp.DhcpServiceInfo( + ip="1.1.1.1", + macaddress="AA:BB:CC:DD:EE:FF", + hostname="any", + ), ) assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "user" @@ -238,11 +238,11 @@ async def test_dhcp_discovery_existing_player(hass): 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", - }, + data=dhcp.DhcpServiceInfo( + ip="1.1.1.1", + macaddress="AA:BB:CC:DD:EE:FF", + hostname="any", + ), ) assert result["type"] == RESULT_TYPE_ABORT