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

View File

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