Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE REQUEST: Add client_id to the MQTT connection #1410

Open
51av0sh opened this issue Jan 20, 2025 · 1 comment
Open

FEATURE REQUEST: Add client_id to the MQTT connection #1410

51av0sh opened this issue Jan 20, 2025 · 1 comment

Comments

@51av0sh
Copy link

51av0sh commented Jan 20, 2025

Would it be possible to add client_id variable to the MQTT connection? I needed to troubleshoot some unrelated issues to docker-wyze-bridge and while doing so I noticed that docker-wyze-bridge uses a random client id on every connection, which seems to be every 20 seconds. I'm not a coder but after feeding the code to chatgpt it seems to be an easy fix, I'm just not sure if this 2 line change would break anything else.

Existing code:

`def mqtt_sub_topic(m_topics: list, callback) -> Optional[paho.mqtt.client.Client]:
"""Connect to mqtt and return the client."""

def on_connect(client, *_):
    """Callback for when the client receives a CONNACK response from the server."""
    client.publish(f"{MQTT_TOPIC}/state", "online", retain=True)
    for topic in m_topics:
        # Clear Retain Flag
        client.publish(f"{MQTT_TOPIC}/{topic}", retain=True)
        client.subscribe(f"{MQTT_TOPIC}/{topic}")

client = paho.mqtt.client.Client(paho.mqtt.client.CallbackAPIVersion.VERSION2)

client.username_pw_set(MQTT_USER, MQTT_PASS or None)
client.user_data_set(callback)
client.on_connect = on_connect
client.will_set(f"{MQTT_TOPIC}/state", payload="offline", qos=1, retain=True)
client.connect(MQTT_HOST, int(MQTT_PORT or 1883), 30)
client.loop_start()

return client`

new code suggested by gpt

`def mqtt_sub_topic(m_topics: list, callback) -> Optional[paho.mqtt.client.Client]:
"""Connect to mqtt and return the client."""

def on_connect(client, *_):
    """Callback for when the client receives a CONNACK response from the server."""
    client.publish(f"{MQTT_TOPIC}/state", "online", retain=True)
    for topic in m_topics:
        client.publish(f"{MQTT_TOPIC}/{topic}", retain=True)
        client.subscribe(f"{MQTT_TOPIC}/{topic}")

# Add a custom client_id here
client_id = "YourUniqueClientID"
client = paho.mqtt.client.Client(client_id, paho.mqtt.client.CallbackAPIVersion.VERSION2)

client.username_pw_set(MQTT_USER, MQTT_PASS or None)
client.user_data_set(callback)
client.on_connect = on_connect
client.will_set(f"{MQTT_TOPIC}/state", payload="offline", qos=1, retain=True)
client.connect(MQTT_HOST, int(MQTT_PORT or 1883), 30)
client.loop_start()

return client`
@51av0sh
Copy link
Author

51av0sh commented Jan 20, 2025

Apologies for the bad formatting. I've tried several times and can't get the code block right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant