mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Enhance info_from_service function in zeroconf integration (#31059)
* enhance zeroconf service info decoding to include raw bytes * Update homeassistant/components/zeroconf/__init__.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * fix test based on last commit * fix test based on last commit * remove .keys() when asserting processed and raw service info properties Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
bc600995c1
commit
ee74f95371
@ -148,15 +148,20 @@ def handle_homekit(hass, info) -> bool:
|
|||||||
|
|
||||||
def info_from_service(service):
|
def info_from_service(service):
|
||||||
"""Return prepared info from mDNS entries."""
|
"""Return prepared info from mDNS entries."""
|
||||||
properties = {}
|
properties = {"_raw": {}}
|
||||||
|
|
||||||
for key, value in service.properties.items():
|
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
|
||||||
|
key = key.decode("ascii")
|
||||||
|
properties["_raw"][key] = value
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if isinstance(value, bytes):
|
if isinstance(value, bytes):
|
||||||
value = value.decode("utf-8")
|
properties[key] = value.decode("utf-8")
|
||||||
properties[key.decode("utf-8")] = value
|
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
_LOGGER.warning("Unicode decode error on %s: %s", key, value)
|
pass
|
||||||
|
|
||||||
address = service.addresses[0]
|
address = service.addresses[0]
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ def get_service_info_mock(service_type, name):
|
|||||||
weight=0,
|
weight=0,
|
||||||
priority=0,
|
priority=0,
|
||||||
server="name.local.",
|
server="name.local.",
|
||||||
properties={b"macaddress": b"ABCDEF012345"},
|
properties={b"macaddress": b"ABCDEF012345", b"non-utf8-value": b"ABCDEF\x8a"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -93,3 +93,16 @@ async def test_homekit_match_full(hass, mock_zeroconf):
|
|||||||
assert len(mock_service_browser.mock_calls) == 1
|
assert len(mock_service_browser.mock_calls) == 1
|
||||||
assert len(mock_config_flow.mock_calls) == 2
|
assert len(mock_config_flow.mock_calls) == 2
|
||||||
assert mock_config_flow.mock_calls[0][1][0] == "hue"
|
assert mock_config_flow.mock_calls[0][1][0] == "hue"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_info_from_service_non_utf8(hass):
|
||||||
|
"""Test info_from_service handles non UTF-8 property values correctly."""
|
||||||
|
service_type = "_test._tcp.local."
|
||||||
|
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(info["properties"]) <= len(raw_info)
|
||||||
|
assert "non-utf8-value" not in info["properties"]
|
||||||
|
assert raw_info["non-utf8-value"] is not None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user