File: message.py

package info (click to toggle)
python-asyncio-mqtt 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 480 kB
  • sloc: python: 1,225; sh: 57; makefile: 5
file content (75 lines) | stat: -rw-r--r-- 2,389 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations

import sys

import paho.mqtt.client as mqtt
from paho.mqtt.properties import Properties

if sys.version_info >= (3, 11):
    from typing import Self
else:
    from typing_extensions import Self

from .topic import Topic, TopicLike
from .types import PayloadType


class Message:
    """Wraps the paho-mqtt message class to allow using our own matching logic.

    This class is not meant to be instantiated by the user. Instead, it is yielded by
    the async generator ``Client.messages``.

    Args:
        topic: The topic the message was published to.
        payload: The message payload.
        qos: The quality of service level of the subscription that matched the message.
        retain: Whether the message is a retained message.
        mid: The message ID.
        properties: (MQTT v5.0 only) The properties associated with the message.

    Attributes:
        topic (aiomqtt.client.Topic):
            The topic the message was published to.
        payload (str | bytes | bytearray | int | float | None):
            The message payload.
        qos (int):
            The quality of service level of the subscription that matched the message.
        retain (bool):
            Whether the message is a retained message.
        mid (int):
            The message ID.
        properties (paho.mqtt.properties.Properties | None):
            (MQTT v5.0 only) The properties associated with the message.
    """

    def __init__(  # noqa: PLR0913
        self,
        topic: TopicLike,
        payload: PayloadType,
        qos: int,
        retain: bool,
        mid: int,
        properties: Properties | None,
    ) -> None:
        self.topic = Topic(topic) if not isinstance(topic, Topic) else topic
        self.payload = payload
        self.qos = qos
        self.retain = retain
        self.mid = mid
        self.properties = properties

    @classmethod
    def _from_paho_message(cls, message: mqtt.MQTTMessage) -> Self:
        return cls(
            topic=message.topic,
            payload=message.payload,
            qos=message.qos,
            retain=message.retain,
            mid=message.mid,
            properties=message.properties if hasattr(message, "properties") else None,
        )

    def __lt__(self, other: Self) -> bool:
        return self.mid < other.mid