Use dataclass properties in hue discovery (#60598)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-11-30 16:36:35 +01:00 committed by GitHub
parent 601ad8f71a
commit d75785d701
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 9 deletions

View File

@ -194,26 +194,31 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
""" """
# Filter out non-Hue bridges #1 # Filter out non-Hue bridges #1
if ( if (
discovery_info.get(ssdp.ATTR_UPNP_MANUFACTURER_URL) discovery_info.upnp.get(ssdp.ATTR_UPNP_MANUFACTURER_URL)
not in HUE_MANUFACTURERURL not in HUE_MANUFACTURERURL
): ):
return self.async_abort(reason="not_hue_bridge") return self.async_abort(reason="not_hue_bridge")
# Filter out non-Hue bridges #2 # Filter out non-Hue bridges #2
if any( if any(
name in discovery_info.get(ssdp.ATTR_UPNP_FRIENDLY_NAME, "") name in discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME, "")
for name in HUE_IGNORED_BRIDGE_NAMES for name in HUE_IGNORED_BRIDGE_NAMES
): ):
return self.async_abort(reason="not_hue_bridge") return self.async_abort(reason="not_hue_bridge")
if ( if (
not discovery_info.get(ssdp.ATTR_SSDP_LOCATION) not discovery_info.ssdp_location
or ssdp.ATTR_UPNP_SERIAL not in discovery_info or ssdp.ATTR_UPNP_SERIAL not in discovery_info.upnp
): ):
return self.async_abort(reason="not_hue_bridge") return self.async_abort(reason="not_hue_bridge")
host = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION]).hostname url = urlparse(discovery_info.ssdp_location)
bridge = await self._get_bridge(host, discovery_info[ssdp.ATTR_UPNP_SERIAL]) if not url.hostname:
return self.async_abort(reason="not_hue_bridge")
bridge = await self._get_bridge(
url.hostname, discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL]
)
await self.async_set_unique_id(bridge.id) await self.async_set_unique_id(bridge.id)
self._abort_if_unique_id_configured( self._abort_if_unique_id_configured(
@ -232,8 +237,8 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
host is already configured and delegate to the import step if not. host is already configured and delegate to the import step if not.
""" """
bridge = await self._get_bridge( bridge = await self._get_bridge(
discovery_info[zeroconf.ATTR_HOST], discovery_info.host,
discovery_info[zeroconf.ATTR_PROPERTIES]["bridgeid"], discovery_info.properties["bridgeid"],
) )
await self.async_set_unique_id(bridge.id) await self.async_set_unique_id(bridge.id)
@ -253,7 +258,7 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
as the unique identifier. Therefore, this method uses discovery without as the unique identifier. Therefore, this method uses discovery without
a unique ID. a unique ID.
""" """
self.bridge = await self._get_bridge(discovery_info[zeroconf.ATTR_HOST]) self.bridge = await self._get_bridge(discovery_info.host)
await self._async_handle_discovery_without_unique_id() await self._async_handle_discovery_without_unique_id()
return await self.async_step_link() return await self.async_step_link()

View File

@ -423,6 +423,26 @@ async def test_bridge_ssdp_missing_serial(hass):
assert result["reason"] == "not_hue_bridge" assert result["reason"] == "not_hue_bridge"
async def test_bridge_ssdp_invalid_location(hass):
"""Test if discovery info is a serial attribute."""
result = await hass.config_entries.flow.async_init(
const.DOMAIN,
context={"source": config_entries.SOURCE_SSDP},
data=ssdp.SsdpServiceInfo(
ssdp_usn="mock_usn",
ssdp_st="mock_st",
ssdp_location="http:///",
upnp={
ssdp.ATTR_UPNP_MANUFACTURER_URL: config_flow.HUE_MANUFACTURERURL[0],
ssdp.ATTR_UPNP_SERIAL: "1234",
},
),
)
assert result["type"] == "abort"
assert result["reason"] == "not_hue_bridge"
async def test_bridge_ssdp_espalexa(hass): async def test_bridge_ssdp_espalexa(hass):
"""Test if discovery info is from an Espalexa based device.""" """Test if discovery info is from an Espalexa based device."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(