Coverage for src / zmqtt / _internal / types / message.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-11-12 08:49 +0000

1from collections.abc import Awaitable, Callable 

2from dataclasses import dataclass, field 

3 

4from zmqtt._internal.packets.properties import PublishProperties 

5from zmqtt._internal.types.qos import QoS 

6 

7 

8@dataclass(slots=True, kw_only=True) 

9class Message: 

10 """Incoming MQTT message as delivered to application code. 

11 

12 Attributes: 

13 topic: The topic on which the message was published. 

14 payload: Raw message body as bytes. 

15 qos: QoS level at which the message was delivered. 

16 retain: ``True`` if the broker sent this as a retained message. 

17 properties: MQTT 5.0 publish properties, or ``None`` for MQTT 3.1.1 

18 connections. 

19 """ 

20 

21 topic: str 

22 payload: bytes 

23 qos: QoS 

24 retain: bool 

25 properties: PublishProperties | None = None # v5 only 

26 _ack_callback: Callable[[], Awaitable[None]] | None = field( 

27 default=None, 

28 repr=False, 

29 init=False, 

30 ) 

31 _resolved: bool = field(default=False, repr=False, init=False) 

32 

33 async def ack(self) -> None: 

34 """ 

35 Send the protocol-level ack for this message. 

36 Idempotent; no-op when auto_ack=True. 

37 """ 

38 if self._resolved: 

39 return 

40 object.__setattr__(self, "_resolved", True) 

41 if self._ack_callback is not None: 

42 await self._ack_callback()