def publish_debounced(self, topic, payload, qos=None, retain=None):
    'Publish a message, with debounce.'
    msg = Message(topic, payload, qos, retain)

    def publish_callback():
        _LOGGER.debug('publish_debounced: publishing %s', msg)
        self._pending_messages.pop(msg)
        self.publish(msg.topic, msg.payload, qos=msg.qos, retain=msg.retain)
    if (msg in self._pending_messages):
        timer = self._pending_messages.pop(msg)
        timer.cancel()
    timer = Timer(DEBOUNCE_TIMEOUT, publish_callback)
    self._pending_messages[msg] = timer
Publish a message, with debounce.
def publish_debounced(self, topic, payload, qos=None, retain=None):
    
    msg = Message(topic, payload, qos, retain)

    def publish_callback():
        _LOGGER.debug('publish_debounced: publishing %s', msg)
        self._pending_messages.pop(msg)
        self.publish(msg.topic, msg.payload, qos=msg.qos, retain=msg.retain)
    if (msg in self._pending_messages):
        timer = self._pending_messages.pop(msg)
        timer.cancel()
    timer = Timer(DEBOUNCE_TIMEOUT, publish_callback)
    self._pending_messages[msg] = timer