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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 29 additions & 11 deletions homeassistant/components/thread/discovery.py
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,31 +57,38 @@
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)

Check failure on line 65 in homeassistant/components/thread/discovery.py

View workflow job for this annotation

GitHub Actions / Check mypy

Incompatible types in assignment (expression has type "dict[bytes, bytes | None]", variable has type "dict[str | bytes, str | bytes | None]") [assignment]

ext_addr = service_properties.get(b"xa")
ext_pan_id = service_properties.get(b"xp")
network_name = try_decode(service_properties.get(b"nn"))

Check failure on line 69 in homeassistant/components/thread/discovery.py

View workflow job for this annotation

GitHub Actions / Check mypy

Argument 1 to "try_decode" has incompatible type "str | bytes | None"; expected "bytes | None" [arg-type]
model_name = try_decode(service_properties.get(b"mn"))

Check failure on line 70 in homeassistant/components/thread/discovery.py

View workflow job for this annotation

GitHub Actions / Check mypy

Argument 1 to "try_decode" has incompatible type "str | bytes | None"; expected "bytes | None" [arg-type]
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"))

Check failure on line 72 in homeassistant/components/thread/discovery.py

View workflow job for this annotation

GitHub Actions / Check mypy

Argument 1 to "try_decode" has incompatible type "str | bytes | None"; expected "bytes | None" [arg-type]
thread_version = try_decode(service_properties.get(b"tv"))

Check failure on line 73 in homeassistant/components/thread/discovery.py

View workflow job for this annotation

GitHub Actions / Check mypy

Argument 1 to "try_decode" has incompatible type "str | bytes | None"; expected "bytes | None" [arg-type]
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)

Check failure on line 80 in homeassistant/components/thread/discovery.py

View workflow job for this annotation

GitHub Actions / Check mypy

Argument 1 to "from_bytes" of "StateBitmap" has incompatible type "str | bytes"; expected "bytes" [arg-type]
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(
addresses=service.parsed_addresses(),
brand=brand,
extended_address=ext_addr.hex() if ext_addr is not None else None,

Check failure on line 91 in homeassistant/components/thread/discovery.py

View workflow job for this annotation

GitHub Actions / Check mypy

Item "str" of "str | bytes" has no attribute "hex" [union-attr]
extended_pan_id=ext_pan_id.hex() if ext_pan_id is not None else None,
model_name=model_name,
network_name=network_name,
Expand Down Expand Up @@ -168,10 +175,21 @@
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
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 @@
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)

Check failure on line 562 in homeassistant/components/zeroconf/__init__.py

View workflow job for this annotation

GitHub Actions / Check mypy

Incompatible types in assignment (expression has type "dict[bytes, bytes | None]", variable has type "dict[str | bytes, str | bytes | None]") [assignment]
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

Check failure on line 565 in homeassistant/components/zeroconf/__init__.py

View workflow job for this annotation

GitHub Actions / Check mypy

Item "str" of "str | bytes" has no attribute "decode" [union-attr]
if v is None
else v.decode("utf-8", "replace")

Check failure on line 567 in homeassistant/components/zeroconf/__init__.py

View workflow job for this annotation

GitHub Actions / Check mypy

Item "str" of "str | bytes" has no attribute "decode" [union-attr]
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
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
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
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
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