From e0ae7a31fe1cc4eec71147c38b3f042d3afaf8ca Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 14 Jun 2023 15:31:52 -1000 Subject: [PATCH] Remove _raw from zeroconf properties (#94615) * Remove _raw from zeroconf properties This was added in #31059 but it appears it was never used. To preserve backwards compatibility, properties are still decoded but utf-8 errors are replaced instead of dropped * Remove _raw from zeroconf properties This was added in #31059 but it appears it was never used. To preserve backwards compatibility, properties are still decoded but utf-8 errors are replaced instead of dropped --- homeassistant/components/zeroconf/__init__.py | 29 ++++++------------- tests/components/zeroconf/test_init.py | 12 ++++---- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/zeroconf/__init__.py b/homeassistant/components/zeroconf/__init__.py index f12752dc5c3..927f0b6db3a 100644 --- a/homeassistant/components/zeroconf/__init__.py +++ b/homeassistant/components/zeroconf/__init__.py @@ -581,26 +581,9 @@ def _stringify_ip_address(ip_addr: IPv4Address | IPv6Address) -> str: def info_from_service(service: AsyncServiceInfo) -> ZeroconfServiceInfo | None: """Return prepared info from mDNS entries.""" - properties: dict[str, Any] = {"_raw": {}} - - for key, value in service.properties.items(): - # See https://ietf.org/rfc/rfc6763.html#section-6.4 and - # https://ietf.org/rfc/rfc6763.html#section-6.5 for expected encodings - # for property keys and values - try: - key = key.decode("ascii") - except UnicodeDecodeError: - _LOGGER.debug( - "Ignoring invalid key provided by [%s]: %s", service.name, key - ) - continue - - properties["_raw"][key] = value - - with suppress(UnicodeDecodeError): - if isinstance(value, bytes): - properties[key] = value.decode("utf-8") - + # See https://ietf.org/rfc/rfc6763.html#section-6.4 and + # https://ietf.org/rfc/rfc6763.html#section-6.5 for expected encodings + # for property keys and values if not (ip_addresses := service.ip_addresses_by_version(IPVersion.All)): return None host: str | None = None @@ -610,6 +593,12 @@ def info_from_service(service: AsyncServiceInfo) -> ZeroconfServiceInfo | None: break if not host: return None + properties: dict[str, Any] = { + k.decode("ascii", "replace"): None + if v is None + else v.decode("utf-8", "replace") + for k, v in service.properties.items() + } assert service.server is not None, "server cannot be none if there are addresses" return ZeroconfServiceInfo( diff --git a/tests/components/zeroconf/test_init.py b/tests/components/zeroconf/test_init.py index fea6b27e208..bd39c00df98 100644 --- a/tests/components/zeroconf/test_init.py +++ b/tests/components/zeroconf/test_init.py @@ -910,13 +910,11 @@ async def test_info_from_service_non_utf8(hass: HomeAssistant) -> None: info = zeroconf.info_from_service( get_service_info_mock(service_type, f"test.{service_type}") ) - raw_info = info.properties.pop("_raw", False) - assert raw_info - assert len(raw_info) == len(PROPERTIES) - 1 - assert NON_ASCII_KEY not in raw_info - assert len(info.properties) <= len(raw_info) - assert "non-utf8-value" not in info.properties - assert raw_info["non-utf8-value"] is NON_UTF8_VALUE + assert NON_ASCII_KEY.decode("ascii", "replace") in info.properties + assert "non-utf8-value" in info.properties + assert info.properties["non-utf8-value"] == NON_UTF8_VALUE.decode( + "utf-8", "replace" + ) async def test_info_from_service_with_addresses(hass: HomeAssistant) -> None: