Skip to content
Snippets Groups Projects
Unverified Commit b4651311 authored by epenet's avatar epenet Committed by GitHub
Browse files

Use ZeroconfServiceInfo in sonos (#60095)

parent b9cbfbae
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,6 @@ import soco
from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.config_entry_flow import DiscoveryFlowHandler
......@@ -31,13 +30,13 @@ class SonosDiscoveryFlowHandler(DiscoveryFlowHandler):
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
"""Handle a flow initialized by zeroconf."""
hostname = discovery_info["hostname"]
hostname = discovery_info[zeroconf.ATTR_HOSTNAME]
if hostname is None or not hostname.lower().startswith("sonos"):
return self.async_abort(reason="not_sonos_device")
await self.async_set_unique_id(self._domain, raise_on_progress=False)
host = discovery_info[CONF_HOST]
mdns_name = discovery_info[CONF_NAME]
properties = discovery_info["properties"]
host = discovery_info[zeroconf.ATTR_HOST]
mdns_name = discovery_info[zeroconf.ATTR_NAME]
properties = discovery_info[zeroconf.ATTR_PROPERTIES]
boot_seqnum = properties.get("bootseq")
model = properties.get("model")
uid = hostname_to_uid(hostname)
......
......@@ -4,6 +4,7 @@ from __future__ import annotations
from unittest.mock import MagicMock, patch
from homeassistant import config_entries, core
from homeassistant.components import zeroconf
from homeassistant.components.sonos.const import DATA_SONOS_DISCOVERY_MANAGER, DOMAIN
......@@ -43,12 +44,12 @@ async def test_zeroconf_form(hass: core.HomeAssistant):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={
"host": "192.168.4.2",
"name": "Sonos-aaa@Living Room._sonos._tcp.local.",
"hostname": "Sonos-aaa",
"properties": {"bootseq": "1234"},
},
data=zeroconf.ZeroconfServiceInfo(
host="192.168.4.2",
name="Sonos-aaa@Living Room._sonos._tcp.local.",
hostname="Sonos-aaa",
properties={"bootseq": "1234"},
),
)
assert result["type"] == "form"
assert result["errors"] is None
......@@ -82,13 +83,13 @@ async def test_zeroconf_sonos_v1(hass: core.HomeAssistant):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={
"host": "192.168.1.107",
"port": 1443,
"hostname": "sonos5CAAFDE47AC8.local.",
"type": "_sonos._tcp.local.",
"name": "Sonos-5CAAFDE47AC8._sonos._tcp.local.",
"properties": {
data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.107",
port=1443,
hostname="sonos5CAAFDE47AC8.local.",
type="_sonos._tcp.local.",
name="Sonos-5CAAFDE47AC8._sonos._tcp.local.",
properties={
"_raw": {
"info": b"/api/v1/players/RINCON_5CAAFDE47AC801400/info",
"vers": b"1",
......@@ -98,7 +99,7 @@ async def test_zeroconf_sonos_v1(hass: core.HomeAssistant):
"vers": "1",
"protovers": "1.18.9",
},
},
),
)
assert result["type"] == "form"
assert result["errors"] is None
......@@ -132,11 +133,11 @@ async def test_zeroconf_form_not_sonos(hass: core.HomeAssistant):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={
"host": "192.168.4.2",
"hostname": "not-aaa",
"properties": {"bootseq": "1234"},
},
data=zeroconf.ZeroconfServiceInfo(
host="192.168.4.2",
hostname="not-aaa",
properties={"bootseq": "1234"},
),
)
assert result["type"] == "abort"
assert result["reason"] == "not_sonos_device"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment