Publish a message to HA MQTT using python

(Core 2025.3.4, Supervisor 2025.03.3, Operating System 15.0, Frontend 20250306 on an AMD64)

I am a complete newbie at MQTT and trying to make a simple publish connection using python from a separate AMD64 machine to record data in MQTT within HA.

So far I have tested against another MQTT server without password authentication and it worked fine. I also set up the user/password in HA and have tested that using a PC based client that works also.

The problem is that when I put a python script together (using Thonny) with the user/password authentication it runs without incident but nothing gets posted in the HA MQTT. I am suspecting it may be something to do with the placement of the user/password authentication but I do not have the experience to determine what I’ve done incorrectly.

Here’s my script

# Test MQTT for GA pumps
import paho.mqtt.client as mqtt      #import the client
username="pumps"                       #HA user
password="pumps"                       #user password
broker_address="192.168.1.99"   #HA broker

#create new instance
client = mqtt.Client("GA_pump")
client.username_pw_set(username, password)
client.connect(broker_address)       #connect
client.publish("ga/w_pump","OFF")    #publish

Any guidance or help would be appreciated.

Are you watching all topics for you message or just one specific topic?

Can you tell if any dakye amicus by looking at logs?

EDIT

Drunk texting

Trying to make sure you are looking at the correct topic.

Use that to debug the messages and to check if your login info is correct
It will show exactly what u are doing in mqtt

I use the mosquito MQTT add on. I think you need to specify a port. I also specify a MQTT version

local_client = mqtt.Client(protocol=mqtt.MQTTv311)
local_client.username_pw_set(LOCAL_USERNAME, LOCAL_PASSWORD)
local_client.connect(LOCAL_BROKER, LOCAL_PORT, 60)

As the other person mentioned, verify you can access the MQTT broker on HA from the computer with the python program. I use MQTT explorer as well for this task.