Optimise use of ZeroconfServiceInfo (#59966)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
ecf00a1eae
commit
e23cc3ecbf
3 changed files with 12 additions and 14 deletions
|
@ -160,20 +160,15 @@ class ForkedDaapdFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Prepare configuration for a discovered forked-daapd device."""
|
"""Prepare configuration for a discovered forked-daapd device."""
|
||||||
version_num = 0
|
version_num = 0
|
||||||
if discovery_info.get(zeroconf.ATTR_PROPERTIES) and discovery_info[
|
zeroconf_properties = discovery_info[zeroconf.ATTR_PROPERTIES]
|
||||||
zeroconf.ATTR_PROPERTIES
|
if zeroconf_properties.get("Machine Name"):
|
||||||
].get("Machine Name"):
|
|
||||||
with suppress(ValueError):
|
with suppress(ValueError):
|
||||||
version_num = int(
|
version_num = int(
|
||||||
discovery_info[zeroconf.ATTR_PROPERTIES]
|
zeroconf_properties.get("mtd-version", "0").split(".")[0]
|
||||||
.get("mtd-version", "0")
|
|
||||||
.split(".")[0]
|
|
||||||
)
|
)
|
||||||
if version_num < 27:
|
if version_num < 27:
|
||||||
return self.async_abort(reason="not_forked_daapd")
|
return self.async_abort(reason="not_forked_daapd")
|
||||||
await self.async_set_unique_id(
|
await self.async_set_unique_id(zeroconf_properties["Machine Name"])
|
||||||
discovery_info[zeroconf.ATTR_PROPERTIES]["Machine Name"]
|
|
||||||
)
|
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
# Update title and abort if we already have an entry for this host
|
# Update title and abort if we already have an entry for this host
|
||||||
|
@ -182,14 +177,14 @@ class ForkedDaapdFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
continue
|
continue
|
||||||
self.hass.config_entries.async_update_entry(
|
self.hass.config_entries.async_update_entry(
|
||||||
entry,
|
entry,
|
||||||
title=discovery_info[zeroconf.ATTR_PROPERTIES]["Machine Name"],
|
title=zeroconf_properties["Machine Name"],
|
||||||
)
|
)
|
||||||
return self.async_abort(reason="already_configured")
|
return self.async_abort(reason="already_configured")
|
||||||
|
|
||||||
zeroconf_data = {
|
zeroconf_data = {
|
||||||
CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
|
CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
|
||||||
CONF_PORT: discovery_info[zeroconf.ATTR_PORT],
|
CONF_PORT: discovery_info[zeroconf.ATTR_PORT],
|
||||||
CONF_NAME: discovery_info[zeroconf.ATTR_PROPERTIES]["Machine Name"],
|
CONF_NAME: zeroconf_properties["Machine Name"],
|
||||||
}
|
}
|
||||||
self.discovery_schema = vol.Schema(fill_in_schema_dict(zeroconf_data))
|
self.discovery_schema = vol.Schema(fill_in_schema_dict(zeroconf_data))
|
||||||
self.context.update({"title_placeholders": zeroconf_data})
|
self.context.update({"title_placeholders": zeroconf_data})
|
||||||
|
|
|
@ -111,6 +111,7 @@ class FreeboxFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Initialize flow from zeroconf."""
|
"""Initialize flow from zeroconf."""
|
||||||
host = discovery_info[zeroconf.ATTR_PROPERTIES]["api_domain"]
|
zeroconf_properties = discovery_info[zeroconf.ATTR_PROPERTIES]
|
||||||
port = discovery_info[zeroconf.ATTR_PROPERTIES]["https_port"]
|
host = zeroconf_properties["api_domain"]
|
||||||
|
port = zeroconf_properties["https_port"]
|
||||||
return await self.async_step_user({CONF_HOST: host, CONF_PORT: port})
|
return await self.async_step_user({CONF_HOST: host, CONF_PORT: port})
|
||||||
|
|
|
@ -130,7 +130,9 @@ async def test_config_flow_no_websocket(hass, config_entry):
|
||||||
async def test_config_flow_zeroconf_invalid(hass):
|
async def test_config_flow_zeroconf_invalid(hass):
|
||||||
"""Test that an invalid zeroconf entry doesn't work."""
|
"""Test that an invalid zeroconf entry doesn't work."""
|
||||||
# test with no discovery properties
|
# test with no discovery properties
|
||||||
discovery_info = zeroconf.ZeroconfServiceInfo(host="127.0.0.1", port=23)
|
discovery_info = zeroconf.ZeroconfServiceInfo(
|
||||||
|
host="127.0.0.1", port=23, properties={}
|
||||||
|
)
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info
|
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info
|
||||||
) # doesn't create the entry, tries to show form but gets abort
|
) # doesn't create the entry, tries to show form but gets abort
|
||||||
|
|
Loading…
Add table
Reference in a new issue