From 58d732d2c913da7272b774f98f13e0cd402121b2 Mon Sep 17 00:00:00 2001 From: Raphael Hehl <7577984+RaHehl@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:39:49 +0100 Subject: [PATCH] Let the multi-tier service announce a configured accessory address The multi-tier service always announced the address of the HAP connection, so a configured stream address could not be used. MultiTierConfig gets an optional accessory_address. --- pyhap/camera_multi_tier.py | 25 ++++++++++++++---- tests/test_camera_multi_tier.py | 45 +++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/pyhap/camera_multi_tier.py b/pyhap/camera_multi_tier.py index d35815c9..1fa9eb46 100644 --- a/pyhap/camera_multi_tier.py +++ b/pyhap/camera_multi_tier.py @@ -62,8 +62,9 @@ class MultiTierSession(StreamSession): ``address`` and the ports are where the controller receives, with the SRTP keys it sent. The accessory sends from ``accessory_address`` and the ``accessory_*_port`` values, by default its address on the controller's - connection and the controller's port numbers. ``handle_prepare`` may - change them. ``video_ssrc`` and ``audio_ssrc`` are the accessory's SSRCs + connection (see ``MultiTierConfig.accessory_address``) and the + controller's port numbers. ``handle_prepare`` may change them. + ``video_ssrc`` and ``audio_ssrc`` are the accessory's SSRCs from the Setup Endpoints answer, the controller filters by them. The ``controller_*_ssrc`` values are what the controller sent with the last start. ``video_tier`` and ``audio_tier`` are what streams once started. @@ -171,6 +172,11 @@ class MultiTierConfig: A started session ends unless :meth:`MultiTierSession.keep_alive` is called at least every ``idle_timeout`` seconds, e.g. for each RTCP packet of the controller. None turns that off. + ``accessory_address`` is the address the Setup Endpoints answer announces + for sessions of its IP version, e.g. for an accessory with several + interfaces. Without it, and for sessions of the other version, + the answer has the accessory's address on the controller's connection. + ``handle_prepare`` may still change it. """ delegate: MultiTierDelegate | None = None @@ -180,6 +186,7 @@ class MultiTierConfig: hook_timeout: float = DEFAULT_HOOK_TIMEOUT setup_timeout: float = 30.0 idle_timeout: float | None = DEFAULT_IDLE_TIMEOUT + accessory_address: str | None = None @classmethod def from_option(cls, value: MultiTierConfig | Mapping[str, Any]) -> MultiTierConfig: @@ -206,6 +213,11 @@ def __post_init__(self) -> None: self.idle_timeout is not None and self.idle_timeout <= 0 ): raise ValueError("multi_tier timeouts must be positive") + address = self.accessory_address + if address is not None and not ( + _valid_address(address, False) or _valid_address(address, True) + ): + raise ValueError("multi_tier accessory_address must be an IP address") def _supported_audio_tiers(audio: hksv3.AudioStreamTier) -> bytes: @@ -360,9 +372,12 @@ async def _setup( if self.delegate is None: logger.warning("[%s] No multi-tier delegate, refusing", session_id) return error - accessory_address = self.camera._stream_address_for( - client_addr, endpoint.is_ipv6 - ) + accessory_address = self.config.accessory_address + if not _valid_address(accessory_address, endpoint.is_ipv6): + # Not set, or of the other IP version. + accessory_address = self.camera._stream_address_for( + client_addr, endpoint.is_ipv6 + ) if accessory_address is None: logger.warning( "[%s] No local address of the controller's IP version", session_id diff --git a/tests/test_camera_multi_tier.py b/tests/test_camera_multi_tier.py index d3dd2001..ecf7120c 100644 --- a/tests/test_camera_multi_tier.py +++ b/tests/test_camera_multi_tier.py @@ -115,6 +115,9 @@ async def test_rtp_services_come_after(stream_camera: Callable[..., Camera]) -> ({"multi_tier": {"hook_timeout": 9}}, "hook_timeout"), ({"webrtc": {"remote_quality": 5}}, "remote_quality"), ({"multi_tier": {"audio": None}}, "exactly one audio tier"), + ({"multi_tier": {"accessory_address": "cam.local"}}, "address must be"), + ({"multi_tier": {"accessory_address": "fe80::1%eth0"}}, "address must be"), + ({"multi_tier": {"accessory_address": "::ffff:1.2.3.4"}}, "address must be"), ], ) async def test_invalid_options( @@ -337,8 +340,45 @@ async def test_setup_answers_from_the_connection( assert address[SETUP_ADDR_INFO["ADDRESS"]] == b"fd00::10" +IPV4 = (b"192.168.1.114", False, "192.168.1.10") +IPV6 = (b"fd00::1", True, "fd00::10") + + +@pytest.mark.parametrize( + ("configured", "family", "announced"), + [ + (None, IPV4, b"192.168.1.10"), + (None, IPV6, b"fd00::10"), + ("192.0.2.10", IPV4, b"192.0.2.10"), + ("192.0.2.10", IPV6, b"fd00::10"), + ("fd00::20", IPV6, b"fd00::20"), + ("fd00::20", IPV4, b"192.168.1.10"), + ], + ids=["unset-v4", "unset-v6", "v4", "v4-on-v6", "v6", "v6-on-v4"], +) +async def test_setup_answers_from_the_configured_address( + stream_camera: Callable[..., Camera], + multi_tier_delegate: AsyncMock, + configured: str | None, + family: tuple[bytes, bool, str], + announced: bytes, +) -> None: + """Test ``accessory_address`` is announced to controllers of its IP version.""" + controller, is_ipv6, local = family + camera = stream_camera( + multi_tier={"delegate": multi_tier_delegate, "accessory_address": configured} + ) + with patch.object(camera.driver, "_local_address", return_value=local): + answer = await _setup(camera, _setup_request(controller, is_ipv6)) + address = tlv.decode(answer[SETUP_TYPES["ADDRESS"]]) + assert address[SETUP_ADDR_INFO["ADDRESS"]] == announced + + +@pytest.mark.parametrize("configured", [None, "192.0.2.10"]) async def test_prepare_may_change_the_answer( - stream_camera: Callable[..., Camera], multi_tier_delegate: AsyncMock + stream_camera: Callable[..., Camera], + multi_tier_delegate: AsyncMock, + configured: str | None, ) -> None: """Test the delegate may answer with other addresses and ports.""" @@ -347,7 +387,8 @@ async def _prepare(session: MultiTierSession) -> None: session.accessory_video_port = 40000 multi_tier_delegate.handle_prepare.side_effect = _prepare - answer = await _setup(stream_camera()) + options = {"delegate": multi_tier_delegate, "accessory_address": configured} + answer = await _setup(stream_camera(multi_tier=options)) address = tlv.decode(answer[SETUP_TYPES["ADDRESS"]]) assert address[SETUP_ADDR_INFO["ADDRESS"]] == b"192.168.1.20" assert address[SETUP_ADDR_INFO["VIDEO_RTP_PORT"]] == struct.pack("