Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 15 additions & 5 deletions pyhap/accessory_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _wrap_service_setter(service, chars, client_addr):
class AccessoryMDNSServiceInfo(ServiceInfo):
"""A mDNS service info representation of an accessory."""

def __init__(self, accessory, state):
def __init__(self, accessory, state, zeroconf_server=None):
self.accessory = accessory
self.state = state

Expand All @@ -131,7 +131,7 @@ def __init__(self, accessory, state):
self.state.mac[-8:].replace(":", ""),
HAP_SERVICE_TYPE,
)
server = "{}-{}.{}".format(
server = zeroconf_server or "{}-{}.{}".format(
self._valid_host_name(),
self.state.mac[-8:].replace(":", ""),
"local.",
Expand Down Expand Up @@ -212,7 +212,8 @@ def __init__(
listen_address=None,
advertised_address=None,
interface_choice=None,
async_zeroconf_instance=None
async_zeroconf_instance=None,
zeroconf_server=None
):
"""
Initialize a new AccessoryDriver object.
Expand Down Expand Up @@ -259,6 +260,10 @@ def __init__(
:param async_zeroconf_instance: An AsyncZeroconf instance. When running multiple accessories or
bridges a single zeroconf instance can be shared to avoid the overhead
of processing the same data multiple times.

:param zeroconf_server: The server name that will be used for the zeroconf
ServiceInfo.
:type zeroconf_server: str
"""
if loop is None:
if sys.platform == "win32":
Expand All @@ -281,6 +286,7 @@ def __init__(

self.accessory = None
self.advertiser = async_zeroconf_instance
self.zeroconf_server = zeroconf_server
self.interface_choice = interface_choice

self.persist_file = os.path.expanduser(persist_file)
Expand Down Expand Up @@ -384,7 +390,9 @@ async def async_start(self):

# Advertise the accessory as a mDNS service.
logger.debug("Starting mDNS.")
self.mdns_service_info = AccessoryMDNSServiceInfo(self.accessory, self.state)
self.mdns_service_info = AccessoryMDNSServiceInfo(
self.accessory, self.state, self.zeroconf_server
)

if not self.advertiser:
zc_args = {}
Expand Down Expand Up @@ -609,7 +617,9 @@ def update_advertisement(self):
def async_update_advertisement(self):
"""Updates the mDNS service info for the accessory from the event loop."""
logger.debug("Updating mDNS advertisement")
self.mdns_service_info = AccessoryMDNSServiceInfo(self.accessory, self.state)
self.mdns_service_info = AccessoryMDNSServiceInfo(
self.accessory, self.state, self.zeroconf_server
)
asyncio.ensure_future(
self.advertiser.async_update_service(self.mdns_service_info)
)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_accessory_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,35 @@ def test_mdns_service_info(driver):
}


def test_mdns_service_info_with_specified_server(driver):
"""Test accessory mdns advert when the server is specified."""
acc = Accessory(driver, "Test Accessory")
driver.add_accessory(acc)
addr = "172.0.0.1"
mac = "00:00:00:00:00:00"
pin = b"123-45-678"
port = 11111
state = State(address=addr, mac=mac, pincode=pin, port=port)
state.setup_id = "abc"
mdns_info = AccessoryMDNSServiceInfo(acc, state, "hap1.local.")
assert mdns_info.type == "_hap._tcp.local."
assert mdns_info.name == "Test Accessory 000000._hap._tcp.local."
assert mdns_info.server == "hap1.local."
assert mdns_info.port == port
assert mdns_info.addresses == [b"\xac\x00\x00\x01"]
assert mdns_info.properties == {
"md": "Test Accessory",
"pv": "1.1",
"id": "00:00:00:00:00:00",
"c#": "1",
"s#": "1",
"ff": "0",
"ci": "1",
"sf": "1",
"sh": "+KjpzQ==",
}


@pytest.mark.parametrize(
"accessory_name, mdns_name, mdns_server",
[
Expand Down