Coverage for faststream/mqtt/broker/config.py: 88%
36 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1from dataclasses import dataclass, field
2from types import MappingProxyType
3from typing import TYPE_CHECKING, Any, Literal, Optional, cast
5from faststream._internal._compat import HAS_OPENTELEMETRY
6from faststream._internal.configs import (
7 BrokerConfig,
8 UnderlyingDriverAnnotation,
9 UnderlyingDriverAnnotations,
10)
11from faststream._internal.parser import DefaultCodec
12from faststream.exceptions import FeatureNotSupportedException, IncorrectState
13from faststream.mqtt.parser import MQTTVersion
14from faststream.mqtt.publisher.producer import ZmqttFakeProducer
16if TYPE_CHECKING:
17 import zmqtt
19 from faststream._internal.types import BrokerMiddleware
20 from faststream.mqtt.publisher.producer import ZmqttBaseProducer
22if HAS_OPENTELEMETRY:
23 from faststream.opentelemetry.middleware import TelemetryMiddleware
26MQTTVersionUnset = cast("str", object())
29def _context_annotations_factory() -> UnderlyingDriverAnnotations:
30 # `annotations` reaches this module through the broker, so the
31 # objects a row needs only exist once the package is built.
32 from zmqtt.client import MQTTClient # ruff: ignore[import-outside-top-level]
34 from faststream.mqtt import annotations # ruff: ignore[import-outside-top-level]
35 from faststream.mqtt.broker.broker import ( # ruff: ignore[import-outside-top-level]
36 MQTTBroker as MQTTBrokerDriver,
37 )
38 from faststream.mqtt.message import MQTTMessage as MQTTMessageDriver # ruff: ignore[import-outside-top-level]
40 return MappingProxyType(
41 {
42 MQTTClient: UnderlyingDriverAnnotation(
43 type_hint=annotations.Client,
44 module="faststream.mqtt.annotations",
45 name="Client",
46 ),
47 MQTTBrokerDriver: UnderlyingDriverAnnotation(
48 type_hint=annotations.MQTTBroker,
49 module="faststream.mqtt.annotations",
50 name="MQTTBroker",
51 ),
52 MQTTMessageDriver: UnderlyingDriverAnnotation(
53 type_hint=annotations.MQTTMessage,
54 module="faststream.mqtt.annotations",
55 name="MQTTMessage",
56 ),
57 },
58 )
61@dataclass(kw_only=True)
62class MQTTBrokerConfig(BrokerConfig):
63 version: MQTTVersion | Literal["unset"] = "unset"
65 producer: "ZmqttBaseProducer" = field(default_factory=ZmqttFakeProducer)
66 _client: Optional["zmqtt.MQTTClient"] = field(default=None, init=False, repr=False)
68 default_driver_annotations: UnderlyingDriverAnnotations = field(
69 default_factory=_context_annotations_factory,
70 )
72 def __post_init__(self) -> None:
73 super().__post_init__()
75 for m in self.broker_middlewares:
76 self._validate_middleware(m)
78 @property
79 def client(self) -> "zmqtt.MQTTClient":
80 if self._client is None: 80 ↛ 81line 80 didn't jump to line 81 because the condition on line 80 was never true
81 msg = "MQTT broker is not connected. Call connect() first."
82 raise IncorrectState(msg)
83 return self._client
85 def connect(self, client: "zmqtt.MQTTClient") -> None:
86 self._client = client
87 self.producer.connect(
88 client, self.fd_config._serializer, codec=self.broker_codec or DefaultCodec()
89 )
91 def disconnect(self) -> None:
92 self._client = None
93 self.producer.disconnect()
95 def add_middleware(self, middleware: "BrokerMiddleware[Any]") -> None:
96 self._validate_middleware(middleware)
97 return super().add_middleware(middleware)
99 def insert_middleware(self, middleware: "BrokerMiddleware[Any]") -> None:
100 self._validate_middleware(middleware)
101 return super().insert_middleware(middleware)
103 def _validate_middleware(self, middleware: "BrokerMiddleware[Any]") -> None:
104 if (
105 HAS_OPENTELEMETRY
106 and self.version == "3.1.1"
107 and isinstance(middleware, TelemetryMiddleware)
108 ):
109 msg = "Opentelementry don`t work in 3.1.1 mqtt"
110 raise FeatureNotSupportedException(msg)