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
This commit is contained in:
J. Nick Koston 2023-06-14 15:31:52 -10:00 committed by GitHub
parent 7b3f100efb
commit e0ae7a31fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 27 deletions

View File

@ -581,26 +581,9 @@ def _stringify_ip_address(ip_addr: IPv4Address | IPv6Address) -> str:
def info_from_service(service: AsyncServiceInfo) -> ZeroconfServiceInfo | None: def info_from_service(service: AsyncServiceInfo) -> ZeroconfServiceInfo | None:
"""Return prepared info from mDNS entries.""" """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 # See https://ietf.org/rfc/rfc6763.html#section-6.4 and
# https://ietf.org/rfc/rfc6763.html#section-6.5 for expected encodings # https://ietf.org/rfc/rfc6763.html#section-6.5 for expected encodings
# for property keys and values # 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")
if not (ip_addresses := service.ip_addresses_by_version(IPVersion.All)): if not (ip_addresses := service.ip_addresses_by_version(IPVersion.All)):
return None return None
host: str | None = None host: str | None = None
@ -610,6 +593,12 @@ def info_from_service(service: AsyncServiceInfo) -> ZeroconfServiceInfo | None:
break break
if not host: if not host:
return None 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" assert service.server is not None, "server cannot be none if there are addresses"
return ZeroconfServiceInfo( return ZeroconfServiceInfo(

View File

@ -910,13 +910,11 @@ async def test_info_from_service_non_utf8(hass: HomeAssistant) -> None:
info = zeroconf.info_from_service( info = zeroconf.info_from_service(
get_service_info_mock(service_type, f"test.{service_type}") get_service_info_mock(service_type, f"test.{service_type}")
) )
raw_info = info.properties.pop("_raw", False) assert NON_ASCII_KEY.decode("ascii", "replace") in info.properties
assert raw_info assert "non-utf8-value" in info.properties
assert len(raw_info) == len(PROPERTIES) - 1 assert info.properties["non-utf8-value"] == NON_UTF8_VALUE.decode(
assert NON_ASCII_KEY not in raw_info "utf-8", "replace"
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
async def test_info_from_service_with_addresses(hass: HomeAssistant) -> None: async def test_info_from_service_with_addresses(hass: HomeAssistant) -> None: