From fcf82a9a0ae14bf4a4a683ce661ca7928a068a73 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Dec 2021 21:32:24 -0600 Subject: [PATCH] Improve SSDP callback performance - Avoid building a few dicts when they are not needed - Use CaseInsensitiveDict.as_dict() when available to avoid __iter__ overhead --- homeassistant/components/ssdp/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/ssdp/__init__.py b/homeassistant/components/ssdp/__init__.py index ea4740be77af0b..46cdb362a7172f 100644 --- a/homeassistant/components/ssdp/__init__.py +++ b/homeassistant/components/ssdp/__init__.py @@ -472,22 +472,24 @@ async def _ssdp_listener_callback( ) location = ssdp_device.location - info_desc = await self._async_get_description_dict(location) or {} + info_desc = None combined_headers = ssdp_device.combined_headers(dst) - info_with_desc = CaseInsensitiveDict(combined_headers, **info_desc) - callbacks = self._async_get_matching_callbacks(combined_headers) matching_domains: set[str] = set() # If there are no changes from a search, do not trigger a config flow if source != SsdpSource.SEARCH_ALIVE: + info_desc = await self._async_get_description_dict(location) or {} + assert isinstance(combined_headers, CaseInsensitiveDict) matching_domains = self.integration_matchers.async_matching_domains( - info_with_desc + CaseInsensitiveDict({**combined_headers.as_dict(), **info_desc}) ) if not callbacks and not matching_domains: return + if info_desc is None: + info_desc = await self._async_get_description_dict(location) or {} discovery_info = discovery_info_from_headers_and_description( combined_headers, info_desc ) @@ -565,7 +567,10 @@ def discovery_info_from_headers_and_description( """Convert headers and description to discovery_info.""" ssdp_usn = combined_headers["usn"] ssdp_st = combined_headers.get("st") - upnp_info = {**info_desc} + if isinstance(info_desc, CaseInsensitiveDict): + upnp_info = {**info_desc.as_dict()} + else: + upnp_info = {**info_desc} # Increase compatibility: depending on the message type, # either the ST (Search Target, from M-SEARCH messages)