Use dataclass properties in syncthru discovery (#60739)

Co-authored-by: epenet <epenet@users.noreply.github.com>
pull/60763/head
epenet 2021-12-01 18:40:27 +01:00 committed by GitHub
parent 5c992ec2cc
commit b65b2c4cd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import ssdp
from homeassistant.const import CONF_NAME, CONF_URL
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import aiohttp_client
from .const import DEFAULT_MODEL, DEFAULT_NAME_TEMPLATE, DOMAIN
@ -33,15 +34,15 @@ class SyncThruConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle import initiated flow."""
return await self.async_step_user(user_input=user_input)
async def async_step_ssdp(self, discovery_info):
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
"""Handle SSDP initiated flow."""
await self.async_set_unique_id(discovery_info[ssdp.ATTR_UPNP_UDN])
await self.async_set_unique_id(discovery_info.upnp[ssdp.ATTR_UPNP_UDN])
self._abort_if_unique_id_configured()
self.url = url_normalize(
discovery_info.get(
discovery_info.upnp.get(
ssdp.ATTR_UPNP_PRESENTATION_URL,
f"http://{urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION]).hostname}/",
f"http://{urlparse(discovery_info.ssdp_location or '').hostname}/",
)
)
@ -50,12 +51,12 @@ class SyncThruConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
):
# Update unique id of entry with the same URL
if not existing_entry.unique_id:
await self.hass.config_entries.async_update_entry(
existing_entry, unique_id=discovery_info[ssdp.ATTR_UPNP_UDN]
self.hass.config_entries.async_update_entry(
existing_entry, unique_id=discovery_info.upnp[ssdp.ATTR_UPNP_UDN]
)
return self.async_abort(reason="already_configured")
self.name = discovery_info.get(ssdp.ATTR_UPNP_FRIENDLY_NAME)
self.name = discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME, "")
if self.name:
# Remove trailing " (ip)" if present for consistency with user driven config
self.name = re.sub(r"\s+\([\d.]+\)\s*$", "", self.name)