Skip to content

Commit 330e36c

Browse files
authored
Ensure the ServiceInfo.key gets updated when the name is changed externally (#645)
1 parent 5ebd954 commit 330e36c

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

tests/test_services.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,3 +1239,22 @@ def mock_incoming_msg(records) -> r.DNSIncoming:
12391239
browser.cancel()
12401240

12411241
zc.close()
1242+
1243+
1244+
def test_changing_name_updates_serviceinfo_key():
1245+
"""Verify a name change will adjust the underlying key value."""
1246+
type_ = "_homeassistant._tcp.local."
1247+
name = "MyTestHome"
1248+
info_service = ServiceInfo(
1249+
type_,
1250+
'%s.%s' % (name, type_),
1251+
80,
1252+
0,
1253+
0,
1254+
{'path': '/~paulsm/'},
1255+
"ash-2.local.",
1256+
addresses=[socket.inet_aton("10.0.1.2")],
1257+
)
1258+
assert info_service.key == "mytesthome._homeassistant._tcp.local."
1259+
info_service.name = "YourTestHome._homeassistant._tcp.local."
1260+
assert info_service.key == "yourtesthome._homeassistant._tcp.local."

zeroconf/_services/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def __init__(
466466
if not type_.endswith(service_type_name(name, strict=False)):
467467
raise BadTypeInNameException
468468
self.type = type_
469-
self.name = name
469+
self._name = name
470470
self.key = name.lower()
471471
if addresses is not None:
472472
self._addresses = addresses
@@ -494,6 +494,17 @@ def __init__(
494494
self.host_ttl = host_ttl
495495
self.other_ttl = other_ttl
496496

497+
@property
498+
def name(self) -> str:
499+
"""The name of the service."""
500+
return self._name
501+
502+
@name.setter
503+
def name(self, name: str) -> None:
504+
"""Replace the the name and reset the key."""
505+
self._name = name
506+
self.key = name.lower()
507+
497508
@property
498509
def addresses(self) -> List[bytes]:
499510
"""IPv4 addresses of this service.

0 commit comments

Comments
 (0)