📝 Add Last Will support to the public MQTTClient API (#45)
Problem
zmqtt already implements MQTT Last Will at the packet/codec level, but itcannot be configured through the public high-level client API.
Currently:
•
Will and WillProperties are only available through private zmqtt._internal modules;•
MQTTClient and create_client() do not accept a will= argument;•
MQTTClient._connect() builds every CONNECT packet without a Will.As a result, users and frameworks built on the public
zmqtt.MQTTClient API,including FastStream, cannot configure an MQTT Last Will without depending on
private implementation details.
Proposed API
from zmqtt import MQTTClient, QoS, Will, WillProperties
will = Will(
topic="devices/device-42/status",
payload=b"offline",
qos=QoS.AT_LEAST_ONCE,
retain=True,
properties=WillProperties(
will_delay_interval=10,
content_type="text/plain",
),
)
client = MQTTClient(
"broker.example.com",
version="5.0",
will=will,
)
For MQTT 3.1.1, the same
Will type should be usable withoutWillProperties.Scope
• Export
Will and WillProperties from the top-level zmqtt package.• Add
will: Will | None = None to MQTTClient.• Add the same argument to
create_client() and all of its typed overloads.• Store the configured Will and pass it to every
Connect packet created bythe client.
• Preserve the same Will configuration when the client reconnects after an
established connection is lost.
• Reject MQTT 5.0 Will properties on MQTT 3.1.1 with a clear error consistent
with other version-specific options.
• Keep the current behavior unchanged when
will=None.Tests
The tests should exercise the public client path rather than only the existing
packet codec:
• MQTT 3.1.1: construct
MQTTClient(..., will=...), decode the emitted CONNECT, and verify the Will topic, payload, QoS, and retain flag.• MQTT 5.0: verify the same fields together with
WillProperties.• Simulate
will receiving of second client when the first reconnected unexpectedlyTests should test public behaviour with real broker instead internals testing and avoid monkey patching.
#enhancement #good_first_issue #faststream #zmqtt
sent via relator