energomera_hass_mqtt.mqtt_client

The package provides additional functionality over aiomqtt.

Classes

DummyLock()

Class providing dummy functionality of asyncio.Lock - that is, no locking is actually done and it reports being always unlocked.

MqttClient(*args[, dry_run])

The class extends the aiomqtt.Client to provide better convenience working with last wills.

class energomera_hass_mqtt.mqtt_client.DummyLock

Bases: Lock

Class providing dummy functionality of asyncio.Lock - that is, no locking is actually done and it reports being always unlocked.

locked()

Provides status of the lock being always unlocked.

Return type:

bool

async acquire()

Does nothing.

Return type:

Literal[True]

release()

Does nothing.

Return type:

None

class energomera_hass_mqtt.mqtt_client.MqttClient(*args, dry_run=False, **kwargs)

Bases: Client

The class extends the aiomqtt.Client to provide better convenience working with last wills. Namely, the original class only allows setting the last will thru its constructor, while the EnergomeraHassMqtt consuming it only has required property for that around actual publish calls.

Parameters:
  • dry_run (bool) – Whether to skip actual connection and publish calls

  • args (Any) – Pass-through positional arguments for parent class

  • kwargs (Any) – Pass-through keyword arguments for parent class

async connect()

Connects to MQTT broker using __aenter__ method of the base class as recommended in https://github.com/sbtinstruments/aiomqtt/blob/main/docs/migration-guide-v2.md. Multiple calls will result only in single call to __aenter__() method of parent class if the MQTT client needs a connection (not being connected or got disconnected), to allow the method to be called within a process loop with no risk of hitting non-reentrant error from base class.

Return type:

None

async disconnect()

Disconnects from MQTT broker using __aexit__ method of the base class as per migration guide above.

Return type:

None

async publish(*args, **kwargs)

Publishes a message to the MQTT broker.

Parameters:
  • args (Any) – Pass-through positional arguments for parent method

  • kwargs (Any) – Pass-through keyword arguments for parent method

Return type:

None

will_set(*args, **kwargs)

Allows setting last will to the underlying MQTT client.

Skips updating the last will if one has already been set (either via the method or through the constructor).

Parameters:
  • args (Any) – Pass-through positional arguments for parent method

  • kwargs (Any) – Pass-through keyword arguments for parent method

Return type:

None

will_clear()

Clears the last will might have been set previously.

Return type:

None

property identifier: str

The client’s identifier.

Note that paho-mqtt stores the client ID as bytes internally. We assume that the client ID is a UTF8-encoded string and decode it first.

property messages: MessagesIterator

Dynamic view of the client’s message queue.

async subscribe(topic, qos=0, options=None, properties=None, *args, timeout=None, **kwargs)

Subscribe to a topic or wildcard.

Return type:

tuple[int, ...] | list[ReasonCode]

Args:

topic: The topic or wildcard to subscribe to. qos: The requested QoS level for the subscription. options: (MQTT v5.0 only) Optional paho-mqtt subscription options. properties: (MQTT v5.0 only) Optional paho-mqtt properties. *args: Additional positional arguments to pass to paho-mqtt’s subscribe

method.

timeout: The maximum time in seconds to wait for the subscription to

complete. Use math.inf to wait indefinitely.

**kwargs: Additional keyword arguments to pass to paho-mqtt’s subscribe

method.

async unsubscribe(topic, properties=None, *args, timeout=None, **kwargs)

Unsubscribe from a topic or wildcard.

Return type:

None

Args:

topic: The topic or wildcard to unsubscribe from. properties: (MQTT v5.0 only) Optional paho-mqtt properties. *args: Additional positional arguments to pass to paho-mqtt’s unsubscribe

method.

timeout: The maximum time in seconds to wait for the unsubscription to

complete. Use math.inf to wait indefinitely.

**kwargs: Additional keyword arguments to pass to paho-mqtt’s unsubscribe

method.