Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump zeroconf to 0.74.0 #97745

Merged
merged 5 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions homeassistant/components/thread/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Callable
import dataclasses
import logging
from typing import cast
from typing import TYPE_CHECKING, cast

from python_otbr_api.mdns import StateBitmap
from zeroconf import BadTypeInNameException, DNSPointer, ServiceListener, Zeroconf
Expand Down Expand Up @@ -57,25 +57,32 @@ def try_decode(value: bytes | None) -> str | None:
except UnicodeDecodeError:
return None

ext_addr = service.properties.get(b"xa")
ext_pan_id = service.properties.get(b"xp")
network_name = try_decode(service.properties.get(b"nn"))
model_name = try_decode(service.properties.get(b"mn"))
service_properties = service.properties
Copy link
Member Author

@bdraco bdraco Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was typed to dict before in upstream zeroconf so mypy never errored before. This design was inherited from many many years ago and changing it would be breaking. The lack of typing was hiding the issue.

if TYPE_CHECKING:
# Service properties are always bytes if they are set from the network.
# For legacy backwards compatibility zeroconf allows properties to be set
# as strings but we never do that so we can safely cast here.
service_properties = cast(dict[bytes, bytes | None], service_properties)

ext_addr = service_properties.get(b"xa")
ext_pan_id = service_properties.get(b"xp")
network_name = try_decode(service_properties.get(b"nn"))
model_name = try_decode(service_properties.get(b"mn"))
server = service.server
vendor_name = try_decode(service.properties.get(b"vn"))
thread_version = try_decode(service.properties.get(b"tv"))
vendor_name = try_decode(service_properties.get(b"vn"))
thread_version = try_decode(service_properties.get(b"tv"))
unconfigured = None
brand = KNOWN_BRANDS.get(vendor_name)
if brand == "homeassistant":
# Attempt to detect incomplete configuration
if (state_bitmap_b := service.properties.get(b"sb")) is not None:
if (state_bitmap_b := service_properties.get(b"sb")) is not None:
try:
state_bitmap = StateBitmap.from_bytes(state_bitmap_b)
if not state_bitmap.is_active:
unconfigured = True
except ValueError:
_LOGGER.debug("Failed to decode state bitmap in service %s", service)
if service.properties.get(b"at") is None:
if service_properties.get(b"at") is None:
unconfigured = True

return ThreadRouterDiscoveryData(
Expand Down Expand Up @@ -168,10 +175,21 @@ async def _add_update_service(self, type_: str, name: str):
return

_LOGGER.debug("_add_update_service %s %s", name, service)
service_properties = service.properties
if TYPE_CHECKING:
# Service properties are always bytes if they are set from the network.
# For legacy backwards compatibility zeroconf allows properties to be set
# as strings but we never do that so we can safely cast here.
service_properties = cast(dict[bytes, bytes | None], service_properties)

if not (xa := service_properties.get(b"xa")):
_LOGGER.debug("_add_update_service failed to find xa in %s", service)
return

# We use the extended mac address as key, bail out if it's missing
try:
extended_mac_address = service.properties[b"xa"].hex()
except (KeyError, UnicodeDecodeError) as err:
extended_mac_address = xa.hex()
except UnicodeDecodeError as err:
_LOGGER.debug("_add_update_service failed to parse service %s", err)
return

Expand Down
12 changes: 10 additions & 2 deletions homeassistant/components/zeroconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
import re
import sys
from typing import Any, Final, cast
from typing import TYPE_CHECKING, Any, Final, cast

import voluptuous as vol
from zeroconf import (
Expand Down Expand Up @@ -553,11 +553,19 @@ def info_from_service(service: AsyncServiceInfo) -> ZeroconfServiceInfo | None:
break
if not host:
return None

service_properties = service.properties
if TYPE_CHECKING:
# Service properties are always bytes if they are set from the network.
# For legacy backwards compatibility zeroconf allows properties to be set
# as strings but we never do that so we can safely cast here.
service_properties = cast(dict[bytes, bytes | None], service_properties)
Copy link
Member Author

@bdraco bdraco Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was typed to dict before in upstream zeroconf so mypy never errored before. This design was inherited from many many years ago and changing it would be breaking. The lack of typing was hiding the issue.


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()
for k, v in service_properties.items()
}

assert service.server is not None, "server cannot be none if there are addresses"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/zeroconf/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "local_push",
"loggers": ["zeroconf"],
"quality_scale": "internal",
"requirements": ["zeroconf==0.72.3"]
"requirements": ["zeroconf==0.74.0"]
}
2 changes: 1 addition & 1 deletion homeassistant/package_constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ voluptuous-serialize==2.6.0
voluptuous==0.13.1
webrtcvad==2.0.10
yarl==1.9.2
zeroconf==0.72.3
zeroconf==0.74.0

# Constrain pycryptodome to avoid vulnerability
# see https://github.com/home-assistant/core/pull/16238
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2752,7 +2752,7 @@ zamg==0.2.4
zengge==0.2

# homeassistant.components.zeroconf
zeroconf==0.72.3
zeroconf==0.74.0

# homeassistant.components.zeversolar
zeversolar==0.3.1
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ youtubeaio==1.1.5
zamg==0.2.4

# homeassistant.components.zeroconf
zeroconf==0.72.3
zeroconf==0.74.0

# homeassistant.components.zeversolar
zeversolar==0.3.1
Expand Down