Use DhcpServiceInfo in squeezebox (#60100)

pull/60119/head
epenet 2021-11-21 23:33:44 +01:00 committed by GitHub
parent 4e1089cedb
commit 382efef2e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 19 deletions

View File

@ -2,12 +2,13 @@
import asyncio import asyncio
from http import HTTPStatus from http import HTTPStatus
import logging import logging
from typing import TYPE_CHECKING
from pysqueezebox import Server, async_discover from pysqueezebox import Server, async_discover
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries, data_entry_flow 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.components.media_player import DOMAIN as MP_DOMAIN
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession 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() 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.""" """Handle dhcp discovery of a Squeezebox player."""
_LOGGER.debug( _LOGGER.debug(
"Reached dhcp discovery of a player with info: %s", discovery_info "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() self._abort_if_unique_id_configured()
_LOGGER.debug("Configuring dhcp player with unique id: %s", self.unique_id) _LOGGER.debug("Configuring dhcp player with unique id: %s", self.unique_id)
registry = async_get(self.hass) 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 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: 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 # this player is already known, so do nothing other than mark as configured

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
from pysqueezebox import Server from pysqueezebox import Server
from homeassistant import config_entries 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.components.squeezebox.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from homeassistant.data_entry_flow import ( from homeassistant.data_entry_flow import (
@ -200,11 +200,11 @@ async def test_dhcp_discovery(hass):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: "1.1.1.1", ip="1.1.1.1",
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "any", hostname="any",
}, ),
) )
assert result["type"] == RESULT_TYPE_FORM assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "edit" 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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: "1.1.1.1", ip="1.1.1.1",
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "any", hostname="any",
}, ),
) )
assert result["type"] == RESULT_TYPE_FORM assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user" 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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: "1.1.1.1", ip="1.1.1.1",
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "any", hostname="any",
}, ),
) )
assert result["type"] == RESULT_TYPE_ABORT assert result["type"] == RESULT_TYPE_ABORT