zigbee.mqtt
zigbee.mqtt
- class platypush.backend.zigbee.mqtt.ZigbeeMqttBackend(host: Optional[str] = None, port: Optional[int] = None, base_topic='zigbee2mqtt', tls_cafile: Optional[str] = None, tls_certfile: Optional[str] = None, tls_keyfile: Optional[str] = None, tls_version: Optional[str] = None, tls_ciphers: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, client_id: Optional[str] = None, *args, **kwargs)[source]
Listen for events on a zigbee2mqtt service.
Triggers:
platypush.message.event.zigbee.mqtt.ZigbeeMqttOnlineEvent
when the service comes online.platypush.message.event.zigbee.mqtt.ZigbeeMqttOfflineEvent
when the service goes offline.platypush.message.event.zigbee.mqtt.ZigbeeMqttDevicePropertySetEvent
when the properties of a connected device change.platypush.message.event.zigbee.mqtt.ZigbeeMqttDevicePairingEvent
when a device is pairing.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceConnectedEvent
when a device connects to the network.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceBannedEvent
when a device is banned from the network.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceRemovedEvent
when a device is removed from the network.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceRemovedFailedEvent
when a request to remove a device from the network fails.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceWhitelistedEvent
when a device is whitelisted on the network.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceRenamedEvent
when a device is renamed on the network.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceBindEvent
when a device bind event occurs.platypush.message.event.zigbee.mqtt.ZigbeeMqttDeviceUnbindEvent
when a device unbind event occurs.platypush.message.event.zigbee.mqtt.ZigbeeMqttGroupAddedEvent
when a group is added.platypush.message.event.zigbee.mqtt.ZigbeeMqttGroupAddedFailedEvent
when a request to add a new group fails.platypush.message.event.zigbee.mqtt.ZigbeeMqttGroupRemovedEvent
when a group is removed.platypush.message.event.zigbee.mqtt.ZigbeeMqttGroupRemovedFailedEvent
when a request to remove a group fails.platypush.message.event.zigbee.mqtt.ZigbeeMqttGroupRemoveAllEvent
when all the devices are removed from a group.platypush.message.event.zigbee.mqtt.ZigbeeMqttGroupRemoveAllFailedEvent
when a request to remove all the devices from a group fails.platypush.message.event.zigbee.mqtt.ZigbeeMqttErrorEvent
when an internal error occurs on the zigbee2mqtt service.
Requires:
paho-mqtt (
pip install paho-mqtt
)The
platypush.plugins.zigbee.mqtt.ZigbeeMqttPlugin
plugin configured.
- __init__(host: Optional[str] = None, port: Optional[int] = None, base_topic='zigbee2mqtt', tls_cafile: Optional[str] = None, tls_certfile: Optional[str] = None, tls_keyfile: Optional[str] = None, tls_version: Optional[str] = None, tls_ciphers: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, client_id: Optional[str] = None, *args, **kwargs)[source]
- Parameters:
host – MQTT broker host (default: host configured on the
zigbee.mqtt
plugin).port – MQTT broker port (default: 1883).
base_topic – Prefix of the topics published by zigbee2mqtt (default: ‘
zigbee2mqtt
’).tls_cafile – If TLS/SSL is enabled on the MQTT server and the certificate requires a certificate authority to authenticate it, ssl_cafile will point to the provided ca.crt file (default: None)
tls_certfile – If TLS/SSL is enabled on the MQTT server and a client certificate it required, specify it here (default: None)
tls_keyfile – If TLS/SSL is enabled on the MQTT server and a client certificate key it required, specify it here (default: None) :type tls_keyfile: str
tls_version – If TLS/SSL is enabled on the MQTT server and it requires a certain TLS version, specify it here (default: None)
tls_ciphers – If TLS/SSL is enabled on the MQTT server and an explicit list of supported ciphers is required, specify it here (default: None)
username – Specify it if the MQTT server requires authentication (default: None)
password – Specify it if the MQTT server requires authentication (default: None)
client_id – MQTT client ID (default:
<device_id>-zigbee-mqtt
, to prevent clashes with theplatypush.backend.mqtt.MqttBackend
client_id
.
- property daemon
A boolean value indicating whether this thread is a daemon thread.
This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when only daemon threads are left.
- getName()
Return a string used for identification purposes only.
This method is deprecated, use the name attribute instead.
- property ident
Thread identifier of this thread or None if it has not been started.
This is a nonzero integer. See the get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.
- isDaemon()
Return whether this thread is a daemon.
This method is deprecated, use the daemon attribute instead.
- is_alive()
Return whether the thread is alive.
This method returns True just before the run() method starts until just after the run() method terminates. See also the module function enumerate().
- join(timeout=None)
Wait until the thread terminates.
This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception or until the optional timeout occurs.
When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.
When the timeout argument is not present or None, the operation will block until the thread terminates.
A thread can be join()ed many times.
join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.
- property name
A string used for identification purposes only.
It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.
- property native_id
Native integral thread ID of this thread, or None if it has not been started.
This is a non-negative integer. See the get_native_id() function. This represents the Thread ID as reported by the kernel.
- on_message(msg)
Callback when a message is received on the backend. It parses and posts the message on the main bus. It should be called by the derived classes whenever a new message should be processed.
- Parameters:
msg – Received message. It can be either a key-value dictionary, a platypush.message.Message object, or a string/byte UTF-8 encoded string
- on_stop()
Callback invoked when the process stops
- register_service(port: Optional[int] = None, name: Optional[str] = None, srv_type: Optional[str] = None, srv_name: Optional[str] = None, udp: bool = False, properties: Optional[Dict] = None)
Initialize the Zeroconf service configuration for this backend.
- Parameters:
port – Service listen port (default: the backend
port
attribute if available, orNone
).name – Service short name (default: backend name).
srv_type – Service type (default:
_platypush-{name}._{proto}.local.
).srv_name – Full service name (default:
{hostname or device_id}.{type}
).udp – Set to True if this is a UDP service.
properties –
Extra properties to be passed on the service. Default:
{ "name": "Platypush", "vendor": "Platypush", "version": "{platypush_version}" }
- run()[source]
Starts the backend thread. To be implemented in the derived classes if the loop method isn’t defined.
- send_event(event, **kwargs)
Send an event message on the backend.
- Parameters:
event – Event to send. It can be a dict, a string/bytes UTF-8 JSON, or a platypush.message.event.Event object.
- send_message(msg, topic: Optional[str] = None, **kwargs)
Sends a platypush.message.Message to a node. To be implemented in the derived classes. By default, if the Redis backend is configured then it will try to deliver the message to other consumers through the configured Redis main queue.
- Parameters:
msg – The message to send
queue_name – Send the message on a specific queue (default: the queue_name configured on the Redis backend)
- send_request(request, on_response=None, response_timeout=5, **kwargs)
Send a request message on the backend.
- Parameters:
request – The request, either a dict, a string/bytes UTF-8 JSON, or a platypush.message.request.Request object.
on_response (function) – Optional callback that will be called when a response is received. If set, this method will synchronously wait for a response before exiting.
response_timeout (float) – If on_response is set, the backend will raise an exception if the response isn’t received within this number of seconds (default: None)
- send_response(response, request, **kwargs)
Send a response message on the backend.
- Parameters:
response – The response, either a dict, a string/bytes UTF-8 JSON, or a
platypush.message.response.Response
object.request – Associated request, used to set the response parameters that will link them
- setDaemon(daemonic)
Set whether this thread is a daemon.
This method is deprecated, use the .daemon property instead.
- setName(name)
Set the name string for this thread.
This method is deprecated, use the name attribute instead.
- start()
Start the thread’s activity.
It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the same thread object.
- stop()
Stops the backend thread by sending a STOP event on its bus
- unregister_service()
Unregister the Zeroconf service configuration if available.