Home Assistant Unofficial Reference 2024.12.1
homeassistant.components.mqtt.client.MQTT Class Reference

Public Member Functions

None __init__ (self, HomeAssistant hass, ConfigEntry config_entry, ConfigType conf)
 
None async_connect (self, asyncio.Future[bool] client_available)
 
None async_disconnect (self, bool disconnect_paho_client=False)
 
None async_init_client (self)
 
None async_publish (self, str topic, PublishPayloadType payload, int qos, bool retain)
 
None async_restore_tracked_subscriptions (self, set[Subscription] subscriptions)
 
None async_start (self, MqttData mqtt_data)
 
Callable[[], None] async_subscribe (self, str topic, Callable[[ReceiveMessage], Coroutine[Any, Any, None]|None] msg_callback, int qos, str|None encoding=None, HassJobType|None job_type=None)
 
None cleanup (self)
 
set[Subscriptionsubscriptions (self)
 

Public Attributes

 conf
 
 config_entry
 
 connected
 
 hass
 
 loop
 

Private Member Functions

None _async_cancel_reconnect (self)
 
AsyncGenerator[None] _async_connect_in_executor (self)
 
None _async_connection_result (self, bool connected)
 
asyncio.Future[None] _async_get_mid_future (self, int mid)
 
None _async_ha_started (self, HomeAssistant _hass)
 
None _async_ha_stop (self, Event _event)
 
None _async_mqtt_on_callback (self, mqtt.Client _mqttc, None _userdata, int mid, tuple[int,...]|mqtt.ReasonCodes|None _granted_qos_reason=None, mqtt.ReasonCodes|None _properties_reason=None)
 
None _async_mqtt_on_connect (self, mqtt.Client _mqttc, None _userdata, dict[str, int] _flags, int result_code, mqtt.Properties|None properties=None)
 
None _async_mqtt_on_disconnect (self, mqtt.Client _mqttc, None _userdata, int result_code, mqtt.Properties|None properties=None)
 
None _async_mqtt_on_message (self, mqtt.Client _mqttc, None _userdata, mqtt.MQTTMessage msg)
 
None _async_on_disconnect (self, int result_code)
 
None _async_on_socket_close (self, mqtt.Client client, Any userdata, SocketType sock)
 
None _async_on_socket_open (self, mqtt.Client client, Any userdata, SocketType sock)
 
None _async_on_socket_register_write (self, mqtt.Client client, Any userdata, SocketType sock)
 
None _async_on_socket_unregister_write (self, mqtt.Client client, Any userdata, SocketType sock)
 
None _async_perform_subscriptions (self)
 
None _async_perform_unsubscribes (self)
 
None _async_queue_resubscribe (self)
 
None _async_queue_subscriptions (self, Iterable[tuple[str, int]] subscriptions, bool queue_only=False)
 
None _async_reader_callback (self, mqtt.Client client)
 
None _async_remove (self, Subscription subscription)
 
None _async_resubscribe_and_publish_birth_message (self, PublishMessage birth_message)
 
None _async_start_misc_periodic (self)
 
None _async_timeout_mid (self, asyncio.Future[None] future)
 
None _async_track_subscription (self, Subscription subscription)
 
None _async_unsubscribe (self, str topic)
 
None _async_untrack_subscription (self, Subscription subscription)
 
None _async_wait_for_mid_or_raise (self, int mid, int result_code)
 
None _async_writer_callback (self, mqtt.Client client)
 
None _discovery_cooldown (self)
 
str _exception_message (self, Callable[[ReceiveMessage], Coroutine[Any, Any, None]|None] msg_callback, ReceiveMessage msg)
 
None _increase_socket_buffer_size (self, SocketType sock)
 
bool _is_active_subscription (self, str topic)
 
list[Subscription_matching_subscriptions (self, str topic)
 
None _on_socket_open (self, mqtt.Client client, Any userdata, SocketType sock)
 
None _on_socket_register_write (self, mqtt.Client client, Any userdata, SocketType sock)
 
None _reconnect_loop (self)
 

Private Attributes

 _available_future
 
 _connection_lock
 
 _ha_started
 
 _last_subscribe
 
 _misc_timer
 
 _mqtt_data
 
 _mqttc
 
 _pending_subscriptions
 
 _pending_unsubscribes
 
 _reconnect_task
 
 _should_reconnect
 
 _subscribe_debouncer
 
 _unsubscribe_debouncer
 

Detailed Description

Home Assistant MQTT client.

Definition at line 360 of file client.py.

Constructor & Destructor Documentation

◆ __init__()

None homeassistant.components.mqtt.client.MQTT.__init__ (   self,
HomeAssistant  hass,
ConfigEntry  config_entry,
ConfigType   conf 
)
Initialize Home Assistant MQTT client.

Definition at line 367 of file client.py.

Member Function Documentation

◆ _async_cancel_reconnect()

None homeassistant.components.mqtt.client.MQTT._async_cancel_reconnect (   self)
private
Cancel the reconnect task.

Definition at line 690 of file client.py.

◆ _async_connect_in_executor()

AsyncGenerator[None] homeassistant.components.mqtt.client.MQTT._async_connect_in_executor (   self)
private

Definition at line 446 of file client.py.

◆ _async_connection_result()

None homeassistant.components.mqtt.client.MQTT._async_connection_result (   self,
bool  connected 
)
private
Handle a connection result.

Definition at line 677 of file client.py.

◆ _async_get_mid_future()

asyncio.Future[None] homeassistant.components.mqtt.client.MQTT._async_get_mid_future (   self,
int  mid 
)
private
Get the future for a mid.

Definition at line 1165 of file client.py.

◆ _async_ha_started()

None homeassistant.components.mqtt.client.MQTT._async_ha_started (   self,
HomeAssistant  _hass 
)
private
Handle HA started.

Definition at line 416 of file client.py.

◆ _async_ha_stop()

None homeassistant.components.mqtt.client.MQTT._async_ha_stop (   self,
Event  _event 
)
private
Handle HA stop.

Definition at line 420 of file client.py.

◆ _async_mqtt_on_callback()

None homeassistant.components.mqtt.client.MQTT._async_mqtt_on_callback (   self,
mqtt.Client  _mqttc,
None  _userdata,
int  mid,
tuple[int, ...] | mqtt.ReasonCodes | None   _granted_qos_reason = None,
mqtt.ReasonCodes | None   _properties_reason = None 
)
private
Publish / Subscribe / Unsubscribe callback.

Definition at line 1146 of file client.py.

◆ _async_mqtt_on_connect()

None homeassistant.components.mqtt.client.MQTT._async_mqtt_on_connect (   self,
mqtt.Client  _mqttc,
None  _userdata,
dict[str, int]  _flags,
int  result_code,
mqtt.Properties | None   properties = None 
)
private
On connect callback.

Resubscribe to all topics we were subscribed to and publish birth
message.

Definition at line 972 of file client.py.

◆ _async_mqtt_on_disconnect()

None homeassistant.components.mqtt.client.MQTT._async_mqtt_on_disconnect (   self,
mqtt.Client  _mqttc,
None  _userdata,
int  result_code,
mqtt.Properties | None   properties = None 
)
private
Disconnected callback.

Definition at line 1174 of file client.py.

◆ _async_mqtt_on_message()

None homeassistant.components.mqtt.client.MQTT._async_mqtt_on_message (   self,
mqtt.Client  _mqttc,
None  _userdata,
mqtt.MQTTMessage   msg 
)
private

Definition at line 1064 of file client.py.

◆ _async_on_disconnect()

None homeassistant.components.mqtt.client.MQTT._async_on_disconnect (   self,
int  result_code 
)
private

Definition at line 1185 of file client.py.

◆ _async_on_socket_close()

None homeassistant.components.mqtt.client.MQTT._async_on_socket_close (   self,
mqtt.Client  client,
Any  userdata,
SocketType   sock 
)
private
Handle socket close.

Definition at line 578 of file client.py.

◆ _async_on_socket_open()

None homeassistant.components.mqtt.client.MQTT._async_on_socket_open (   self,
mqtt.Client  client,
Any  userdata,
SocketType   sock 
)
private
Handle socket open.

Definition at line 562 of file client.py.

◆ _async_on_socket_register_write()

None homeassistant.components.mqtt.client.MQTT._async_on_socket_register_write (   self,
mqtt.Client  client,
Any  userdata,
SocketType   sock 
)
private
Register the socket for writing.

Definition at line 608 of file client.py.

◆ _async_on_socket_unregister_write()

None homeassistant.components.mqtt.client.MQTT._async_on_socket_unregister_write (   self,
mqtt.Client  client,
Any  userdata,
SocketType   sock 
)
private
Unregister the socket for writing.

Definition at line 618 of file client.py.

◆ _async_perform_subscriptions()

None homeassistant.components.mqtt.client.MQTT._async_perform_subscriptions (   self)
private
Perform MQTT client subscriptions.

Definition at line 882 of file client.py.

◆ _async_perform_unsubscribes()

None homeassistant.components.mqtt.client.MQTT._async_perform_unsubscribes (   self)
private
Perform pending MQTT client unsubscribes.

Definition at line 931 of file client.py.

◆ _async_queue_resubscribe()

None homeassistant.components.mqtt.client.MQTT._async_queue_resubscribe (   self)
private
Queue subscriptions on reconnect.

self._async_perform_subscriptions must be called
after this method to actually subscribe.

Definition at line 1028 of file client.py.

◆ _async_queue_subscriptions()

None homeassistant.components.mqtt.client.MQTT._async_queue_subscriptions (   self,
Iterable[tuple[str, int]]  subscriptions,
bool   queue_only = False 
)
private
Queue requested subscriptions.

Definition at line 782 of file client.py.

◆ _async_reader_callback()

None homeassistant.components.mqtt.client.MQTT._async_reader_callback (   self,
mqtt.Client  client 
)
private
Handle reading data from the socket.

Definition at line 499 of file client.py.

◆ _async_remove()

None homeassistant.components.mqtt.client.MQTT._async_remove (   self,
Subscription  subscription 
)
private
Remove subscription.

Definition at line 853 of file client.py.

◆ _async_resubscribe_and_publish_birth_message()

None homeassistant.components.mqtt.client.MQTT._async_resubscribe_and_publish_birth_message (   self,
PublishMessage   birth_message 
)
private
Resubscribe to all topics and publish birth message.

Definition at line 951 of file client.py.

◆ _async_start_misc_periodic()

None homeassistant.components.mqtt.client.MQTT._async_start_misc_periodic (   self)
private
Start the misc periodic.

Definition at line 505 of file client.py.

◆ _async_timeout_mid()

None homeassistant.components.mqtt.client.MQTT._async_timeout_mid (   self,
asyncio.Future[None]  future 
)
private
Timeout waiting for a mid.

Definition at line 1204 of file client.py.

◆ _async_track_subscription()

None homeassistant.components.mqtt.client.MQTT._async_track_subscription (   self,
Subscription  subscription 
)
private
Track a subscription.

This method does not send a SUBSCRIBE message to the broker.

The caller is responsible clearing the cache of _matching_subscriptions.

Definition at line 749 of file client.py.

◆ _async_unsubscribe()

None homeassistant.components.mqtt.client.MQTT._async_unsubscribe (   self,
str  topic 
)
private
Unsubscribe from a topic.

Definition at line 864 of file client.py.

◆ _async_untrack_subscription()

None homeassistant.components.mqtt.client.MQTT._async_untrack_subscription (   self,
Subscription  subscription 
)
private
Untrack a subscription.

This method does not send an UNSUBSCRIBE message to the broker.

The caller is responsible clearing the cache of _matching_subscriptions.

Definition at line 762 of file client.py.

◆ _async_wait_for_mid_or_raise()

None homeassistant.components.mqtt.client.MQTT._async_wait_for_mid_or_raise (   self,
int  mid,
int  result_code 
)
private
Wait for ACK from broker or raise on error.

Definition at line 1209 of file client.py.

◆ _async_writer_callback()

None homeassistant.components.mqtt.client.MQTT._async_writer_callback (   self,
mqtt.Client  client 
)
private
Handle writing data to the socket.

Definition at line 594 of file client.py.

◆ _discovery_cooldown()

None homeassistant.components.mqtt.client.MQTT._discovery_cooldown (   self)
private
Wait until all discovery and subscriptions are processed.

Definition at line 1234 of file client.py.

◆ _exception_message()

str homeassistant.components.mqtt.client.MQTT._exception_message (   self,
Callable[[ReceiveMessage], Coroutine[Any, Any, None] | None]  msg_callback,
ReceiveMessage  msg 
)
private
Return a string with the exception message.

Definition at line 798 of file client.py.

◆ _increase_socket_buffer_size()

None homeassistant.components.mqtt.client.MQTT._increase_socket_buffer_size (   self,
SocketType  sock 
)
private
Increase the socket buffer size.

Definition at line 522 of file client.py.

◆ _is_active_subscription()

bool homeassistant.components.mqtt.client.MQTT._is_active_subscription (   self,
str  topic 
)
private
Check if a topic has an active subscription.

Definition at line 627 of file client.py.

◆ _matching_subscriptions()

list[Subscription] homeassistant.components.mqtt.client.MQTT._matching_subscriptions (   self,
str  topic 
)
private

Definition at line 1050 of file client.py.

◆ _on_socket_open()

None homeassistant.components.mqtt.client.MQTT._on_socket_open (   self,
mqtt.Client  client,
Any  userdata,
SocketType   sock 
)
private
Handle socket open.

Definition at line 553 of file client.py.

◆ _on_socket_register_write()

None homeassistant.components.mqtt.client.MQTT._on_socket_register_write (   self,
mqtt.Client  client,
Any  userdata,
SocketType   sock 
)
private
Register the socket for writing.

Definition at line 599 of file client.py.

◆ _reconnect_loop()

None homeassistant.components.mqtt.client.MQTT._reconnect_loop (   self)
private
Reconnect to the MQTT server.

Definition at line 696 of file client.py.

◆ async_connect()

None homeassistant.components.mqtt.client.MQTT.async_connect (   self,
asyncio.Future[bool]  client_available 
)
Connect to the host. Does not process messages yet.

Definition at line 648 of file client.py.

◆ async_disconnect()

None homeassistant.components.mqtt.client.MQTT.async_disconnect (   self,
bool   disconnect_paho_client = False 
)
Stop the MQTT client.

We only disconnect grafully if disconnect_paho_client is set, but not
when Home Assistant is shut down.

Definition at line 710 of file client.py.

◆ async_init_client()

None homeassistant.components.mqtt.client.MQTT.async_init_client (   self)
Initialize paho client.

Definition at line 461 of file client.py.

◆ async_publish()

None homeassistant.components.mqtt.client.MQTT.async_publish (   self,
str  topic,
PublishPayloadType  payload,
int  qos,
bool   retain 
)
Publish a MQTT message.

Definition at line 633 of file client.py.

◆ async_restore_tracked_subscriptions()

None homeassistant.components.mqtt.client.MQTT.async_restore_tracked_subscriptions (   self,
set[Subscription]   subscriptions 
)
Restore tracked subscriptions after reload.

Definition at line 740 of file client.py.

◆ async_start()

None homeassistant.components.mqtt.client.MQTT.async_start (   self,
MqttData  mqtt_data 
)
Start Home Assistant MQTT client.

Definition at line 424 of file client.py.

◆ async_subscribe()

Callable[[], None] homeassistant.components.mqtt.client.MQTT.async_subscribe (   self,
str  topic,
Callable[[ReceiveMessage], Coroutine[Any, Any, None] | None]  msg_callback,
int  qos,
str | None   encoding = None,
HassJobType | None   job_type = None 
)
Set up a subscription to a topic with the provided qos.

Definition at line 815 of file client.py.

◆ cleanup()

None homeassistant.components.mqtt.client.MQTT.cleanup (   self)
Clean up listeners.

Definition at line 440 of file client.py.

◆ subscriptions()

set[Subscription] homeassistant.components.mqtt.client.MQTT.subscriptions (   self)
Return the tracked subscriptions.

Definition at line 433 of file client.py.

Member Data Documentation

◆ _available_future

homeassistant.components.mqtt.client.MQTT._available_future
private

Definition at line 654 of file client.py.

◆ _connection_lock

homeassistant.components.mqtt.client.MQTT._connection_lock
private

Definition at line 391 of file client.py.

◆ _ha_started

homeassistant.components.mqtt.client.MQTT._ha_started
private

Definition at line 388 of file client.py.

◆ _last_subscribe

homeassistant.components.mqtt.client.MQTT._last_subscribe
private

Definition at line 927 of file client.py.

◆ _misc_timer

homeassistant.components.mqtt.client.MQTT._misc_timer
private

Definition at line 518 of file client.py.

◆ _mqtt_data

homeassistant.components.mqtt.client.MQTT._mqtt_data
private

Definition at line 429 of file client.py.

◆ _mqttc

homeassistant.components.mqtt.client.MQTT._mqttc
private

Definition at line 496 of file client.py.

◆ _pending_subscriptions

homeassistant.components.mqtt.client.MQTT._pending_subscriptions
private

Definition at line 907 of file client.py.

◆ _pending_unsubscribes

homeassistant.components.mqtt.client.MQTT._pending_unsubscribes
private

Definition at line 937 of file client.py.

◆ _reconnect_task

homeassistant.components.mqtt.client.MQTT._reconnect_task
private

Definition at line 685 of file client.py.

◆ _should_reconnect

homeassistant.components.mqtt.client.MQTT._should_reconnect
private

Definition at line 655 of file client.py.

◆ _subscribe_debouncer

homeassistant.components.mqtt.client.MQTT._subscribe_debouncer
private

Definition at line 393 of file client.py.

◆ _unsubscribe_debouncer

homeassistant.components.mqtt.client.MQTT._unsubscribe_debouncer
private

Definition at line 403 of file client.py.

◆ conf

homeassistant.components.mqtt.client.MQTT.conf

Definition at line 374 of file client.py.

◆ config_entry

homeassistant.components.mqtt.client.MQTT.config_entry

Definition at line 373 of file client.py.

◆ connected

homeassistant.components.mqtt.client.MQTT.connected

Definition at line 387 of file client.py.

◆ hass

homeassistant.components.mqtt.client.MQTT.hass

Definition at line 371 of file client.py.

◆ loop

homeassistant.components.mqtt.client.MQTT.loop

Definition at line 372 of file client.py.


The documentation for this class was generated from the following file: